JsonDataObjects序列和还原

JsonDataObjects号称DELPHI最快的JSON库,且支持跨平台。

// cxg 2017-9-12
// Use JsonDataObjects(cross platform json library)
// Use delphi 10.2.1

unit ujson;

interface

uses
System.SysUtils, soap.EncdDecd, Web.HTTPApp, System.NetEncoding, Data.DB,
System.Classes, JsonDataObjects;

// {"data":[{"field1":value1,"field2":value2}]};
function datasetToJson(dataset: TDataSet): string;

// {"data":[{"field1":value1,"field2":value2}]};
procedure jsonToDataset(const json: string; dataset: TDataSet);

// {"cols":[{"name":"field1","size":0,"type":"int"}]"data":[{"field1":value1}]};
function datasetToJsonCols(dataset: TDataSet): string;

// {
// "update":[{"table":"表1","where":"字段1=1","字段1":"1","字段2":0}]
// ,"insert":[{"table":"表1","字段1":"1","字段2":0}]
// ,"delete":[{"table":"表1","where":"字段1=1"}]
// };
procedure parseJsonSql(const json: string; outsql: TStrings);

implementation

procedure parseJsonSql(const json: string; outsql: TStrings);
// {
// "update":[{"table":"表1","where":"字段1=1","字段1":"1","字段2":0}]
// ,"insert":[{"table":"表1","字段1":"1","字段2":0}]
// ,"delete":[{"table":"表1","where":"字段1=1"}]
// };
var
obj, childobj: TJsonObject;
tablename, sql, lname, lvalue, where: string;
i, j: Integer;
function _getValue(value: PJsonDataValue): string;
{
TJsonDataType = (
jdtNone, jdtString, jdtInt, jdtLong, jdtULong, jdtFloat, jdtDateTime
, jdtBool, jdtArray, jdtObject
);
}
begin
case Value.Typ of
jdtString: Result := QuotedStr(UTF8ToString(rawbytestring(TNetEncoding.URL.Decode(value.Value)))); // 解码
jdtBool: Result := BoolToStr(value.BoolValue);
jdtFloat: Result := FloatToStr(value.FloatValue);
jdtInt, jdtLong, jdtULong: Result := IntToStr(value.IntValue);
jdtDateTime: Result := DateTimeToStr(value.DateTimeValue);
end;
end;
begin
if outsql = nil then
Exit;
outsql.Clear;
obj := TJsonObject.Parse(json) as TJsonObject;
try
// 解析并生成insert sql begin-----------------------------------
for i := 0 to obj.A[‘insert‘].Count - 1 do
begin
childobj := obj.A[‘insert‘].O[i];
lname := ‘‘;
lvalue := ‘‘;
for j := 0 to childobj.Count - 1 do
begin
if childobj.Names[j] = ‘table‘ then
begin
tablename := childobj.Items[j].Value;
Continue;
end;
if lname = ‘‘ then
lname := childobj.Names[j]
else
lname := lname + ‘,‘ + childobj.Names[j];
if lvalue = ‘‘ then
lvalue := _getValue(childobj.Items[j])
else
lvalue := lvalue + ‘,‘ + _getValue(childobj.Items[j]);
end;
sql := ‘insert into ‘ + tablename + ‘ (‘ + lname + ‘) values (‘ + lvalue + ‘)‘;
outsql.Add(sql);
end;
// 解析并生成insert sql end----------------------------------------
// 解析并生成update sql begin---------------------------------------
for i := 0 to obj.A[‘update‘].Count - 1 do
begin
childobj := obj.A[‘update‘].O[i];
lname := ‘‘;
lvalue := ‘‘;
for j := 0 to childobj.Count - 1 do
begin
if childobj.Names[j] = ‘table‘ then
begin
tablename := childobj.Items[j].Value;
Continue;
end
else
if childobj.Names[j] = ‘where‘ then
begin
where := childobj.Items[j].Value;
Continue;
end;
if lname = ‘‘ then
lname := childobj.Names[j] + ‘=‘ + _getValue(childobj.Items[j])
else
lname := lname + ‘,‘ + childobj.Names[j] + ‘=‘ + _getValue(childobj.Items[j]);
end;
sql := ‘update ‘ + tablename + ‘ set ‘ + lname + ‘ where ‘ + where;
outsql.Add(sql);
end;
// 解析并生成update sql end--------------------------------------------
// 解析并生成delete sql begin--------------------------------------------
for i := 0 to obj.A[‘delete‘].Count - 1 do
begin
childobj := obj.A[‘delete‘].O[i];
lname := ‘‘;
lvalue := ‘‘;
for j := 0 to childobj.Count - 1 do
begin
if childobj.Names[j] = ‘table‘ then
begin
tablename := childobj.Items[j].Value;
Continue;
end
else
if childobj.Names[j] = ‘where‘ then
begin
where := childobj.Items[j].Value;
Continue;
end;
end;
sql := ‘delete from ‘ + tablename + ‘ where ‘ + where;
outsql.Add(sql);
end;
// 解析并生成delete sql end-------------------------------------------
finally
obj.free;
end;
end;

