Code Monkey home page Code Monkey logo

rest-json-helpers's Introduction

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<T>

JsonObjectList<T> is a generic attribute that must be derived to resolve the generic. The derived attribute can then be applied to a TObjectList<T> descendant. It makes the list being serialized to an array of object and back again.

Each field holding such a list requires a JSONOwned(False) attribute to avoid the list instance being destroyed during deserialisation.

Usage:

type
  TContact = class
    ...
  end;

  JsonObjectListContactAttribute = class(JsonObjectList<TContact>); { the class keyword is mandatory }
  
type  { this type keyword is mandatory }
  [JsonObjectListContact]
  TContactList = class(TIdentList<TContact>);

type
  TMyAddressBook = class
  private
    [JSONOwned(False)] { this attribute is mandatory to avoid FContacts being destroyed }
    FContacts: TContactList;
    ...

The new attribute (JsonObjectListContactAttribute) as well as the new list (TContactList) must not be an alias - the class keyword is mandatory. Also there must be at least one type keyword between the declaration and the use of the attribute.

Note: The previous version required the declaration of an TObjectListInterceptor<T> descendant, which was given to a non-generic version of the attribute. The new approach is much easier to use.

TConvert

TConvert is a record providing some helpful methods to convert to and from Json. In addition to Json objects it can also produce Json arrays, as well as revert them with its FromJSONArray methods.

rest-json-helpers's People

Contributors

uweraabe avatar wlandgraf avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.