Serialization Mormot Object with Arraydinamico

Asked

Viewed 49 times

0

type
     TRecordInstructions = record
     TextInstructions   : RawUTF8;
end;

TArrayInstructions = array of TRecordInstructions;

type
  TDoctoVO = class
  private
      p_customer     : Integer;
      p_messages     : RawUTF8;
      p_expire       : TDateTime;
      p_instructions : TArrayInstructions;
  published
      property customer     : Integer             read p_customer      write p_customer;
      property messages     : RawUTF8             read p_messages     write p_messages;
      property expire       : TDateTime           read p_expire       write p_expire;
      property instructions : TArrayInstructions  read p_instructions write p_instructions;
      constructor Create;
      destructor Destroy; override;
  end;type
     TRecordInstructions = record
     TextInstructions   : RawUTF8;
end;

TArrayInstructions = array of TRecordInstructions;

type
  TDoctoVO = class
  private
      p_customer     : Integer;
      p_messages     : RawUTF8;
      p_expire       : TDateTime;
      p_instructions : TArrayInstructions;
  published
      property customer     : Integer             read p_customer      write p_customer;
      property messages     : RawUTF8             read p_messages     write p_messages;
      property expire       : TDateTime           read p_expire       write p_expire;
      property instructions : TArrayInstructions  read p_instructions write p_instructions;
      constructor Create;
      destructor Destroy; override;
  end;`

.
.

 TDoctoVO }

constructor TDoctoVO.Create;
begin
    SetLength(p_instructions, 4);
end;

.
.
.

destructor TDoctoVO.Destroy;
begin
    FreeAndNil(p_instructions);
    inherited;
end;

Estou tentando serializar assim:


  var
  DoctoVO : TDoctoVO;
  DoctoVO := TDoctoVO.Create;

begin

DoctoVO.customer  := 1;
DoctoVO.messages  := 'Test Mesagens';
DoctoVO.expire    := StrToDate('2017-07-19');

DoctoVO.instructions[0].TextInstructions := 'Text1';
DoctoVO.instructions[1].TextInstructions := 'Text2';
DoctoVO.instructions[2].TextInstructions := 'Text3';
DoctoVO.instructions[3].TextInstructions := 'Text4';

Memo.Lines.Text := JSONReformat(ObjectToJson(DoctoVO));
end;

Answer:

{ "Search": 1, "messages": "Test Merges", "expire": "2017-07-19", "Instructions": [ "Textinstructions": "Text1" }, ː "Textinstructions": "Text2" }, { "Textinstructions": "Text3" }, ː "Textinstructions": "Text4" } ] }

I need it like this:

{ "Customer": 1, "messages": "Test Mergers", "expire"
: "2017-07-19", "Instructions" ["Text1","Text2","Text23","Text4"] }

Can someone help me?

1 answer

0

TInstructions = array of String;

  type
     TCustomersVO = class
    private
      fcustomer     : integer;
      fmessages     : String;
      fexpire       : TDateTime;
      finstructions : TInstructions;
    published
      property customer     : integer       read fcustomer     write fcustomer;
      property messages     : String        read fmessages     write fmessages;
      property expire       : TDateTime     read fexpire       write fexpire;
      property instructions : TInstructions read finstructions write finstructions;
    end;

.
.
.
var
  CustomersVO  : TCustomersVO;
  Instructions : TInstructions;
begin

  CustomersVO := TCustomersVO.Create;

  SetLength(Instructions,4);

  instructions[0] := 'Text1';
  instructions[1] := 'Text2';
  instructions[2] := 'Text3';
  instructions[3] := 'Text4';

  CustomersVO.customer     := 1;
  CustomersVO.messages     := 'Messages';
  CustomersVO.expire       := Date;
  CustomersVO.instructions := Instructions;

  Memo.Lines.Clear;

  Memo.Lines.Text :=  JSONReformat(ObjectToJson(CustomersVO,[]));

  FreeAndNil(CustomersVO);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.