Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Object To Json representation problem #9

Open
hafedh-trimeche opened this issue Jan 3, 2021 · 0 comments
Open

Object To Json representation problem #9

hafedh-trimeche opened this issue Jan 3, 2021 · 0 comments

Comments

@hafedh-trimeche
Copy link
Contributor

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"
}


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant