diff --git a/docs/images/examples/ex_01.png b/docs/images/examples/ex_01.png index a8d810a..9ad5ebc 100644 Binary files a/docs/images/examples/ex_01.png and b/docs/images/examples/ex_01.png differ diff --git a/src/Luban.Job.Cfg/Source/Defs/TTypeTemplateExtends.cs b/src/Luban.Job.Cfg/Source/Defs/TTypeTemplateExtends.cs index f90f001..b2293ad 100644 --- a/src/Luban.Job.Cfg/Source/Defs/TTypeTemplateExtends.cs +++ b/src/Luban.Job.Cfg/Source/Defs/TTypeTemplateExtends.cs @@ -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); } } diff --git a/src/Luban.Job.Cfg/Source/Generate/CsCodeJsonRender.cs b/src/Luban.Job.Cfg/Source/Generate/CsCodeJsonRender.cs index 07ce7e8..b874f56 100644 --- a/src/Luban.Job.Cfg/Source/Generate/CsCodeJsonRender.cs +++ b/src/Luban.Job.Cfg/Source/Generate/CsCodeJsonRender.cs @@ -32,10 +32,10 @@ namespace {{x.namespace_with_top_module}} /// 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 ~}} diff --git a/src/Luban.Job.Cfg/Source/Generate/CsCodeUnityJsonRender.cs b/src/Luban.Job.Cfg/Source/Generate/CsCodeUnityJsonRender.cs new file mode 100644 index 0000000..adb06ad --- /dev/null +++ b/src/Luban.Job.Cfg/Source/Generate/CsCodeUnityJsonRender.cs @@ -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}} +{ + +/// +/// {{x.comment}} +/// +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 ~}} + /// + /// {{field.comment}} + /// + 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 _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 _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}} +{ + +/// +/// {{x.comment}} +/// +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({{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({{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 _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 ~}} + /// + /// {{field.comment}} + /// + 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 _tables) + { + _data.Resolve(_tables); + OnResolveFinish(_tables); + } + + {{~end~}} + + partial void OnResolveFinish(Dictionary _tables); +} + +} +"); + var result = template.RenderCode(p); + + return result; + } + + + [ThreadStatic] + private static Template t_serviceRender; + public override string RenderService(string name, string module, List 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 ~}} + /// + /// {{table.comment}} + /// + public {{table.full_name}} {{table.name}} {get; } + {{~end~}} + + public {{name}}(System.Func loader) + { + var tables = new System.Collections.Generic.Dictionary(); + {{~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; + } + } +} diff --git a/src/Luban.Job.Cfg/Source/JobController.cs b/src/Luban.Job.Cfg/Source/JobController.cs index 75a8d5d..1717536 100644 --- a/src/Luban.Job.Cfg/Source/JobController.cs +++ b/src/Luban.Job.Cfg/Source/JobController.cs @@ -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": diff --git a/src/Luban.Job.Cfg/Source/TypeVisitors/CsJsonUmarshal.cs b/src/Luban.Job.Cfg/Source/TypeVisitors/CsJsonDeserialize.cs similarity index 55% rename from src/Luban.Job.Cfg/Source/TypeVisitors/CsJsonUmarshal.cs rename to src/Luban.Job.Cfg/Source/TypeVisitors/CsJsonDeserialize.cs index bdc2912..616e441 100644 --- a/src/Luban.Job.Cfg/Source/TypeVisitors/CsJsonUmarshal.cs +++ b/src/Luban.Job.Cfg/Source/TypeVisitors/CsJsonDeserialize.cs @@ -4,9 +4,9 @@ using System; namespace Luban.Job.Cfg.TypeVisitors { - class CsJsonUserialize : ITypeFuncVisitor + class CsJsonDeserialize : ITypeFuncVisitor { - 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) diff --git a/src/Luban.Job.Cfg/Source/TypeVisitors/CsJsonUnityDeserialize.cs b/src/Luban.Job.Cfg/Source/TypeVisitors/CsJsonUnityDeserialize.cs new file mode 100644 index 0000000..e3b94a2 --- /dev/null +++ b/src/Luban.Job.Cfg/Source/TypeVisitors/CsJsonUnityDeserialize.cs @@ -0,0 +1,133 @@ +using Luban.Job.Common.Types; +using Luban.Job.Common.TypeVisitors; +using System; + +namespace Luban.Job.Cfg.TypeVisitors +{ + class CsUnityJsonDeserialize : ITypeFuncVisitor + { + 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}; }}"; + } + } +}