function datasetToJsonCols(dataset: TDataSet): string;
// {"cols":[{"name":"field1","size":0,"type":"int"}]"data":[{"field1":value1}]};
var
i: Integer;
obj, childobj: TJsonObject;
field: TField;
blob: TStringStream;

function _getFieldType(fld: TField): string;
begin
case fld.DataType of
ftBoolean:
Result := ‘bool‘;
ftSmallint, ftInteger, ftWord, ftAutoInc:
Result := ‘int‘;
ftLargeint:
Result := ‘int64‘;
ftFloat, ftBCD, ftCurrency:
Result := ‘float‘;
ftTimeStamp, ftDate, ftTime, ftDateTime:
Result := ‘datetime‘;
ftString, ftFixedChar, ftMemo, ftWideString:
Result := ‘string‘;
ftBytes, ftVarBytes, ftBlob, ftGraphic, ftOraBlob, ftOraClob:
Result := ‘blob‘;
end;
end;

begin
// {"cols":[{"name":"c1","size":0,"type":"int"}]"data":[{"c1":1}]};
Result := ‘{"result":"false"}‘;
if (dataset = nil) or (not dataset.Active) then
Exit;
obj := TJsonObject.Create;
obj.A[‘cols‘];
obj.A[‘data‘];
dataset.First;
for i := 0 to dataset.FieldCount - 1 do // fill cols array
begin
field := dataset.Fields[i];
childobj := obj.A[‘cols‘].AddObject;
childobj.S[‘name‘] := field.FieldName;
childobj.I[‘size‘] := field.Size;
childobj.S[‘type‘] := _getFieldType(field);
childobj.B[‘required‘] := field.Required;
childobj.B[‘readonly‘] := field.ReadOnly;
end;
// fill data array
dataset.First;
while not dataset.Eof do
begin
childobj := obj.A[‘data‘].AddObject;
for i := 0 to dataset.FieldCount - 1 do
begin
field := dataset.Fields[i];
if field.IsNull then
childobj.S[field.FieldName] := ‘‘
else
begin
case field.DataType of
ftBoolean:
childobj.B[field.FieldName] := field.AsBoolean;
ftSmallint, ftInteger, ftWord, ftAutoInc:
childobj.I[field.FieldName] := field.AsInteger;
ftLargeint:
childobj.L[field.FieldName] := TLargeintField(field).AsLargeInt;
ftFloat, ftBCD, ftCurrency:
childobj.F[field.FieldName] := field.AsFloat;
ftTimeStamp, ftDate, ftTime, ftDateTime:
childobj.D[field.FieldName] := field.AsDateTime;
ftString, ftFixedChar, ftMemo, ftWideString:
childobj.S[field.FieldName] := TNetEncoding.URL.Encode(string(UTF8Encode(field.AsString))); // 编码
ftBytes, ftVarBytes, ftBlob, ftGraphic, ftOraBlob, ftOraClob:
begin
blob := TStringStream.Create(‘‘);
try
TBlobField(field).SaveToStream(blob);
childobj.S[field.FieldName] := EncodeString(blob.DataString); // base64 编码
finally
blob.Free;
end;
end;
end;
end;
end; // end for
dataset.Next;
end; // end while
Result := obj.ToString;
end;

