#1 2014-05-09 12:24:35

ebaptistella
Member
From: Santa Catarina
Registered: 2014-01-23
Posts: 9

Error JsonToObject (property is an object)

Hello!

I'm having trouble to convert Json for object using the JsonToObject function.
I have the following structure of DTOs:

TDTOBasePersistent = class(TPersistentWithCustomCreate)
  private
    Fbase_per: RawUtf8;
  published
    property base_per: RawUtf8 read Fbase_per write Fbase_per;
  end;

  TDTOBaseCollectionItem = class(TCollectionItem)
  private
    Fbase_coll: RawUtf8;
  published
    property base_coll: RawUtf8 read Fbase_coll write Fbase_coll;
  end;

  TDTOLevel1 = class(TDTOBasePersistent)
  private
    Flevel1: RawUtf8;
  published
    property level1: RawUtf8 read Flevel1 write Flevel1;
  end;

  TDTOLevel2 = class(TDTOBaseCollectionItem)
  private
    Flevel2: RawUtf8;
    Flevel1_inherited: TDTOLevel1;
  published
    property level2: RawUtf8 read Flevel2 write Flevel2;
    property level1_inherited: TDTOLevel1 read Flevel1_inherited write Flevel1_inherited;
  end;

  TDTOMyList = class(TCollection)
  private
    function Add: TDTOLevel2;
  public
    function GetItem(Index: Integer): TDTOLevel2;
    property Item[Index: Integer]: TDTOLevel2 read GetItem;
  end;

Note, I have a collection (TDTOMyList) of TDTOLevel2.
TDTOLevel2 an inheritance of TDTOBaseCollectionItem and has an attribute (level1_inherited) which is a TDTOLevel1 object.
TDTOLevel1 has inherited of TDTOBasePersistent.

When I serializo my object in JSON everything goes perfectly:

var
  oLevel2: TDTOLevel2;
  oLevel1: TDTOLevel1;
begin
  oList := TDTOMyList.Create(TDTOLevel2);
  try
    // Create level 1
    oLevel1          := TDTOLevel1.Create;
    oLevel1.level1   := 'Level 1';
    oLevel1.base_per := 'Base Per';

    // Add DTO Level 2 in List
    oLevel2                  := oList.Add;
    oLevel2.level2           := '1- Level 2';
    oLevel2.Fbase_coll       := '1- Base Coll';
    oLevel2.level1_inherited := oLevel1;

    oLevel2                  := oList.Add;
    oLevel2.level2           := '2- Level 2';
    oLevel2.Fbase_coll       := '2- Base Coll';
    oLevel2.level1_inherited := oLevel1;

    Memo1.Lines.Add(ObjectToJSON(oList));
  finally
    FreeAndNil(oList);
  end;

But when I try to turn this JSON Object I have problems:

var
  isValid: Boolean;
  sJson: RawByteString;
begin
  sJson := Memo1.Lines.Text;

  oList := TDTOMyList.Create(TDTOLevel2);
  try
    JSONToObject(oList, @sJson[1], isValid);
    if (not(isValid)) then
      raise Exception.Create('Error into JSONToObject');
	  
	ShowMessage('JSON is valid');
  finally
    FreeAndNil(oList);
  end;

I'm debug the unit mormot.pas (function JSONToObject) and realized that the problem occurs when you try to convert the JSON for the object "level1_inherited".

This is my string JSON:

[
   {
      "level2":"1- Level 2",
      "level1_inherited":{
         "level1":"Level 1",
         "base_per":"Base Per"
      },
      "base_coll":"1- Base Coll"
   },
   {
      "level2":"2- Level 2",
      "level1_inherited":{
         "level1":"Level 1",
         "base_per":"Base Per"
      },
      "base_coll":"2- Base Coll"
   }
]

Can you help me with this problem?

Offline

#2 2014-05-09 13:55:33

ebaptistella
Member
From: Santa Catarina
Registered: 2014-01-23
Posts: 9

Re: Error JsonToObject (property is an object)

I found a solution.

You need to create an instance of TDTOLevel1 in TDTOLevel2 class:

TDTOLevel2.Create constructor (Collection: TCollection);
begin
   inherited;

   Flevel1_inherited: TDTOLevel1.Create(nil);
end;

Last edited by ebaptistella (2014-05-09 13:56:21)

Offline

Board footer

Powered by FluxBB