[Pas2js] Nested objects

Michael Van Canneyt michael at freepascal.org
Thu Dec 10 11:51:40 CET 2020



On Thu, 10 Dec 2020, Ondrej Pokorny wrote:

> 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.

> 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.


More information about the Pas2js mailing list