superobject 序列 数据集 之间转化

Easter79
• 阅读 704

superobject 序列数据集

unit uDBJson;

interface

{$HINTS OFF}

uses SysUtils, Classes, Variants, DB, DBClient, SuperObject;

type TTableJSon = class

private const cstFieldType = 'FieldType';

const cstFieldName = 'FieldName';

const cstFieldSize = 'FieldSize';

const cstJsonType = 'JsonType';

const cstRequired = 'Required';

const cstFieldIndex = 'FieldIndex';

const cstCols = 'Cols';

const cstData = 'Data';

public class function DataSetToJson(DataSet: TDataSet): ISuperObject; class function DataSetToJson2(DataSet: TDataSet): string; class function CreateFieldByJson(Fields: TFieldDefs; ColsJson: ISuperObject): Boolean; class procedure ImportDataFromJSon(DataSet: TDataSet; DataJson: ISuperObject); class function JSonToClientDataset(CDS: TClientDataSet; Json: ISuperObject) : Boolean; class function GetValue(Json: ISuperObject; const Name: string): Variant;

class function CreateJsonValue(Json: ISuperObject; const Name: string;
  const Value: Variant): Boolean;
class function CreateJsonValueByField(Json: ISuperObject;
  Field: TField): Boolean;
class function GetValue2Field(Field: TField;
  JsonValue: ISuperObject): Variant;

end;

implementation

uses TypInfo, encddecd;

{ TTableJSon }

class function TTableJSon.JSonToClientDataset(CDS: TClientDataSet; Json: ISuperObject): Boolean; var ColsJson: ISuperObject; begin Result := False; if Json = nil then Exit; CDS.Close; CDS.Data := Null; // 创建字段 ColsJson := Json.O[cstCols]; CreateFieldByJson(CDS.FieldDefs, ColsJson); if CDS.FieldDefs.Count > 0 then CDS.CreateDataSet; ImportDataFromJSon(CDS, Json.O[cstData]); Result := True; end;

class function TTableJSon.CreateFieldByJson(Fields: TFieldDefs; ColsJson: ISuperObject): Boolean; var SubJson: ISuperObject; ft: TFieldType; begin Result := False; Fields.DataSet.Close; Fields.Clear; for SubJson in ColsJson do begin ft := TFieldType(GetEnumValue(TypeInfo(TFieldType), 'ft' + SubJson.S[cstFieldType])); if ft = ftAutoInc then // 自增字段不能录入,必须更改 ft := ftInteger; Fields.Add(SubJson.S[cstFieldName], ft, SubJson.I[cstFieldSize], SubJson.B[cstRequired]); end; Result := True; end;

class function TTableJSon.CreateJsonValue(Json: ISuperObject; const Name: string; const Value: Variant): Boolean; begin Result := False; Json.O[Name] := SO(Value); Result := True; end;

class function TTableJSon.CreateJsonValueByField(Json: ISuperObject; Field: TField): Boolean; begin Result := False; if Field Is TDateTimeField then Json.O[Field.FieldName] := SO(Field.AsDateTime) else if Field is TBlobField then Json.S[Field.FieldName] := EncodeString(Field.AsString) else Json.O[Field.FieldName] := SO(Field.Value); Result := True; end;

class function TTableJSon.GetValue(Json: ISuperObject; const Name: string): Variant; begin case Json.DataType of stNull: Result := Null; stBoolean: Result := Json.B[Name]; stDouble: Result := Json.D[Name]; stCurrency: Result := Json.C[Name]; stInt: Result := Json.I[Name]; stString: Result := Json.S[Name]; end; end;

class function TTableJSon.GetValue2Field(Field: TField; JsonValue: ISuperObject): Variant; begin if JsonValue.DataType = stNull then Result := Null else if Field is TDateTimeField then Result := JavaToDelphiDateTime(JsonValue.AsInteger) else if (Field is TIntegerField) or (Field is TLargeintField) then Result := JsonValue.AsInteger else if Field is TNumericField then Result := JsonValue.AsDouble else if Field is TBooleanField then Result := JsonValue.AsBoolean else if Field is TStringField then Result := JsonValue.AsString else if Field is TBlobField then Result := DecodeString(JsonValue.AsString) end;

class procedure TTableJSon.ImportDataFromJSon(DataSet: TDataSet; DataJson: ISuperObject); var SubJson: ISuperObject; iter: TSuperObjectIter; begin if not DataSet.Active then DataSet.Open; DataSet.DisableControls; try for SubJson in DataJson do begin DataSet.Append; if ObjectFindFirst(SubJson, iter) then begin repeat if DataSet.FindField(iter.Ite.Current.Name) <> nil then DataSet.FindField(iter.Ite.Current.Name).Value := GetValue2Field(DataSet.FindField(iter.Ite.Current.Name), iter.Ite.Current.Value); until not ObjectFindNext(iter); end; DataSet.Post; end; finally DataSet.EnableControls; end; end;

class function TTableJSon.DataSetToJson(DataSet: TDataSet): ISuperObject; procedure GetFieldTypeInfo(Field: TField; var Fieldtyp, JsonTyp: string); begin Fieldtyp := GetEnumName(TypeInfo(TFieldType), ord(Field.DataType)); Delete(Fieldtyp, 1, 2); if Field is TStringField then JsonTyp := 'string' else if Field is TDateTimeField then JsonTyp := 'integer' else if (Field is TIntegerField) or (Field is TLargeintField) then JsonTyp := 'integer' else if Field is TCurrencyField then JsonTyp := 'currency' else if Field is TNumericField then JsonTyp := 'double' else if Field is TBooleanField then JsonTyp := 'boolean' else JsonTyp := 'variant'; end;

