Skip to content

Object To Json representation problem #9

@hafedh-trimeche

Description

@hafedh-trimeche

Hello,

This code

unit Main;
interface

uses
  Winapi.Windows, REST.JSON, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls,
  JsonsUtilsEx;
type
  TForm1 = class(TForm)
    Memo1: TMemo;
    ObjectBtn: TButton;
    procedure ObjectBtnClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

  TJObject=class
  private
    FP1 : Integer;
    FP2 : TBytes;
    FP3 : string;
    FP4 : TDateTime;
  published
    property P1 : Integer read FP1 write FP1;
    property P2 : TBytes  read FP2 write FP2;
    property P3 : string  read FP3 write FP3;
    property P4 : TDateTime read FP4 write FP4;
  end;

var
  Form1: TForm1;

implementation
{$R *.dfm}

procedure TForm1.ObjectBtnClick(Sender: TObject);
var
  O      : TJObject;
  sJson  : string;
begin
  O     := TJObject.create;
  O.P1  := 11;
  O.P2  := [3,2,1];
  O.P3  := 'String';
  O.P4  := now;
  sJson := __ObjectToJson(O);
  Memo1.Lines.Add(sJson);
  O.P2  := [];
  __jsonToObject(sJson,TObject(O));
  sJson := __ObjectToJson(O);
  Memo1.Lines.Add(sJson);
  FreeAndNil(O);
end;

initialization
  ReportMemoryLeaksOnShutdown := True;
finalization
end.

generated this Json:

{
"P1" : 11 , 
"P2" : [ -1761541629,-2139084140,0 ] , 
"P3" : "String" , 
"P4" : "2021-01-03T21:58:55.055Z"
}

The output would be:

{
"P1" : 11 , 
"P2" : [ 3,2,1 ] , 
"P3" : "String" , 
"P4" : "2021-01-03T21:58:55.055Z"
}


Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions