Book Image

Delphi Cookbook

By : Daniele Teti
Book Image

Delphi Cookbook

By: Daniele Teti

Overview of this book

Table of Contents (14 chapters)
Delphi Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Serializing objects to JSON and back using RTTI


When you are using a domain model pattern (and you should do most of the time for non-trivial applications), the entities managed by your program are contained in objects. An object has a state and methods to change its state, just like any actual object in the real world.

Getting ready

As the datasets in the previous recipe is very popular with the needs to serialize an object in a JSON object, send the object somewhere, and then recreate that object as it was before. In this recipe, we'll use the new TJSON class present in Delphi XE6 and will extend it with new functionalities.

How to do it...

Let's execute the following steps to serialize objects to JSON:

  1. Create a new VCL forms application.

  2. Drop four TButtons components and a TMemo component on the form. Organize the TButton component in a single row as a sort of toolbar and align the TMemo component to cover the remaining part of the form.

  3. Name the TButton components as follows:

    • btnObjToJSON

    • ...