#1 2021-07-04 10:04:09

jsmart
Member
Registered: 2014-09-19
Posts: 8

Equivalent of RecordLoadJSON and RecordSaveJSON for mobile clients

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

#2 2021-07-04 13:50:10

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

Re: Equivalent of RecordLoadJSON and RecordSaveJSON for mobile clients

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

Board footer

Powered by FluxBB