You are not logged in.
Pages: 1
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
What's your TJsonRecord definition in the second case?
Offline
type
TJsonRecord =
record
Name : string;
ByteArray : array[1..3] of Byte;
Bytes : TBytes;
end;
Offline
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
Embarcadero® Delphi 11 Version 28.0.48361.3236
Offline
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!
Offline
You need to change record to packed record, also change Name type from string to RawUtf8 type.
Online
Please note that there is no Name field inside the TJsonRecord:
type
TJsonRecord =
record
ByteArray : array[1..3] of Byte;
end;
Offline
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
Please note that there is no Name field inside the TJsonRecord:
There is in your first post and also dynamic not static array, TBytes!
Online
Pages: 1