DataSet Serialize
用于Delphi和Lazarus (FPC)的JSON到数据集和数据集到JSON转换器
源码下载地址:https://github.com/viniciussanchez/dataset-serialize
uses DataSet.Serialize;
数据集序列为json
var LJSONArray: TJSONArray; LJSONObject: TJSONObject; begin LJSONObject := qrySamples.ToJSONObject(); // 导出单个记录 LJSONArray := qrySamples.ToJSONArray(); // 导出所有记录 end;
保存和加载数据集的结构
var LJSONArray: TJSONArray; begin LJSONArray := qrySamples.SaveStructure; qrySamples.LoadStructure(LJSONArray, True); end;
验证json
begin LJSONArray := qrySamples.ValidateJSON('{"country":"Brazil"}'); end;
从json加载
begin qrySamples.LoadFromJSON('{"firstName":"Vinicius Sanchez","country":"Brazil"}'); end;
从JSON修改记录
begin qrySamples.MergeFromJSONObject('{"firstName":"Vinicius","country":"United States"}'); end;
配置
- 日期输入为UTC(时区)
TDataSetSerializeConfig.GetInstance.DateInputIsUTC := True;
- 导出空值
TDataSetSerializeConfig.GetInstance.export.ExportNullValues := True;
- 仅导出可见字段
TDataSetSerializeConfig.GetInstance.export.ExportOnlyFieldsVisible := True;
- 将子数据集导出为 JSON 对象(当您只有 1 条记录时)
TDataSetSerializeConfig.GetInstance.export.ExportChildDataSetAsJsonObject := False;
- 仅导入可见的字段
TDataSetSerializeConfig.GetInstance.import.ImportOnlyFieldsVisible := True;
- 案例名称定义
// cndNone, cndLower, cndUpper, cndLowerCamelCase, cndUpperCamelCase
TDataSetSerializeConfig.GetInstance.CaseNameDefinition := cndLowerCamelCase;
cndNone:
[ { "MOB_ICADASTRO":11795,"MOB_EMI_REG":6,"CODIGODOCLIENTE":1,"CDOCUMEN1":"999999" } ]
cndLower:
[ { "mob_icadastro":11795,"mob_emi_"cododig6": :1,"cdocumen1":"999999" } ]
cndUpper:
[ { "MOB_ICADASTRO":11795,"MOB_EMI_REG":6,"CODIGODOCLIENTE":1,"CDOCUMEN1":"999999" } ]
cndLowerCamelCase"mobIcadastro":11795,"mobEmiReg":6,"codigodocliente":1,"cdocumen1":"999999" } ]
cndUpperCamelCase::
[ { "MobIcadastro":11795,"MobEmiReg":6,"Codigodocliente":1 "Cdocumen1":"999999" } ]
- 格式日期(导出字段类型等于 ftDate)
TDataSetSerializeConfig.GetInstance.export.FormatDate := ' YYYY-MM-DD ' ;
- 格式货币(导出字段类型等于 ftCurrency)
TDataSetSerializeConfig.GetInstance.export.FormatCurrency := ' 0.00## ' ;
- 定义数据集前缀
TDataSetSerializeConfig.GetInstance.DataSetPrefix := [ ' mt ' , ' qry ' ];