#1 2024-01-10 13:31:11

liam1983
Member
Registered: 2021-01-01
Posts: 10

How to use ObjectLoadJson to deserialize json not generated by mORMot?

Hello,

I try to deserialize JSON from a rest service

type
  TecRmiData = class
  private
    FTypeId: string;
    FTcdTypeId: string;
    FHerstSchluessel: string;
  public
    property TypeId: string read FTypeId write FTypeId;
    property TcdTypeId: string read FTcdTypeId write FTcdTypeId;
    property HerstSchluessel: string read FHerstSchluessel
      write FHerstSchluessel;

  end;

type
  TKbaDataResult = class(TObject)
  private
    FSearchKey: string;
    FTooMuchResults: Boolean;
    FNumberOfResults: Integer;
    FNumberOfPossibleResults: Integer;
    FFoundData: TArray<TecRmiData>;
  public
    constructor Create;
    destructor Destroy; override;

    property SearchKey: string read FSearchKey write FSearchKey;
    property TooMuchResults: Boolean read FTooMuchResults write FTooMuchResults;
    property NumberOfResults: Integer read FNumberOfResults
      write FNumberOfResults;
    property NumberOfPossibleResults: Integer read FNumberOfPossibleResults
      write FNumberOfPossibleResults;
    property FoundData: TArray<TecRmiData> read FFoundData;
  end;

Like this:

  
 t := TKbaDataResult.Create;
 valid := ObjectLoadJson(t, StringToUtf8(restRequest.Response.JSONText));

Valid is true but the object isnt populated with any data.

Offline

#2 2024-01-10 14:51:59

tbo
Member
Registered: 2015-04-20
Posts: 353

Re: How to use ObjectLoadJson to deserialize json not generated by mORMot?

liam1983 wrote:

Valid is true but the object isnt populated with any data.

Only "published" properties are serialized. If you want to serialize other properties, you must register your own functions with TRttiJson.RegisterCustomSerializer().

With best regards
Thomas

Offline

#3 2024-01-10 15:47:32

liam1983
Member
Registered: 2021-01-01
Posts: 10

Re: How to use ObjectLoadJson to deserialize json not generated by mORMot?

Thanks! My problem is that with 500 child objects in the list, the whole process lasts 10 seconds for deserialisation. Is there a way to speed it up?

Offline

#4 2024-01-10 16:16:47

tbo
Member
Registered: 2015-04-20
Posts: 353

Re: How to use ObjectLoadJson to deserialize json not generated by mORMot?

liam1983 wrote:

My problem is that with 500 child objects in the list, the whole process lasts 10 seconds for deserialisation. Is there a way to speed it up?

Something else is wrong. In this article you will find some benchmark values for comparison. The time for 500 objects should only be a few milliseconds.

With best regards
Thomas

Last edited by tbo (2024-01-10 16:17:38)

Offline

#5 2024-01-10 16:22:09

liam1983
Member
Registered: 2021-01-01
Posts: 10

Re: How to use ObjectLoadJson to deserialize json not generated by mORMot?

tbo wrote:
liam1983 wrote:

My problem is that with 500 child objects in the list, the whole process lasts 10 seconds for deserialisation. Is there a way to speed it up?

Something else is wrong. In this article you will find some benchmark values for comparison. The time for 500 objects should only be a few milliseconds.

With best regards
Thomas

I believe this also, but how can I search for the reason? Because its not a very complex thing here.

Offline

#6 2024-01-10 20:48:22

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

Re: How to use ObjectLoadJson to deserialize json not generated by mORMot?

Your class need to have its RTTI, inherit from TPersistent/TSynPersistent and have the properties "published" not public.
But since you use a dynamic array published property to store class instances, you should inherit from TSynAutoCreateFields to release all published T*ObjArray fields.

500 children in 10 seconds does not make any sense.
It should be in less than a millisecond.
Somethings is wrong in your code, I guess.

Please provide us with a https://stackoverflow.com/help/minimal- … le-example
and follow the Forum Rules https://synopse.info/forum/misc.php?action=rules : do not put the code directly in the forum.

Offline

Board footer

Powered by FluxBB