#1 2024-06-17 11:19:27

Hafedh TRIMECHE
Member
Registered: 2016-09-18
Posts: 37

Json Serialize static array problem

Hello,

This code outputs

{"Name":"Name Surname","Bytes":[10,20,30]}

type
  TJsonRecord =
  record
    Name      : string;
    Bytes     : TBytes;
  end;

procedure doSynJson;
var
  JsonRecord : TJsonRecord;
  Json       : string;
begin
  JsonRecord.Name         := 'Name Surname';
  JsonRecord.Bytes        := [10,20,30];
  Json := string(RecordSaveJson(JsonRecord,TypeInfo(TJsonRecord)));
  Memo1.Lines.Add(Json);
end;

but this one outputs:

"￰GE4AYQBtAGUAIABTAHUAcgBuAGEAbQBlAAECA6tTAQAAAAADAAAAAAoUHg=="

With a non displayed characters a the beginning

procedure doSynJson;
var
  JsonRecord : TJsonRecord;
  Json       : string;
begin
  JsonRecord.Name         := 'Name Surname';
  JsonRecord.ByteArray[1] := 1;
  JsonRecord.ByteArray[2] := 2;
  JsonRecord.ByteArray[3] := 3;
  JsonRecord.Bytes        := [10,20,30];
  Json                    := string(RecordSaveJson(JsonRecord,TypeInfo(TJsonRecord)));
  Memo1.Lines.Add(Json);
end;

Last edited by Hafedh TRIMECHE (2024-06-17 11:21:07)

Offline

#2 2024-06-17 14:39:05

RaelB
Member
Registered: 2010-08-04
Posts: 57

Re: Json Serialize static array problem

What's your TJsonRecord definition in the second case?

Offline

#3 2024-06-17 16:21:26

Hafedh TRIMECHE
Member
Registered: 2016-09-18
Posts: 37

Re: Json Serialize static array problem

type
  TJsonRecord =
  record
    Name      : string;
    ByteArray : array[1..3] of Byte;
    Bytes     : TBytes;
  end;

Offline

#4 2024-06-17 16:33:10

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

Re: Json Serialize static array problem

It needs enhanced record RTTI by default, so only since Delphi 2010.

On FPC, you need to register your type by using a text definition.
Otherwise there is a fallback to binary + base-64 serialization, as you observed.

Offline

#5 2024-06-17 16:36:35

Hafedh TRIMECHE
Member
Registered: 2016-09-18
Posts: 37

Re: Json Serialize static array problem

Embarcadero® Delphi 11 Version 28.0.48361.3236

Offline

#6 2024-06-17 16:49:57

Hafedh TRIMECHE
Member
Registered: 2016-09-18
Posts: 37

Re: Json Serialize static array problem

Code changed to:

type
  TJsonRecord =
  record
    ByteArray : array[1..3] of Byte;
  end;
procedure doSynJson;
var
  JsonRecord : TJsonRecord;
  RawJson    : RawByteString;
  Json       : string;
begin
  JsonRecord.ByteArray[1] := 1;
  JsonRecord.ByteArray[2] := 2;
  JsonRecord.ByteArray[3] := 3;
  RawJson                 := RecordSaveJson(JsonRecord,TypeInfo(TJsonRecord));
  Json                    := string(RawJson);
  Memo1.Lines.Add(Json);
end;

The result includes a binary data!

https://mega.nz/file/S85jnAaY#QUViwaxYs … v-XgxtOBEI

Offline

#7 2024-06-17 18:41:04

ttomas
Member
Registered: 2013-03-08
Posts: 135

Re: Json Serialize static array problem

You need to change record to packed record, also change Name type from string to RawUtf8 type.

Offline

#8 2024-06-17 18:43:33

Hafedh TRIMECHE
Member
Registered: 2016-09-18
Posts: 37

Re: Json Serialize static array problem

Please note that there is no Name field inside the TJsonRecord:

type
  TJsonRecord =
  record
    ByteArray : array[1..3] of Byte;
  end;

Offline

#9 2024-06-18 01:38:33

keinn
Member
Registered: 2014-10-20
Posts: 105

Re: Json Serialize static array problem

also fail at record with static array field serialize/unserialiaze , by RecordSaveJson/RecordLoadJson
such as

  TTestResult = packed record
    code:Integer;
    data:array of packed record
      box:array[0..3] of array[0..1] of Integer;
      score:Double;
      msg:RawUtf8;
    end;
  end;

with this json data:
{"code":100,"data":[{"box":[[0,1],[57,1],[57,23],[0,23]],"score":0.8374950289726257,"msg":"110 3300"}]}

RecordLoadJson will just not work

with Delphi 12

Last edited by keinn (2024-06-18 01:41:07)

Offline

#10 2024-06-18 07:22:33

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

Re: Json Serialize static array problem

Static arrays are not fully supported yet.
Use a dynamic array instead, or write your own serializer/unserializer callback.

Offline

#11 2024-06-18 14:09:14

ttomas
Member
Registered: 2013-03-08
Posts: 135

Re: Json Serialize static array problem

Hafedh TRIMECHE wrote:

Please note that there is no Name field inside the TJsonRecord:

There is in your first post and also dynamic not static array, TBytes!

Offline

Board footer

Powered by FluxBB