#1 2013-05-21 17:33:30

Luzcka
Member
From: Brazil / Chile
Registered: 2012-12-07
Posts: 20

Object to JSON

Hi!
I'm new in this tool and have some questions, but to facilitate research in the forum'll ask the questions in separate threads and obviously after searching, reading and trying to solve it myself.
The problem in question is to convert an object to JSON, where the manual and examples I use ObjectToJSON taking care to use the directive {$ M +} or the class is a descendant of TPersistence, this works perfectly ... but have difficulty in properties that are a TObjectList <> or are Dynamic Arrays of objects ... I understand I could use TJSONSerializer.RegisterCustomSerializer creating a class with callback's for reading and writing. Below is a code example and would like to know the best way to solve (or only one way) the problem.

{$M+}
type
  TDataType = (tlTemperature, tlHumidity);
  TData = class
    private
      FPortId  : Integer;
      FDataType: TDataType;
      FValue   : Double;
      function Get_PortId: Integer;
      function Get_DataType: TDataType;
      function Get_Value: Double;
      procedure Set_PortId(const Value: Integer);
      procedure Set_DataType(const Value: TDataType);
      procedure Set_Value(const Value: Double);
    published
      property PortId  : Integer read Get_PortId write Set_PortId;
      property DataType: TDataType read Get_DataType write Set_DataType;
      property Value   : Double read Get_Value write Set_Value;
  end;

  TDataList = class (TList<TData>)
  end;
  TDataArray = Array of TData;

  TAcquisitionBoard = class
    private
      FIdentify: RawUTF8;
      FModel   : RawUTF8;
      FData : TDataArray;
      function Get_Identify: RawUTF8;
      function Get_Model: RawUTF8;
      function Get_Data: TDataArray;
      procedure Set_Identify(const Value: RawUTF8);
      procedure Set_Model(const Value: RawUTF8);
    public
      constructor Create;
      destructor Destroy; override;

      function AddData: TData;
    published
      property Identify: RawUTF8 read Get_Identify write Set_Identify;
      property Model   : RawUTF8 read Get_Model write Set_Model;
      property Data    : TDataArray read Get_Data;
  end;

Whereas the objects have the following data:

Identify: 12345 Model: A2013
Temperature: 23.79
Temperature: 30.02

When running memoJSON.Lines.Text: = UTF8ToString (ObjectToJSON (FAcquisitionBoard, []));

I get: {"Identify": "12345", "Model": "A2013", "Data": []}
Without informing the content of the property "Data".

How to get complete information? Data as Dynamic Array or TObjectList<> or other structure...

Thanks in advance!

Offline

#2 2013-05-21 18:31:18

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

Re: Object to JSON

You can not have a dynamic array of classes.
Nor TObjectList<...> kind of property.

Use a collection or a TObjectList.

Or a dynamic array of records, with custom JSON serialization.

See the SAD 1.18 pdf for further note.

See also latest sample 20, in the source code repository.

Online

#3 2013-05-21 21:11:05

Luzcka
Member
From: Brazil / Chile
Registered: 2012-12-07
Posts: 20

Re: Object to JSON

Thanks, I'll download the version 1.18 for analyze SAD and sample 20.

Offline

Board footer

Powered by FluxBB