procedure jsonToDataset(const json: string; dataset: TDataSet);
// {"data":[{"field1":value1,"field2":value2}]};
var
obj, childobj: TJsonObject;
i, j: Integer;
field: TField;
blob: TStringStream;
begin
if (dataset = nil) or (not dataset.Active) or (json = ‘{"result":"false"}‘) then
Exit;
obj := TJsonObject.Parse(json) as TJsonObject;
dataset.DisableControls;
try
for i := 0 to obj.A[‘data‘].Count - 1 do
begin
dataset.Append;
childobj := obj.A[‘data‘].O[i];
if childobj = nil then
continue;
for j := 0 to dataset.FieldCount - 1 do
begin
field := dataset.Fields[j];
if field = nil then
Continue;
case field.datatype of
ftBoolean:
field.AsBoolean := childobj.B[field.FieldName];
ftFloat, ftBCD, ftCurrency:
field.AsFloat := childobj.F[field.FieldName];
ftSmallint, ftInteger, ftWord, ftAutoInc:
field.AsInteger := childobj.I[field.FieldName];
ftString, ftFixedChar, ftMemo, ftWideString:
field.AsString :=UTF8ToString(rawbytestring( TNetEncoding.URL.Decode(childobj.S[field.FieldName]))); // 解码
ftTimeStamp, ftDate, ftTime, ftDateTime:
field.AsDateTime := childobj.D[field.FieldName];
ftLargeint:
TLargeintField(field).AsLargeInt := childobj.L[field.FieldName];
ftBytes, ftVarBytes, ftBlob, ftGraphic, ftOraBlob, ftOraClob:
begin
blob := TStringStream.Create(DecodeString(childobj.S[field.FieldName]));
try
TBlobField(field).LoadFromStream(blob);
finally
blob.Free;
end;
end;
end;
end;
dataset.Post;
end;
finally
dataset.EnableControls;
obj.free;
end;
end;

function datasetToJson(dataset: TDataSet): string;
// {"data":[{"field1":value1,"field2":value2}]};
var
i: Integer;
obj, childobj: TJsonObject;
field: TField;
blob: TStringStream;
begin
Result := ‘{"result":"false"}‘;
if (dataset = nil) or (not dataset.Active) then
Exit;
obj := TJsonObject.Create;
try
obj.A[‘data‘];
dataset.First;
while not dataset.Eof do
begin
childobj := obj.A[‘data‘].AddObject;
for i := 0 to dataset.FieldCount - 1 do
begin
field := dataset.Fields[i];
if field.IsNull then
childobj.S[field.FieldName] := ‘‘
else
begin
case field.datatype of
ftBoolean:
childobj.B[field.FieldName] := field.AsBoolean;
ftSmallint, ftInteger, ftWord, ftAutoInc:
childobj.I[field.FieldName] := field.AsInteger;
ftLargeint:
childobj.L[field.FieldName] := TLargeintField(field).AsLargeInt;
ftCurrency, ftFloat, ftBCD:
childobj.F[field.FieldName] := field.AsFloat;
ftTimeStamp, ftDate, ftTime, ftDateTime:
childobj.D[field.FieldName] := field.AsDateTime;
ftString, ftFixedChar, ftMemo, ftWideString:
childobj.S[field.FieldName] := TNetEncoding.URL.Encode(string(UTF8Encode(field.AsString)));
ftBytes, ftVarBytes, ftBlob, ftGraphic, ftOraBlob, ftOraClob:
begin
blob := TStringStream.Create(‘‘);
try
TBlobField(field).SaveToStream(blob);
childobj.S[field.FieldName] := EncodeString(blob.DataString); // base64 编码
finally
blob.Free;
end;
end;
end;
end;
end; // end for
dataset.Next;
end; // end while
Result := obj.ToString;
finally
obj.Free;
end;
end;

end.

时间: 2024-10-03 13:44:37

