Sign in Join

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

Mailing lists / fpc-pascal / 2016-06

Finding the path of a TJSONObject?

4 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,

Is there a method (just so you know GetPath() wasn't it) that returns the "path" or hierarchy of a TJSONObject instance?

eg:
I can do:

  obj := lRootData.Find('Report.Pages.0.Watermark.Font');

But how do I do the reverse. Lets say I have a TJSONObject instance in the obj variable. How do I query the path to that object in the whole JSON hierarchy - the 'Report.Pages.0.Wartermark.Font' text listed above?

TJSONObject has no Parent or Owner property either? So I'm currently using a hack-ish solution, and would like to know if there is a better solution available.

I'm using FPC 2.6.4. If fpjson is improved in FPC 3.0 or 3.1, then I'll simply copy that unit out and use that instead. I haven't checked the fpjson code in FPC 3.0 or 3.1 yet.

Regards,
Graeme

Reply by email

Michael Van Canneyt
to Graeme Geldenhuys
Graeme Geldenhuys wrote 13 lines
Hi, Is there a method (just so you know GetPath() wasn't it) that returns the "path" or hierarchy of a TJSONObject instance? eg: I can do: obj := lRootData.Find('Report.Pages.0.Watermark.Font'); But how do I do the reverse. Lets say I have a TJSONObject instance in the obj variable. How do I query the path to that object in the whole JSON hierarchy - the 'Report.Pages.0.Wartermark.Font' text listed above?

You can't.

There is no reference to the owner.

Michael.

Reply by email

Graeme Geldenhuys
to Michael Van Canneyt
Michael Van Canneyt wrote
You can't. There is no reference to the owner.

After I posted the message I took a closer look at the fpjson code and came to the same conclusion. Apparently the same thing is true for other JSON implementations too.

This is rather baffling - why can't you traverse backwards? It is such a logical thing with any hierarchy of data. And judging by the amount of questions on StackOverflow and other places on the Internet, it seems many people would like a .Parent property too.

Would a patch for that be accepted?

Regards,
Graeme

Reply by email

Michael Van Canneyt
to Graeme Geldenhuys
Graeme Geldenhuys wrote 15 lines
On 2016-06-16 16:48, Michael Van Canneyt wrote: > You can't. > > There is no reference to the owner. After I posted the message I took a closer look at the fpjson code and came to the same conclusion. Apparently the same thing is true for other JSON implementations too. This is rather baffling - why can't you traverse backwards? It is such a logical thing with any hierarchy of data. And judging by the amount of questions on StackOverflow and other places on the Internet, it seems many people would like a .Parent property too. Would a patch for that be accepted?

No.

It increases memory usage, which I want to be kept to an absolute minimum, and I believe the use case for this kind of request to be very marginal. In 99% of cases you will do the JSONPath() approach using find.

I realize someone is out of luck if he is in the 1% marginal cases, but I really don't want to clutter the implementation for this.

There are hooks to instantiate custom descendent types if someone is so inclined, and this can be used to instantiate types that have a parent.

I see more use in a SAX-kind of parser, which could be used to construct a 'map' of the data in case such a thing is needed, which could be used for the purpose of finding a parent. This is still on my todo list.

Michael.

Reply by email