Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2017-01

JSON parser error due to my data values.

7 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Graeme Geldenhuys

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
Graeme Geldenhuys
to Graeme Geldenhuys

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
Karoly Balogh (Charlie/SGR
to Graeme Geldenhuys: I have the following snippet in a JSON file. ================ "Settings": { "SQLDir":…

Hi,

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

Graeme Geldenhuys
to Graeme Geldenhuys: Just to be clear regarding the problem. If I change my JSON file so the data values…
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
Graeme Geldenhuys
to Karoly Balogh (Charlie/SGR: Yes. See: http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf…
Karoly Balogh 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
geneb
to Karoly Balogh (Charlie/SGR: Yes. See: http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf…
Show quoted text 7 lines
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_!
Graeme Geldenhuys
to geneb
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