#1 2026-01-08 21:15:24

StraighFree
Member
Registered: 2025-06-07
Posts: 4

Deserializing JSON on mormot2

Hello,

I'm using the mormot2 in a project. In this project I make one request to a external API. This API was returning the following object:

{
    "id": 1,
    "name": null,
    "language_id": null,
    "created_at": "2025-08-22T03:54:57.000000Z",
    "updated_at": "2025-08-22T03:54:57.000000Z",
    "active": true
}

And I'm using this code to get the catalog and synchronize in my application:

procedure TSyncThread.SyncCatalog;
var
  Json: RawUtf8;
  Valid: Boolean;
  CatalogVar: variant;
  CatalogModel: TCatalog;
begin
  try
    Valid := false;
    Json := StringToUtf8(FApiConnector.getCatalog);

    CatalogVar := _JsonFast(Json);

    try
      CatalogModel := TCatalog.Create;
      JsonToObject(CatalogModel, Pointer(Json), Valid);
      if (Valid) then
      begin
        TDatabaseServer.GetInstance.getServer.CreateOrUpdate(CatalogModel);
        SyncCategories(CatalogVar.categories);
      end;
    finally
      CatalogModel.Free;
    end;
  except
    on exception do
    raise;
  end;
end;

After some time, the external API was changed and introduced a new property position to the API response:

{
    "id": 1,
    "name": null,
    "language_id": null,
    "created_at": "2025-08-22T03:54:57.000000Z",
    "updated_at": "2025-08-22T03:54:57.000000Z",
    "active": true,
    "position": 1
}

After that, my code to deserialize the JSON above stop to working and the var Valid is everytime false and the var CatalogModel dont have any other properties filled.

I did a search on forum, but I didnt find any similar case.

Is it the expected behavior? Have in the mormot2 any way to deserialize automatically a JSON and just ignore the properties that is not existing in the application class? Or in that case I will need to do the desearialization manually, property by property?

Any help is welcome.

Thank you.

Last edited by StraighFree (2026-01-08 21:16:59)

Offline

#2 2026-01-08 22:42:37

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

Re: Deserializing JSON on mormot2

JsonToObject(CatalogModel, Pointer(Json), Valid, Nil, JSONPARSER_TOLERANTOPTIONS);

Offline

#3 2026-01-09 14:44:28

StraighFree
Member
Registered: 2025-06-07
Posts: 4

Re: Deserializing JSON on mormot2

Hi tbo,

Thank you for the fast response.
I tried to use this

JsonToObject(CatalogModel, Pointer(Json), Valid, Nil, JSONPARSER_TOLERANTOPTIONS);

But I still facing the same behavior. If my application class dont have an property present in the JSON, the Valid is false and the properties on CatalogModel is not filled.

Do you have any other suggestion?

Thanks

Last edited by StraighFree (2026-01-09 18:43:26)

Offline

#4 2026-01-09 18:43:17

StraighFree
Member
Registered: 2025-06-07
Posts: 4

Re: Deserializing JSON on mormot2

Hello,

I checked the mormot2 have the method CatalogModel.FillFrom(JsonString), that I can use to fill the properties for a model. In that, case we dont have the boolean value to check but we can fill the properties for a model with the value from a JSON.

Thank you for the help tbo

Offline

#5 2026-01-09 18:52:59

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 15,372
Website

Re: Deserializing JSON on mormot2

You are messing with the Json buffer variable, including a pointer(Json) which would change it in place.

Please consider using no pointer() but the function with a regular RawUtf8 parameter.

Offline

Board footer

Powered by FluxBB