Serialization Mormot Object with ArrayDynamic

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:

  

{"customer": 1, "messages": "Test Message", "expire":   "2017-07-19", "instructions": [{"TextInstructions": "Text1"         }, {"TextInstructions": "Text2"}, {             "TextInstructions": "Text3"}, {"TextInstructions": "Text4"         }]}

I need this:

  

{"customer": 1, "messages": "Test Message", "expire"
  : "2017-07-19", "instructions": ["Text1", "Text2", "Text23", "Text4"]   }

Can anyone help me?

    
asked by anonymous 19.07.2017 / 21:37

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);
    
29.09.2017 / 05:12