// The New datasnap already supports tparams as a parameter in the remote method, and will automatically sequence tparams without the need to manually sequence it.
// Only the usage of some JSON sequences is recorded here, which has no practical significance.
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 ('ype ype ', 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 ('ype ype ', 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 ('ype ype '). 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 ('ype ype '). 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.