diff --git a/src/Luban.Job.Cfg/Source/TypeVisitors/JavaJsonDeserialize.cs b/src/Luban.Job.Cfg/Source/TypeVisitors/JavaJsonDeserialize.cs index 75d78cd..977135e 100644 --- a/src/Luban.Job.Cfg/Source/TypeVisitors/JavaJsonDeserialize.cs +++ b/src/Luban.Job.Cfg/Source/TypeVisitors/JavaJsonDeserialize.cs @@ -5,81 +5,81 @@ using System; namespace Luban.Job.Cfg.TypeVisitors { - class JavaJsonDeserialize : ITypeFuncVisitor + class JavaJsonDeserialize : ITypeFuncVisitor { public static JavaJsonDeserialize Ins { get; } = new(); - public string Accept(TBool type, string json, string x) + public string Accept(TBool type, string json, string x, int depth) { return $"{x} = {json}.getAsBoolean();"; } - public string Accept(TByte type, string json, string x) + public string Accept(TByte type, string json, string x, int depth) { return $"{x} = {json}.getAsByte();"; } - public string Accept(TShort type, string json, string x) + public string Accept(TShort type, string json, string x, int depth) { return $"{x} = {json}.getAsShort();"; } - public string Accept(TFshort type, string json, string x) + public string Accept(TFshort type, string json, string x, int depth) { return $"{x} = {json}.getAsShort();"; } - public string Accept(TInt type, string json, string x) + public string Accept(TInt type, string json, string x, int depth) { return $"{x} = {json}.getAsInt();"; } - public string Accept(TFint type, string json, string x) + public string Accept(TFint type, string json, string x, int depth) { return $"{x} = {json}.getAsInt();"; } - public string Accept(TLong type, string json, string x) + public string Accept(TLong type, string json, string x, int depth) { return $"{x} = {json}.getAsLong();"; } - public string Accept(TFlong type, string json, string x) + public string Accept(TFlong type, string json, string x, int depth) { return $"{x} = {json}.getAsLong();"; } - public string Accept(TFloat type, string json, string x) + public string Accept(TFloat type, string json, string x, int depth) { return $"{x} = {json}.getAsFloat();"; } - public string Accept(TDouble type, string json, string x) + public string Accept(TDouble type, string json, string x, int depth) { return $"{x} = {json}.getAsDouble();"; } - public string Accept(TEnum type, string json, string x) + public string Accept(TEnum type, string json, string x, int depth) { return $"{x} = {json}.getAsInt();"; } - public string Accept(TString type, string json, string x) + public string Accept(TString type, string json, string x, int depth) { return $"{x} = {json}.getAsString();"; } - public string Accept(TBytes type, string json, string x) + public string Accept(TBytes type, string json, string x, int depth) { throw new NotSupportedException(); } - public string Accept(TText type, string json, string x) + public string Accept(TText type, string json, string x, int depth) { return $"{json}.getAsJsonObject().get(\"{DText.KEY_NAME}\").getAsString(); {x} = {json}.getAsJsonObject().get(\"{DText.TEXT_NAME}\").getAsString();"; } - public string Accept(TBean type, string json, string x) + public string Accept(TBean type, string json, string x, int depth) { if (type.IsDynamic) { @@ -91,42 +91,66 @@ namespace Luban.Job.Cfg.TypeVisitors } } - public string Accept(TArray type, string json, string x) + public string Accept(TArray type, string json, string x, int depth) { - return $"{{ com.google.gson.JsonArray _json0_ = {json}.getAsJsonArray(); int _n = _json0_.size(); {x} = new {type.ElementType.Apply(JavaDefineTypeName.Ins)}[_n]; int _index=0; for(JsonElement __e : _json0_) {{ {type.ElementType.Apply(JavaDefineTypeName.Ins)} __v; {type.ElementType.Apply(this, "__e", "__v")} {x}[_index++] = __v; }} }}"; + string __n = $"__n{depth}"; + string __e = $"__e{depth}"; + string __v = $"__v{depth}"; + string __index = $"__index{depth}"; + string typeStr = $"{type.ElementType.Apply(CsDefineTypeName.Ins)}[{__n}]"; + if (type.Dimension > 1) + { + if (type.FinalElementType == null) + { + throw new System.Exception("多维数组没有元素类型"); + } + typeStr = $"{type.FinalElementType.Apply(CsUnderingDefineTypeName.Ins)}[{__n}]"; + for (int i = 0; i < type.Dimension - 1; i++) + { + typeStr += "[]"; + } + } + return $"{{ com.google.gson.JsonArray _json{depth}_ = {json}.getAsJsonArray(); int {__n} = _json{depth}_.size(); {x} = new {typeStr}; int {__index}=0; for(JsonElement {__e} : _json{depth}_) {{ {type.ElementType.Apply(JavaDefineTypeName.Ins)} {__v}; {type.ElementType.Apply(this, $"{__e}", $"{__v}", depth + 1)} {x}[{__index}++] = {__v}; }} }}"; } - public string Accept(TList type, string json, string x) + public string Accept(TList type, string json, string x, int depth) { - return $"{{ com.google.gson.JsonArray _json0_ = {json}.getAsJsonArray(); {x} = new {type.Apply(JavaDefineTypeName.Ins)}(_json0_.size()); for(JsonElement __e : _json0_) {{ {type.ElementType.Apply(JavaDefineTypeName.Ins)} __v; {type.ElementType.Apply(this, "__e", "__v")} {x}.add(__v); }} }}"; + string __e = $"_e{depth}"; + string __v = $"_v{depth}"; + return $"{{ com.google.gson.JsonArray _json{depth}_ = {json}.getAsJsonArray(); {x} = new {type.Apply(JavaDefineTypeName.Ins)}(_json{depth}_.size()); for(JsonElement {__e} : _json{depth}_) {{ {type.ElementType.Apply(JavaDefineTypeName.Ins)} {__v}; {type.ElementType.Apply(this, __e, __v, depth + 1)} {x}.add({__v}); }} }}"; } - public string Accept(TSet type, string json, string x) + public string Accept(TSet type, string json, string x, int depth) { - return $"{{ com.google.gson.JsonArray _json0_ = {json}.getAsJsonArray(); {x} = new {type.Apply(JavaDefineTypeName.Ins)}(_json0_.size()); for(JsonElement __e : _json0_) {{ {type.ElementType.Apply(JavaDefineTypeName.Ins)} __v; {type.ElementType.Apply(this, "__e", "__v")} {x}.add(__v); }} }}"; + string __e = $"_e{depth}"; + string __v = $"_v{depth}"; + return $"{{ com.google.gson.JsonArray _json{depth}_ = {json}.getAsJsonArray(); {x} = new {type.Apply(JavaDefineTypeName.Ins)}(_json{depth}_.size()); for(JsonElement {__e} : _json{depth}_) {{ {type.ElementType.Apply(JavaDefineTypeName.Ins)} {__v}; {type.ElementType.Apply(this, __e, __v, depth + 1)} {x}.add({__v}); }} }}"; } - public string Accept(TMap type, string json, string x) + public string Accept(TMap type, string json, string x, int depth) { - return @$"{{ com.google.gson.JsonArray _json0_ = {json}.getAsJsonArray(); {x} = new {type.Apply(JavaDefineTypeName.Ins)}(_json0_.size()); for(JsonElement __e : _json0_) {{ {type.KeyType.Apply(JavaDefineTypeName.Ins)} __k; {type.KeyType.Apply(this, "__e.getAsJsonArray().get(0)", "__k")} {type.ValueType.Apply(JavaDefineTypeName.Ins)} __v; {type.ValueType.Apply(this, "__e.getAsJsonArray().get(1)", "__v")} {x}.put(__k, __v); }} }}"; + string __e = $"_e{depth}"; + string __k = $"_k{depth}"; + string __v = $"_v{depth}"; + return @$"{{ com.google.gson.JsonArray _json{depth}_ = {json}.getAsJsonArray(); {x} = new {type.Apply(JavaDefineTypeName.Ins)}(_json{depth}_.size()); for(JsonElement {__e} : _json{depth}_) {{ {type.KeyType.Apply(JavaDefineTypeName.Ins)} {__k}; {type.KeyType.Apply(this, $"{__e}.getAsJsonArray().get(0)", __k, depth + 1)} {type.ValueType.Apply(JavaDefineTypeName.Ins)} {__v}; {type.ValueType.Apply(this, $"{__e}.getAsJsonArray().get(1)", __v, depth + 1)} {x}.put({__k}, {__v}); }} }}"; } - public string Accept(TVector2 type, string json, string x) + public string Accept(TVector2 type, string json, string x, int depth) { - return $"{{ com.google.gson.JsonObject _json0_ = {json}.getAsJsonObject(); float __x; {TFloat.Ins.Apply(this, "_json0_.get(\"x\")", "__x") } float __y; {TFloat.Ins.Apply(this, "_json0_.get(\"y\")", "__y") } {x} = new {type.Apply(JavaDefineTypeName.Ins)}(__x, __y); }}"; + return $"{{ com.google.gson.JsonObject _json{depth}_ = {json}.getAsJsonObject(); float __x; {TFloat.Ins.Apply(this, $"_json{depth}_.get(\"x\")", "__x", depth)} float __y; {TFloat.Ins.Apply(this, $"_json{depth}_.get(\"y\")", "__y", depth)} {x} = new {type.Apply(JavaDefineTypeName.Ins)}(__x, __y); }}"; } - public string Accept(TVector3 type, string json, string x) + public string Accept(TVector3 type, string json, string x, int depth) { - return $"{{ com.google.gson.JsonObject _json0_ = {json}.getAsJsonObject(); float __x; {TFloat.Ins.Apply(this, "_json0_.get(\"x\")", "__x") } float __y; {TFloat.Ins.Apply(this, "_json0_.get(\"y\")", "__y") } float __z; {TFloat.Ins.Apply(this, "_json0_.get(\"z\")", "__z") } {x} = new {type.Apply(JavaDefineTypeName.Ins)}(__x, __y,__z); }}"; + return $"{{ com.google.gson.JsonObject _json{depth}_ = {json}.getAsJsonObject(); float __x; {TFloat.Ins.Apply(this, $"_json{depth}_.get(\"x\")", "__x", depth)} float __y; {TFloat.Ins.Apply(this, $"_json{depth}_.get(\"y\")", "__y", depth)} float __z; {TFloat.Ins.Apply(this, $"_json{depth}_.get(\"z\")", "__z", depth)} {x} = new {type.Apply(JavaDefineTypeName.Ins)}(__x, __y,__z); }}"; } - public string Accept(TVector4 type, string json, string x) + public string Accept(TVector4 type, string json, string x, int depth) { - return $"{{ com.google.gson.JsonObject _json0_ = {json}.getAsJsonObject(); float __x; {TFloat.Ins.Apply(this, "_json0_.get(\"x\")", "__x") } float __y; {TFloat.Ins.Apply(this, "_json0_.get(\"y\")", "__y") } float __z; {TFloat.Ins.Apply(this, "_json0_.get(\"z\")", "__z") } float __w; {TFloat.Ins.Apply(this, "_json0_.get(\"w\")", "__w") } {x} = new {type.Apply(JavaDefineTypeName.Ins)}(__x, __y, __z, __w); }}"; + return $"{{ com.google.gson.JsonObject _json{depth}_ = {json}.getAsJsonObject(); float __x; {TFloat.Ins.Apply(this, $"_json{depth}_.get(\"x\")", "__x", depth)} float __y; {TFloat.Ins.Apply(this, $"_json{depth}_.get(\"y\")", "__y", depth)} float __z; {TFloat.Ins.Apply(this, $"_json{depth}_.get(\"z\")", "__z", depth)} float __w; {TFloat.Ins.Apply(this, $"_json{depth}_.get(\"w\")", "__w", depth)} {x} = new {type.Apply(JavaDefineTypeName.Ins)}(__x, __y, __z, __w); }}"; } - public string Accept(TDateTime type, string json, string x) + public string Accept(TDateTime type, string json, string x, int depth) { return $"{x} = {json}.getAsInt();"; } diff --git a/src/Luban.Job.Cfg/Source/Utils/TTypeTemplateExtends.cs b/src/Luban.Job.Cfg/Source/Utils/TTypeTemplateExtends.cs index 469baed..3089b53 100644 --- a/src/Luban.Job.Cfg/Source/Utils/TTypeTemplateExtends.cs +++ b/src/Luban.Job.Cfg/Source/Utils/TTypeTemplateExtends.cs @@ -111,11 +111,11 @@ namespace Luban.Job.Cfg.Utils { if (type.IsNullable) { - return $"{{ if ({jsonName}.has(\"{jsonFieldName}\") && !{jsonName}.get(\"{jsonFieldName}\").isJsonNull()) {{ {type.Apply(TypeVisitors.JavaJsonDeserialize.Ins, $"{jsonName}.get(\"{jsonFieldName}\")", fieldName)} }} else {{ {fieldName} = null; }} }}"; + return $"{{ if ({jsonName}.has(\"{jsonFieldName}\") && !{jsonName}.get(\"{jsonFieldName}\").isJsonNull()) {{ {type.Apply(TypeVisitors.JavaJsonDeserialize.Ins, $"{jsonName}.get(\"{jsonFieldName}\")", fieldName, 0)} }} else {{ {fieldName} = null; }} }}"; } else { - return type.Apply(TypeVisitors.JavaJsonDeserialize.Ins, $"{jsonName}.get(\"{jsonFieldName}\")", fieldName); + return type.Apply(TypeVisitors.JavaJsonDeserialize.Ins, $"{jsonName}.get(\"{jsonFieldName}\")", fieldName, 0); } } diff --git a/src/Luban.Job.Common/Source/TypeVisitors/JavaDeserializeVisitor.cs b/src/Luban.Job.Common/Source/TypeVisitors/JavaDeserializeVisitor.cs index 2db7dde..4b781cc 100644 --- a/src/Luban.Job.Common/Source/TypeVisitors/JavaDeserializeVisitor.cs +++ b/src/Luban.Job.Common/Source/TypeVisitors/JavaDeserializeVisitor.cs @@ -11,11 +11,11 @@ namespace Luban.Job.Common.TypeVisitors { if (type.IsNullable) { - return $"if({bufName}.readBool()){{ {type.Apply(JavaUnderingDeserializeVisitor.Ins, bufName, fieldName)} }} else {{ {fieldName} = null; }}"; + return $"if({bufName}.readBool()){{ {type.Apply(JavaUnderingDeserializeVisitor.Ins, bufName, fieldName, 0)} }} else {{ {fieldName} = null; }}"; } else { - return type.Apply(JavaUnderingDeserializeVisitor.Ins, bufName, fieldName); + return type.Apply(JavaUnderingDeserializeVisitor.Ins, bufName, fieldName, 0); } } } diff --git a/src/Luban.Job.Common/Source/TypeVisitors/JavaUnderingDeserializeVisitor.cs b/src/Luban.Job.Common/Source/TypeVisitors/JavaUnderingDeserializeVisitor.cs index a2280ba..2938c20 100644 --- a/src/Luban.Job.Common/Source/TypeVisitors/JavaUnderingDeserializeVisitor.cs +++ b/src/Luban.Job.Common/Source/TypeVisitors/JavaUnderingDeserializeVisitor.cs @@ -3,122 +3,151 @@ using Luban.Job.Common.TypeVisitors; namespace Luban.Job.Common.TypeVisitors { - class JavaUnderingDeserializeVisitor : ITypeFuncVisitor + class JavaUnderingDeserializeVisitor : ITypeFuncVisitor { public static JavaUnderingDeserializeVisitor Ins { get; } = new JavaUnderingDeserializeVisitor(); - public string Accept(TBool type, string bufName, string fieldName) + public string Accept(TBool type, string bufName, string fieldName, int depth) { return $"{fieldName} = {bufName}.readBool();"; } - public string Accept(TByte type, string bufName, string fieldName) + public string Accept(TByte type, string bufName, string fieldName, int depth) { return $"{fieldName} = {bufName}.readByte();"; } - public string Accept(TShort type, string bufName, string fieldName) + public string Accept(TShort type, string bufName, string fieldName, int depth) { return $"{fieldName} = {bufName}.readShort();"; } - public string Accept(TFshort type, string bufName, string fieldName) + public string Accept(TFshort type, string bufName, string fieldName, int depth) { return $"{fieldName} = {bufName}.readFshort();"; } - public string Accept(TInt type, string bufName, string fieldName) + public string Accept(TInt type, string bufName, string fieldName, int depth) { return $"{fieldName} = {bufName}.readInt();"; } - public string Accept(TFint type, string bufName, string fieldName) + public string Accept(TFint type, string bufName, string fieldName, int depth) { return $"{fieldName} = {bufName}.readFint();"; } - public string Accept(TLong type, string bufName, string fieldName) + public string Accept(TLong type, string bufName, string fieldName, int depth) { return $"{fieldName} = {bufName}.readLong();"; } - public string Accept(TFlong type, string bufName, string fieldName) + public string Accept(TFlong type, string bufName, string fieldName, int depth) { return $"{fieldName} = {bufName}.readFlong();"; } - public string Accept(TFloat type, string bufName, string fieldName) + public string Accept(TFloat type, string bufName, string fieldName, int depth) { return $"{fieldName} = {bufName}.readFloat();"; } - public string Accept(TDouble type, string bufName, string fieldName) + public string Accept(TDouble type, string bufName, string fieldName, int depth) { return $"{fieldName} = {bufName}.readDouble();"; } - public string Accept(TEnum type, string bufName, string fieldName) + public string Accept(TEnum type, string bufName, string fieldName, int depth) { return $"{fieldName} = {bufName}.readInt();"; } - public string Accept(TString type, string bufName, string fieldName) + public string Accept(TString type, string bufName, string fieldName, int depth) { return $"{fieldName} = {bufName}.readString();"; } - public string Accept(TBytes type, string bufName, string fieldName) + public string Accept(TBytes type, string bufName, string fieldName, int depth) { return $"{fieldName} = {bufName}.readBytes();"; } - public string Accept(TText type, string bufName, string fieldName) + public string Accept(TText type, string bufName, string fieldName, int depth) { return $"{bufName}.readString(); {fieldName} = {bufName}.readString();"; } - public string Accept(TBean type, string bufName, string fieldName) + public string Accept(TBean type, string bufName, string fieldName, int depth) { return $"{fieldName} = {type.Bean.FullNameWithTopModule}.deserialize{type.Bean.Name}({bufName});"; } - public string Accept(TArray type, string bufName, string fieldName) + public string Accept(TArray type, string bufName, string fieldName, int depth) { - return $"{{int n = Math.min({bufName}.readSize(), {bufName}.size());{fieldName} = new {type.ElementType.Apply(JavaDefineTypeName.Ins)}[n];for(int i = 0 ; i < n ; i++) {{ {type.ElementType.Apply(JavaDefineTypeName.Ins)} _e;{type.ElementType.Apply(this, bufName, "_e")} {fieldName}[i] = _e;}}}}"; + string __n = $"__n{depth}"; + string __e = $"__e{depth}"; + string __v = $"__v{depth}"; + string __i = $"__i{depth}"; + string typeStr = $"{type.ElementType.Apply(CsDefineTypeName.Ins)}[{__n}]"; + if (type.Dimension > 1) + { + if (type.FinalElementType == null) + { + throw new System.Exception("多维数组没有元素类型"); + } + typeStr = $"{type.FinalElementType.Apply(CsUnderingDefineTypeName.Ins)}[{__n}]"; + for (int i = 0; i < type.Dimension - 1; i++) + { + typeStr += "[]"; + } + } + return $"{{int {__n} = Math.min({bufName}.readSize(), {bufName}.size());{fieldName} = new {type.ElementType.Apply(JavaDefineTypeName.Ins)}[{__n}];for(int {__i} = 0 ; {__i} < {__n} ; {__i}++) {{ {type.ElementType.Apply(JavaDefineTypeName.Ins)} {__e};{type.ElementType.Apply(this, bufName, __e, depth + 1)} {fieldName}[{__i}] = {__e};}}}}"; } - public string Accept(TList type, string bufName, string fieldName) + public string Accept(TList type, string bufName, string fieldName, int depth) { - return $"{{int n = Math.min({bufName}.readSize(), {bufName}.size());{fieldName} = new {type.Apply(JavaDefineTypeName.Ins)}(n);for(int i = 0 ; i < n ; i++) {{ {type.ElementType.Apply(JavaBoxDefineTypeName.Ins)} _e; {type.ElementType.Apply(this, bufName, "_e")} {fieldName}.add(_e);}}}}"; + string __n = $"__n{depth}"; + string __e = $"__e{depth}"; + string __v = $"__v{depth}"; + string __i = $"__i{depth}"; + return $"{{int {__n} = Math.min({bufName}.readSize(), {bufName}.size());{fieldName} = new {type.Apply(JavaDefineTypeName.Ins)}({__n});for(int {__i} = 0 ; {__i} < {__n} ; {__i}++) {{ {type.ElementType.Apply(JavaBoxDefineTypeName.Ins)} {__e}; {type.ElementType.Apply(this, bufName, __e, depth + 1)} {fieldName}.add({__e});}}}}"; } - public string Accept(TSet type, string bufName, string fieldName) + public string Accept(TSet type, string bufName, string fieldName, int depth) { - return $"{{int n = Math.min({bufName}.readSize(), {bufName}.size());{fieldName} = new {type.Apply(JavaDefineTypeName.Ins)}(n * 3 / 2);for(int i = 0 ; i < n ; i++) {{ {type.ElementType.Apply(JavaBoxDefineTypeName.Ins)} _e; {type.ElementType.Apply(this, bufName, "_e")} {fieldName}.add(_e);}}}}"; + string __n = $"__n{depth}"; + string __e = $"__e{depth}"; + string __v = $"__v{depth}"; + string __i = $"__i{depth}"; + return $"{{int {__n} = Math.min({bufName}.readSize(), {bufName}.size());{fieldName} = new {type.Apply(JavaDefineTypeName.Ins)}({__n} * 3 / 2);for(int {__i} = 0 ; {__i} < {__n} ; {__i}++) {{ {type.ElementType.Apply(JavaBoxDefineTypeName.Ins)} {__e}; {type.ElementType.Apply(this, bufName, __e, depth + 1)} {fieldName}.add({__e});}}}}"; } - public string Accept(TMap type, string bufName, string fieldName) + public string Accept(TMap type, string bufName, string fieldName, int depth) { - return $"{{int n = Math.min({bufName}.readSize(), {bufName}.size());{fieldName} = new {type.Apply(JavaDefineTypeName.Ins)}(n * 3 / 2);for(int i = 0 ; i < n ; i++) {{ {type.KeyType.Apply(JavaBoxDefineTypeName.Ins)} _k; {type.KeyType.Apply(this, bufName, "_k")} {type.ValueType.Apply(JavaBoxDefineTypeName.Ins)} _v; {type.ValueType.Apply(this, bufName, "_v")} {fieldName}.put(_k, _v);}}}}"; + string __n = $"__n{depth}"; + string __k = $"__k{depth}"; + string __v = $"__v{depth}"; + string __i = $"__i{depth}"; + return $"{{int {__n} = Math.min({bufName}.readSize(), {bufName}.size());{fieldName} = new {type.Apply(JavaDefineTypeName.Ins)}({__n} * 3 / 2);for(int {__i} = 0 ; {__i} < {__n} ; {__i}++) {{ {type.KeyType.Apply(JavaBoxDefineTypeName.Ins)} {__k}; {type.KeyType.Apply(this, bufName, __k, depth + 1)} {type.ValueType.Apply(JavaBoxDefineTypeName.Ins)} {__v}; {type.ValueType.Apply(this, bufName, __v, depth + 1)} {fieldName}.put({__k}, {__v});}}}}"; } - public string Accept(TVector2 type, string bufName, string fieldName) + public string Accept(TVector2 type, string bufName, string fieldName, int depth) { return $"{fieldName} = {bufName}.readVector2();"; } - public string Accept(TVector3 type, string bufName, string fieldName) + public string Accept(TVector3 type, string bufName, string fieldName, int depth) { return $"{fieldName} = {bufName}.readVector3();"; } - public string Accept(TVector4 type, string bufName, string fieldName) + public string Accept(TVector4 type, string bufName, string fieldName, int depth) { return $"{fieldName} = {bufName}.readVector4();"; } - public string Accept(TDateTime type, string bufName, string fieldName) + public string Accept(TDateTime type, string bufName, string fieldName, int depth) { return $"{fieldName} = {bufName}.readInt();"; }