JsonDataObjects序列和还原的相关文章

优秀的数据序列和还原类----TSimpleMsgPack

有了它,从此数据的序列和还原不再是梦魔. 适用极广:可以适用于所有版本的DELPHI,不论D7还是最新的XE10.1,都支持.激动! 封装极简:完全独立的一个单元即可,不需引用任何其它的单元. 用法极简:调用非常简单. 其支持的序列的数据格式非常丰富. 下面列出该类的一些常用的接口方法: procedure LoadBinaryFromStream(pvStream: TStream; pvLen: cardinal = 0); procedure SaveBinaryToStream(pvSt

关于数据(对象)的序列和还原

有时我们会把对象的序列叫做流化,这很形象.任何对象最终必须被序列成TBYTES,然后才能将它进行网络传输.序列,我们首先要想到三个对象:TOBJECT--TSTREAM--TBYTES.TSTREAM是序列和还原的中间桥梁.许多对象,某些数据集都提供了SAVETOSTREAM()和LOADFROMSTREAM().接下来道破天机:可以说对象流化是非常方便的,流转化为TBYTES也是非常容易的.具体的细节代码,可参考本人的博客上的相关系列文章.

TynSerial序列(还原)TClientDataSet

可以一次性序列(还原)多个TClientDataSet. 1)TClientDataSet查询数据 procedure TForm1.Qrys(accountno, sql, sql2: string; DataSet, DataSet2: TClientDataSet); // TClientDataSet主从表查询 var url: SockString; i: integer; serial: TynSerial; begin serial := TynSerial.Create; try

结构体的序列和还原(使用Move方法)

// 定义结构体 typeTmyRec= recordname:string[10];password:string[10];end; function RawToBytes(const AValue; const ASize: Integer): TBytes;beginSetLength(Result, ASize);if ASize > 0 then beginMove(AValue, Result[0], ASize);end;end; procedure BytesToRaw(cons

firedac数据集的序列和还原

procedure TForm1.Button1Click(Sender: TObject);var stream, stream2: TMemoryStream; buf: TBytes;begin // 查询 FDQuery1.Close; FDQuery1.SQL.Clear;FDQuery1.Open('select * from t2'); // 序列为流 stream := TMemoryStream.Create; FDQuery1.SaveToStream(stream, TFD

结构体的序列和还原

// 定义结构体 type TmyRec= record name:string[10]; password:string[10]; end; function RawToBytes(const AValue; const ASize: Integer): TBytes;begin SetLength(Result, ASize); if ASize > 0 then begin Move(AValue, Result[0], ASize); end;end; procedure BytesTo

TQJson序列和还原clientdataset.data

TQJson是QDAC开源项目的JSON对象,其特点是跨平台,功能强大. 序列 procedure TForm2.Button2Click(Sender: TObject);var AJson: TQJson;begin AJson := TQJson.Create; try AJson.AsVariant := ClientDataSet1.Data; if SaveDialog1.Execute then AJson.SaveToFile(SaveDialog1.FileName, teUt

oracle 序列sequence

查询所有的序列: select 'create sequence '||sequence_name|| ' minvalue '||min_value|| ' maxvalue '||max_value|| ' start with '||last_number|| ' increment by '||increment_by|| (case when cycle_flag='N' then '' else ' cycle ' end) || (case when order_flag='N'

《剑指offer》面试题24 二叉搜索树的后序遍历序列 Java版

(判断一个元素均不相同的序列是否为一个BST的LRD) 书中方法:首先对于二叉搜索树,左子树中的所有元素小于根节点小于右子树中的所有元素,然后后序遍历序列最后一个元素是根节点,这是我们已知的条件.这道题不禁让人想起用一个普通二叉树的前序(或后序)遍历序列加上中序遍历序列就可以还原一棵二叉树,在那道题中,我们知道前序或后序序列就能开始组织根节点,但是因为我们无法确定子树的个数,所以还需要一个辅助的序列来确定子树范围.但如果改成BST的前序或后序遍历序列,我们就可以直接组织二叉树,BST的特性帮我们