JSON serialized object

Source: Internet
Author: User

// 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.