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

Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder. Design. Code. Compile. Deploy.

Start Free Trial   Free Delphi Community Edition   Free C++Builder Community Edition   Upgrade Today  

  • I get the following. {"name":"Name","prop1":"p1","subFoo":{"name":"subFoo","prop1":"p2","subFoo":null}} {"name":"New Name","prop1":"p1","subFoo":{"name":"subFoo","prop1":"p2","subFoo":null}} When you de-serialize it converts values back that are properties. I tested with the following class. TFoo = class private Fname: string; Fprop1: string; FsubFoo: TFoo; public property name : string read Fname write Fname; property prop1 : string read Fprop1 write Fprop1; property subFoo : TFoo read FsubFoo write FsubFoo; end;
  • I tried this out for an object with a nested object and wasn't able to convert back. Is there support for nested objects? Foo := TFoo.Create(); Foo.name := 'Name'; Foo.prop1 := 'p1'; subFoo := TFoo.Create; subFoo.name := 'subFoo'; subFoo.prop1 := 'p2'; Foo.subFoo :=subfoo; memo1.Lines.Text := TJson.ObjectToJsonString(Foo); NewFoo:=TJson.JsonToObject(memo1.lines.text); NewFoo.name := 'New Name'; memo1.Lines.Add(TJson.ObjectToJsonString(NewFoo)); (output) {"name":"Name","prop1":"p1","subFoo":{"name":"subFoo","prop1":"p2","subFoo":null}} {"name":"New Name","prop1":"","subFoo":null}
  • Some notes on how this actually works: JSONTOBject conversion looks for fields only. In other words, properties are ignored for good reasons. The converter cut off a single leading "F" and applies lowerCamel case: TFoo.FFee becomes {"foo": 100} There is a "hint" mechanism to tell the converter about names and to leave out certain fields: [JSONName('FirstName')] FFoo: string; That would generate {"FirstName":"Hello World"} (Note: case is exactly as specified!) JSONAttribute is in REST.Json.Types [JSONMarshalled(false)] FFoo: string; That would ignore the FFoo field. Fields that exist in the Json, but have no corresponding part in the TObject counterpart will silently be ignored.