Skip to content
Some helpers for serializing objects to JSON and back using the standard Delphi library.
Pascal
Branch: master
Clone or download

Latest commit

Fetching latest commit…
Cannot retrieve the latest commit at this time.

Files

Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
source
.gitignore
LICENSE
README.md

README.md

REST-JSon-Helpers

The unit provides some helper classes to make serializing classes into JSON a bit easier.

JsonUTCDate

The JsonUTCDate attribute applied to ca field results in the field value being converted into UTC format when serialized into JSON format. Of course the conversion is reverted when the field is set from JSON. If the field value equals cNoDate (i.e. "0000-00-00T00:00:00.000Z") conversion from/into local time format is skipped. When writing such a value it will result in a JSON null object value.

Usage:

type
  TMyClass = class
  private
    [JsonUTCDate]
    FStartDate: TDateTime;
    [JsonUTCDate]
    FEndDate: TDateTime;
    ...
  end;

JsonStrings

The JsonStrings attribute applied to a TStrings or descendant will serialize the strings to an array of string and vice versa. Note that it should always be accompanied by JSONOwned(False).

Usage:

type
  TMyClass = class
  private
    [JSONOwned(False), JSONStrings]
    FLines: TStringList;
    ...
  end;

JsonObjectList

JsonObjectList is an attribute that should be applied to a TObjectList instance or descendant. It makes the object being serialized to an array of object and back again. It needs an interceptor class derived from TObjectListInterceptor as parameter. It also requires a JSONOwned(False) companion.

Usage:

type
  TContactList = TIdentList<TContact>;
  TContactListInterceptor = TObjectListInterceptor<TContact>;

type
  TMyAddressBook = class
  private
    [JSONOwned(False), JsonObjectList(TContactListInterceptor)]
    FContacts: TContactList;
    ...

The alias TContactList is actually not needed, but the TContactListInterceptor is mandatory - as well as at least one type keyword between the alias declaration and the use of the alias inside the attribute.

You can’t perform that action at this time.