| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274 |
- {******************************************************************************}
- { }
- { 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;
- /// <summary>
- /// The attribute [NeonProperty] is used to indicate the property name in JSON.
- /// </summary>
- /// <remarks>
- /// Read + Write Attribute
- /// </remarks>
- NeonPropertyAttribute = class(NeonNamedAttribute);
- /// <summary>
- /// The attribute [NeonEnum] is used to indicate the names of an enum
- /// </summary>
- /// <remarks>
- /// Read + Write Attribute
- /// </remarks>
- NeonEnumNamesAttribute = class(TCustomAttribute)
- private
- FNames: TArray<string>;
- public
- constructor Create(const ANames: string);
- property Names: TArray<string> read FNames write FNames;
- end;
- /// <summary>
- /// 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.
- /// </summary>
- /// <remarks>
- /// Read + Write Attribute
- /// </remarks>
- NeonIgnoreAttribute = class(NeonAttribute);
- /// <summary>
- /// The Neon annotation NeonInclude tells Neon to include the property (or field)
- /// based on the value
- /// </summary>
- /// <remarks>
- /// Write Attribute
- /// </remarks>
- IncludeIf = (
- /// <summary>
- /// Include the member if it's not nil
- /// </summary>
- NotNull,
- /// <summary>
- /// Include the member if the value it's not empty
- /// </summary>
- NotEmpty,
- /// <summary>
- /// Include the member if it's value it's not the default value
- /// </summary>
- NotDefault,
- /// <summary>
- /// Include the member always
- /// </summary>
- Always,
- /// <summary>
- /// Include the member based on the result of the function specified as string
- /// (default function is ShouldInclude)
- /// </summary>
- CustomFunction);
- TIncludeValue = record
- Present: Boolean;
- Value: IncludeIf;
- IncludeFunction: string;
- end;
- /// <summary>
- /// The Neon annotation NeonInclude tells Neon to include the property (or field)
- /// based on the value of the enumeration Include
- /// </summary>
- 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;
- /// <summary>
- /// 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.
- /// </summary>
- /// <remarks>
- /// Read + Write Attribute
- /// </remarks>
- NeonIgnorePropertiesAttribute = class(NeonAttribute);
- /// <summary>
- /// The NeonIgnoreType Neon annotation is used to mark a whole type (class) to be
- /// ignored everywhere that type is used.
- /// </summary>
- /// <remarks>
- /// Read + Write Attribute
- /// </remarks>
- NeonIgnoreTypeAttribute = class(NeonAttribute);
- /// <summary>
- /// The Neon attribute NeonMembers is used to tell Neon to change the Members
- /// when reading/writing a specific record/object
- /// </summary>
- /// <remarks>
- /// Read + Write Attribute
- /// </remarks>
- NeonMembersSetAttribute = class(NeonAttribute)
- private
- FValue: TNeonMembersSet;
- public
- constructor Create(const AValue: TNeonMembersSet);
- property Value: TNeonMembersSet read FValue write FValue;
- end;
- /// <summary>
- /// The Neon attribute NeonVisibilityAttribute is used to tell Neon to change the Visibility
- /// when reading/writing a specific record/object
- /// </summary>
- /// <remarks>
- /// Read + Write Attribute
- /// </remarks>
- NeonVisibilityAttribute = class(NeonAttribute)
- private
- FValue: TNeonVisibility;
- public
- constructor Create(const AValue: TNeonVisibility);
- property Value: TNeonVisibility read FValue write FValue;
- end;
- /// <summary>
- /// The NeonSerialize Neon annotation is used to specify a custom serializer for a
- /// field in a Delphi object.
- /// </summary>
- 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;
- /// <summary>
- /// The Neon annotation NeonDeserialize is used to specify a custom de-serializer
- /// class for a given field in a Delphi object.
- /// </summary>
- NeonDeserializeAttribute = class(NeonSerializeAttribute);
- /// <summary>
- /// 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.
- /// </summary>
- NeonValueAttribute = class(NeonAttribute);
- NeonMethodAttribute = class(NeonAttribute);
- NeonSerializerMethodAttribute = class(NeonAttribute);
- /// <summary>
- /// 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.
- /// </summary>
- 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.
|