type
// C:\Lazarus\Lazarus.1.8.2-FPC.3.0.4-(32)\fpc\3.0.4\source\packages\fcl-json\src\fpjsonrtti.pp
TJSONFiler = Class(TComponent)
Protected
Procedure Error(Const Msg : String);
Procedure Error(Const FMT : String; Args : Array of const);
end;
TJSONStreamer = Class(TJSONFiler)
private
FAfterStreamObject: TJSONStreamEvent;
FBeforeStreamObject: TJSONStreamEvent;
FChildProperty: String;
FDateTimeFormat: String;
FOnStreamProperty: TJSONPropertyEvent;
FOptions: TJSONStreamOptions;
function GetChildProperty: String;
function IsChildStored: boolean;
function StreamChildren(AComp: TComponent): TJSONArray;
protected
function StreamClassProperty(Const AObject: TObject): TJSONData; virtual;
Function StreamProperty(Const AObject : TObject; Const PropertyName : String) : TJSONData;
Function StreamProperty(Const AObject : TObject; PropertyInfo : PPropInfo) : TJSONData;
Function FormatDateProp(const DateTime : TDateTime) : TJSONString;
Public
Constructor Create(AOwner : TComponent); override;
Destructor Destroy;override;
//
// Basic functions
//
// Use RTTI to stream object.
// If AObject is of type TStrings or TCollection, special treatment occurs:
// TStrings results in { Strings: [S,S,S] } or { Strings: { "S1" : O1, "S2" : O2 }} depending on Options.
// Collection results in { Items: [I,I,I] }
Function ObjectToJSON(Const AObject : TObject) : TJSONObject;
// Stream a collection - always returns an array
function StreamCollection(Const ACollection: TCollection): TJSONArray;
// Stream an objectlist - always returns an array
function StreamObjectList(Const AnObjectList: TObjectList): TJSONArray;
// Stream a List - always returns an array
function StreamTList(Const AList: TList): TJSONArray;
// Stream a TStrings instance as an array
function StreamTStringsArray(Const AStrings: TStrings): TJSONArray;
// Stream a TStrings instance as an object
function StreamTStringsObject(Const AStrings: TStrings): TJSONObject;
// Stream a TStrings instance. Takes into account Options.
function StreamTStrings(Const AStrings: TStrings): TJSONData;
// Stream a variant as JSON.
function StreamVariant(const Data: Variant): TJSONData; virtual;
//
// Some utility functions.
//
// Call ObjectToJSON and convert result to JSON String.
Function ObjectToJSONString(AObject : TObject) : TJSONStringType;
// Convert TSTrings to JSON string with array or Object.
Function StringsToJSON(Const Strings : TStrings; AsObject : Boolean = False) : TJSONStringType;
// Convert collection to JSON string
Function CollectionToJSON(Const ACollection : TCollection) : TJSONStringType;
// Convert variant to JSON String
Function VariantToJSON(Const Data : Variant) : TJSONStringType;
Published
// Format used when formatting DateTime values. Only used in conjunction with jsoDateTimeToString
Property DateTimeFormat : String Read FDateTimeFormat Write FDateTimeFormat;
// Options to use when streaming
Property Options : TJSONStreamOptions Read FOptions Write FOptions;
// Called before streaming an object with ObjectToJSON
Property BeforeStreamObject : TJSONStreamEvent Read FBeforeStreamObject Write FBeforeStreamObject;
// Called After streaming an object with ObjectToJSON
Property AfterStreamObject : TJSONStreamEvent Read FAfterStreamObject Write FAfterStreamObject;
// Called whenever a property was streamed. If Res is nil on return, no property is added.
Property OnStreamProperty : TJSONPropertyEvent Read FOnStreamProperty Write FOnStreamProperty;
// Property name to use when streaming child components. Default is "Children"
Property ChildProperty : String Read GetChildProperty Write FChildProperty Stored IsChildStored;
end;
TJSONDeStreamer = Class(TJSONFiler)
private
FAfterReadObject: TJSONStreamEvent;
FBeforeReadObject: TJSONStreamEvent;
FDateTimeFormat: String;
FOnGetObject: TJSONGetObjectEvent;
FOnPropError: TJSONpropertyErrorEvent;
FOnRestoreProp: TJSONRestorePropertyEvent;
FCaseInsensitive : Boolean;
FOptions: TJSONDestreamOptions;
procedure DeStreamClassProperty(AObject: TObject; PropInfo: PPropInfo; PropData: TJSONData);
function GetCaseInsensitive: Boolean;
procedure SetCaseInsensitive(AValue: Boolean);
protected
// Try to parse a date.
Function ExtractDateTime(S : String): TDateTime;
function GetObject(AInstance : TObject; const APropName: TJSONStringType; D: TJSONObject; PropInfo: PPropInfo): TObject;
procedure DoRestoreProperty(AObject: TObject; PropInfo: PPropInfo; PropData: TJSONData); virtual;
Function ObjectFromString(Const JSON : TJSONStringType) : TJSONData; virtual;
procedure RestoreProperty(AObject: TObject; PropInfo: PPropInfo; PropData: TJSONData);
Public
Constructor Create(AOwner : TComponent); override;
Destructor Destroy; override;
// Convert JSON object to properties of AObject
Procedure JSONToObject(Const JSON : TJSONStringType; AObject : TObject);
Procedure JSONToObject(Const JSON : TJSONObject; AObject : TObject);
// Convert JSON object/array to collection.
Procedure JSONToCollection(Const JSON : TJSONStringType; ACollection : TCollection);
Procedure JSONToCollection(Const JSON : TJSONData; ACollection : TCollection);
// Convert JSON array/object/string to TStrings
Procedure JSONToStrings(Const JSON : TJSONStringType; AStrings : TSTrings);
Procedure JSONToStrings(Const JSON : TJSONData; AStrings : TSTrings);
// Convert JSON data to a variant. Supports simple data types and arrays.
Function JSONToVariant(Data: TJSONData): Variant;
Function JSONToVariant(Data: TJSONStringType): Variant;
// Triggered at the start of each call to JSONToObject
Property BeforeReadObject : TJSONStreamEvent Read FBeforeReadObject Write FBeforeReadObject;
// Triggered at the end of each call to JSONToObject (not if exception happens)
Property AfterReadObject : TJSONStreamEvent Read FAfterReadObject Write FAfterReadObject;
// Called when a property will be restored. If 'Handled' is True on return, property is considered restored.
Property OnRestoreProperty : TJSONRestorePropertyEvent Read FOnRestoreProp Write FOnRestoreProp;
// Called when an error occurs when restoring a property. If Continue is False on return, exception is re-raised.
Property OnPropertyError : TJSONpropertyErrorEvent Read FOnPropError Write FOnPropError;
// Called when a object-typed property must be restored, and the property is Nil. Must return an instance for the property.
// Published Properties of the instance will be further restored with available data.
Property OngetObject : TJSONGetObjectEvent Read FOnGetObject Write FOnGetObject;
// JSON is by definition case sensitive. Should properties be looked up case-insentive ?
Property CaseInsensitive : Boolean Read GetCaseInsensitive Write SetCaseInsensitive ; deprecated;
// DateTime format. If not set, RFC3339DateTimeFormat is assumed.
// If set, it will be used as an argument to ScanDateTime. If that fails, StrToDateTime is used.
Property DateTimeFormat : String Read FDateTimeFormat Write FDateTimeFormat;
// Options overning the behaviour
Property Options : TJSONDestreamOptions Read FOptions Write FOptions;
end;