#1 2021-11-27 13:44:41

dcoun
Member
From: Crete, Greece
Registered: 2020-02-18
Posts: 416

parse a JSON array with JsonArrayDecode in Mormot2

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

#2 2021-11-27 14:35:03

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,527
Website

Re: parse a JSON array with JsonArrayDecode in Mormot2

Use a Dynamic array instead.

Offline

#3 2021-11-27 16:27:40

dcoun
Member
From: Crete, Greece
Registered: 2020-02-18
Posts: 416

Re: parse a JSON array with JsonArrayDecode in Mormot2

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

#4 2021-11-27 17:58:29

dcoun
Member
From: Crete, Greece
Registered: 2020-02-18
Posts: 416

Re: parse a JSON array with JsonArrayDecode in Mormot2

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

#5 2021-11-27 21:45:07

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,527
Website

Re: parse a JSON array with JsonArrayDecode in Mormot2

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

#6 2021-11-27 22:09:53

dcoun
Member
From: Crete, Greece
Registered: 2020-02-18
Posts: 416

Re: parse a JSON array with JsonArrayDecode in Mormot2

Thank you again
You have right about the documentation of the not needed '['
But, is the "inc(n);" needed?

Offline

#7 2021-11-28 21:30:50

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,527
Website

Re: parse a JSON array with JsonArrayDecode in Mormot2

Yes, of course, you are right.

Please check my last commit.

Offline

Board footer

Powered by FluxBB