#2 Re: mORMot 2 » Problems when serialize enums or set of enum in mormot2 » 2023-10-19 06:40:36

spr

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

#3 mORMot 2 » Problems when using custom reader / writer for JSON serialization and » 2023-10-12 10:53:18

spr
Replies: 1

Hello everyone,

Today, during the mORMot2 migration process, I encountered an issue related to JSON serialization and deserialization. I observed a different behavior in mORMot2 compared to mORMot1. To replicate the problem, please note that custom reader and writer procedures are registered for a class, TBase. In mORMot1, all descendants of TBase (TChild = class(TBase)) were serialized and deserialized using these reader and writer procedure. However, this is no longer the case in mORMot2. I now need to register the same reader and writer methods for each descendant.

TJSONSerializer.RegisterClassForJSON(TBase);
TJSONSerializer.RegisterClassForJSON(TChild );


TRttiJson.RegisterCustomSerializerClass(TBase,
    TBaseClassJSON.Reader, TBaseClassJSON.Writer);

//in mormot2 this is necessary
TRttiJson.RegisterCustomSerializerClass(TChild ,
    TBaseClassJSON.Reader, TBaseClassJSON.Writer);

Is this an unintended issue or an intentional change in mORMot2? It would be beneficial if the behavior in mORMot2 aligns with that of mORMot1, as otherwise, I would need to register the same reader and writer methods separately for all my descendants.

Thanks in Advance

#4 Re: mORMot 2 » Problems when serialize enums or set of enum in mormot2 » 2023-09-13 14:37:09

spr

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.

#5 mORMot 2 » Problems when serialize enums or set of enum in mormot2 » 2023-09-12 12:23:38

spr
Replies: 3

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

Board footer

Powered by FluxBB