Hi, have some way to quickly access nested objects (TJSObject).
Mailing lists / pas2js / 2020-12
Nested objects
15 messages · 3 people ·
- Hheliosroots 7
- MMichael Van Canneyt 7
- OOndrej Pokorny 1
Reply by email Subscribe to this list Your subscription
Read-only archive. Use j and k to move between messages.
heliosroots wroteHi, 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.
Michael Van Canneyt wrote 12 lines
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
We can create a class helper for that: TJSObjectHelper = Class helper for TJSObject…
It's a good alternative, but I particularly avoid the use of helper...
function GetNestedValue(ATarget: JSValue; APath: string; ADefaultValue:
JSValue): JSValue;
function SetNestedValue(ATarget: JSValue; APath: string; ANewValue:
JSValue): boolean;so I'm not limited to using only TJSObject
aPath is a JSON-path? I didn't know Pas2JS supports JSON-path internally. Btw., to get a…
The idea is to use the primitive data directly ... without having to make big parses or casts, your idea is good but I would use fpjson that makes a parser and some cast ...
aPath is a JSON-path? I didn't know Pas2JS supports JSON-path internally. Btw., to get a…
Ondrej Pokorny wrote 15 lines
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
I sense a JSUtils unit taking shape ;-)
Michael.
The idea is to use the primitive data directly ... without having to make big parses or…
heliosroots wroteThe idea is to use the primitive data directly ... without having to make big parses or casts, your idea is good but I would use fpjson that makes a parser and some cast ...
Sorry, I am not sure I understand what you mean ?
Michael.
sorry i expressed myself badly! "I would not use fpjson which makes a parser and some cast".
heliosroots wrotesorry 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.
did a test implementation ... not case sensitive, what do you think?
jsutils.pas <http://pas2js.38893.n8.nabble.com/file/t17/jsutils.pas>
heliosroots wrotedid a test implementation ... not case sensitive, what do you think? jsutils.pas <http://pas2js.38893.n8.nabble.com/file/t17/jsutils.pas>
Nice :-)
JSON is case sensitive.
Maybe make case insensitive an option ?
(fpjson also has an optional case-insensitive find)
Michael.
I made a change, if you want to incorporate in the core feel free!
jsutils.pas <http://pas2js.38893.n8.nabble.com/file/t17/jsutils.pas>
heliosroots wroteI made a change, if you want to incorporate in the core feel free! jsutils.pas <http://pas2js.38893.n8.nabble.com/file/t17/jsutils.pas>
Any chance of supporting arrays ?
See:
https://tools.ietf.org/html/rfc6901
Michael.
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);heliosroots wrote 5 lines
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.
