//新的DATASNAP已经支持TPARAMS作为远程方法里面的参数,会自动序列TPARAMS,无需手动序列它。
//在此只是记录一些JSON序列的用法,无实际意义
unit uSerialize;
interface
uses
System.SysUtils, Data.Win.ADODB, Data.DBXJSON, Data.DBXJSONReflect,
System.Variants, Data.DB;
type
TSerialize = class
public
function Serialize(const ModuleId: string; SqlId: Integer; p: TParameters): TJSONValue; overload;
function Serialize(const ModuleId: string; SqlId: Integer; p: TParams): TJSONValue; overload;
end;
TDeSerialize = class
private
FModuleId: string;
FSqlId: Integer;
public
property ModuleId: string read FModuleId;
property SqlId: Integer read FSqlId;
function DeSerialize(v: TJSONValue; P: TParameters): TDeSerialize; overload;
function DeSerialize(v: TJSONValue; P: TParams): TDeSerialize; overload;
end;
implementation
function TSerialize.Serialize(const ModuleId: string; SqlId: Integer; p: TParameters): TJSONValue;
var
jo: TJSONObject;
ja: TJSONArray;
i: integer;
begin
Result := nil;
if ModuleId = '' then Exit;
if SqlId = 0 then Exit;
if not Assigned(P) then Exit;
ja := TJSONArray.Create;
ja.AddElement(TJSONString.Create(ModuleId));
ja.AddElement(TJSONNumber.Create(SqlId));
i := 0;
while i < P.Count do
begin
jo := TJSONObject.Create;
jo.AddPair('Name', P.Items[i].Name);
jo.AddPair('DataType', TJSONNumber.Create(Ord(P.Items[i].DataType)));
jo.AddPair('Value', VarToStr(P.Items[i].Value));
ja.AddElement(jo);
Inc(i);
end;
Result := ja;
end;
function TSerialize.Serialize(const ModuleId: string; SqlId: Integer; p: TParams): TJSONValue;
var
jo: TJSONObject;
ja: TJSONArray;
i: integer;
begin
Result := nil;
if ModuleId = '' then exit;
if SqlId = 0 then exit;
if not Assigned(p) then exit;
ja := TJSONArray.Create;
ja.AddElement(TJSONString.Create(ModuleId));
ja.AddElement(TJSONNumber.Create(SqlId));
i := 0;
while i < p.Count do
begin
jo := TJSONObject.Create;
jo.AddPair('Name', P.Items[i].Name);
jo.AddPair('DataType', TJSONNumber.Create(Ord(p.Items[i].DataType)));
jo.AddPair('Value', VarToStr(P.Items[i].Value));
ja.AddElement(jo);
Inc(i);
end;
Result := ja;
end;
function TDeSerialize.DeSerialize(v: TJSONValue; P: TParameters): TDeSerialize;
var
i: Integer;
ja: TJSONArray;
jo: TJSONObject;
begin
Result := nil;
if not Assigned(P) then exit;
if v.Null then exit;
p.Clear;
ja := v as TJSONArray;
FModuleId := TJSONString(ja.Get(0)).Value;
FSqlId := TJSONNumber(ja.Get(1)).AsInt;
for i := 2 to ja.Size - 1 do
begin
jo := TJSONObject(ja.Get(i));
P.CreateParameter(jo.Get('Name').JsonValue.Value,
TFieldType(TJSONNumber(jo.Get('DataType').JsonValue).AsInt),
pdInput,
SizeOf(jo.Get('Value').JsonValue.Value),
jo.Get('Value').JsonValue.Value);
end;
Result := Self;
end;
function TDeSerialize.DeSerialize(v: TJSONValue; P: TParams): TDeSerialize;
var
i: Integer;
ja: TJSONArray;
jo: TJSONObject;
begin
Result := nil;
if not Assigned(p) then exit;
if v.Null then exit;
P.Clear;
ja := v as TJSONArray;
FModuleId := TJSONString(ja.Get(0)).Value;
FSqlId := TJSONNumber(ja.Get(1)).AsInt;
for i := 2 to ja.Size - 1 do
begin
jo := TJSONObject(ja.Get(i));
P.CreateParam(TFieldType(TJSONNumber(jo.Get('DataType').JsonValue).AsInt),
jo.Get('Name').JsonValue.Value,
ptInput);
P.Items[i].Value := jo.Get('Value').JsonValue.Value;
P.Items[i].Size := SizeOf(jo.Get('Value').JsonValue.Value);
end;
end;
end.