You are not logged in.
Hi
I utilise your RecordSaveJSON and RecordLoadJSON to serialize/deserialize my json between my ms windows client and server.
This works extremely well and very fast (obviously). Plus only one line...
However, I'm trying to create a mobile client (Android) now to speak to my remote server and trying to see if there's anything equivalent that I can use there.
I'm looking into the SynCrossPlatformJSON unit, but looking through the doco, that only works with TPersistent descendants? My DTO are records. Can you suggest anything? I'd like to keep the same DTO structure of records.
Currently, this is what I have working via Windows:
DTO:
type
TAccountDetailsDTORec = packed record
Id: Integer;
Description: string;
FNDepositAmount: Currency;
OrdId: Integer;
end;
TAccountDetailsDTORecArray = TArray<TAccountDetailsDTORec>;
and my common, generic methods for deserializing are:
class function TmORMotRecParser.JsonToDTO<T>(const JsonString: string): T;
begin
if not RecordLoadJson(Result, StringToUTF8(JsonString), TypeInfo(T)) then
raise EParserException.CreateFmt('Unable to parse DTO Json string: "%s"', [JsonString]);
end;
// for arrays
class function TmORMotRecParser.JsonToDTOArray<T>(const JsonString: string): T;
begin
DynArrayLoadJSON(Result, Pointer(StringToUTF8(JsonString)), TypeInfo(T));
end;
and serializing:
class function TmORMotRecParser.DTOToJson<T>(const DTO: T): string;
begin
Result := UTF8ToString(RecordSaveJSON(DTO, TypeInfo(T)));
end;
Offline
You are right: SynCrossPlatformJSON does not directly support records.
They are supported when the client source code is generated, from some interface based services.
See the documentation about it.
Offline