Delphi JSON serialization using Rtti |
||
Update notesThe updated version of the JSON Serializer library is available in article JSON object serializer for Delphi
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 typeFor defining a data type, you need to attach the TclJsonPropertyAttribute attribute to the property you want to serialize.
Serialize an instance of data type to JSON1. Create an instance of data type.
2. Serialize the object to the JSON string.
Deserialize an instance of data type from JSONCall to the TclJsonSerializer.JsonToObject class method and specify both the serializeble data type and the JSON-encoded source string.
Supported compiler versionsJson 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 codeDownload Clever Internet Suite Old version DownloadDownload the previous version of Json Serializer The library is distributed under the terms of the GNU Lesser General Public License version 3, GNU General Public License
Sergey Shirokov
|