Skip to content

Commit

Permalink
升级Newtonsoft.Json 12.0.3->13.0.1;新增"AllString"选项,勾选后所有的Vaule都将转为Strin…
Browse files Browse the repository at this point in the history
…g(方便使用者按需解析使用,如(LitJson.JsonMapper.ToObject<List<Dictionary<string, string>>>(textAsset.text)))
  • Loading branch information
BlurFeng committed Aug 29, 2021
1 parent 0092834 commit 0797ceb
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 54 deletions.
2 changes: 1 addition & 1 deletion GUI/DataManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public void loadExcel(Program.Options options)
mCSharp = new CSDefineGenerator(excelPath, excel, options.ExcludePrefix);

//-- 导出JSON
mJson = new JsonExporter(excel, options.Lowcase, options.ExportArray, options.DateFormat, options.ForceSheetName, header, options.ExcludePrefix, options.CellJson);
mJson = new JsonExporter(excel, options.Lowcase, options.ExportArray, options.DateFormat, options.ForceSheetName, header, options.ExcludePrefix, options.CellJson, options.AllString);
}
}
}
87 changes: 50 additions & 37 deletions GUI/MainForm.Designer.cs

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions GUI/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ private void loadExcelAsync(string path)
options.ForceSheetName = this.comboBoxSheetName.SelectedIndex == 0;
options.ExcludePrefix = this.textBoxExculdePrefix.Text;
options.CellJson = this.checkBoxCellJson.Checked;
options.AllString = this.checkBoxAllString.Checked;

