РУС Site Map Contact Us Home
E-mail Newsletter
Subscribe to get informed about
Clever Components news.

Your Name:
Your Email:
 
SUBSCRIBE
 
Previous Newsletters
 




Products Articles Downloads Order Support
Customer Portal   

Delphi JSON serialization using Rtti

Delphi->Json->Java

An updated version of the JSON Serializer library is available in article JSON object serializer for Delphi
The updated library is completely Open Source. You can serialize and deserialize arrays and unions of objects of different types, deserialize inherited objects, serialize empty strings and many more.

JSON (JavaScript Object Notation) is an efficient data encoding format that enables fast exchanges of data between clients and AJAX-enabled Web services.

This article demonstrates how to serialize Delphi type objects into JSON-encoded data and then deserialize data in the JSON format back into instances of Delphi types using the Json Serializer. The example includes both the source code of the Json Serializer class (TclJsonSerializer) and a unit-test code that demonstrates how to serialize and deserizlise differrent data types, inlcuding Delphi strings, integers, objects and arrays.

TclJsonSerializer utilizes the RTTI library and custom Delphi attributes for linking user-defined Delphi objects and properties with the corresponding JSON data parts.

The December's update provides separated classes for the abstract JSON structures and parser implementation. This helps users to change the program so that it does the same job without the non-free library - Clever Internet Suite. Minor fixes and improvements were included as well.

Define a data type

For defining a data type, you need to attach the TclJsonPropertyAttribute attribute to the property you want to serialize.

// [Delphi]
TclTestObject = class
private
    FStringValue: string;
    FIntegerValue: Integer;
    FIntArray: TArray<Integer>;
public
    [TclJsonString('stringValue')]
    property StringValue: string read FStringValue write FStringValue;

    [TclJsonProperty('integerValue')]
    property IntegerValue: Integer read FIntegerValue write FIntegerValue;

    [TclJsonProperty('intArray')]
    property IntArray: TArray<Integer> read FIntArray write FIntArray;
end;

Serialize an instance of data type to JSON

1. Create an instance of data type.

// [Delphi]
obj := TclTestObject.Create();
obj.StringValue := 'test';
obj.IntegerValue := 123;

SetLength(intArr, 2);
obj.IntArray := intArr;
intArr[0] := 111;
intArr[1] := 222;

2. Serialize the object to the JSON string.

// [Delphi]
json := TclJsonSerializer.ObjectToJson(obj);

Deserialize an instance of data type from JSON

Call to the TclJsonSerializer.JsonToObject class method and specify both the serializeble data type and the JSON-encoded source string.

// [Delphi]
obj := TclJsonSerializer.JsonToObject(TclTestObject, jsonEtalon) as TclTestObject;

Supported compiler versions

Json Serializer can be used in RAD Studio XE3 and later. If you modify the sources and remove all references to the RAD Studio namespaces in the 'uses' sections, you can use the library in RAD Studio 2009, 2010, XE and XE2 as well.

Download source code

Download Json Serializer

Download Clever Internet Suite

Old version Download

Download the previous version of Json Serializer

The library is distributed under the terms of the GNU Lesser General Public License version 3GNU General Public License

 

Sergey Shirokov
Clever Components team
www.clevercomponents.com

    Copyright © 2000-2017