JSON parser error due to my data values.

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

JSON parser error due to my data values.

Graeme Geldenhuys-6
Hi,

I have the following snippet in a JSON file.

================
        "Settings": {
                "SQLDir": "d:\data"
        }

================

When I load the complete JSON file using FPC's jsonparser.pp unit I get
a parser error.

"Invalid character at line 6, pos 16: 'd' "


It seems like it is trying to interpret the field data value like a
C/C++ escape sequence. eg: \n meaning NewLine

I such escape sequence processing a JSON standard? Can I tell the JSON
parser to treat data as-is, without any interpretation?


Regards,
  Graeme

--
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://fpgui.sourceforge.net/

My public PGP key:  http://tinyurl.com/graeme-pgp
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: JSON parser error due to my data values.

Graeme Geldenhuys-6
Just to be clear regarding the problem.

If I change my JSON file so the data values reads...

  "d:/data"

instead of the original:   "d:\data"

...then FPC's JSON parser has no problem.

So it seems the FPC JSON parser doesn't like DOS\Windows paths as data
values. Surely any JSON parser shouldn't try and interpret the data
stored in a JSON file?


ps:
   I'm using FPC 2.6.4 under Windows 32bit.


Regards,
  Graeme

--
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://fpgui.sourceforge.net/

My public PGP key:  http://tinyurl.com/graeme-pgp
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: JSON parser error due to my data values.

Karoly Balogh (Charlie/SGR)
In reply to this post by Graeme Geldenhuys-6
Hi,

On Thu, 26 Jan 2017, Graeme Geldenhuys wrote:

> It seems like it is trying to interpret the field data value like a
> C/C++ escape sequence. eg: \n meaning NewLine
>
> I such escape sequence processing a JSON standard?

Yes. See:
http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf

Figure 5.

> Can I tell the JSON parser to treat data as-is, without any
> interpretation?

That is probably a bad idea, as it goes against JSON standards. Basically
the JSON you're trying to feed it is invalid. Fix the JSON, don't try to
force the parser to work it around.

Charlie
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: JSON parser error due to my data values.

Graeme Geldenhuys-6
In reply to this post by Graeme Geldenhuys-6
On 2017-01-26 10:54, Graeme Geldenhuys wrote:
> So it seems the FPC JSON parser doesn't like DOS\Windows paths as data
> values.

I guess the alternative (in case \<something> is supposed to be
interpreted as an escape sequence) is to store my DOS\Windows paths with
forward slashes like UNIX, then use SetDirSeparators() in my program to
fix the resulting string.

eg   "d:/data"


Regards,
  Graeme

--
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://fpgui.sourceforge.net/

My public PGP key:  http://tinyurl.com/graeme-pgp
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: JSON parser error due to my data values.

Graeme Geldenhuys-6
In reply to this post by Karoly Balogh (Charlie/SGR)
On 2017-01-26 10:56, Karoly Balogh (Charlie/SGR) wrote:
>> Is such escape sequence processing a JSON standard?
>
> Yes. See:
> http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf


Ah, thanks for confirming that. I see now the relevant JSON standards
information regarding a string value is as follows:

==================================
string
    ""
    " chars "
chars
    char
    char chars
char
    any-Unicode-character-
        except-"-or-\-or-
        control-character
    \"
    \\
    \/
    \b
    \f
    \n
    \r
    \t
    \u four-hex-digits
==================================



> That is probably a bad idea, as it goes against JSON standards. Basically
> the JSON you're trying to feed it is invalid. Fix the JSON, don't try to
> force the parser to work it around.

Thanks, I'll use forward slashes instead and then SetDirSeparators() in
my code after reading in those values.

Looking into the fpjson.pp unit I see the following two functions...

Function StringToJSONString(const S : TJSONStringType) : TJSONStringType;
Function JSONStringToString(const S : TJSONStringType) : TJSONStringType;


Which does all those conversions for you too. Good to know! ;-)


Regards,
  Graeme

--
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://fpgui.sourceforge.net/

My public PGP key:  http://tinyurl.com/graeme-pgp
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: JSON parser error due to my data values.

geneb
In reply to this post by Karoly Balogh (Charlie/SGR)
> On Thu, 26 Jan 2017, Graeme Geldenhuys wrote:
>
>> It seems like it is trying to interpret the field data value like a
>> C/C++ escape sequence. eg: \n meaning NewLine
>>
>> I such escape sequence processing a JSON standard?
>

Does it work if you try "d:\\data"?

g.

--
Proud owner of F-15C 80-0007
http://www.f15sim.com - The only one of its kind.
http://www.diy-cockpits.org/coll - Go Collimated or Go Home.
Some people collect things for a hobby.  Geeks collect hobbies.

ScarletDME - The red hot Data Management Environment
A Multi-Value database for the masses, not the classes.
http://scarlet.deltasoft.com - Get it _today_!
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: JSON parser error due to my data values.

Graeme Geldenhuys-6
On 2017-01-26 14:31, geneb wrote:
> Does it work if you try "d:\\data"?


Yes. My problem was I didn't realise the JSON standard supports escaped
sequences.

My program is only reading from the JSON file at this point, not
writing. The JSON file is manually created with a text editor. So to be
on the safe side, I now implemented my code to support both "d:/data"
and "d:\\data" string data formats.


Regards,
  Graeme

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal