#2 Re: mORMot 1 » Record property serialization in Delphi < 2010, no type info? » 2017-12-03 19:21:14

On a side note, adding dummy string to TRange was one of the first things I have tried. ObjectToJson still wouldn't parse it. Curious about ab's answer though.

I have one last question, as I ran into a different issue. I thought about using a TStringList field. Is it supported out of the box?

  TEnemy = class(TPersistent)
  private
    fEnabled: Boolean;
    fName: string;
    fList: TStringList;
  public
    constructor Create;
    destructor Destroy; override;
  published
    property Enabled: Boolean read fEnabled write fEnabled;
    property Name: string read fName write fName;
    property List: TStringList read fList write fList;
  end;
  
destructor TEnemy.Destroy;
begin
   fList.Free;
end;

constructor TEnemy.Create;
begin
   fList := TStringList.Create;
end;

When using it like this:

var
 en: TEnemy;
begin
 en := TEnemy.Create;
 en.List.Add('test');
 ol := TObjectList.Create;
 ol.Add(en);
 ObjectToJsonFile(ol, 'test.json');
 ol.Clear;
 JsonFileToObject('test.json', ol, TEnemy);
 ShowMessage(IntToStr(ol.count));
 ol.Free;

JsonFileToObject fails, although the object list is serialized correctly by ObjectToJsonFile. What I am missing here?
Thanks for all your time.

#3 Re: mORMot 1 » Record property serialization in Delphi < 2010, no type info? » 2017-12-03 14:29:38

ab wrote:

As a workaround, you may define the published property as a variant (storing the information as a TDocVariant object), with a public TOffense record property, and getter/setter methods.

  TRange = record
    Min, Max: Integer;
  end;

  TOffense = record
    Damage, AttackSpeed: TRange;
  end;

  TEnemy = class(TPersistent)
  private
    fEnabled: Boolean;
    fName: string; 
    function GetOffense: Variant;
    procedure SetOffense(Value: Variant);
  public
    _Offense: TOffense;
  published
    property Enabled: Boolean read fEnabled write fEnabled;
    property Name: string read fName write fName;
    property Offense: Variant read GetOffense write SetOffense;
  end;
  
  

function TEnemy.GetOffense: Variant;
begin
   Result := _Obj(['Damage',_Obj(['Min',_Offense.Damage.Min,'Max',_Offense.Damage.Min]),
		   'AttackSpeed',_Obj(['Min',_Offense.AttackSpeed.Min,'Max',_Offense.AttackSpeed.Max]]);
end;
  
procedure TEnemy.SetOffense(Value: Variant);
begin
   with _Json(Value) do
   begin
     _Offense.Damage.Min := Damage.Min;
     ...
   end;
end;

Did you mean something like this?

#4 Re: mORMot 1 » Record property serialization in Delphi < 2010, no type info? » 2017-12-03 09:56:03

I have read that and already tried it, see my first post. It doesn't work, because records doesn't have type info (at least in Delphi 7) as ab said.
I need to serialize TObjectList of objects of TEnemy class, which have a record property. Is there any way to do this, other than exposing each record field as a property or upgrading to Delphi 2010?

#5 Re: mORMot 1 » Record property serialization in Delphi < 2010, no type info? » 2017-12-02 22:34:36

Thanks for response. I cannot use record as I'm storing the objects in TObjectList. I changed it to

TRange = record
    Min, Max: Integer;
  end;

  TOffense = record
    Damage, AttackSpeed: TRange;
  end;

  TEnemy = class(TPersistent)
  private
    fEnabled: Boolean;
    fName: string;
    fOffense: TOffense;
  published
    property Enabled: Boolean read fEnabled write fEnabled;
    property Name: string read fName write fName;
    property DamageMin: Integer read fOffense.Damage.Min;
    property DamageMax: Integer read fOffense.Damage.Max;
    property AttackSpeedMin: Integer read fOffense.AttackSpeed.Min;
    property AttackSpeedMax: Integer read fOffense.AttackSpeed.Max;
  end;

and it works.. but seems not elegant to me. What is the recommended way to serialize TObjectList of objects with record/custom type fields?

#6 mORMot 1 » Record property serialization in Delphi < 2010, no type info? » 2017-12-02 16:28:05

salowabicz
Replies: 13
 TRange = record
    Min, Max: Integer;
  end;

  TOffense = record
    Damage, AttackSpeed: TRange;
  end;

  TEnemy = class(TPersistent)
  private
    fEnabled: Boolean;
    fName: string;
    fOffense: TOffense;
  published
    property Enabled: Boolean read fEnabled write fEnabled;
    property Name: string read fName write fName;
    property Offense: TOffense read fOffense;
  end
 

Hello.
Can anyone help me to serialize objects of above class?

ObjectToJson skips the Offense property...

I tried to

const
__TRange = 'Min,Max Integer';
__TOffense  = Damage,AttackSpeed TRange';

TTextWriter.RegisterCustomJSONSerializerFromText(TypeInfo(TRange), __TRange);
TTextWriter.RegisterCustomJSONSerializerFromText(TypeInfo(TOffense), __TOffense);

 

Compilation fails saying that TRange and TOffense have no type info.
I'm using Delphi 7. Is there really no easy way to serialize such a simple object?

Board footer

Powered by FluxBB