From bea3edf0cadcda974e069980a9e68b7500207f46 Mon Sep 17 00:00:00 2001 From: walon Date: Fri, 20 Aug 2021 16:59:52 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E8=B0=83=E6=95=B4=E3=80=91cfg=20data?= =?UTF-8?q?=5Fjson2=E8=BE=93=E5=87=BA=E7=9A=84json=E9=A1=B6=E5=B1=82?= =?UTF-8?q?=E7=BB=93=E6=9E=84=E7=94=B1array=E8=B0=83=E6=95=B4=E4=B8=BAobje?= =?UTF-8?q?ct?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Source/DataExporters/JsonExportor.cs | 47 ++++++++++++++++++- .../Source/Utils/DataExporterUtil.cs | 4 +- 2 files changed, 48 insertions(+), 3 deletions(-) diff --git a/src/Luban.Job.Cfg/Source/DataExporters/JsonExportor.cs b/src/Luban.Job.Cfg/Source/DataExporters/JsonExportor.cs index 857a44e..190ebfa 100644 --- a/src/Luban.Job.Cfg/Source/DataExporters/JsonExportor.cs +++ b/src/Luban.Job.Cfg/Source/DataExporters/JsonExportor.cs @@ -11,7 +11,7 @@ namespace Luban.Job.Cfg.DataExporters { public static JsonExportor Ins { get; } = new JsonExportor(); - public void WriteList(List datas, DefAssembly ass, Utf8JsonWriter x) + public void WriteAsArray(List datas, DefAssembly ass, Utf8JsonWriter x) { x.WriteStartArray(); foreach (var d in datas) @@ -21,6 +21,51 @@ 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) + { + case RawDefs.ETableMode.ONE: + { + this.Accept(datas[0].Data, ass, x); + break; + } + case RawDefs.ETableMode.MAP: + { + + x.WriteStartObject(); + string indexName = table.IndexField.Name; + foreach (var rec in datas) + { + var indexFieldData = rec.Data.GetField(indexName); + + x.WritePropertyName(ToStringValue(indexFieldData)); + this.Accept(rec.Data, ass, x); + } + + x.WriteEndObject(); + break; + } + default: + { + throw new NotSupportedException($"not support table mode:{table.Mode}"); + } + } + } + public void Accept(DBool type, DefAssembly ass, Utf8JsonWriter x) { x.WriteBooleanValue(type.Value); diff --git a/src/Luban.Job.Cfg/Source/Utils/DataExporterUtil.cs b/src/Luban.Job.Cfg/Source/Utils/DataExporterUtil.cs index 2ecb186..6dcd02e 100644 --- a/src/Luban.Job.Cfg/Source/Utils/DataExporterUtil.cs +++ b/src/Luban.Job.Cfg/Source/Utils/DataExporterUtil.cs @@ -44,12 +44,12 @@ namespace Luban.Job.Cfg.Utils }); if (dataType == "data_json") { - JsonExportor.Ins.WriteList(records, table.Assembly, jsonWriter); + JsonExportor.Ins.WriteAsArray(records, table.Assembly, jsonWriter); } else { - Json2Exportor.Ins.WriteList(records, table.Assembly, jsonWriter); + Json2Exportor.Ins.WriteAsObject(table, records, table.Assembly, jsonWriter); } jsonWriter.Flush(); return System.Text.Encoding.UTF8.GetString(DataUtil.StreamToBytes(ss));