【新增】cfg 新增 cs_code_unity_json 类型生成支持。 使用 SimpleJSON 。
【调整】略微调整了 code_code_json生成(主要是将_buf改名为_json)main
parent
cf651b6686
commit
0e872b5610
Binary file not shown.
|
Before Width: | Height: | Size: 54 KiB After Width: | Height: | Size: 44 KiB |
|
|
@ -12,11 +12,23 @@ namespace Luban.Job.Cfg.Defs
|
|||
{
|
||||
if (type.IsNullable)
|
||||
{
|
||||
return $"{{ var _j = {bufName}.GetProperty(\"{jsonFieldName}\"); if (_j.ValueKind != JsonValueKind.Null) {{ {type.Apply(CsJsonUserialize.Ins, "_j", fieldName)} }} else {{ {fieldName} = null; }} }}";
|
||||
return $"{{ var _j = {bufName}.GetProperty(\"{jsonFieldName}\"); if (_j.ValueKind != JsonValueKind.Null) {{ {type.Apply(TypeVisitors.CsJsonDeserialize.Ins, "_j", fieldName)} }} else {{ {fieldName} = null; }} }}";
|
||||
}
|
||||
else
|
||||
{
|
||||
return type.Apply(CsJsonUserialize.Ins, $"{bufName}.GetProperty(\"{jsonFieldName}\")", fieldName);
|
||||
return type.Apply(TypeVisitors.CsJsonDeserialize.Ins, $"{bufName}.GetProperty(\"{jsonFieldName}\")", fieldName);
|
||||
}
|
||||
}
|
||||
|
||||
public static string CsUnityJsonDeserialize(string bufName, string fieldName, string jsonFieldName, TType type)
|
||||
{
|
||||
if (type.IsNullable)
|
||||
{
|
||||
return $"{{ var _j = {bufName}[\"{jsonFieldName}\"]; if (_j.Tag != JSONNodeType.None && _j.Tag != JSONNodeType.NullValue) {{ {type.Apply(TypeVisitors.CsUnityJsonDeserialize.Ins, "_j", fieldName)} }} else {{ {fieldName} = null; }} }}";
|
||||
}
|
||||
else
|
||||
{
|
||||
return type.Apply(TypeVisitors.CsUnityJsonDeserialize.Ins, $"{bufName}[\"{jsonFieldName}\"]", fieldName);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -32,10 +32,10 @@ namespace {{x.namespace_with_top_module}}
|
|||
/// </summary>
|
||||
public {{x.cs_class_modifier}} partial class {{name}} : {{if parent_def_type}} {{parent}} {{else}} Bright.Config.BeanBase {{end}}
|
||||
{
|
||||
public {{name}}(JsonElement _buf) {{if parent_def_type}} : base(_buf) {{end}}
|
||||
public {{name}}(JsonElement _json) {{if parent_def_type}} : base(_json) {{end}}
|
||||
{
|
||||
{{~ for field in export_fields ~}}
|
||||
{{cs_json_deserialize '_buf' field.cs_style_name field.name field.ctype}}
|
||||
{{cs_json_deserialize '_json' field.cs_style_name field.name field.ctype}}
|
||||
{{~if field.index_field~}}
|
||||
foreach(var _v in {{field.cs_style_name}}) { {{field.cs_style_name}}_Index.Add(_v.{{field.index_field.cs_style_name}}, _v); }
|
||||
{{~end~}}
|
||||
|
|
@ -52,18 +52,18 @@ public {{x.cs_class_modifier}} partial class {{name}} : {{if parent_def_type}} {
|
|||
{{~end~}}
|
||||
}
|
||||
|
||||
public static {{name}} Deserialize{{name}}(JsonElement _buf)
|
||||
public static {{name}} Deserialize{{name}}(JsonElement _json)
|
||||
{
|
||||
{{~if x.is_abstract_type~}}
|
||||
switch (_buf.GetProperty(""__type__"").GetString())
|
||||
switch (_json.GetProperty(""__type__"").GetString())
|
||||
{
|
||||
{{~for child in x.hierarchy_not_abstract_children~}}
|
||||
case ""{{child.name}}"": return new {{child.full_name}}(_buf);
|
||||
case ""{{child.name}}"": return new {{child.full_name}}(_json);
|
||||
{{~end~}}
|
||||
default: throw new SerializationException();
|
||||
}
|
||||
{{~else~}}
|
||||
return new {{x.full_name}}(_buf);
|
||||
return new {{x.full_name}}(_json);
|
||||
{{~end~}}
|
||||
}
|
||||
|
||||
|
|
@ -148,12 +148,12 @@ public sealed partial class {{name}}
|
|||
private readonly Dictionary<{{cs_define_type key_type}}, {{cs_define_type value_type}}> _dataMap;
|
||||
private readonly List<{{cs_define_type value_type}}> _dataList;
|
||||
|
||||
public {{name}}(JsonElement _buf)
|
||||
public {{name}}(JsonElement _json)
|
||||
{
|
||||
_dataMap = new Dictionary<{{cs_define_type key_type}}, {{cs_define_type value_type}}>();
|
||||
_dataList = new List<{{cs_define_type value_type}}>();
|
||||
|
||||
foreach(JsonElement _row in _buf.EnumerateArray())
|
||||
foreach(JsonElement _row in _json.EnumerateArray())
|
||||
{
|
||||
var _v = {{cs_define_type value_type}}.Deserialize{{value_type.bean.name}}(_row);
|
||||
_dataList.Add(_v);
|
||||
|
|
@ -185,11 +185,11 @@ public sealed partial class {{name}}
|
|||
|
||||
private readonly {{cs_define_type value_type}} _data;
|
||||
|
||||
public {{name}}(JsonElement _buf)
|
||||
public {{name}}(JsonElement _json)
|
||||
{
|
||||
int n = _buf.GetArrayLength();
|
||||
int n = _json.GetArrayLength();
|
||||
if (n != 1) throw new SerializationException(""table mode=one, but size != 1"");
|
||||
_data = {{cs_define_type value_type}}.Deserialize{{value_type.bean.name}}(_buf[0]);
|
||||
_data = {{cs_define_type value_type}}.Deserialize{{value_type.bean.name}}(_json[0]);
|
||||
}
|
||||
|
||||
{{~ for field in value_type.bean.hierarchy_export_fields ~}}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,279 @@
|
|||
using Luban.Job.Cfg.Defs;
|
||||
using Scriban;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Luban.Job.Cfg.Generate
|
||||
{
|
||||
class CsCodeUnityJsonRender : CsCodeRenderBase
|
||||
{
|
||||
[ThreadStatic]
|
||||
private static Template t_beanRender;
|
||||
public override string Render(DefBean b)
|
||||
{
|
||||
var template = t_beanRender ??= Template.Parse(@"
|
||||
using Bright.Serialization;
|
||||
using System.Collections.Generic;
|
||||
using SimpleJSON;
|
||||
|
||||
{{
|
||||
name = x.name
|
||||
parent_def_type = x.parent_def_type
|
||||
parent = x.parent
|
||||
export_fields = x.export_fields
|
||||
hierarchy_export_fields = x.hierarchy_export_fields
|
||||
}}
|
||||
|
||||
namespace {{x.namespace_with_top_module}}
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// {{x.comment}}
|
||||
/// </summary>
|
||||
public {{x.cs_class_modifier}} partial class {{name}} : {{if parent_def_type}} {{parent}} {{else}} Bright.Config.BeanBase {{end}}
|
||||
{
|
||||
public {{name}}(JSONNode _json) {{if parent_def_type}} : base(_json) {{end}}
|
||||
{
|
||||
{{~ for field in export_fields ~}}
|
||||
{{cs_unity_json_deserialize '_json' field.cs_style_name field.name field.ctype}}
|
||||
{{~if field.index_field~}}
|
||||
foreach(var _v in {{field.cs_style_name}}) { {{field.cs_style_name}}_Index.Add(_v.{{field.index_field.cs_style_name}}, _v); }
|
||||
{{~end~}}
|
||||
{{~end~}}
|
||||
}
|
||||
|
||||
public {{name}}({{~for field in hierarchy_export_fields }}{{cs_define_type field.ctype}} {{field.name}}{{if !for.last}},{{end}} {{end}}) {{if parent_def_type}} : base({{- for field in parent_def_type.hierarchy_export_fields }}{{field.name}}{{if !for.last}},{{end}}{{end}}) {{end}}
|
||||
{
|
||||
{{~ for field in export_fields ~}}
|
||||
this.{{field.cs_style_name}} = {{field.name}};
|
||||
{{~if field.index_field~}}
|
||||
foreach(var _v in {{field.cs_style_name}}) { {{field.cs_style_name}}_Index.Add(_v.{{field.index_field.cs_style_name}}, _v); }
|
||||
{{~end~}}
|
||||
{{~end~}}
|
||||
}
|
||||
|
||||
public static {{name}} Deserialize{{name}}(JSONNode _json)
|
||||
{
|
||||
{{~if x.is_abstract_type~}}
|
||||
string type = _json[""__type__""];
|
||||
switch (type)
|
||||
{
|
||||
{{~for child in x.hierarchy_not_abstract_children~}}
|
||||
case ""{{child.name}}"": return new {{child.full_name}}(_json);
|
||||
{{~end~}}
|
||||
default: throw new SerializationException();
|
||||
}
|
||||
{{~else~}}
|
||||
return new {{x.full_name}}(_json);
|
||||
{{~end~}}
|
||||
}
|
||||
|
||||
{{~ for field in export_fields ~}}
|
||||
/// <summary>
|
||||
/// {{field.comment}}
|
||||
/// </summary>
|
||||
public readonly {{cs_define_type field.ctype}} {{field.cs_style_name}};
|
||||
{{~if field.index_field~}}
|
||||
public readonly Dictionary<{{cs_define_type field.index_field.ctype}}, {{cs_define_type field.ctype.element_type}}> {{field.cs_style_name}}_Index = new Dictionary<{{cs_define_type field.index_field.ctype}}, {{cs_define_type field.ctype.element_type}}>();
|
||||
{{~end~}}
|
||||
{{~if field.gen_ref~}}
|
||||
public {{field.cs_ref_validator_define}}
|
||||
{{~end~}}
|
||||
{{~end~}}
|
||||
|
||||
{{~if !x.is_abstract_type~}}
|
||||
public const int ID = {{x.id}};
|
||||
public override int GetTypeId() => ID;
|
||||
{{~end~}}
|
||||
|
||||
public {{x.cs_method_modifier}} void Resolve(Dictionary<string, object> _tables)
|
||||
{
|
||||
{{~if parent_def_type~}}
|
||||
base.Resolve(_tables);
|
||||
{{~end~}}
|
||||
{{~ for field in export_fields ~}}
|
||||
{{~if field.gen_ref~}}
|
||||
{{cs_ref_validator_resolve field}}
|
||||
{{~else if field.has_recursive_ref~}}
|
||||
{{cs_recursive_resolve field '_tables'}}
|
||||
{{~end~}}
|
||||
{{~end~}}
|
||||
OnResolveFinish(_tables);
|
||||
}
|
||||
|
||||
partial void OnResolveFinish(Dictionary<string, object> _tables);
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return ""{{full_name}}{ ""
|
||||
{{~ for field in hierarchy_export_fields ~}}
|
||||
+ ""{{field.cs_style_name}}:"" + {{cs_to_string field.cs_style_name field.ctype}} + "",""
|
||||
{{~end~}}
|
||||
+ ""}"";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
");
|
||||
var result = template.RenderCode(b);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
[ThreadStatic]
|
||||
private static Template t_tableRender;
|
||||
public override string Render(DefTable p)
|
||||
{
|
||||
var template = t_tableRender ??= Template.Parse(@"
|
||||
using Bright.Serialization;
|
||||
using System.Collections.Generic;
|
||||
using SimpleJSON;
|
||||
|
||||
{{
|
||||
name = x.name
|
||||
key_type = x.key_ttype
|
||||
key_type1 = x.key_ttype1
|
||||
key_type2 = x.key_ttype2
|
||||
value_type = x.value_ttype
|
||||
}}
|
||||
|
||||
namespace {{x.namespace_with_top_module}}
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// {{x.comment}}
|
||||
/// </summary>
|
||||
public sealed partial class {{name}}
|
||||
{
|
||||
{{~if x.is_map_table ~}}
|
||||
private readonly Dictionary<{{cs_define_type key_type}}, {{cs_define_type value_type}}> _dataMap;
|
||||
private readonly List<{{cs_define_type value_type}}> _dataList;
|
||||
|
||||
public {{name}}(JSONNode _json)
|
||||
{
|
||||
_dataMap = new Dictionary<{{cs_define_type key_type}}, {{cs_define_type value_type}}>();
|
||||
_dataList = new List<{{cs_define_type value_type}}>();
|
||||
|
||||
foreach(JSONNode _row in _json.Children)
|
||||
{
|
||||
var _v = {{cs_define_type value_type}}.Deserialize{{value_type.bean.name}}(_row);
|
||||
_dataList.Add(_v);
|
||||
_dataMap.Add(_v.{{x.index_field.cs_style_name}}, _v);
|
||||
}
|
||||
}
|
||||
|
||||
public Dictionary<{{cs_define_type key_type}}, {{cs_define_type value_type}}> DataMap => _dataMap;
|
||||
public List<{{cs_define_type value_type}}> DataList => _dataList;
|
||||
|
||||
{{~if value_type.is_dynamic~}}
|
||||
public T GetOrDefaultAs<T>({{cs_define_type key_type}} key) where T : {{cs_define_type value_type}} => _dataMap.TryGetValue(key, out var v) ? (T)v : null;
|
||||
public T GetAs<T>({{cs_define_type key_type}} key) where T : {{cs_define_type value_type}} => (T)_dataMap[key];
|
||||
{{~end~}}
|
||||
public {{cs_define_type value_type}} GetOrDefault({{cs_define_type key_type}} key) => _dataMap.TryGetValue(key, out var v) ? v : null;
|
||||
public {{cs_define_type value_type}} Get({{cs_define_type key_type}} key) => _dataMap[key];
|
||||
public {{cs_define_type value_type}} this[{{cs_define_type key_type}} key] => _dataMap[key];
|
||||
|
||||
public void Resolve(Dictionary<string, object> _tables)
|
||||
{
|
||||
foreach(var v in _dataList)
|
||||
{
|
||||
v.Resolve(_tables);
|
||||
}
|
||||
OnResolveFinish(_tables);
|
||||
}
|
||||
|
||||
{{~else~}}
|
||||
|
||||
private readonly {{cs_define_type value_type}} _data;
|
||||
|
||||
public {{name}}(JSONNode _json)
|
||||
{
|
||||
if(!_json.IsArray)
|
||||
{
|
||||
throw new SerializationException();
|
||||
}
|
||||
if (_json.Count != 1) throw new SerializationException(""table mode=one, but size != 1"");
|
||||
_data = {{cs_define_type value_type}}.Deserialize{{value_type.bean.name}}(_json[0]);
|
||||
}
|
||||
|
||||
{{~ for field in value_type.bean.hierarchy_export_fields ~}}
|
||||
/// <summary>
|
||||
/// {{field.comment}}
|
||||
/// </summary>
|
||||
public {{cs_define_type field.ctype}} {{field.cs_style_name}} => _data.{{field.cs_style_name}};
|
||||
{{~if field.ref~}}
|
||||
public {{field.cs_ref_type_name}} {{field.cs_ref_var_name}} => _data.{{field.cs_ref_var_name}};
|
||||
{{~end~}}
|
||||
{{~end~}}
|
||||
|
||||
public void Resolve(Dictionary<string, object> _tables)
|
||||
{
|
||||
_data.Resolve(_tables);
|
||||
OnResolveFinish(_tables);
|
||||
}
|
||||
|
||||
{{~end~}}
|
||||
|
||||
partial void OnResolveFinish(Dictionary<string, object> _tables);
|
||||
}
|
||||
|
||||
}
|
||||
");
|
||||
var result = template.RenderCode(p);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
[ThreadStatic]
|
||||
private static Template t_serviceRender;
|
||||
public override string RenderService(string name, string module, List<DefTable> tables)
|
||||
{
|
||||
var template = t_serviceRender ??= Template.Parse(@"
|
||||
using Bright.Serialization;
|
||||
using SimpleJSON;
|
||||
{{
|
||||
name = x.name
|
||||
namespace = x.namespace
|
||||
tables = x.tables
|
||||
}}
|
||||
namespace {{namespace}}
|
||||
{
|
||||
|
||||
public sealed partial class {{name}}
|
||||
{
|
||||
{{~for table in tables ~}}
|
||||
/// <summary>
|
||||
/// {{table.comment}}
|
||||
/// </summary>
|
||||
public {{table.full_name}} {{table.name}} {get; }
|
||||
{{~end~}}
|
||||
|
||||
public {{name}}(System.Func<string, JSONNode> loader)
|
||||
{
|
||||
var tables = new System.Collections.Generic.Dictionary<string, object>();
|
||||
{{~for table in tables ~}}
|
||||
{{table.name}} = new {{table.full_name}}(loader(""{{table.output_data_file}}""));
|
||||
tables.Add(""{{table.full_name}}"", {{table.name}});
|
||||
{{~end~}}
|
||||
|
||||
{{~for table in tables ~}}
|
||||
{{table.name}}.Resolve(tables);
|
||||
{{~end~}}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
");
|
||||
var result = template.RenderCode(new
|
||||
{
|
||||
Name = name,
|
||||
Namespace = module,
|
||||
Tables = tables,
|
||||
});
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -45,7 +45,7 @@ namespace Luban.Job.Cfg
|
|||
[Option("output_data_json_monolithic_file", Required = false, HelpText = "output monolithic json file")]
|
||||
public string OutputDataJsonMonolithicFile { get; set; }
|
||||
|
||||
[Option("gen_types", Required = true, HelpText = "code_cs_bin,code_cs_json,code_lua_bin,code_java_bin,code_go_bin,code_go_json,code_cpp_bin,code_python27_json,code_python3_json,code_typescript_bin,code_typescript_json,data_bin,data_lua,data_json,data_json_monolithic . can be multi")]
|
||||
[Option("gen_types", Required = true, HelpText = "code_cs_bin,code_cs_json,code_cs_unity_json,code_lua_bin,code_java_bin,code_go_bin,code_go_json,code_cpp_bin,code_python27_json,code_python3_json,code_typescript_bin,code_typescript_json,data_bin,data_lua,data_json,data_json_monolithic . can be multi")]
|
||||
public string GenType { get; set; }
|
||||
|
||||
[Option('s', "service", Required = true, HelpText = "service")]
|
||||
|
|
@ -79,6 +79,7 @@ namespace Luban.Job.Cfg
|
|||
{
|
||||
case "code_cs_bin": return new CsCodeBinRender();
|
||||
case "code_cs_json": return new CsCodeJsonRender();
|
||||
case "code_cs_unity_json": return new CsCodeUnityJsonRender();
|
||||
case "code_java_bin": return new JavaCodeBinRender();
|
||||
case "code_go_bin": return new GoCodeBinRender();
|
||||
case "code_go_json": return new GoCodeJsonRender();
|
||||
|
|
@ -101,7 +102,9 @@ namespace Luban.Job.Cfg
|
|||
switch (genType)
|
||||
{
|
||||
case "code_cs_bin":
|
||||
case "code_cs_json": return ELanguage.CS;
|
||||
case "code_cs_json":
|
||||
case "code_cs_unity_json":
|
||||
return ELanguage.CS;
|
||||
case "code_java_bin": return ELanguage.JAVA;
|
||||
case "code_go_bin":
|
||||
case "code_go_json": return ELanguage.GO;
|
||||
|
|
@ -329,6 +332,7 @@ namespace Luban.Job.Cfg
|
|||
{
|
||||
case "code_cs_bin":
|
||||
case "code_cs_json":
|
||||
case "code_cs_unity_json":
|
||||
case "code_java_bin":
|
||||
case "code_go_bin":
|
||||
case "code_go_json":
|
||||
|
|
|
|||
|
|
@ -4,9 +4,9 @@ using System;
|
|||
|
||||
namespace Luban.Job.Cfg.TypeVisitors
|
||||
{
|
||||
class CsJsonUserialize : ITypeFuncVisitor<string, string, string>
|
||||
class CsJsonDeserialize : ITypeFuncVisitor<string, string, string>
|
||||
{
|
||||
public static CsJsonUserialize Ins { get; } = new CsJsonUserialize();
|
||||
public static CsJsonDeserialize Ins { get; } = new();
|
||||
|
||||
public string Accept(TBool type, string json, string x)
|
||||
{
|
||||
|
|
@ -85,37 +85,37 @@ namespace Luban.Job.Cfg.TypeVisitors
|
|||
|
||||
public string Accept(TArray type, string json, string x)
|
||||
{
|
||||
return $"{{ var _json = {json}; int _n = _json.GetArrayLength(); {x} = new {type.ElementType.CsUnderingDefineType()}[_n]; int _index=0; foreach(JsonElement __e in _json.EnumerateArray()) {{ {type.ElementType.CsUnderingDefineType()} __v; {type.ElementType.Apply(this, "__e", "__v")} {x}[_index++] = __v; }} }}";
|
||||
return $"{{ var _json0 = {json}; int _n = _json0.GetArrayLength(); {x} = new {type.ElementType.CsUnderingDefineType()}[_n]; int _index=0; foreach(JsonElement __e in _json0.EnumerateArray()) {{ {type.ElementType.CsUnderingDefineType()} __v; {type.ElementType.Apply(this, "__e", "__v")} {x}[_index++] = __v; }} }}";
|
||||
}
|
||||
|
||||
public string Accept(TList type, string json, string x)
|
||||
{
|
||||
return $"{{ var _json = {json}; {x} = new {type.CsUnderingDefineType()}(_json.GetArrayLength()); foreach(JsonElement __e in _json.EnumerateArray()) {{ {type.ElementType.CsUnderingDefineType()} __v; {type.ElementType.Apply(this, "__e", "__v")} {x}.Add(__v); }} }}";
|
||||
return $"{{ var _json0 = {json}; {x} = new {type.CsUnderingDefineType()}(_json0.GetArrayLength()); foreach(JsonElement __e in _json0.EnumerateArray()) {{ {type.ElementType.CsUnderingDefineType()} __v; {type.ElementType.Apply(this, "__e", "__v")} {x}.Add(__v); }} }}";
|
||||
}
|
||||
|
||||
public string Accept(TSet type, string json, string x)
|
||||
{
|
||||
return $"{{ var _json = {json}; {x} = new {type.CsUnderingDefineType()}(_json.GetArrayLength()); foreach(JsonElement __e in _json.EnumerateArray()) {{ {type.ElementType.CsUnderingDefineType()} __v; {type.ElementType.Apply(this, "__e", "__v")} {x}.Add(__v); }} }}";
|
||||
return $"{{ var _json0 = {json}; {x} = new {type.CsUnderingDefineType()}(_json0.GetArrayLength()); foreach(JsonElement __e in _json0.EnumerateArray()) {{ {type.ElementType.CsUnderingDefineType()} __v; {type.ElementType.Apply(this, "__e", "__v")} {x}.Add(__v); }} }}";
|
||||
}
|
||||
|
||||
public string Accept(TMap type, string json, string x)
|
||||
{
|
||||
return @$"{{ var _json = {json}; {x} = new {type.CsUnderingDefineType()}(_json.GetArrayLength()); foreach(JsonElement __e in _json.EnumerateArray()) {{ {type.KeyType.CsUnderingDefineType()} __k; {type.KeyType.Apply(this, "__e[0]", "__k")} {type.ValueType.CsUnderingDefineType()} __v; {type.ValueType.Apply(this, "__e[1]", "__v")} {x}.Add(__k, __v); }} }}";
|
||||
return @$"{{ var _json0 = {json}; {x} = new {type.CsUnderingDefineType()}(_json0.GetArrayLength()); foreach(JsonElement __e in _json0.EnumerateArray()) {{ {type.KeyType.CsUnderingDefineType()} __k; {type.KeyType.Apply(this, "__e[0]", "__k")} {type.ValueType.CsUnderingDefineType()} __v; {type.ValueType.Apply(this, "__e[1]", "__v")} {x}.Add(__k, __v); }} }}";
|
||||
}
|
||||
|
||||
public string Accept(TVector2 type, string json, string x)
|
||||
{
|
||||
return $"{{ var _json = {json}; float __x; {TFloat.Ins.Apply(this, "_json.GetProperty(\"x\")", "__x") } float __y; {TFloat.Ins.Apply(this, "_json.GetProperty(\"y\")", "__y") } {x} = new System.Numerics.Vector2(__x, __y); }}";
|
||||
return $"{{ var _json0 = {json}; float __x; {TFloat.Ins.Apply(this, "_json0.GetProperty(\"x\")", "__x") } float __y; {TFloat.Ins.Apply(this, "_json0.GetProperty(\"y\")", "__y") } {x} = new System.Numerics.Vector2(__x, __y); }}";
|
||||
}
|
||||
|
||||
public string Accept(TVector3 type, string json, string x)
|
||||
{
|
||||
return $"{{ var _json = {json}; float __x; {TFloat.Ins.Apply(this, "_json.GetProperty(\"x\")", "__x") } float __y; {TFloat.Ins.Apply(this, "_json.GetProperty(\"y\")", "__y") } float __z; {TFloat.Ins.Apply(this, "_json.GetProperty(\"z\")", "__z") } {x} = new System.Numerics.Vector3(__x, __y,__z); }}";
|
||||
return $"{{ var _json0 = {json}; float __x; {TFloat.Ins.Apply(this, "_json0.GetProperty(\"x\")", "__x") } float __y; {TFloat.Ins.Apply(this, "_json0.GetProperty(\"y\")", "__y") } float __z; {TFloat.Ins.Apply(this, "_json0.GetProperty(\"z\")", "__z") } {x} = new System.Numerics.Vector3(__x, __y,__z); }}";
|
||||
}
|
||||
|
||||
public string Accept(TVector4 type, string json, string x)
|
||||
{
|
||||
return $"{{ var _json = {json}; float __x; {TFloat.Ins.Apply(this, "_json.GetProperty(\"x\")", "__x") } float __y; {TFloat.Ins.Apply(this, "_json.GetProperty(\"y\")", "__y") } float __z; {TFloat.Ins.Apply(this, "_json.GetProperty(\"z\")", "__z") } float __w; {TFloat.Ins.Apply(this, "_json.GetProperty(\"w\")", "__w") } {x} = new System.Numerics.Vector4(__x, __y, __z, __w); }}";
|
||||
return $"{{ var _json0 = {json}; float __x; {TFloat.Ins.Apply(this, "_json0.GetProperty(\"x\")", "__x") } float __y; {TFloat.Ins.Apply(this, "_json0.GetProperty(\"y\")", "__y") } float __z; {TFloat.Ins.Apply(this, "_json0.GetProperty(\"z\")", "__z") } float __w; {TFloat.Ins.Apply(this, "_json0.GetProperty(\"w\")", "__w") } {x} = new System.Numerics.Vector4(__x, __y, __z, __w); }}";
|
||||
}
|
||||
|
||||
public string Accept(TDateTime type, string json, string x)
|
||||
|
|
@ -0,0 +1,133 @@
|
|||
using Luban.Job.Common.Types;
|
||||
using Luban.Job.Common.TypeVisitors;
|
||||
using System;
|
||||
|
||||
namespace Luban.Job.Cfg.TypeVisitors
|
||||
{
|
||||
class CsUnityJsonDeserialize : ITypeFuncVisitor<string, string, string>
|
||||
{
|
||||
public static CsUnityJsonDeserialize Ins { get; } = new();
|
||||
|
||||
public string Accept(TBool type, string json, string x)
|
||||
{
|
||||
return $"{{ if(!{json}.IsBoolean) {{ throw new SerializationException(); }} {x} = {json}; }}";
|
||||
}
|
||||
|
||||
public string Accept(TByte type, string json, string x)
|
||||
{
|
||||
return $"{{ if(!{json}.IsNumber) {{ throw new SerializationException(); }} {x} = {json}; }}";
|
||||
}
|
||||
|
||||
public string Accept(TShort type, string json, string x)
|
||||
{
|
||||
return $"{{ if(!{json}.IsNumber) {{ throw new SerializationException(); }} {x} = {json}; }}";
|
||||
}
|
||||
|
||||
public string Accept(TFshort type, string json, string x)
|
||||
{
|
||||
return $"{{ if(!{json}.IsNumber) {{ throw new SerializationException(); }} {x} = {json}; }}";
|
||||
}
|
||||
|
||||
public string Accept(TInt type, string json, string x)
|
||||
{
|
||||
return $"{{ if(!{json}.IsNumber) {{ throw new SerializationException(); }} {x} = {json}; }}";
|
||||
}
|
||||
|
||||
public string Accept(TFint type, string json, string x)
|
||||
{
|
||||
return $"{{ if(!{json}.IsNumber) {{ throw new SerializationException(); }} {x} = {json}; }}";
|
||||
}
|
||||
|
||||
public string Accept(TLong type, string json, string x)
|
||||
{
|
||||
return $"{{ if(!{json}.IsNumber) {{ throw new SerializationException(); }} {x} = {json}; }}";
|
||||
}
|
||||
|
||||
public string Accept(TFlong type, string json, string x)
|
||||
{
|
||||
return $"{{ if(!{json}.IsNumber) {{ throw new SerializationException(); }} {x} = {json}; }}";
|
||||
}
|
||||
|
||||
public string Accept(TFloat type, string json, string x)
|
||||
{
|
||||
return $"{{ if(!{json}.IsNumber) {{ throw new SerializationException(); }} {x} = {json}; }}";
|
||||
}
|
||||
|
||||
public string Accept(TDouble type, string json, string x)
|
||||
{
|
||||
return $"{{ if(!{json}.IsNumber) {{ throw new SerializationException(); }} {x} = {json}; }}";
|
||||
}
|
||||
|
||||
public string Accept(TEnum type, string json, string x)
|
||||
{
|
||||
return $"{{ if(!{json}.IsNumber) {{ throw new SerializationException(); }} {x} = ({type.CsUnderingDefineType()}){json}.AsInt; }}";
|
||||
}
|
||||
|
||||
public string Accept(TString type, string json, string x)
|
||||
{
|
||||
return $"{{ if(!{json}.IsString) {{ throw new SerializationException(); }} {x} = {json}; }}";
|
||||
}
|
||||
|
||||
public string Accept(TBytes type, string json, string x)
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
|
||||
public string Accept(TText type, string json, string x)
|
||||
{
|
||||
return $"{{ if(!{json}.IsString) {{ throw new SerializationException(); }} {x} = {json}; }}";
|
||||
}
|
||||
|
||||
public string Accept(TBean type, string json, string x)
|
||||
{
|
||||
return $"{{ if(!{json}.IsObject) {{ throw new SerializationException(); }} {x} = {type.CsUnderingDefineType()}.Deserialize{type.Bean.Name}({json}); }}";
|
||||
}
|
||||
|
||||
public string Accept(TArray type, string json, string x)
|
||||
{
|
||||
string tempJsonName = $"_json1";
|
||||
return $"{{ var {tempJsonName} = {json}; if(!{tempJsonName}.IsArray) {{ throw new SerializationException(); }} int _n = {tempJsonName}.Count; {x} = new {type.ElementType.CsUnderingDefineType()}[_n]; int _index=0; foreach(JSONNode __e in {tempJsonName}.Children) {{ {type.ElementType.CsUnderingDefineType()} __v; {type.ElementType.Apply(this, "__e", "__v")} {x}[_index++] = __v; }} }}";
|
||||
}
|
||||
|
||||
public string Accept(TList type, string json, string x)
|
||||
{
|
||||
string tempJsonName = $"_json1";
|
||||
return $"{{ var {tempJsonName} = {json}; if(!{tempJsonName}.IsArray) {{ throw new SerializationException(); }} {x} = new {type.CsUnderingDefineType()}({tempJsonName}.Count); foreach(JSONNode __e in {tempJsonName}.Children) {{ {type.ElementType.CsUnderingDefineType()} __v; {type.ElementType.Apply(this, "__e", "__v")} {x}.Add(__v); }} }}";
|
||||
}
|
||||
|
||||
public string Accept(TSet type, string json, string x)
|
||||
{
|
||||
string tempJsonName = $"_json1";
|
||||
return $"{{ var {tempJsonName} = {json}; if(!{tempJsonName}.IsArray) {{ throw new SerializationException(); }} {x} = new {type.CsUnderingDefineType()}(/*{tempJsonName}.Count*/); foreach(JSONNode __e in {tempJsonName}.Children) {{ {type.ElementType.CsUnderingDefineType()} __v; {type.ElementType.Apply(this, "__e", "__v")} {x}.Add(__v); }} }}";
|
||||
}
|
||||
|
||||
public string Accept(TMap type, string json, string x)
|
||||
{
|
||||
string tempJsonName = $"_json1";
|
||||
return @$"{{ var {tempJsonName} = {json}; if(!{tempJsonName}.IsArray) {{ throw new SerializationException(); }} {x} = new {type.CsUnderingDefineType()}({tempJsonName}.Count); foreach(JSONNode __e in {tempJsonName}.Children) {{ {type.KeyType.CsUnderingDefineType()} __k; {type.KeyType.Apply(this, "__e[0]", "__k")} {type.ValueType.CsUnderingDefineType()} __v; {type.ValueType.Apply(this, "__e[1]", "__v")} {x}.Add(__k, __v); }} }}";
|
||||
}
|
||||
|
||||
public string Accept(TVector2 type, string json, string x)
|
||||
{
|
||||
string tempJsonName = $"_json2";
|
||||
return $"{{ var {tempJsonName} = {json}; if(!{tempJsonName}.IsObject) {{ throw new SerializationException(); }} float __x; {TFloat.Ins.Apply(this, $"{tempJsonName}[\"x\"]", "__x") } float __y; {TFloat.Ins.Apply(this, $"{tempJsonName}[\"y\"]", "__y") } {x} = new System.Numerics.Vector2(__x, __y); }}";
|
||||
}
|
||||
|
||||
public string Accept(TVector3 type, string json, string x)
|
||||
{
|
||||
string tempJsonName = $"_json2";
|
||||
return $"{{ var {tempJsonName} = {json}; if(!{tempJsonName}.IsObject) {{ throw new SerializationException(); }} float __x; {TFloat.Ins.Apply(this, $"{tempJsonName}[\"x\"]", "__x") } float __y; {TFloat.Ins.Apply(this, $"{tempJsonName}[\"y\"]", "__y") } float __z; {TFloat.Ins.Apply(this, $"{tempJsonName}[\"z\"]", "__z") } {x} = new System.Numerics.Vector3(__x, __y,__z); }}";
|
||||
}
|
||||
|
||||
public string Accept(TVector4 type, string json, string x)
|
||||
{
|
||||
string tempJsonName = $"_json2";
|
||||
return $"{{ var {tempJsonName} = {json}; if(!{tempJsonName}.IsObject) {{ throw new SerializationException(); }} float __x; {TFloat.Ins.Apply(this, $"{tempJsonName}[\"x\"]", "__x") } float __y; {TFloat.Ins.Apply(this, $"{tempJsonName}[\"y\"]", "__y") } float __z; {TFloat.Ins.Apply(this, $"{tempJsonName}[\"z\"]", "__z") } float __w; {TFloat.Ins.Apply(this, $"{tempJsonName}[\"w\"]", "__w") } {x} = new System.Numerics.Vector4(__x, __y, __z, __w); }}";
|
||||
}
|
||||
|
||||
public string Accept(TDateTime type, string json, string x)
|
||||
{
|
||||
return $"{{ if(!{json}.IsNumber) {{ throw new SerializationException(); }} {x} = {json}; }}";
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue