Sign in Join

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

Mailing lists / pas2js / 2020-12

Nested objects

15 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

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

Michael Van Canneyt
to heliosroots
heliosroots wrote
Hi, have some way to quickly access nested objects (TJSObject).

We can create a class helper for that:

TJSObjectHelper = Class helper for TJSObject
   Find(const aPath : String) : JSValue;
   FindObject(const aPath : String) : TJSObject;
end;

What do you think ?

Michael.

Reply by email

Ondrej Pokorny
to Michael Van Canneyt
Michael Van Canneyt wrote 12 lines
On Thu, 10 Dec 2020, heliosroots wrote: > Hi, have some way to quickly access nested objects (TJSObject). We can create a class helper for that: TJSObjectHelper = Class helper for TJSObject   Find(const aPath : String) : JSValue;   FindObject(const aPath : String) : TJSObject; end; What do you think ?

aPath is a JSON-path? I didn't know Pas2JS supports JSON-path internally.

Btw., to get a string value I use:

function JSONValueToString(const aValue: JSValue; const aDef: string):
string;
begin
   if not TryJSONValueToString(aValue, Result) then
     Result := aDef;
end;
function TryJSONValueToString(const aValue: JSValue; out outValue:
string): Boolean;
begin
   Result := isString(aValue);
   if Result then
     outValue := string(aValue)
   else
     outValue := '';
end;

A similar approach can be used for TJSObject:

function JSONValueToObject(const aValue: JSValue): TJSObject;
function TryJSONValueToObject(const aValue: JSValue; out outObject:
TJSObject): Boolean;

I don't know if these routines really belong to the RTL but if you add FindObject() then a global JSONValueToObject() or JSValueToObject() would be good.

Ondrej

Reply by email

Michael Van Canneyt
to Ondrej Pokorny: aPath is a JSON-path? I didn't know Pas2JS supports JSON-path internally. Btw., to get a…
Ondrej Pokorny wrote 15 lines
On 10.12.2020 11:13, Michael Van Canneyt wrote: > On Thu, 10 Dec 2020, heliosroots wrote: > >> Hi, have some way to quickly access nested objects (TJSObject). > > We can create a class helper for that: > > TJSObjectHelper = Class helper for TJSObject >   Find(const aPath : String) : JSValue; >   FindObject(const aPath : String) : TJSObject; > end; > > What do you think ? aPath is a JSON-path? I didn't know Pas2JS supports JSON-path internally.

It does not.

The above would need to be coded; RFC 6901 can be used as a reference for aPath of course.

Show quoted text 28 lines
Btw., to get a string value I use: function JSONValueToString(const aValue: JSValue; const aDef: string): string; begin   if not TryJSONValueToString(aValue, Result) then     Result := aDef; end; function TryJSONValueToString(const aValue: JSValue; out outValue: string): Boolean; begin   Result := isString(aValue);   if Result then     outValue := string(aValue)   else     outValue := ''; end; A similar approach can be used for TJSObject: function JSONValueToObject(const aValue: JSValue): TJSObject; function TryJSONValueToObject(const aValue: JSValue; out outObject: TJSObject): Boolean; I don't know if these routines really belong to the RTL but if you add FindObject() then a global JSONValueToObject() or JSValueToObject() would be good.

I sense a JSUtils unit taking shape ;-)

Michael.

Reply by email

Michael Van Canneyt
to heliosroots
heliosroots wrote
sorry i expressed myself badly! "I would not use fpjson which makes a parser and some cast".

All is clear now.

I agree. No worries, TJSObject is in the JS unit, so no use of FPJSON.

I'll go for a unit JSUtils with a helper and a global routine. The routines proposed by Ondrej also could be put there.

Michael.

Reply by email

Michael Van Canneyt
to heliosroots
heliosroots wrote 5 lines
I did not understand the question? was inspired by fpjson findpath ... please if you can test it. you can use it for example! GetNestedValue (Any, 'Path. [3]. [0]', Def); GetNestedValue (Any, '[0] .Path.Path. [2]', Def);

Please check the example on:

https://tools.ietf.org/html/rfc6901#page-6

But it uses a different syntax...

Maybe an array is also considered an object, and it will work as you wrote it. I will test :-)

Michael.

Reply by email