#1 2013-05-25 13:54:03

rossmcm
Member
Registered: 2012-09-19
Posts: 9

Are there any (really simple) examples of JSON encoding and decoding?

I've decided to use the JSON routines in SynCommons.pas to serialize some data, but I'm unsure as to how to go about it.

I'm familiar with JSON syntax, but found the interface of the TTextWriter class a little daunting.

If I had a simple Delphi program fragment:

    procedure TForm1.Button1Click(Sender: TObject);

    type
        TMyDataType = record
          mrInteger : integer ;
          mrString  : string ;
          mrArray1  : array [1..10] of double ;
          mrArray2  : array of double ;
          mrArray3  : array of record
                        Field1 : integer ;
                        Field2 : char ;
                        end ;
          end ;

    var
        MyData  : TMyDataType ;
        W         : TTextWriter ;

    begin
    try
        W := TTextWriter.CreateOwnedStream ;

        ...???

        Memo1.Lines.Add (W.Text)  ;

    finally
        FreeAndNil (W) ;
        end ;
    end ;

What would the code in the missing bit look like?  I get the feeling you could do it all with one statement with routines that took open numbers of parameters, but I'm uncertain if this is the best approach.  Any advice appreciated!
TIA
R

Offline

#2 2013-05-28 08:42:03

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

Re: Are there any (really simple) examples of JSON encoding and decoding?

By default, for instance during interface-based service call, any record parameter or function result will be serialized with our proprietary binary (and optimized layout) - i.e. RecordLoad and RecordSave functions - then encoded in Base-64, to be stored as plain text within the JSON stream.

But it will work only between pure Delphi client and server programs, since the binary format matches the low-level representation of the Delphi record in memory.

See also http://blog.synopse.info/post/2012/05/0 … of-records for custom serialization.

Offline

#3 2013-05-29 00:36:42

rossmcm
Member
Registered: 2012-09-19
Posts: 9

Re: Are there any (really simple) examples of JSON encoding and decoding?

Thanks for replying.  I was intending the JSON to be human-readable, mainly because the data will then be in a format that can easily be read by third parties.  If I had a data type as described in my OP, and I had written a JSON file like this:

    {
    "MyRecord" : {
        "mrInteger" : 12,
        "mrString"  : "Hello",
        "mrArray1"  : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
        "mrArray2"  : [11.1, 12.2, 13.3],
        "mrArray3"  : [
            {
             "Field1" : 11,
             "Field2" : "A"
            },
            {
             "Field1" : 33,
             "Field2" : "B"
            },
            {
             "Field1" : 55,
             "Field2" : "C"
            }
          ]
       }
    }

then what would the code look like to parse this file and place the data into the same record structure that is was originally generated from?

Is this possible at all with the Synopse JSON routines?

Incidentally, I also implemented an XML serialiser for the data structure I am storing using the default Delphi XML document.  To save a 32 kbyte structure to a file took 120 seconds.  I recoded it to use Synopse JSON encoding and it now takes 0.14 seconds!  So you can see why I am keen to use the Synopse decoder as well.

Many thanks,
Ross

Last edited by rossmcm (2013-05-29 01:00:29)

Offline

#4 2013-05-29 09:04:47

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

Re: Are there any (really simple) examples of JSON encoding and decoding?

0.14 seconds sounds slow for only 32 KB of data, for our JSON routines.
It should be less than 1 ms for 32 KB IMHO...

With the current state, you will have either:
- To use classes and not records, as stated by the sample 20;
- To write your own custom serializer for the record.

You can also create a ticket to use the enhanced RTTI (available since Delphi 2010) to serialize such records.
See http://synopse.info/fossil/reportlist (after anonymous login to the fossil repository)
with a link to this forum thread (for feedback and ideas)

Offline

Board footer

Powered by FluxBB