How to convert an object to JSON and back with a single line of code

Ever wanted to take an Object into a format that is easily persisted and back? Well now you can. New in XE6 is the REST.JSON unit. This allows you access to TJSON a class with some very helpful class methods. Using TJSON you can convert an object to a JSON string and back with a little help from generics along the way.

The following code uses a class called TFoo that has a Foo and a Fee property (string and Integer) Using TJson you can then see how to covert the object to a string and back ready for storage, transport etc.

uses REST.JSON; // Also new System.JSON
procedure TForm1.Button1Click(Sender: TObject);
var
  Foo: TFoo;
begin
  Foo := TFoo.Create;
  try
    Foo.Foo := 'Hello World';
    Foo.Fee := 42;
    Memo1.Lines.Text := TJson.ObjectToJsonString(Foo);
  finally
    Foo.Free;
  end;
  Foo := TJson.JsonToObject<TFoo>(Memo1.Lines.Text);
  try
    Foo.Fee := 100;
    Memo1.Lines.Add(TJson.ObjectToJsonString(Foo));
  finally
    Foo.Free;
  end;
end;

If you want to watch this being demo’ed this very show video shows it in action http://youtu.be/TSqWoFvjj5g

3 thoughts on “How to convert an object to JSON and back with a single line of code”

  1. How can this be done with Interfaces?
    ObjectToJsonString requires object and if I put ObjectToJsonString(TMyClass(MyInterfaceVariable)) rtl complains “There not enough RTTI information”

    1. The following works for me….make sure you have RTTI enabled in the project.

      unit Unit2;

      interface

      uses
      System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
      FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.StdCtrls,
      FMX.Controls.Presentation, FMX.ScrollBox, FMX.Memo;

      type
      IFoo = interface
      [‘{2DB7BA3B-8BDC-49C9-8BFC-C2438D4580E0}’]
      procedure SetFee(const Value: Integer);
      procedure SetFoo(const Value: string);
      function GetFee: Integer;
      function GetFoo: string;
      property Foo : string read GetFoo write SetFoo;
      property Fee : Integer read GetFee write SetFee;
      end;

      TFoo = class(TInterfacedObject, IFoo)
      private
      FFee: Integer;
      FFoo: string;
      procedure SetFee(const Value: Integer);
      procedure SetFoo(const Value: string);
      function GetFee: Integer;
      function GetFoo: string;
      published
      property Foo : string read GetFoo write SetFoo;
      property Fee : Integer read GetFee write SetFee;
      end;

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

      var
      Form2: TForm2;

      implementation

      uses Rest.JSON;
      {$R *.fmx}

      procedure TForm2.Button1Click(Sender: TObject);
      var
      Foo: IFoo;
      begin
      Foo := TFoo.Create;
      try
      Foo.Foo := ‘Hello World’;
      Foo.Fee := 42;
      Memo1.Lines.Text := TJson.ObjectToJsonString(TFoo(Foo));
      finally
      end;
      Foo := TJson.JsonToObject(Memo1.Lines.Text);
      try
      Foo.Fee := 100;
      Memo1.Lines.Add(TJson.ObjectToJsonString(TFoo(Foo)));
      finally
      end;
      end;

      procedure TForm2.FormCreate(Sender: TObject);
      begin

      end;

      { TFoo }

      function TFoo.GetFee: Integer;
      begin
      Result := FFee;
      end;

      function TFoo.GetFoo: string;
      begin
      Result := FFoo;
      end;

      procedure TFoo.SetFee(const Value: Integer);
      begin
      FFee := Value;
      end;

      procedure TFoo.SetFoo(const Value: string);
      begin
      FFoo := Value;
      end;

Leave a Reply

Your email address will not be published. Required fields are marked *