#1 2018-01-29 01:51:38

skipjack
Member
Registered: 2018-01-29
Posts: 3

bug with JSON parsing

This seems to be a bug with json parsing. Please try my code.

all fields are fine except bids that's every time is 0



type
  TRL i= packed record
    timestamp: integer;
    asks     : array of array[0..1] of string;
    bids     : array of array[0..1] of string;
  end;

var   s : UTF8String;
      RLi: TRLi;

s:= quoted text below
.............
        RecordLoadJSON(RLi, @s[1], typeinfo(TRLi) );
        Caption := Length(RLi.asks).ToString + ':' +  Length(RLi.bids).ToString; //208 : 0
..................

Last edited by skipjack (2018-01-29 15:07:49)

Offline

#2 2018-01-29 05:06:41

Alek
Member
From: Russia
Registered: 2014-07-04
Posts: 44

Re: bug with JSON parsing

Check your json at least one online validator. https://jsonformatter.org/ https://codebeautify.org/jsonvalidator https://jsonlint.com/ You can see, that your data is not correct.

Last edited by Alek (2018-01-29 05:07:36)

Offline

#3 2018-01-29 09:03:05

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

Re: bug with JSON parsing

Please DO NOT post such big amount of code or JSON in the forum!
Check the https://synopse.info/forum/misc.php?action=rules !
8)

Also note that array[0..1] kind of record type definition is not supported.
Use a dynamic array instead.

Then, as Alek stated, ensure that your input JSON is correct.. wink

Offline

#4 2018-01-29 17:48:38

skipjack
Member
Registered: 2018-01-29
Posts: 3

Re: bug with JSON parsing

ab can you please explain why this happens:

Here is working type declaration for the code below :
type
  TRLi = packed record
    timestamp: string;
    bids  : array of array[0..1] of string;
    asks     : array of array[0..1] of string;
  end;

And here is declaration that won't work:
type
  TRLi = packed record
    timestamp: string;
    asks : array of array[0..1] of string;
    bids     : array of array[0..1] of string;   
  end;

both types are same, but RecordLoadJSON doesn't work with second Type as I expect

procedure TForm1.Button1Click(Sender: TObject);
var   s : UTF8String;
      RLi: TRLi;

begin
      s:=  '{"timestamp": "1","asks": [["2", "3"], ["4", "5"]],"bids": [["6", "7"], ["8", "9"], ["10", "11"]]}'  ;
      RecordLoadJSON(RLi, @s[1], typeinfo(TRLi) );
      Caption := Length(RLi.asks).ToString + ':' +  Length(RLi.bids).ToString; //2 : 3
end;

Last edited by skipjack (2018-01-29 17:56:28)

Offline

#5 2018-01-29 19:11:23

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

Re: bug with JSON parsing

As I wrote above:

Also note that array[0..1] kind of record type definition is not supported.
Use a dynamic array instead.

Offline

#6 2018-01-29 20:42:08

skipjack
Member
Registered: 2018-01-29
Posts: 3

Re: bug with JSON parsing

I can't understand what to do, may be spot of code will help me to understand.

if you mean use of  asks : array of array of string; instead of asks : array of array[0..1] of string;
it doesn't work.

Last edited by skipjack (2018-01-29 20:43:38)

Offline

#7 2018-01-29 20:59:19

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

Re: bug with JSON parsing

Please define "it doesn't work" in this context.

Offline

#8 2018-01-30 10:54:47

esmondb
Member
From: London
Registered: 2010-07-20
Posts: 299

Re: bug with JSON parsing

Maybe defining a record then an array would help

Offline

Board footer

Powered by FluxBB