#1 2012-05-23 08:54:27

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

leading zeros in JSON

I'm trying to store a 14 digit barcode number as a RawUTF8 field. Occasionally a number will start with zero which causes a JSON parsing error.

As a work-around I could prefix all the numbers with an underscore but is there a better way?

Thanks

Offline

#2 2012-05-23 09:19:47

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

Re: leading zeros in JSON

In which context is the parsing error occurring?

At low-level of the ORM, what is stored within a RawUTF8 should remain as stored.

Only when displayed, the column may be identified as a number, then triggering such an error.

Could you provide some code to reproduce the issue?

Offline

#3 2012-05-23 10:34:13

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

Re: leading zeros in JSON

The parsing error is happening in javascript (jquery ajax call). It seems that a number starting with zero isn't strictly valid JSON.

The data is coming from an EPOS system which puts an underscore in front of the number. I was stripping it out as I couldn't see the point of it but it's probably easier to just leave it in.

Offline

#4 2012-05-23 12:40:56

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

Re: leading zeros in JSON

In fact, when you retrieve a number starting with a 0, it is not a valid JSON number, unless the next character is '.' or a plain zero.
That is, '0', '0.123' are valid numbers, and '0123' is not.
And JSON does not allow '+' to indicate positive numbers (only '-' sign).
See http://json.org/

I did not know this, and find it to be weird.
Sounds like a C-like-behavior inherited into JavaScript (for octal/hexadecimal notation)...

I've modified the ORM source code to reflect this unexpected JSON definition, about '0' and '+'.
See http://synopse.info/fossil/info/7f82fd67bd

Thanks for the feedback.
Coming from a pascal point of view, this is "bizarre". smile

Offline

#5 2012-05-30 12:00:24

mpv
Member
From: Ukraine
Registered: 2012-03-24
Posts: 1,543
Website

Re: leading zeros in JSON

2 ab.
I use overridden version of TSQLTable.GetJSONValues (when expant=false I write resp in {"fields":["fld1", "fld2"], "data": [[val1, val2,..valN],[val1, val2,..valN],...[]]} - it's more native for JavaScript client, but found a bug in your and my code

function TSQLTable.GetJSONValues(
       line:    if IsString(U^) then begin
must be:    if IsStringJSON(U^) then begin

it's important for string fields started from ZERO must me "code": "01", then IsStrint(U^) I got "code": 01 and JavaScript think code is integer..

may be exist other place where is changes necessary.....
I add ticket.

Offline

#6 2012-05-30 16:30:47

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

Re: leading zeros in JSON

Nice catch.

I made a code review.
It should be fixed by http://synopse.info/fossil/info/eb2b772e52

Thanks for the feedback.

Offline

Board footer

Powered by FluxBB