#1 2025-11-27 07:58:38

dka1
Member
From: Greece
Registered: 2017-07-25
Posts: 30

json string as IDocAny

Hello ab,
I am study the new mormot2 json IDocDict,IDocList it is very usefull to handle json structure by interfaces.
I see DocDict('{}') and DocList('[]') but how load a json string that do not know if is object or array.
Can I build a DocAny that load object or array json string?

Thanks

Offline

#2 2025-11-27 16:18:06

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 15,308
Website

Re: json string as IDocAny

IDocDict is a dictionary/object.
IDocList is a list/array.
If you want to store anything use a variant.
And behind the scene both IDocDict/IDocList store their data within a custom variant type - a TDocVariant.

Offline

#3 2025-12-01 07:45:21

dka1
Member
From: Greece
Registered: 2017-07-25
Posts: 30

Re: json string as IDocAny

Thank you ab
i create bellow simple DocAny function
if you adopt it add it at mormot.core.variants

function DocAny(const json: RawUtf8; model: TDocVariantModel = mFastFloat): IDocAny;
begin
  if Copy(json,1,1) = '[' then
    result:= DocList(model)
  else
    result := DocDict(model);
  result.SetJson(json);
end;

Offline

#4 2025-12-01 08:14:04

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 15,308
Website

Re: json string as IDocAny

Ah ok now I understand.
You want a IDocAny factory class.

Please try with
https://github.com/synopse/mORMot2/commit/e712c2f6f

Offline

Board footer

Powered by FluxBB