diff --git a/src/Luban.Job.Cfg/Source/Defs/TTypeTemplateExtends.cs b/src/Luban.Job.Cfg/Source/Defs/TTypeTemplateExtends.cs index 800f6cc..8af03d2 100644 --- a/src/Luban.Job.Cfg/Source/Defs/TTypeTemplateExtends.cs +++ b/src/Luban.Job.Cfg/Source/Defs/TTypeTemplateExtends.cs @@ -12,7 +12,7 @@ namespace Luban.Job.Cfg.Defs { if (type.IsNullable) { - return $"{{ var _j = {bufName}.GetProperty(\"{jsonFieldName}\"); if (_j.ValueKind != JsonValueKind.Null) {{ {type.Apply(TypeVisitors.CsJsonDeserialize.Ins, "_j", fieldName)} }} else {{ {fieldName} = null; }} }}"; + return $"{{ if ({bufName}.TryGetProperty(\"{jsonFieldName}\", out var _j) && _j.ValueKind != JsonValueKind.Null) {{ {type.Apply(TypeVisitors.CsJsonDeserialize.Ins, "_j", fieldName)} }} else {{ {fieldName} = null; }} }}"; } else { @@ -156,14 +156,52 @@ namespace Luban.Job.Cfg.Defs } } - public static string Py3Deserialize(string fieldName, string jsonFieldName, TType type) + public static string Py3DeserializeValue(string fieldName, string jsonVarName, TType type) { - return type.Apply(PyDeserializeVisitor.Py3Ins, $"{jsonFieldName}", fieldName); + if (type.IsNullable) + { + return $"if {jsonVarName} != None: {type.Apply(PyUnderingDeserializeVisitor.Py3Ins, jsonVarName, fieldName)}"; + } + else + { + return type.Apply(PyUnderingDeserializeVisitor.Py3Ins, jsonVarName, fieldName); + } } - public static string Py27Deserialize(string fieldName, string jsonFieldName, TType type) + public static string Py27DeserializeValue(string fieldName, string jsonVarName, TType type) { - return type.Apply(PyDeserializeVisitor.Py27Ins, $"{jsonFieldName}", fieldName); + if (type.IsNullable) + { + return $"if {jsonVarName} != None: {type.Apply(PyUnderingDeserializeVisitor.Py27Ins, jsonVarName, fieldName)}"; + } + else + { + return type.Apply(PyUnderingDeserializeVisitor.Py3Ins, jsonVarName, fieldName); + } + } + + public static string Py3DeserializeField(string fieldName, string jsonVarName, string jsonFieldName, TType type) + { + if (type.IsNullable) + { + return $"if {jsonVarName}.get('{jsonFieldName}') != None: {type.Apply(PyUnderingDeserializeVisitor.Py3Ins, $"{jsonVarName}['{jsonFieldName}']", fieldName)}"; + } + else + { + return type.Apply(PyUnderingDeserializeVisitor.Py3Ins, $"{jsonVarName}['{jsonFieldName}']", fieldName); + } + } + + public static string Py27DeserializeField(string fieldName, string jsonVarName, string jsonFieldName, TType type) + { + if (type.IsNullable) + { + return $"if {jsonVarName}.get('{jsonFieldName}') != None: {type.Apply(PyUnderingDeserializeVisitor.Py3Ins, $"{jsonVarName}['{jsonFieldName}']", fieldName)}"; + } + else + { + return type.Apply(PyUnderingDeserializeVisitor.Py27Ins, $"{jsonVarName}['{jsonFieldName}']", fieldName); + } } } } diff --git a/src/Luban.Job.Cfg/Source/Generate/Python3CodeJsonRender.cs b/src/Luban.Job.Cfg/Source/Generate/Python3CodeJsonRender.cs index 0b327cb..670e6de 100644 --- a/src/Luban.Job.Cfg/Source/Generate/Python3CodeJsonRender.cs +++ b/src/Luban.Job.Cfg/Source/Generate/Python3CodeJsonRender.cs @@ -48,10 +48,7 @@ class {{name}} {{if parent_def_type}}({{parent_def_type.py_full_name}}){{else if {{parent_def_type.py_full_name}}.__init__(self, _json_) {{~end~}} {{~ for field in export_fields ~}} - {{~if !field.ctype.is_nullable~}} - if _json_['{{field.name}}'] == None: raise Exception() - {{~end~}} - {{py3_deserialize ('self.' + field.py_style_name) ('_json_[""' + field.name + '""]') field.ctype}} + {{py3_deserialize_field ('self.' + field.py_style_name) '_json_' field.name field.ctype}} {{~end~}} {{~if export_fields.empty?}} pass @@ -83,7 +80,7 @@ class {{name}}: self._dataList = [] for _json2_ in _json_: - {{py3_deserialize '_v' '_json2_' value_type}} + {{py3_deserialize_value '_v' '_json2_' value_type}} self._dataList.append(_v) self._dataMap[_v.{{x.index_field.py_style_name}}] = _v @@ -96,7 +93,7 @@ class {{name}}: def __init__(self, _json_): if (len(_json_) != 1): raise Exception('table mode=one, but size != 1') - {{py3_deserialize 'self._data' '_json_[0]' value_type}} + {{py3_deserialize_value 'self._data' '_json_[0]' value_type}} def getData(self) : return self._data diff --git a/src/Luban.Job.Cfg/Source/Generate/PythonCodeRenderBase.cs b/src/Luban.Job.Cfg/Source/Generate/PythonCodeRenderBase.cs index 1b32d0e..a876fd2 100644 --- a/src/Luban.Job.Cfg/Source/Generate/PythonCodeRenderBase.cs +++ b/src/Luban.Job.Cfg/Source/Generate/PythonCodeRenderBase.cs @@ -55,7 +55,7 @@ class {{x.py_full_name}}: {{comment}} ''' {{~end~}} -class {{py_full_name}}: +class {{py_full_name}}(Enum): {{~ for item in items ~}} {{~if item.comment != '' ~}} '''