Sign in Join

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

Mailing lists / fpc-pascal / 2017-02

TJSONData.FindPath() troubles where data contains dots.

5 messages · 2 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've studied the following documenation to make sure I'm using FindPath() correctly.

http://www.freepascal.org/docs-html/3.0.0/fcl/fpjson/tjsondata.findpath.html

Now, I have the following JSON data.... I omitted what is not relevant. ========================
{
...snip...
"VersionDependencies": {
"2.5.0": {
"packages": "master",
"framework": "3.11.0"
}
}
}
========================

I can do Data.FindPath("VersionDependencies") and it finds the data node without problems.

But when I try:

var
  ver: string:
begin
  ver := '2.5.0';
  ...snip...
  Data.FindPath('VersionDependencies.'+ver)

It never finds the "2.5.0" data node. I'm assuming the dots in the version string is what is causing the problem in fcl-json. Initially I thought I could add extra quotes around the version string. Like so:

Data.FindPath('VersionDependencies.'''+ver+'''')

But that didn't work either.

Is this a bug of some sorts, or is there another way around this problem?

I guess my only option is to use TJSONenum and iterate of the "VersionDependencies" data, and manually look for the data node I'm interested in. Like so:

  d := Data.FindPath('VersionDependencies');
  for ItrItem in d do
    if d.Key = ver then
       ...snip...

Is there another way of finding the data I'm interested in?

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

Reply by email

Michael Van Canneyt
to Graeme Geldenhuys
Graeme Geldenhuys wrote 7 lines
Hi, I've studied the following documenation to make sure I'm using FindPath() correctly. http://www.freepascal.org/docs-html/3.0.0/fcl/fpjson/tjsondata.findpath.html

since . is used as a delimiter between path segments, you indeed cannot find paths that contain a . in a segment.

I see no nice way out of this.

One way is to allow to escape dots in the path.
But because every character is allowed in a javascript object property, that would mean that \. can also be a correct property name, and so we need to introduce \\ as an escape for \...

It could be added.

Michael

Reply by email

Graeme Geldenhuys
to Michael Van Canneyt
Michael Van Canneyt wrote 6 lines
One way is to allow to escape dots in the path. But because every character is allowed in a javascript object property, that would mean that \. can also be a correct property name, and so we need to introduce \\ as an escape for \... It could be added.

No, I think that will just get messy (your choice). I was just wondering if I was doing something wrong. Either way, I managed to use TJSONenum and it worked just fine for my needs.

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

Reply by email

Michael Van Canneyt
to Graeme Geldenhuys
Graeme Geldenhuys wrote 12 lines
On 2017-02-08 16:33, Michael Van Canneyt wrote: > One way is to allow to escape dots in the path. > But because every character is allowed in a javascript object property, that > would mean that \. can also be a correct property name, and so we need to > introduce \\ as an escape for \... > > It could be added. No, I think that will just get messy (your choice). I was just wondering if I was doing something wrong. Either way, I managed to use TJSONenum and it worked just fine for my needs.

Indeed. If you experiment in the browser console:

var obj = {};
undefined
obj
Object { }
obj["1.2.3"] = 'ok';
"ok"
obj
Object { 1.2.3: "ok" }
obj['allez"na'] = 'ok';
"ok"
obj
Object { 1.2.3: "ok", allez"na: "ok" }
obj['not\nice']="beeeh"
"beeeh"
obj
Object { 1.2.3: "ok", allez"na: "ok", not
ice: "beeeh" }
obj['not\ nice']="beeeh"
"beeeh"
obj
Object { 1.2.3: "ok", allez"na: "ok", not
ice: "beeeh", not nice: "beeeh" }

Not so nice at all...

Michael.

Reply by email