#1 2023-09-12 12:23:38

spr
Member
Registered: 2023-09-12
Posts: 5

Problems when serialize enums or set of enum in mormot2

Hello everyone,

I've encountered the following issue after migrating from Mormot1 to Mormot2 and unfortunately haven't found a solution yet. When using the objectToJson function with the parameters woHumanReadable or woEnumSetsAsText, the generated JSON can no longer be validly created into an object using the jsonToObject function. The set of enum values is empty in the resulting object, and the enum contains the first value. However, if you don't use woEnumSetsAsText or woHumanReadable, so that sets and enums are included in the JSON as ordinal values, creating the object from the JSON string works as expected. I can provide an example for you to reproduce the issue. Here's a brief example code. Unfortunately, this aspect is currently preventing the upgrade to Mormot 2.1 stable.

I hope you can assist me with this issue. It's possible that I may have forgotten a necessary register call in Mormot2 now.

Type
  TSimpleEnum = (enTest, enTest2);
  TEnumSet = set of TSimpleEnum;

  TSimpleExample = class(TPersistent)
  private
    FFullName: string;
    FEnumSet: TEnumSet;
    FEnum: TSimpleEnum;
    procedure SetEnumSet(const Value: TEnumSet);
    procedure SetFullName(const Value: string);
    procedure SetEnum(const Value: TSimpleEnum);
  published
    property FullName: string read FFullName write SetFullName;
    property EnumSet : TEnumSet read FEnumSet write SetEnumSet;
    property Enum: TSimpleEnum read FEnum write SetEnum;
  end;

procedure testit;
begin
  TJSONSerializer.RegisterClassForJSON([TSimpleExample]);

  var oExample := TSimpleExample.Create;
  oExample.FullName := 'ABC';
  oExample.EnumSet := [enTest2];
  oExample.Enum := enTest;

  var sJSON := OBjecttoJSON(oExample,[woHumanReadable]);

  oExample.Free;
  oExample := TSimpleExample.Create;
  var bValid: boolean;

  JsonToObject(oExample,Pointer(StringToUtf8(sJSON)),bValid,nil);
  //bValid = false
  //oExample.EnumSet is Empty
end;

Thanks in advance
  Sebastian

Offline

#2 2023-09-13 13:14:00

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

Re: Problems when serialize enums or set of enum in mormot2

The issue comes from the use of setter.

First of all, ensure you really need a setter method, because it has an overhead, and makes the code more verbose.

Then the following should fix the problem:
https://github.com/synopse/mORMot2/commit/679935b6
(including associated regression tests from your own code)

Offline

#3 2023-09-13 14:37:09

spr
Member
Registered: 2023-09-12
Posts: 5

Re: Problems when serialize enums or set of enum in mormot2

Hello Arnaud,

ab wrote:

The issue comes from the use of setter.

thank you for your quick help. In the meantime, removing the setter methods in the area of Sets and Enums seems to be a solution.
I will validate your patch from the GitHub repo in the coming days to verify if the previous use of setters, as in mormot1, still works.

Greetings
  Sebastian

Edit:

The patch from current github repo works for me, so i still can using setter for complex types.

Last edited by spr (2023-09-15 07:37:44)

Offline

#4 2023-10-19 06:40:36

spr
Member
Registered: 2023-09-12
Posts: 5

Re: Problems when serialize enums or set of enum in mormot2

Hello Arnoud,

I have come across a similar issue now, and perhaps you could provide a good explanation and a patch for this as well. When I use setters for properties of type TArray<T>, I encounter difficulties parsing the JSON and converting it into an object. However, when I remove the setter and use the private field instead, it functions as intended, and JSONToObject returns true for the valid parameter. Nevertheless, I require the setter functionality when assigning the dynamic array.

If you need an example give me a hint.

Best regards,
Sebastian

Offline

Board footer

Powered by FluxBB