Instant.Neon.Serializers
Neon-based JSON serialization for InstantObjects.
Overview
The Instant.Neon.Serializers unit provides JSON serialization/deserialization for InstantObjects using the Neon library, enabling seamless integration with REST APIs.
Key Features:
- Automatic InstantObject to JSON conversion
- Custom serializers for all InstantObject attribute types
- Support for object references, parts, and collections
- Lazy loading control
- Cycle detection and prevention
- Integration with WiRL and MARS Curiosity REST frameworks
Key Classes
TInstantNeonSerializer
Custom Neon serializer for TInstantObject descendants.
Features:
- Serializes all published properties
- Handles InstantObject-specific attribute types
- Prevents infinite loops with cycle detection
- Controls lazy loading behavior
TInstantNeonVisibilityManager
Manages which properties are serialized.
Visibility Levels:
mvPublic- Public propertiesmvPublished- Published properties (default)mvProtected- Protected properties
Usage Patterns
Basic JSON Serialization
pascal
uses
Neon.Core.Serializers.DB, Instant.Neon.Serializers;
var
Company: TCompany;
JSON: string;
Config: INeonConfiguration;
begin
Company := TCompany.Retrieve('COMP001', False, False, Connector);
try
Config := TNeonConfiguration.Default
.SetMemberCase(TNeonCase.CamelCase)
.SetVisibility([mvPublic, mvPublished]);
JSON := TNeon.ObjectToJSON(Company, Config);
ShowMessage(JSON);
finally
Company.Free;
end;
end;JSON Deserialization
pascal
var
Company: TCompany;
JSON: string;
Config: INeonConfiguration;
begin
JSON := '{"id":"COMP001","name":"Acme Corp","city":"Springfield"}';
Config := TNeonConfiguration.Default
.SetMemberCase(TNeonCase.CamelCase);
Company := TNeon.JSONToObject<TCompany>(JSON, Config);
try
Company.Store; // Save to database
finally
Company.Free;
end;
end;REST API Integration (WiRL)
pascal
uses
Instant.Neon.Serializers, WiRL.Core.Application;
procedure ConfigureWiRL(App: TWiRLApplication);
begin
// Register Neon with InstantObjects support
App.Plugin.Configure<IWiRLConfigurationNeon>
.SetUseUTCDate(True)
.SetMemberCase(TNeonCase.CamelCase)
.SetVisibility([mvPublic, mvPublished])
.BackToConfig
.ConfigureNeonProvider;
end;REST API Integration (MARS)
pascal
uses
Instant.Neon.Serializers, MARS.Core.Application;
procedure ConfigureMARS(App: TMARSApplication);
begin
// Neon automatically configured for InstantObjects
App.SetSerializerType<TNeonSerializer>;
end;Configuration Options
Case Conversion
pascal
Config.SetMemberCase(TNeonCase.CamelCase); // myProperty
Config.SetMemberCase(TNeonCase.PascalCase); // MyProperty
Config.SetMemberCase(TNeonCase.LowerCase); // myproperty
Config.SetMemberCase(TNeonCase.UpperCase); // MYPROPERTY
Config.SetMemberCase(TNeonCase.SnakeCase); // my_propertyDate/Time Handling
pascal
Config.SetUseUTCDate(True); // Convert to UTC
Config.SetUseUTCDate(False); // Use local timeVisibility Control
pascal
Config.SetVisibility([mvPublic, mvPublished]); // Public and published
Config.SetVisibility([mvPublished]); // Published onlyAttribute Type Handling
Simple Attributes
pascal
// String, Integer, Float, Boolean, Currency
property Name: string; // → "name": "John"
property Age: Integer; // → "age": 30
property Active: Boolean; // → "active": trueDateTime Attributes
pascal
property BirthDate: TDateTime; // → "birthDate": "1990-01-15T00:00:00Z"Reference Attributes
pascal
property Company: TCompany; // → "company": {"id": "COMP001"}
// Or fully expanded if loadedPart Attributes
pascal
property Address: TAddress; // → "address": {"street": "...", "city": "..."}
// Always fully serializedParts Collection
pascal
property Contacts: TInstantParts; // → "contacts": [{"name": "..."}, {"name": "..."}]References Collection
pascal
property Orders: TInstantReferences; // → "orders": [{"id": "ORD001"}, {"id": "ORD002"}]Best Practices
1. Use Camel Case for REST APIs
pascal
Config.SetMemberCase(TNeonCase.CamelCase);
// JavaScript/JSON convention2. Control Lazy Loading
pascal
// Load references before serialization
Company.Orders.Load;
// Or serialize IDs only (default behavior)3. Handle Cycles
pascal
// Neon automatically detects cycles
// References serialize as IDs to prevent infinite loops4. Use UTC for Dates
pascal
Config.SetUseUTCDate(True);
// Consistent timezone handlingComplete Example
See JSON Serialization with Neon for complete examples including:
- Basic serialization
- Custom DTOs
- REST API integration
- Collection handling
See Also
- JSON Serialization with Neon - Complete tutorial
- WiRL REST Server - WiRL integration
- MARS REST Server - MARS integration
- InstantPersistence - Core classes
Source Code
File: Instant.Neon.Serializers.pasLocation: Source/Core/
Summary
Instant.Neon.Serializers provides seamless JSON serialization for InstantObjects using the Neon library.
Key Features:
- Automatic JSON conversion
- All attribute types supported
- Cycle detection
- REST framework integration (WiRL, MARS)
Best for:
- REST APIs
- JSON data exchange
- Modern web applications
