DELPHI XE2 to serialize objects in JSON format

Source: Internet
Author: User

Tag: obj Chinese string send item BSP write VAR property ascii

DELPHI XE2 to serialize objects in JSON format

The following code tests pass. The problem is the Chinese inside, after deserialization is garbled.

1. Serialized object is a string, Subject inside the Chinese looks normal, is still Chinese;

2. After deserializing the object, the Chinese inside the Subject is garbled.

XE2 processing Unicode is still a problem.

Titemrecord = Class

Private

fid:string;

fsubject:string;

Fadate:tdatetime;

Published

Property id:string read FID write FID;

Property subject:string read Fsubject write fsubject;

Property Adate:tdatetime read Fadate write fadate;

End

Procedure Tform1.button3click (Sender:tobject);
Var
Jo:tjsonobject;
P:tjsonpair;
A:tjsonarray;
B:tbytes;
s:string;
Jm:tjsonmarshal;
Jum:tjsonunmarshal;
Item:titemrecord;
V, V2:tjsonvalue;

Uj:tjsonunmarshal;
Begin
Using JSON
Jm:=tjsonmarshal.create;

Item: = titemrecord.create;

With ClientDataSet1 do
Begin
Item.id: = Fieldbyname (' ID '). asstring;
Item.Subject: = Fieldbyname (' Subject '). asstring;
Item.adate: = Fieldbyname (' adate '). Asdatetime;
End

V:=jm.  Marshal (Item); Serialization of
S: = v.tostring;

Item.free;
V.free;

Memo1.Lines.Text: = S;


-------------------deserialization-------------
JO: = tjsonobject.create;
JO.  Parse (Bytesof (s), 0, Length (s)); To change a string back to a Json object


UJ: = tjsonunmarshal.create;
Item: = UJ.  Unmarshal (JO) as Titemrecord; Change the Json object back to my own object.


MEMO1.LINES.ADD ('-----------');

Memo1.Lines.Add (' ID = ' + item.id);
Memo1.Lines.Add (' Subject = ' + Item.Subject); Problem: The Chinese character coding inside the string is not well, there is a problem.
Memo1.Lines.Add (' Date = ' + datetimetostr (item.adate));
End

----------------------------------------------------delimiter-------------------------

As mentioned earlier, when the object is output to a string using the ToString method, the Chinese inside the object is correct inside the string, and the string is written TMemo the Chinese is normal. But if you put this string in JO. Parse (Bytesof (s), 0, Length (s)); Statement to the object, the value of the text field in the object is garbled.

Using: B: = TEncoding.ASCII.GetBytes (S), the way to get the tbytes used for Parse, out of the object, Chinese value is also garbled;

If you use B: = TEncoding.Unicode.GetBytes (S), the method obtained by Tbytes is used for Parse to not get the correct object, i.e. run Jo.parse (B, 0); Occurs when an exception is encountered.

Test, do not output string, but directly output tbytes, and then take this tbytes to Parse, get the object, in the Text field value OK, no garbled.

SetLength (B, 200);

I: = v.tobytes (B, 0);

JO: = tjsonobject.create;

Jo.parse (B, 0);

UJ: = tjsonunmarshal.create;

Item: = UJ. Unmarshal (JO) as Titemrecord;

This gets the Item object, where the text segment value is not garbled.

That is, its ToString output does not know what encoding, it needs to change its code back to Tbytes line. Simple Bytesof (S) is not good.

----------------------------

Continuation test: Using UTF8 string

S: = v.tostring;
S8: = Utf8encode (S); s8:utf8string;

V2: = Tjsonobject.parsejsonvalue (S8); V2:tjsonvalue;

Then, Item: = UJ. Unmarshal (V2) as Titemrecord; You can successfully get an object with Chinese text field values!

That is, DELPHI's own JSON library does not handle UNICODE double-byte strings correctly, but can handle UTF8 strings.

-------------------------

Continue: To deal with Chinese, the above using Jo.parse (B, 0); The way, directly processing the Chinese string, the obtained object of the text field value will be garbled.

This is the right approach:

V:tjsonvalue;

V: = Tjsonobject.parsejsonvalue (S);

UJ: = tjsonunmarshal.create;

Item: = UJ. Unmarshal (V) as Titemrecord;

This gets the object, the Chinese value of the field is not garbled, normal.

Summary: Instead of parsing the string as a JSON object with the parse method of the Tjsonobject object, you should use the class method Tjsonobject.parsejsonvalue (S) to get the Tjsonvalue object, and then take this jsonval UE object to de-serialize the object, Chinese no problem.

---------------------------------

Summary: Around a big circle, in fact very simple! It is possible to process Unicode strings directly. It's just that DELPHI's approach to the objects inside the JSON library is not intuitive.

Http://blog.sina.com.cn/s/blog_722bc92e0101hl88.html

DELPHI XE2 to serialize objects in JSON format

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.