diff --git a/src/Luban.Job.Cfg/Source/DataExporters/JsonExportor.cs b/src/Luban.Job.Cfg/Source/DataExporters/JsonExportor.cs index 190ebfa..e67a18f 100644 --- a/src/Luban.Job.Cfg/Source/DataExporters/JsonExportor.cs +++ b/src/Luban.Job.Cfg/Source/DataExporters/JsonExportor.cs @@ -21,19 +21,6 @@ namespace Luban.Job.Cfg.DataExporters x.WriteEndArray(); } - public string ToStringValue(DType data) - { - switch (data) - { - case DInt dint: return dint.Value.ToString(); - case DLong dlong: return dlong.Value.ToString(); - case DString dstring: return dstring.Value; - case DEnum denum: return denum.Value.ToString(); - case DShort dshort: return dshort.Value.ToString(); - default: throw new NotSupportedException($"data_json2 not support key type:{data.GetType().Name}"); - } - } - public void WriteAsObject(DefTable table, List datas, DefAssembly ass, Utf8JsonWriter x) { switch (table.Mode) @@ -52,7 +39,7 @@ namespace Luban.Job.Cfg.DataExporters { var indexFieldData = rec.Data.GetField(indexName); - x.WritePropertyName(ToStringValue(indexFieldData)); + x.WritePropertyName(indexFieldData.Apply(ToJsonPropertyNameVisitor.Ins)); this.Accept(rec.Data, ass, x); } diff --git a/src/Luban.Job.Cfg/Source/DataVisitors/ToJsonPropertyNameVisitor.cs b/src/Luban.Job.Cfg/Source/DataVisitors/ToJsonPropertyNameVisitor.cs new file mode 100644 index 0000000..c81312b --- /dev/null +++ b/src/Luban.Job.Cfg/Source/DataVisitors/ToJsonPropertyNameVisitor.cs @@ -0,0 +1,129 @@ +using Luban.Job.Cfg.Datas; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Luban.Job.Cfg.DataVisitors +{ + class ToJsonPropertyNameVisitor : IDataFuncVisitor + { + public static ToJsonPropertyNameVisitor Ins { get; } = new(); + + public string Accept(DBool type) + { + throw new NotSupportedException(); + } + + public string Accept(DByte type) + { + return type.Value.ToString(); + } + + public string Accept(DShort type) + { + return type.Value.ToString(); + } + + public string Accept(DFshort type) + { + return type.Value.ToString(); + } + + public string Accept(DInt type) + { + return type.Value.ToString(); + } + + public string Accept(DFint type) + { + return type.Value.ToString(); + } + + public string Accept(DLong type) + { + return type.Value.ToString(); + } + + public string Accept(DFlong type) + { + return type.Value.ToString(); + } + + public string Accept(DFloat type) + { + throw new NotSupportedException(); + } + + public string Accept(DDouble type) + { + throw new NotSupportedException(); + } + + public string Accept(DEnum type) + { + return type.Value.ToString(); + } + + public string Accept(DString type) + { + return type.Value.ToString(); + } + + public string Accept(DBytes type) + { + throw new NotSupportedException(); + } + + public string Accept(DText type) + { + throw new NotSupportedException(); + } + + public string Accept(DBean type) + { + throw new NotSupportedException(); + } + + public string Accept(DArray type) + { + throw new NotSupportedException(); + } + + public string Accept(DList type) + { + throw new NotSupportedException(); + } + + public string Accept(DSet type) + { + throw new NotSupportedException(); + } + + public string Accept(DMap type) + { + throw new NotSupportedException(); + } + + public string Accept(DVector2 type) + { + throw new NotSupportedException(); + } + + public string Accept(DVector3 type) + { + throw new NotSupportedException(); + } + + public string Accept(DVector4 type) + { + throw new NotSupportedException(); + } + + public string Accept(DDateTime type) + { + throw new NotSupportedException(); + } + } +} diff --git a/src/Luban.Job.Cfg/Source/JobController.cs b/src/Luban.Job.Cfg/Source/JobController.cs index 1a61b50..e4b8483 100644 --- a/src/Luban.Job.Cfg/Source/JobController.cs +++ b/src/Luban.Job.Cfg/Source/JobController.cs @@ -46,7 +46,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_cs_unity_json,code_lua_bin,code_java_bin,code_go_bin,code_go_json,code_cpp_bin,code_python3_json,code_typescript_bin,code_typescript_json,data_bin,data_lua,data_json,data_json2,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_python3_json,code_typescript_bin,code_typescript_json,data_bin,data_lua,data_json,data_json_monolithic,data_resources . can be multi")] public string GenType { get; set; } [Option('s', "service", Required = true, HelpText = "service")] @@ -126,8 +126,7 @@ namespace Luban.Job.Cfg switch (genType) { case "data_bin": return "bin"; - case "data_json": - case "data_json2": return "json"; + case "data_json": return "json"; case "data_lua": return "lua"; default: throw new Exception($"not support output data type:{genType}"); } @@ -168,7 +167,8 @@ namespace Luban.Job.Cfg errMsg = "--outputcodedir missing"; return false; } - if (genTypes.Any(t => t.StartsWith("data_", StringComparison.Ordinal))) + List dataGenTypes = genTypes.Where(t => t.StartsWith("data_", StringComparison.Ordinal)).ToList(); + if (dataGenTypes.Count > 0) { if (string.IsNullOrWhiteSpace(inputDataDir)) { @@ -384,7 +384,6 @@ namespace Luban.Job.Cfg } case "data_bin": case "data_json": - case "data_json2": case "data_lua": { await CheckLoadCfgDataAsync(); @@ -787,7 +786,7 @@ namespace {ctx.TopModule} { allJsonTask.Add(Task.Run(() => { - return (string)DataExporterUtil.ToOutputData(c, ctx.Assembly.GetTableExportDataList(c), "data_json2"); + return (string)DataExporterUtil.ToOutputData(c, ctx.Assembly.GetTableExportDataList(c), "data_json"); })); } @@ -800,7 +799,7 @@ namespace {ctx.TopModule} { lines.Add(","); } - lines.Add($"\"{exportTables[i].NeedExport}\":"); + lines.Add($"\"{exportTables[i].FullName}\":"); lines.Add(await allJsonTask[i]); } lines.Add("}"); diff --git a/src/Luban.Job.Cfg/Source/Utils/DataExporterUtil.cs b/src/Luban.Job.Cfg/Source/Utils/DataExporterUtil.cs index 6dcd02e..d69027e 100644 --- a/src/Luban.Job.Cfg/Source/Utils/DataExporterUtil.cs +++ b/src/Luban.Job.Cfg/Source/Utils/DataExporterUtil.cs @@ -30,7 +30,6 @@ namespace Luban.Job.Cfg.Utils return bytes; } case "data_json": - case "data_json2": { // data_json与data_json2格式区别在于 // data_json的map格式是 [[key1,value1],[] ..] @@ -42,15 +41,7 @@ namespace Luban.Job.Cfg.Utils SkipValidation = false, Encoder = System.Text.Encodings.Web.JavaScriptEncoder.Create(System.Text.Unicode.UnicodeRanges.All), }); - if (dataType == "data_json") - { - JsonExportor.Ins.WriteAsArray(records, table.Assembly, jsonWriter); - } - else - { - - Json2Exportor.Ins.WriteAsObject(table, records, table.Assembly, jsonWriter); - } + Json2Exportor.Ins.WriteAsObject(table, records, table.Assembly, jsonWriter); jsonWriter.Flush(); return System.Text.Encoding.UTF8.GetString(DataUtil.StreamToBytes(ss)); }