//-- start import
this.backgroundWorker.RunWorkerAsync(options);
Expand Down
30 changes: 18 additions & 12 deletions JsonExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public string context {
/// 构造函数:完成内部数据创建
/// </summary>
/// <param name="excel">ExcelLoader Object</param>
public JsonExporter(ExcelLoader excel, bool lowcase, bool exportArray, string dateFormat, bool forceSheetName, int headerRows, string excludePrefix, bool cellJson)
public JsonExporter(ExcelLoader excel, bool lowcase, bool exportArray, string dateFormat, bool forceSheetName, int headerRows, string excludePrefix, bool cellJson, bool allString)
{
mHeaderRows = headerRows - 1;
List<DataTable> validSheets = new List<DataTable>();
Expand All @@ -52,8 +52,7 @@ public JsonExporter(ExcelLoader excel, bool lowcase, bool exportArray, string da
{ // single sheet

//-- convert to object
object sheetValue = convertSheet(validSheets[0], exportArray, lowcase, excludePrefix, cellJson);

object sheetValue = convertSheet(validSheets[0], exportArray, lowcase, excludePrefix, cellJson, allString);
//-- convert to json string
mContext = JsonConvert.SerializeObject(sheetValue, jsonSettings);
}
Expand All @@ -63,7 +62,7 @@ public JsonExporter(ExcelLoader excel, bool lowcase, bool exportArray, string da
Dictionary<string, object> data = new Dictionary<string, object>();
foreach (var sheet in validSheets)
{
object sheetValue = convertSheet(sheet, exportArray, lowcase, excludePrefix, cellJson);
object sheetValue = convertSheet(sheet, exportArray, lowcase, excludePrefix, cellJson, allString);
data.Add(sheet.TableName, sheetValue);
}

Expand All @@ -72,15 +71,15 @@ public JsonExporter(ExcelLoader excel, bool lowcase, bool exportArray, string da
}
}

private object convertSheet(DataTable sheet, bool exportArray, bool lowcase, string excludePrefix, bool cellJson)
private object convertSheet(DataTable sheet, bool exportArray, bool lowcase, string excludePrefix, bool cellJson, bool allString)
{
if (exportArray)
return convertSheetToArray(sheet, lowcase, excludePrefix, cellJson);
return convertSheetToArray(sheet, lowcase, excludePrefix, cellJson, allString);
else
return convertSheetToDict(sheet, lowcase, excludePrefix, cellJson);
return convertSheetToDict(sheet, lowcase, excludePrefix, cellJson, allString);
}

private object convertSheetToArray(DataTable sheet, bool lowcase, string excludePrefix, bool cellJson)
private object convertSheetToArray(DataTable sheet, bool lowcase, string excludePrefix, bool cellJson, bool allString)
{
List<object> values = new List<object>();

Expand All @@ -90,7 +89,7 @@ private object convertSheetToArray(DataTable sheet, bool lowcase, string exclude
DataRow row = sheet.Rows[i];

values.Add(
convertRowToDict(sheet, row, lowcase, firstDataRow, excludePrefix, cellJson)
convertRowToDict(sheet, row, lowcase, firstDataRow, excludePrefix, cellJson, allString)
);
}

Expand All @@ -100,7 +99,7 @@ private object convertSheetToArray(DataTable sheet, bool lowcase, string exclude
/// <summary>
/// 以第一列为ID,转换成ID->Object的字典对象
/// </summary>
private object convertSheetToDict(DataTable sheet, bool lowcase, string excludePrefix, bool cellJson)
private object convertSheetToDict(DataTable sheet, bool lowcase, string excludePrefix, bool cellJson, bool allString)
{
Dictionary<string, object> importData =
new Dictionary<string, object>();
Expand All @@ -113,7 +112,7 @@ private object convertSheetToDict(DataTable sheet, bool lowcase, string excludeP
if (ID.Length <= 0)
ID = string.Format("row_{0}", i);

var rowObject = convertRowToDict(sheet, row, lowcase, firstDataRow, excludePrefix, cellJson);
var rowObject = convertRowToDict(sheet, row, lowcase, firstDataRow, excludePrefix, cellJson, allString);
// 多余的字段
// rowObject[ID] = ID;
importData[ID] = rowObject;
Expand All @@ -125,7 +124,7 @@ private object convertSheetToDict(DataTable sheet, bool lowcase, string excludeP
/// <summary>
/// 把一行数据转换成一个对象,每一列是一个属性
/// </summary>
private Dictionary<string, object> convertRowToDict(DataTable sheet, DataRow row, bool lowcase, int firstDataRow, string excludePrefix, bool cellJson)
private Dictionary<string, object> convertRowToDict(DataTable sheet, DataRow row, bool lowcase, int firstDataRow, string excludePrefix, bool cellJson, bool allString)
{
var rowData = new Dictionary<string, object>();
int col = 0;
Expand Down Expand Up @@ -167,6 +166,13 @@ private Dictionary<string, object> convertRowToDict(DataTable sheet, DataRow row
value = (int)num;
}

//全部转换为string
//方便LitJson.JsonMapper.ToObject<List<Dictionary<string, string>>>(textAsset.text)等使用方式 之后根据自己的需求进行解析
if (allString)
{
value = value.ToString();
}

string fieldName = column.ToString();
// 表头自动转换成小写
if (lowcase)
Expand Down
7 changes: 7 additions & 0 deletions Program.Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@ public bool CellJson {
get;
set;
}

[Option('l', "all_string", Required = false, DefaultValue = false, HelpText = "all string")]
public bool AllString
{
get;
set;
}
}
}
}
2 changes: 1 addition & 1 deletion Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ private static void Run(Options options)
ExcelLoader excel = new ExcelLoader(excelPath, header);

//-- export
JsonExporter exporter = new JsonExporter(excel, options.Lowcase, options.ExportArray, dateFormat, options.ForceSheetName, header, options.ExcludePrefix, options.CellJson);
JsonExporter exporter = new JsonExporter(excel, options.Lowcase, options.ExportArray, dateFormat, options.ForceSheetName, header, options.ExcludePrefix, options.CellJson, options.AllString);
exporter.SaveToFile(exportPath, cd);

//-- 生成C#定义文件
Expand Down
4 changes: 2 additions & 2 deletions excel2json.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@
<Reference Include="ICSharpCode.SharpZipLib, Version=1.2.0.246, Culture=neutral, PublicKeyToken=1b03e6acf1164f73, processorArchitecture=MSIL">
<HintPath>packages\SharpZipLib.1.2.0\lib\net45\ICSharpCode.SharpZipLib.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>packages\Newtonsoft.Json.12.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
<Reference Include="Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>packages\Newtonsoft.Json.13.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
Expand Down
2 changes: 1 addition & 1 deletion packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
<package id="ExcelDataReader" version="3.6.0" targetFramework="net451" />
<package id="ExcelDataReader.DataSet" version="3.6.0" targetFramework="net451" />
<package id="FCTB" version="2.16.24" targetFramework="net451" />
<package id="Newtonsoft.Json" version="12.0.3" targetFramework="net451" />
<package id="Newtonsoft.Json" version="13.0.1" targetFramework="net451" />
<package id="SharpZipLib" version="1.2.0" targetFramework="net451" />
</packages>

0 comments on commit 0797ceb

Please sign in to comment.