{******************************************************************************}
{ }
{ Neon: Serialization Library for Delphi }
{ Copyright (c) 2018-2019 Paolo Rossi }
{ https://github.com/paolo-rossi/neon-library }
{ }
{******************************************************************************}
{ }
{ Licensed under the Apache License, Version 2.0 (the "License"); }
{ you may not use this file except in compliance with the License. }
{ You may obtain a copy of the License at }
{ }
{ http://www.apache.org/licenses/LICENSE-2.0 }
{ }
{ Unless required by applicable law or agreed to in writing, software }
{ distributed under the License is distributed on an "AS IS" BASIS, }
{ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. }
{ See the License for the specific language governing permissions and }
{ limitations under the License. }
{ }
{******************************************************************************}
unit Neon.Core.Attributes;
interface
{$SCOPEDENUMS ON}
uses
System.Classes, System.SysUtils, System.Rtti,
Neon.Core.Types;
type
NeonAttribute = class(TCustomAttribute)
end;
NeonNamedAttribute = class(NeonAttribute)
private
FValue: string;
public
constructor Create(const AValue: string);
property Value: string read FValue write FValue;
end;
///
/// The attribute [NeonProperty] is used to indicate the property name in JSON.
///
///
/// Read + Write Attribute
///
NeonPropertyAttribute = class(NeonNamedAttribute);
///
/// The attribute [NeonEnum] is used to indicate the names of an enum
///
///
/// Read + Write Attribute
///
NeonEnumNamesAttribute = class(TCustomAttribute)
private
FNames: TArray;
public
constructor Create(const ANames: string);
property Names: TArray read FNames write FNames;
end;
///
/// The Neon attribute [NeonIgnore] is used to tell Neon to ignore a certain property (field)
/// of a Delphi object. The property is ignored both when reading JSON into Delphi objects, and
/// when writing Delphi objects into JSON.
///
///
/// Read + Write Attribute
///
NeonIgnoreAttribute = class(NeonAttribute);
///
/// The Neon annotation NeonInclude tells Neon to include the property (or field)
/// based on the value
///
///
/// Write Attribute
///
IncludeIf = (
///
/// Include the member if it's not nil
///
NotNull,
///
/// Include the member if the value it's not empty
///
NotEmpty,
///
/// Include the member if it's value it's not the default value
///
NotDefault,
///
/// Include the member always
///
Always,
///
/// Include the member based on the result of the function specified as string
/// (default function is ShouldInclude)
///
CustomFunction);
TIncludeValue = record
Present: Boolean;
Value: IncludeIf;
IncludeFunction: string;
end;
///
/// The Neon annotation NeonInclude tells Neon to include the property (or field)
/// based on the value of the enumeration Include
///
NeonIncludeAttribute = class(TCustomAttribute)
private
FIncludeValue: TIncludeValue;
public
constructor Create(AIncludeValue: IncludeIf = IncludeIf.Always; const AIncludeFunction: string = 'ShouldInclude');
property IncludeValue: TIncludeValue read FIncludeValue write FIncludeValue;
end;
///
/// The NeonIgnoreProperties Neon annotation is used to specify a list of properties
/// of a class to ignore. The NeonIgnoreProperties annotation is placed above the
/// class declaration instead of above the individual properties (fields) to ignore.
///
///
/// Read + Write Attribute
///
NeonIgnorePropertiesAttribute = class(NeonAttribute);
///
/// The NeonIgnoreType Neon annotation is used to mark a whole type (class) to be
/// ignored everywhere that type is used.
///
///
/// Read + Write Attribute
///
NeonIgnoreTypeAttribute = class(NeonAttribute);
///
/// The Neon attribute NeonMembers is used to tell Neon to change the Members
/// when reading/writing a specific record/object
///
///
/// Read + Write Attribute
///
NeonMembersSetAttribute = class(NeonAttribute)
private
FValue: TNeonMembersSet;
public
constructor Create(const AValue: TNeonMembersSet);
property Value: TNeonMembersSet read FValue write FValue;
end;
///
/// The Neon attribute NeonVisibilityAttribute is used to tell Neon to change the Visibility
/// when reading/writing a specific record/object
///
///
/// Read + Write Attribute
///
NeonVisibilityAttribute = class(NeonAttribute)
private
FValue: TNeonVisibility;
public
constructor Create(const AValue: TNeonVisibility);
property Value: TNeonVisibility read FValue write FValue;
end;
///
/// The NeonSerialize Neon annotation is used to specify a custom serializer for a
/// field in a Delphi object.
///
NeonSerializeAttribute = class(NeonAttribute)
private
FClazz: TClass;
FName: string;
public
constructor Create(const AClass: TClass); overload;
constructor Create(const AName: string); overload;
property Clazz: TClass read FClazz write FClazz;
property Name: string read FName write FName;
end;
///
/// The Neon annotation NeonDeserialize is used to specify a custom de-serializer
/// class for a given field in a Delphi object.
///
NeonDeserializeAttribute = class(NeonSerializeAttribute);
///
/// The Neon annotation NeonValue tells Neon that Neon should not attempt to
/// serialize the object itself, but rather call a method on the object which
/// serializes the object to a TJSONValue.
///
NeonValueAttribute = class(NeonAttribute);
NeonMethodAttribute = class(NeonAttribute);
NeonSerializerMethodAttribute = class(NeonAttribute);
///
/// The NeonRawValue annotation tells Neon that this property value should written
/// directly as it is to the JSON output. If the property is a String Neon would
/// normally have enclosed the value in quotation marks, but if annotated with the
/// NeonRawValue property Neon won't do that.
///
NeonRawValueAttribute = class(NeonAttribute);
{
//Read Annotations
NeonSetterAttribute = class(NeonAttribute);
NeonAnySetterAttribute = class(NeonAttribute);
NeonCreatorAttribute = class(NeonAttribute);
NeonInjectAttribute = class(NeonAttribute);
//Write Annotations
NeonGetterAttribute = class(NeonAttribute);
NeonAnyGetterAttribute = class(NeonAttribute);
}
implementation
uses
System.StrUtils, System.DateUtils;
{ NeonNamedAttribute }
constructor NeonNamedAttribute.Create(const AValue: string);
begin
FValue := AValue;
end;
{ NeonMembersTypeAttribute }
constructor NeonMembersSetAttribute.Create(const AValue: TNeonMembersSet);
begin
FValue := AValue;
end;
{ NeonVisibilityAttribute }
constructor NeonVisibilityAttribute.Create(const AValue: TNeonVisibility);
begin
FValue := AValue;
end;
constructor NeonSerializeAttribute.Create(const AClass: TClass);
begin
FClazz := AClass;
end;
constructor NeonSerializeAttribute.Create(const AName: string);
begin
FName := AName;
end;
{ NeonIncludeAttribute }
constructor NeonIncludeAttribute.Create(AIncludeValue: IncludeIf; const AIncludeFunction: string);
begin
FIncludeValue.Present := True;
FIncludeValue.Value := AIncludeValue;
FIncludeValue.IncludeFunction := AIncludeFunction;
end;
{ NeonEnumNamesAttribute }
constructor NeonEnumNamesAttribute.Create(const ANames: string);
begin
FNames := ANames.Split([',']);
end;
end.