[Pas2js] Nested objects

Ondrej Pokorny lazarus at kluug.net
Thu Dec 10 11:32:15 CET 2020


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.

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


More information about the Pas2js mailing list