#1 2016-04-05 16:51:03

ethanyager
Member
Registered: 2016-04-05
Posts: 3

Multidimensional Array support for JSON Serialization of Record

Hi, I have a record containing a multidimensional array and I am getting an error when trying to serialize.

__TMyRecord = 'MyInt: integer; MyString: string; MyArray: array of array of string';

TMyRecord = record
  MyInt: Integer;
  MyStr: string;
  MyArray: Array of Array of string;
end;

If I register the type / add values and try to serialize the record:

procedure TForm1.Button1Click(Sender: TObject);
var
  Myrecord: TMyRecord;
  U: string;
begin
  try
    TTextWriter.RegisterCustomJSONSerializerFromText(
      TypeInfo(TMyRecord),__TMyRecord);
  except
    on e: exception do ShowMessage('ser: ' + e.Message);
  end;
  MyRecord.MyInt:= 10;
  MyRecord.MyString := 'MyString';
  SetLength(MyRecord.MyArray, 1,1);
  MyRecord.MyArray[0,0] := '00';
  MyRecord.MyArray[1,0] := '10';
  MyRecord.MyArray[0,1] := '01';
  MyRecord.MyArray[1,1] := '11';
  U := RecordSaveJSON(MyRecord,TypeInfo(TMyRecord));
  edit1.Text := u;
end;

It fails with: TJSONRecordTextDefinition.Parse: expected syntax is "array of record" or "array of SimpleType"

Does MORMot support multidimensional arrays somehow, like in my example? (not arrays of record)

If not, can someone point me in the right direction of how to add multidimensional array support? The JSON and class definitions are locked and I need to read/write JSON like this:

{
"MyInt":10,
"MyString":"MyString",
"MyArray": [
                  ["00", "10"],
                  ["01","11"]
                ]
}

I have some that are up to 4 or 5 dimensional arrays.

Thanks
Ethan

Offline

#2 2016-04-05 17:44:12

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

Re: Multidimensional Array support for JSON Serialization of Record

This is not supported yet, but by a custom serializer.
See http://synopse.info/files/html/Synopse% … #TITLE_231

If you use a TDocVariantData, you would have no problem to handle such JSON.

Online

#3 2016-04-05 19:46:12

ethanyager
Member
Registered: 2016-04-05
Posts: 3

Re: Multidimensional Array support for JSON Serialization of Record

Thanks, Ab,

With the custom serializer are you suggesting to define the input parameter aValue as a TDocVariantData and then let the writer add the array automatically, then just reverse that for the reader? (Sorry I am unfamiliar with mORMot but am trying to wrap my head around it)

Using previous example:

TMyRecord = record
  MyInt: Integer;
  MyStr: string;
  MyArray: TDocVariantData;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  Myrecord: TMyRecord;
  U: string;
begin
  try
    TTextWriter.RegisterCustomJSONSerializerFromText(
      TypeInfo(TDocVariantData),__TDocVariantData);
  except
    on e: exception do ShowMessage('ser: ' + e.Message);
  end;
  try
    TTextWriter.RegisterCustomJSONSerializerFromText(
      TypeInfo(TMyRecord),__TMyRecord);
  except
    on e: exception do ShowMessage('ser: ' + e.Message);
  end;
  MyRecord.MyInt:= 10;
  MyRecord.MyString := 'MyString';

  V := _Arr(['1','2','3']);

  U := RecordSaveJSON(MyRecord,TypeInfo(TMyRecord));
  edit1.Text := u;
end;

And then custom handle the TDocVariantData type?

class procedure TMyCustomHandler.Writer(const aWriter: TTextWriter; const aValue);
var
  V: TDocVariantData absolute aValue;
begin
  aWriter.AddVariant(V); // something like this?
end;

Thanks
Ethan

Offline

#4 2016-04-06 08:08:42

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

Re: Multidimensional Array support for JSON Serialization of Record

You may use a TDocVariant for parsing or producing the values.
But the idea could be to use lower-level JSON parsing, as documented.

Online

Board footer

Powered by FluxBB