Giter VIP home page Giter VIP logo

delphi-neon's Introduction

Neon - Serialization Library for Delphi


Neon Library

What is Neon

Neon is a serialization library for Delphi that helps you to convert (back and forth) objects and other values to JSON. It supports simple Delphi types but also complex class and records. Please take a look at the Demo to see

General Features

Configuration

Extensive configuration through INeonConfiguration interface:

  • Word case (UPPERCASE, lowercase, PascalCase, camelCase, snake_case)
  • CuStOM CAse (through anonymous method)
  • Member types (Fields, Properties)
  • Option to ignore the "F" if you choose to serialize the fields
  • Member visibility (private, protected, public, published)
  • Custom serializer registration
  • Use UTC date in serialization

Delphi Types Support

Neon supports the (de)serialization of most Delphi standard types, records, array and of course classes. Classes can be complex as you want them to be, can contain array, (generic) lists, sub-classes, record, etc...

Simple values

  • Basic types: string, Integer, Double, Boolean, TDateTime

Complex values

  • Arrays of (basic types, records, classes, etc...)
  • Records with fields of (basic types, records, classes, arrays, etc...)
  • Classes with fields of (basic types, records, classes, arrays, etc...)
  • Generic lists
  • Dictionaries (key must be of type string)
  • Streamable classes

Custom Serializers

  • Inherit from TCustomSerializer and register this new class in the configuration

Todo

Features
  • Better way to register (and find) a custom serializer
Code
  • Unit Tests

Prerequisite

This library has been tested with Delphi 10.3 Rio Delphi 10.2 Tokyo, Delphi 10.1 Berlin, but with a minimum amount of work it should compile with Delphi XE7 and higher

Libraries/Units dependencies

This library has no dependencies on external libraries/units.

Delphi units used:

  • System.JSON (DXE6+)
  • System.Rtti (D2010+)
  • System.Generics.Collections (D2009+)

Installation

Simply add the source path "Source" to your Delphi project path and.. you are good to go!

Code Examples

Serialize an object

To

Using TNeon utility class

The easiest way to serialize and deserialize is to use the TNeon utility class:

Object serialization:

var
  LJSON: TJSONValue;
begin
  LJSON := TNeon.ObjectToJSON(AObject);
  try
    Memo1.Lines.Text := TJSONUtils.PrettyPrint(LJSON);
  finally
    LJSON.Free;
  end;
end;

Object deserialization:

var
  LJSON: TJSONValue;
begin
  LJSON := TJSONObject.ParseJSONValue(Memo1.Lines.Text);
  try
    TNeon.JSONToObject(AObject, LJSON, AConfig);
  finally
    LJSON.Free;
  end;

Using TNeonSerializer and TNeonDeserializer classes

Using the TNeonSerializerJSON and TNeonDeserializerJSON classes you have more control over the process.

Object serialization:

var
  LJSON: TJSONValue;
  LWriter: TNeonSerializerJSON;
begin
  LWriter := TNeonSerializerJSON.Create(AConfig);
  try
    LJSON := LWriter.ObjectToJSON(AObject);
    try
      Memo1.Lines.Text := TJSONUtils.PrettyPrint(LJSON);
      MemoError.Lines.AddStrings(LWriter.Errors);
    finally
      LJSON.Free;
    end;
  finally
    LWriter.Free;
  end;
end;

Object deserialization:

var
  LJSON: TJSONValue;
  LReader: TNeonDeserializerJSON;
begin
  LJSON := TJSONObject.ParseJSONValue(Memo1.Lines.Text);
  if not Assigned(LJSON) then
    raise Exception.Create('Error parsing JSON string');

  try
    LReader := TNeonDeserializerJSON.Create(AConfig);
    try
      LReader.JSONToObject(AObject, LJSON);
      MemoError.Lines.AddStrings(LWriter.Errors);
    finally
      LReader.Free;
    end;
  finally
    LJSON.Free;
  end;

Neon configuration

It's very easy to configure Neon,

var
  LConfig: INeonConfiguration;
begin
  LConfig := TNeonConfiguration.Default
    .SetMemberCase(TNeonCase.SnakeCase)     // Case settings
    .SetMembers(TNeonMembers.Properties)    // Member type settings
    .SetIgnoreFieldPrefix(True)             // F Prefix settings
    .SetVisibility([mvPublic, mvPublished]) // Visibility settings

    // Custom serializer registration
    .GetSerializers.RegisterSerializer(TGUIDSerializer)
  ;
end;

Paolo Rossi

delphi-neon's People

Contributors

paolo-rossi avatar ccy avatar mattiavicari avatar davidevisconti avatar gcarneiroa avatar gliden avatar jensmertelmeyer avatar

Watchers

James Cloos 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.