0

In my Delphi 10.1 Berlin Datasnap REST application, I need to customize the JSON serializaton of an object.

I would like to find a solution that makes use of the JSONReflect attribute, and doesn't involve the creation of Converters and Reverters for every specific field, as described in this article by Daniele Teti.

In particular, I'm trying to serialize an object that contains:

  • a binary file, to convert in JSON representation - like a byte array
  • some TDateTime fields, to convert in a String with ISO format

I have found a technical PDF document by Marco Cantù, that talks about JSONReflect attribute to enable conversion of fields, but I cannot find documentation about it.

Anyone can help me, please?

Danilo Casa
  • 506
  • 1
  • 9
  • 18

1 Answers1

1

Use of JSONReflect attribute automatically implies use of converters and reverters. Delphi XE6 ships with sample project MarshallUnmarshall where JSON serialization is covered. RAD Studio Demo Code is also available online.

If you want to serialize an object that contains a TDateTime field in a string with ISO format, you can also use standard Tjson class defined in Rest.Json unit. It contains an ObjectToJsonString method. In the AOptions parameter you can specify to format dates using ISO standard.

class function ObjectToJsonString(AObject: TObject; AOptions: TJsonOptions = [joDateIsUTC, joDateFormatISO8601]): string;

To serialize a binary file in JSON Daniele Teti has sample code in his Delphi Cookbook. Unfortunately I can not share sample code I think. Recommended reading! Second edition has just appeared.

Erwin
  • 1,896
  • 1
  • 11
  • 17