You are not logged in.
I noticed that JsonArrayDecode is nowhere used in Mormot2 but it exists as a function in file mormot.core.json
is it a kind of unfinished code? Can I use it in cross platform apps?
how can I use it to parse an array of of JSON objects created by RecordSaveJson like the following?
[{"id":1,"nam":"name1"},{"id":2,"nam":"name2"}]
I was thinking of getting each JsonObject as rawutf8 and transform it with RecordLoadJson
Thank you in advance
Offline
Using Mormot2, is there an existing way to parse a json array to have the same deserializing with RecordLoadJson without using variants?
I am a bit confused with this function (JsonArrayDecode), can you help me please?
Offline
I think I found something:
a) The P should not start with '[', last ']' is needed
b) an inc(n) is missing and at the end the values are always nil
function JsonArrayDecode(P: PUtf8Char; out Values: TPUtf8CharDynArray): boolean;
var
n, max: integer;
parser: TJsonGotoEndParser;
begin
result := false;
max := 0;
n := 0;
{%H-}parser.Init({strict=}false, nil);
P := GotoNextNotSpace(P);
if P^ <> ']' then
repeat
if max = n then
begin
max := NextGrow(max);
SetLength(Values, max);
end;
Values[n] := P;
P := parser.GotoEnd(P);
if P = nil then
exit; // invalid content, or #0 reached
//todo: needs an inc(n); n never incs
if P^ <> ',' then
break;
inc(P);
until false;
if P^ = ']' then
begin
SetLength(Values, n);
result := true;
end
else
Values := nil;
end;
Last edited by dcoun (2021-11-27 22:21:55)
Offline
Please don't put so much code in this forum, as stated by the forum rules you accepted.
https://synopse.info/forum/misc.php?action=rules
This function work as expected, and as documented: "incoming P^ should point to the first char AFTER the initial '['".
It is a low-level function, not a function for your use case.
Don't use RecordLoadJson, but just use DynArrayLoadJson for what you need - as I wrote above.
Offline
Thank you again
You have right about the documentation of the not needed '['
But, is the "inc(n);" needed?
Offline