#1 2014-07-04 03:19:16

chula
Member
Registered: 2014-06-18
Posts: 19

Newbie need help in JSON

I am still playing around with mORMot framework........amazing framework....

I have heavily used SuperObject for JSON manipulation. Now I want to study the JSON features of mORMot and found there is no separate Class Reference or Help file.

Anyway I have looked into the SAD document and tried to find a JSON function equivalent to TSuperObject.ParseString(). Maybe I had overlooked, I only found functions which require passing TypeInfo (i.e. know the JSON structure beforehand) or found xxxxxRead() functions that could read JSON string saved by xxxxSave() functions.

Say the JSON string returned from our server has many levels depth, and I just want the data started from level 3 with field name "data". I don't want to build a big Record structure before I can use just the sub-sub-sub Record field, like the RecordLoadJSON().

So, which function can read a random JSON string into a mORMot JSON object? Thanks!

Last edited by chula (2014-07-04 03:21:07)

Offline

#2 2014-07-04 05:40:13

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,603
Website

Re: Newbie need help in JSON

The 1.18 SAD PDF has a full per-unit help description.

Take a look at TDocVariant in the SAD.
See also http://blog.synopse.info/post/2014/02/2 … riant-type

Offline

#3 2014-07-04 07:26:02

chula
Member
Registered: 2014-06-18
Posts: 19

Re: Newbie need help in JSON

ab wrote:

The 1.18 SAD PDF has a full per-unit help description.

Take a look at TDocVariant in the SAD.
See also http://blog.synopse.info/post/2014/02/2 … riant-type

Thanks for the info and I could make it work now. However, I run into an encoding problem:

var
  v: variant;
begin
  with TStringStream.Create('', TEncoding.UTF8) do
  try
    LoadFromFile('something.json');
    memo1.Lines.text := DataString;

    v := _json(DataString);
    //v := _json(StringToUTF8(DataString));

  finally
    free;
  end;

I have the JSON file that was UTF8 encoded, so I created the TStringStream with UTF8 encoding and display it firstly inside a Memo in order to make sure the encoding was correct.

No matter I added StringToUTF8/UTF8ToString or not, the variant resulted from _json corrupted my Chinese characters.

What should I do please?

Offline

#4 2014-07-04 07:32:29

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,603
Website

Re: Newbie need help in JSON

Why are you using a TStringStream here?

Just use

v := _jsonfast(StringFromFile('something.json'));

It will be just faster and less error-prone.

For streaming pure UTF-8 content, I would rather use our TRawByteStringStream.
It will avoid any slow conversion to UTF-16 as with the regular TStringStream.

Within the TDocVariant, be aware that the UTF-8 content is stored not as string/UnicodeString by as RawUTF8.
But it should preserve the Chinese characters as expected.

Offline

#5 2014-07-04 08:04:38

chula
Member
Registered: 2014-06-18
Posts: 19

Re: Newbie need help in JSON

ab wrote:

Why are you using a TStringStream here?

Just use

v := _jsonfast(StringFromFile('something.json'));

It will be just faster and less error-prone.

For streaming pure UTF-8 content, I would rather use our TRawByteStringStream.
It will avoid any slow conversion to UTF-16 as with the regular TStringStream.

Within the TDocVariant, be aware that the UTF-8 content is stored not as string/UnicodeString by as RawUTF8.
But it should preserve the Chinese characters as expected.

Thanks! v := _jsonfast(StringFromFile('something.json')); is more concise.

However, the Chinese chars are still corrupted. My test results were:
1. if the JSON file is UTF8 encoded WITH BOM, v is unassigned. Seems error occurred.
2. if the JSON file is UTF8 encoded WITHOUT BOM, v stored the JSON correctly with the Chinese chars corrupted.

(I used Notepad++ to change the encoding of the JSON file, and I am using Delphi XE)

In case you want to take a try, this are my characters inside the JSON file:

{"msg":"","success":true,"result":{"data":{"1000":["垛","垜"],"1001":["坨","垚"]}}}

Offline

#6 2014-07-04 09:17:31

chula
Member
Registered: 2014-06-18
Posts: 19

Re: Newbie need help in JSON

Sorry, I have overlooked your words: " stored not as string/UnicodeString by as RawUTF8", the Chinese chars were reserved when I consumed them, although they looks corrupted when I investigate the variant content.

But the With BOM problem still persists.

Last edited by chula (2014-07-04 09:18:19)

Offline

#7 2014-07-04 09:59:08

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,603
Website

Re: Newbie need help in JSON

No, the text is there, but it is UTF-8 encoded!
What you see in the debugger is the Chinese characters encoded as UTF-8!
smile

In fact, this is a limitation of the debugger: it is not able to display a RawUTF8 as expected, when converted from a variant, whereas a plain RawUTF8 is decoded as expected...
You have to explicitly type-case a TDocVariant member into a string to retrieve the expected content in the debugger.

Whereas for code point of view, it works as expected.

If you write:

var doc: Variant;
    str: string;
begin
    doc := _Json('{"msg":"","success":true,"result":{"data":{"1000":["日本","垜"],"1001":["坨","垚"]}}}');
    assert(boolean(doc.success));
    str := doc.result;   // explicit conversion to string
    showmessage(str);  // here str = '{"data":{"1000":["日本","垜"],"1001":["坨","垚"]}}'
    showmessage(doc.result); // implicit conversion to string - will display {"data":{"1000":["日本","垜"],"1001":["坨","垚"]}}

Offline

#8 2014-07-04 10:36:34

chula
Member
Registered: 2014-06-18
Posts: 19

Re: Newbie need help in JSON

I see. Thank you!

Offline

Board footer

Powered by FluxBB