#1 Re: mORMot 1 » Unable to get ObjectToJSON working. » 2017-08-17 06:50:07

Thanks, didn't see the warnings. Works now.

#2 mORMot 1 » Unable to get ObjectToJSON working. » 2017-08-15 08:27:56

mustbe2
Replies: 2

Hi,

Im new to Delphi and i am assiged to webenable the Delphi project here. We like to connect my C# ASP services to the Delphi program.

In my code below ObjectToJSON works, but not the opposite way; so no object is created or filled with the JSONContent. What ami i doing wrong.

Thanks smile


unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,  Vcl.Controls, Vcl.Forms, Vcl.Dialogs,  generics.collections, mORMot, mORMotSQLite3, SynSQLite3Static, mORMotHttpServer, SynCommons, Vcl.StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

  SampleClass = class(TSynPersistent)
  private
    Fstr: string;
    Fn: integer;
  published
    property Str: String read Fstr write Fstr;
  published
    property n: integer read Fn write Fn;
  end;

var
  Form1: TForm1;
  c: SampleClass;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  JSONContent: string;
  newObj: SampleClass;
  utf8: PUTF8char;
  JsonResult: boolean;

begin
  c := SampleClass.Create;
  c.Str := 'String';
  c.n := 99;
  JSONContent := ObjectToJSON(c);
  Memo1.lines.Add(JSONContent);

  newObj := SampleClass.Create;
  TJSONSerializer.RegisterClassForJSON(SampleClass);
  utf8 := JSONToObject(newObj, pointer(JSONContent), JsonResult, SampleClass, [j2oSetterNoCreate]);

  Memo1.lines.Add('JSONToObject result: ' + JsonResult.ToString);
  // JsonResult is false here, newObj is not filled.

  JSONContent := ObjectToJSON(newObj);
  Memo1.lines.Add(JSONContent)
end;

end.

Board footer

Powered by FluxBB