#1 2016-11-11 08:59:51

g00gle
Member
Registered: 2016-10-24
Posts: 8

RecordLoadJSON fail case

Here is sample, RecordLoadJSON fail at "cost"

    TR = packed record
    errorId          :  Integer;
    errorCode	       :  String;
    errorDescription :	String;
    status           :	String;
    solution         :
                        record
                        text : string;
                        url  : string;
                        end;
    cost             :	Double;
    ip               :	String;
    createTime       :	Integer;
    endTime	         :  Integer;
    solveCount       :	Integer;
  end;
....

var 
 R: TR;
 s: string;

s:= '{"errorId":0,"status":"ready","solution":{"text":"failed","url":"http:\/\/69.39.239.233\/101\/147884423179984.png"},"cost":"0.001000","ip":"95.143.198.250","createTime":1478844231,"endTime":1478844239,"solveCount":"0"}';

SynCommons.RecordLoadJSON(R, s, TypeInfo(TR) );

Last edited by g00gle (2016-11-11 10:14:31)

Offline

#2 2016-11-11 12:35:12

turrican
Member
From: Barcelona
Registered: 2015-06-05
Posts: 94
Website

Re: RecordLoadJSON fail case

This is working to me...

Two things :

   1.- You are passing in JSON a (cost) Double variable like a string, this will not be parsed. Pass it without quotes.
   2.- You are passing in JSON a (solveCount) Integer variable like a string, this will not be parsed. Pass it without quotes.

Look at your JSON and adapt it.

Last edited by turrican (2016-11-11 12:39:51)

Offline

#3 2016-11-11 12:54:33

g00gle
Member
Registered: 2016-10-24
Posts: 8

Re: RecordLoadJSON fail case

turrican
This is not JSON from my side. :\
and every thing works if change double to string,
but i think it must work without this change.

Offline

#4 2016-11-11 12:56:20

turrican
Member
From: Barcelona
Registered: 2015-06-05
Posts: 94
Website

Re: RecordLoadJSON fail case

g00gle wrote:

turrican
This is not JSON from my side. :\
and every thing works if change double to string,
but i think it must work without this change.

So, where is crashing? Or where is the fault of the program?

Offline

#5 2016-11-11 13:21:03

turrican
Member
From: Barcelona
Registered: 2015-06-05
Posts: 94
Website

Re: RecordLoadJSON fail case

Offline

#6 2016-11-11 14:56:14

g00gle
Member
Registered: 2016-10-24
Posts: 8

Re: RecordLoadJSON fail case

04f1690a-a838-11e6-9372-e42148d86ac9.gif


unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, SynCommons, Vcl.StdCtrls;

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

   TR = packed record
    errorId          :  Integer;
    errorCode	       :  String;
    errorDescription :	String;
    status           :	String;
    solution         :
                        record
                        text : string;
                        url  : string;
                        end;
    cost             :	Double;
    ip               :	String;
    createTime       :	Integer;
    endTime	         :  Integer;
    solveCount       :	Integer;
  end;


var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
  var
 R: TR;
 s: string;
begin
  s:= '{"errorId":0,"status":"ready","solution":{"text":"failed","url":"http:\/\/69.39.239.233\/101\/147884423179984.png"},"cost":"0.001000","ip":"95.143.198.250","createTime":1478844231,"endTime":1478844239,"solveCount":"0"}';
  SynCommons.RecordLoadJSON(R, s, TypeInfo(TR) );
  Memo1.Lines.Add(    R.errorId.ToString  );
  Memo1.Lines.Add(     R.errorCode	      );
  Memo1.Lines.Add(     R.errorDescription );
  Memo1.Lines.Add(     R.status           );
  Memo1.Lines.Add(     R.solution.text    );
  Memo1.Lines.Add(     R.solution.url     );
  Memo1.Lines.Add(  'COST: ' + Format('%3.6f', [R.cost])        );
  Memo1.Lines.Add(  'IP: '  + R.ip                 );
  Memo1.Lines.Add(     R.createTime.ToString         );
  Memo1.Lines.Add(     R.endTime.ToString             );
  Memo1.Lines.Add(     R.solveCount.ToString           );
end;

Last edited by g00gle (2016-11-11 14:57:12)

Offline

#7 2016-11-14 09:42:46

turrican
Member
From: Barcelona
Registered: 2015-06-05
Posts: 94
Website

Re: RecordLoadJSON fail case

Correct your JSON :

s:= '{"errorId":0,"status":"ready","solution":{"text":"failed","url":"http:\/\/69.39.239.233\/101\/147884423179984.png"},"cost":0.001000,"ip":"95.143.198.250","createTime":1478844231,"endTime":1478844239,"solveCount":0}';

Offline

#8 2016-11-14 12:21:18

g00gle
Member
Registered: 2016-10-24
Posts: 8

Re: RecordLoadJSON fail case

2 turrican
read careful  This is not JSON from my side. :\ http://synopse.info/forum/viewtopic.php … 303#p22303

Last edited by g00gle (2016-11-14 12:22:23)

Offline

#9 2016-11-14 13:42:26

turrican
Member
From: Barcelona
Registered: 2015-06-05
Posts: 94
Website

Re: RecordLoadJSON fail case

So change your model to next one :

TR = packed record
    errorId          :  Integer;
    errorCode           :  String;
    errorDescription  :    String;
    status            :    String;
    solution         :
                        record
                        text : string;
                        url  : string;
                        end;
    cost            :    string;
    ip               :    String;
    createTime       :    Integer;
    endTime              :  Integer;
    solveCount       :    string;
  end;

Offline

#10 2016-11-15 03:05:36

g00gle
Member
Registered: 2016-10-24
Posts: 8

Re: RecordLoadJSON fail case

2 turrican
Are you seriously ?  lol
I'm strongly recommend to you read my second post: http://synopse.info/forum/viewtopic.php … 303#p22303

Last edited by g00gle (2016-11-15 03:07:01)

Offline

#11 2016-11-15 03:21:50

g00gle
Member
Registered: 2016-10-24
Posts: 8

Re: RecordLoadJSON fail case

And if you not agree that float can be readed from double quotes in case when we know that is it valid float then I'm not agree with you.

Offline

#12 2016-11-15 13:01:17

leus
Member
Registered: 2012-09-05
Posts: 79

Re: RecordLoadJSON fail case

It doesn't matter if you agree, quoted strings are not doubles and the JSON definition is clear about it. It is up to you to use something like TryStrToFloat() with the given values to get your actual value. Perhaps use a secondary getter method or a intermediate class?

Offline

#13 2016-11-15 14:02:09

turrican
Member
From: Barcelona
Registered: 2015-06-05
Posts: 94
Website

Re: RecordLoadJSON fail case

leus wrote:

It doesn't matter if you agree, quoted strings are not doubles and the JSON definition is clear about it.

Thanks

Last edited by turrican (2016-11-15 14:02:23)

Offline

Board footer

Powered by FluxBB