#1 Re: mORMot 1 » JSONToObject example - nested objects » 2013-12-13 01:52:48

Thanks for your help Arnaud,

I just downloaded the nightly build yesterday before doing my tests. SynCommons.pas is dated Nov 29, 2013.

This is the full JSON that I need to serialize:

    JSONOut:='{"result":{"ok":"true","message":"","syncTime":"2013-12-12T20:55:47.000000"},
             "transactions":['+
             '{"TRTYPE":"INCOME","TRDATE":"2013-12-12 20:45:20","TRAA":"1.23","TRCAT1":"1","TRCAT2":"2","TRCAT3":"3","TRRMK":"Remark","TRACID":"4"},'+
             '{"TRTYPE":"EXPENSE","TRDATE":"2013-12-12 20:45:20","TRAA":"1.23","TRCAT1":"1","TRCAT2":null,"TRCAT3":null,"TRRMK":"Remark","TRACID":"5"}'+
             ']}';

I tried everything I could find in the demos/forum: JSONToObject and LoadFromJSON. Nothing worked so far.
And my java/php developer will not bend any further. :-)

Can you advise what should I use? Records or Objects?

Appreciate your help!

#2 Re: mORMot 1 » JSONToObject example - nested objects » 2013-12-12 20:35:48

Hi Arnaud,

You are right - JSON with nested records is received from non-Mormot server. My server side is developed with Java->hibernate->php.

I tried to use TTextWriter.RegisterCustomJSONSerializerFromText as you suggested.
It threw me an error when the parser came across the nested object. This one: "ESynException.Create('V1,V2,..V16')".

Unit SynCommons.pas
...

procedure TJSONCustomParserFromTextDefinition.Parse
begin
...
    case P^ of
    ',': if PropsMax=cardinal(high(Props)) then
          raise ESynException.Create('V1,V2,..V16') else begin
          inc(P);
          inc(PropsMax);
          continue;
        end;
    ':': inc(P);
    end;
...

Still I was able to convince my java/php developer to get rid of these trees and just send flat JSON.
So I am good. :-)

#3 mORMot 1 » JSONToObject example - nested objects » 2013-12-10 02:26:58

Serrg
Replies: 17

I am trying to parse the following JSON string:

{"transactions":[
{
 "TRTYPE":"INCOME",
 "TRDATE":"2013-12-09 02:30:04",
 "TRAA":"1.23",
 "TRCAT1":{"TITYPE":"C1","TIID":"1","TICID":"","TIDSC30":"description1","TIORDER":"0","TIDEL":"false"},
 "TRCAT2":{"TITYPE":"C2","TIID":"2","TICID":"","TIDSC30":"description2","TIORDER":"0","TIDEL":"false"},
 "TRCAT3":{"TITYPE":"C3","TIID":"3","TICID":"","TIDSC30":"description3","TIORDER":"0","TIDEL":"false"},
 "TRRMK":"Remark",
 "TRACID":{"TITYPE":"AC","TIID":"4","TICID":"","TIDSC30":"account1","TIORDER":"0","TIDEL":"false"}
},
{
  "TRTYPE":"INCOME",
...
}
]}    

I've declared the following classes:

type
  TTitleRecord = class(TCollectionItem)
  private
    fTITYPE: RawUTF8;
    fTIID: RawUTF8;
    fTICID: RawUTF8;
    fTIDSC30: RawUTF8;
    fTIORDER: RawUTF8;
    fTIDEL: RawUTF8;
  published
    property TITYPE: RawUTF8 read fTITYPE write fTITYPE;
    property TIID: RawUTF8 read fTIID write fTIID;
    property TICID: RawUTF8 read fTICID write fTICID;
    property TIDSC30: RawUTF8 read fTIDSC30 write fTIDSC30;
    property TIORDER: RawUTF8 read fTIORDER write fTIORDER;
    property TIDEL: RawUTF8 read fTIDEL write fTIDEL;
  end;

  TTransactionRecord = class(TCollectionItem)
  private
    fTRAA: RawUTF8;
    fTRTYPE: RawUTF8;
    fTRTYPEID: integer;
    fTRCAT1: TTitleRecord;
    fTRCAT2: TTitleRecord;
    fTRCAT3: TTitleRecord;
    fTRDATE: RawUTF8;
    fTRRMK: RawUTF8;
    fTRACID: TTitleRecord;
  published
    property TRAA: RawUTF8 read fTRAA write fTRAA;
    property TRTYPE: RawUTF8 read fTRTYPE write fTRTYPE;
    property TRTYPEID: integer read fTRTYPEID write fTRTYPEID;
    property TRCAT1: TTitleRecord read fTRCAT1 write fTRCAT1;
    property TRCAT2: TTitleRecord read fTRCAT2 write fTRCAT2;
    property TRCAT3: TTitleRecord read fTRCAT3 write fTRCAT3;
    property TRDATE: RawUTF8 read fTRDATE write fTRDATE;
    property TRRMK: RawUTF8 read fTRRMK write fTRRMK;
    property TRACID: TTitleRecord read fTRACID write fTRACID;
  end;

  TTransactionRecords = class(TInterfacedCollection)
  private
    function GetCollItem(aIndex: Integer): TTransactionRecord;
  protected
    class function GetClass: TCollectionItemClass; override;
  public
    function Add: TTransactionRecord;
    property Item[aIndex: Integer]: TTransactionRecord read GetCollItem; default;
  end;

function TTransactionRecords.GetCollItem(aIndex: Integer): TTransactionRecord;
begin
  result := TTransactionRecord(GetItem(aIndex));
end;

class function TTransactionRecords.GetClass: TCollectionItemClass;
begin
  result := TTransactionRecord;
end;

function TTransactionRecords.Add: TTransactionRecord;
begin
  result := TTransactionRecord(inherited Add);
end;

When I feed TTransactionRecords object to JSONToObject function - it fails to parse it. It stumbles when it parses TRCAT1 which is TTitleRecord type. I guess I can't make TTransactionRecord a TCollectionItem if it contains other objects... Am I on the right page?

Help with proper class definitions would be greatly appreciated!

#4 Re: mORMot 1 » Client only. Send JSON data via http Post. Receive and parse JSON. » 2013-10-23 15:11:39

Zdorovenki Buli MPV,

Demo #16 - "Execute SQL via services" has some good examples of how to use ObjectToJSON.

Now about TWinHttp vs. THttpClientSocket... Documentation says TWinHttp seems to be faster than THttpClientSocket.

Is there any sample code on how to work with TWinHttp. I didn't find any so far. I found only THttpClientSocket in Demo #13.

#5 Re: mORMot 1 » Client only. Send JSON data via http Post. Receive and parse JSON. » 2013-10-22 20:51:20

OK. I read some more today...
To answer my own question - Yes, I can do simple http post requests from Delphi using Synopse Framework.
It is described in Demo# 13 - Standalone JSON SQL Server.

It is really simple comparing to the code I found yesterday which showed how to create post requests using Wininet API.

Now I need to find out how to form my JSON string.

As I understood I cannot use TSQLHttpClient on the client if I do not use Synopse on the server side.
I have to use THttpClientSocket and form my string with some JSON functions.

Please correct me if I am wrong.

#6 mORMot 1 » Client only. Send JSON data via http Post. Receive and parse JSON. » 2013-10-22 17:24:00

Serrg
Replies: 7

My task sounds pretty simple though I have never worked with http before... so I am not even sure I am on the right forum... :-)

We have MSSQL database on the server. Server-side talking to database is implemented in php.

I have a Delphi (BDS2006) client that needs to exchange data with that MSSQL database. Our php developer send me a couple of php file names with JSON format that he expects. He explained to me that I need to send JSON data to the server via http post method.

Question: Is it something that can be easily done with Synopse classes?

If so - are there any demo projects that would demonstrate how to send http post request?

Thanks in advance!

Board footer

Powered by FluxBB