var sj, aj, sj2: ISuperObject; I: Integer; Fieldtyp, JsonTyp: string; List: TStringList; begin sj := SO(); // 创建列 aj := SA([]); List := TStringList.Create; try List.Sorted := True;

for I := 0 to DataSet.FieldCount - 1 do
begin
  sj2 := SO();
  GetFieldTypeInfo(DataSet.Fields[I], Fieldtyp, JsonTyp);

  sj2.S[cstFieldName] := DataSet.Fields[I].FieldName;
  sj2.S[cstFieldType] := Fieldtyp;
  sj2.S[cstJsonType] := JsonTyp;
  sj2.I[cstFieldSize] := DataSet.Fields[I].Size;
  sj2.B[cstRequired] := DataSet.Fields[I].Required;
  sj2.I[cstFieldIndex] := DataSet.Fields[I].Index;
  aj.AsArray.Add(sj2);
  List.Add(DataSet.Fields[I].FieldName + '=' + JsonTyp);
end;
sj.O['Cols'] := aj;
// 创建数据集的数据
DataSet.DisableControls;

DataSet.First;
aj := SA([]);
while not DataSet.Eof do
begin
  sj2 := SO();
  for I := 0 to DataSet.FieldCount - 1 do
  begin
    if VarIsNull(DataSet.Fields[I].Value) then
      sj2.O[DataSet.Fields[I].FieldName] := SO(Null)
    else
    begin
      CreateJsonValueByField(sj2, DataSet.Fields[I]);
    end;
  end;
  aj.AsArray.Add(sj2);
  DataSet.Next;
end;
sj.O['Data'] := aj;
Result := sj;

finally List.Free; DataSet.EnableControls; end; end;

class function TTableJSon.DataSetToJson2(DataSet: TDataSet): string; procedure GetFieldTypeInfo(Field: TField; var Fieldtyp, JsonTyp: string); begin Fieldtyp := GetEnumName(TypeInfo(TFieldType), ord(Field.DataType)); Delete(Fieldtyp, 1, 2); if Field is TStringField then JsonTyp := 'string' else if Field is TDateTimeField then JsonTyp := 'integer' else if (Field is TIntegerField) or (Field is TLargeintField) then JsonTyp := 'integer' else if Field is TCurrencyField then JsonTyp := 'currency' else if Field is TNumericField then JsonTyp := 'double' else if Field is TBooleanField then JsonTyp := 'boolean' else JsonTyp := 'variant'; end;

var sj, aj, sj2: ISuperObject; I: Integer; Fieldtyp, JsonTyp: string; List: TStringList; begin sj := SO(); // 创建列 aj := SA([]); List := TStringList.Create; try List.Sorted := True;

for I := 0 to DataSet.FieldCount - 1 do
begin
  sj2 := SO();
  GetFieldTypeInfo(DataSet.Fields[I], Fieldtyp, JsonTyp);

  sj2.S[cstFieldName] := DataSet.Fields[I].FieldName;
  sj2.S[cstFieldType] := Fieldtyp;
  sj2.S[cstJsonType] := JsonTyp;
  sj2.I[cstFieldSize] := DataSet.Fields[I].Size;
  sj2.B[cstRequired] := DataSet.Fields[I].Required;
  sj2.I[cstFieldIndex] := DataSet.Fields[I].Index;
  aj.AsArray.Add(sj2);
  List.Add(DataSet.Fields[I].FieldName + '=' + JsonTyp);
end;
sj.O['Cols'] := aj;
// 创建数据集的数据
DataSet.DisableControls;

DataSet.First;
aj := SA([]);
while not DataSet.Eof do
begin
  sj2 := SO();
  for I := 0 to DataSet.FieldCount - 1 do
  begin
    if VarIsNull(DataSet.Fields[I].Value) then
      sj2.O[DataSet.Fields[I].FieldName] := SO(Null)
    else
    begin
      CreateJsonValueByField(sj2, DataSet.Fields[I]);
    end;
  end;
  aj.AsArray.Add(sj2);
  DataSet.Next;
end;
sj.O['Data'] := aj;
Result := sj.AsString;

finally List.Free; DataSet.EnableControls; end; end;

end.

点赞
收藏
评论区
推荐文章
blmius blmius
3年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Wesley13 Wesley13
3年前
java将前端的json数组字符串转换为列表
记录下在前端通过ajax提交了一个json数组的字符串,在后端如何转换为列表。前端数据转化与请求varcontracts{id:'1',name:'yanggb合同1'},{id:'2',name:'yanggb合同2'},{id:'3',name:'yang
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
待兔 待兔
3个月前
手写Java HashMap源码
HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22
Jacquelyn38 Jacquelyn38
3年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
Wesley13 Wesley13
3年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Wesley13 Wesley13
3年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Stella981 Stella981
3年前
Django中Admin中的一些参数配置
设置在列表中显示的字段,id为django模型默认的主键list_display('id','name','sex','profession','email','qq','phone','status','create_time')设置在列表可编辑字段list_editable
Wesley13 Wesley13
3年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
9个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这
Easter79
Easter79
Lv1
今生可爱与温柔,每一样都不能少。
文章
2.8k
粉丝
5
获赞
1.2k