// SUPEROBJECT 序列数据集 cxg 2017-1-12
// {"data":[{"c1":1,"c2":1}]};
// DELPHI7 适用

unit superobjectDB;

interface

uses
SysUtils, db, DBClient, superobject, EncdDecd, Classes, Httpapp;

// {"data":[{"c1":1,"c2":1}]};
function datasetToJson(dataset: TDataSet): string;

procedure jsonToDataset(const json: string; dataset: TDataSet);
// {"cols":[{"name":"c1","size":0,"type":"int"}]"data":[{"c1":1}]};

function datasetToJson2(dataset: TDataSet): string;

implementation

procedure jsonToDataset(const json: string; dataset: TDataSet);
var
jo, jf: ISuperObject;
i, j: Integer;
ja: TSuperArray;
field: TField;
blob: TStringStream;
begin
if (dataset = nil) or (not dataset.Active) then
Exit;
jo := SO(json);
ja := jo.A['data'];
dataset.DisableControls;
try
for i := 0 to ja.Length - 1 do
begin
dataset.Append;
jf := ja.O[i];
if jf = 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 := jf[field.FieldName].AsBoolean;
ftFloat, ftBCD:
field.AsFloat := jf[field.FieldName].AsDouble;
ftCurrency:
field.AsCurrency := jf[field.FieldName].AsCurrency;
ftSmallint, ftInteger, ftWord, ftAutoInc:
field.AsInteger := jf[field.FieldName].AsInteger;
ftString, ftFixedChar, ftMemo, ftWideString:
field.AsString := HTTPDecode(jf[field.FieldName].AsString);
ftTimeStamp, db.ftDate, ftTime, ftDateTime:
field.AsDateTime := jf[field.FieldName].AsDouble;
ftBytes, ftVarBytes, DB.ftBlob, ftGraphic, ftOraBlob, ftOraClob:
begin
blob := TStringStream.Create(DecodeString(jf[field.FieldName].AsString));
try
TBlobField(field).LoadFromStream(blob);
finally
blob.Free;
end;
end;
end;
end;
dataset.Post;
end;
finally
dataset.EnableControls;
end;
end;

function datasetToJson(dataset: TDataSet): string;
var
i: Integer;
jo, ja, jf: ISuperObject;
fld: TField;
blob: TStringStream;
begin
Result := '{"return":"null"}';
if (dataset = nil) or (not dataset.Active) then
Exit;
jo := SO();
ja := SA([]);
dataset.First;
while not dataset.Eof do
begin
jf := SO();
for i := 0 to dataset.FieldCount - 1 do
begin
fld := dataset.Fields[i];
if fld.IsNull then
jf.S[fld.FieldName] := ''
else
begin
case fld.DataType of
ftBoolean:
jf.B[fld.FieldName] := fld.AsBoolean;
ftSmallint, ftInteger, ftWord, ftAutoInc:
jf.I[fld.FieldName] := fld.AsInteger;
ftLargeint:
jf.I[fld.FieldName] := TLargeintField(fld).AsLargeInt;
ftCurrency:
jf.C[fld.FieldName] := fld.AsCurrency;
ftFloat, ftBCD:
jf.D[fld.FieldName] := fld.AsFloat;
ftTimeStamp, db.ftDate, ftTime, ftDateTime:
jf.D[fld.FieldName] := fld.AsDateTime;
ftString, ftFixedChar, ftMemo, ftWideString:
jf.S[fld.FieldName] := HttpEncode(fld.AsString);
ftBytes, ftVarBytes, DB.ftBlob, ftGraphic, ftOraBlob, ftOraClob:
begin
blob := TStringStream.Create('');
try
TBlobField(fld).SaveToStream(blob);
jf.S[fld.FieldName] := EncodeString(blob.DataString); // base64 encode
finally
blob.Free;
end;
end;
end;
end;
end;
ja.AsArray.Add(jf);
dataset.Next;
end;
jo.O['data'] := ja;
Result := jo.AsString;
end;

function datasetToJson2(dataset: TDataSet): string;
var
i: Integer;
jo, ja, jf, jo2, ja2: ISuperObject;
fld: 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';
ftCurrency: Result := 'currency';
ftFloat, ftBCD: Result := 'float';
ftTimeStamp, db.ftDate, ftTime, ftDateTime: Result := 'datetime';
ftString, ftFixedChar, ftMemo, ftWideString: Result := 'string';
ftBytes, ftVarBytes, DB.ftBlob, ftGraphic, ftOraBlob, ftOraClob: Result := 'blob';
end;
end;

begin
// {"cols":[{"name":"c1","size":0,"type":"int"}]"data":[{"c1":1}]};
Result := '{"return":"null"}';
if (dataset = nil) or (not dataset.Active) then
Exit;
jo := SO();
// cols
ja2 := SA([]);
dataset.First;
for i := 0 to dataset.FieldCount - 1 do
begin
fld := dataset.Fields[i];
jo2 := SO();
jo2.S['name'] := fld.FieldName;
jo2.I['size'] := fld.Size;
jo2.S['type'] := _getFieldType(fld);
jo2.B['required'] := fld.Required;
jo2.B['readonly'] := fld.ReadOnly;
ja2.AsArray.Add(jo2);
end;
jo.O['cols'] := ja2;
// data
ja := SA([]);
dataset.First;
while not dataset.Eof do
begin
jf := SO();
for i := 0 to dataset.FieldCount - 1 do
begin
fld := dataset.Fields[i];
if fld.IsNull then
jf.S[fld.FieldName] := ''
else
begin
case fld.DataType of
ftBoolean:
jf.B[fld.FieldName] := fld.AsBoolean;
ftSmallint, ftInteger, ftWord, ftAutoInc:
jf.I[fld.FieldName] := fld.AsInteger;
ftLargeint:
jf.I[fld.FieldName] := TLargeintField(fld).AsLargeInt;
ftCurrency:
jf.C[fld.FieldName] := fld.AsCurrency;
ftFloat, ftBCD:
jf.D[fld.FieldName] := fld.AsFloat;
ftTimeStamp, db.ftDate, ftTime, ftDateTime:
jf.D[fld.FieldName] := fld.AsDateTime;
ftString, ftFixedChar, ftMemo, ftWideString:
jf.S[fld.FieldName] := HttpEncode(fld.AsString);
ftBytes, ftVarBytes, DB.ftBlob, ftGraphic, ftOraBlob, ftOraClob:
begin
blob := TStringStream.Create('');
try
TBlobField(fld).SaveToStream(blob);
jf.S[fld.FieldName] := EncodeString(blob.DataString); // base64 encode
finally
blob.Free;
end;
end;
end;
end;
end;
ja.AsArray.Add(jf);
dataset.Next;
end;
jo.O['data'] := ja;
Result := jo.AsString;
end;

end.

05-11 19:39