Neon.Core.Attributes.pas 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. {******************************************************************************}
  2. { }
  3. { Neon: Serialization Library for Delphi }
  4. { Copyright (c) 2018-2019 Paolo Rossi }
  5. { https://github.com/paolo-rossi/neon-library }
  6. { }
  7. {******************************************************************************}
  8. { }
  9. { Licensed under the Apache License, Version 2.0 (the "License"); }
  10. { you may not use this file except in compliance with the License. }
  11. { You may obtain a copy of the License at }
  12. { }
  13. { http://www.apache.org/licenses/LICENSE-2.0 }
  14. { }
  15. { Unless required by applicable law or agreed to in writing, software }
  16. { distributed under the License is distributed on an "AS IS" BASIS, }
  17. { WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. }
  18. { See the License for the specific language governing permissions and }
  19. { limitations under the License. }
  20. { }
  21. {******************************************************************************}
  22. unit Neon.Core.Attributes;
  23. interface
  24. {$SCOPEDENUMS ON}
  25. uses
  26. System.Classes, System.SysUtils, System.Rtti,
  27. Neon.Core.Types;
  28. type
  29. NeonAttribute = class(TCustomAttribute)
  30. end;
  31. NeonNamedAttribute = class(NeonAttribute)
  32. private
  33. FValue: string;
  34. public
  35. constructor Create(const AValue: string);
  36. property Value: string read FValue write FValue;
  37. end;
  38. /// <summary>
  39. /// The attribute [NeonProperty] is used to indicate the property name in JSON.
  40. /// </summary>
  41. /// <remarks>
  42. /// Read + Write Attribute
  43. /// </remarks>
  44. NeonPropertyAttribute = class(NeonNamedAttribute);
  45. /// <summary>
  46. /// The attribute [NeonEnum] is used to indicate the names of an enum
  47. /// </summary>
  48. /// <remarks>
  49. /// Read + Write Attribute
  50. /// </remarks>
  51. NeonEnumNamesAttribute = class(TCustomAttribute)
  52. private
  53. FNames: TArray<string>;
  54. public
  55. constructor Create(const ANames: string);
  56. property Names: TArray<string> read FNames write FNames;
  57. end;
  58. /// <summary>
  59. /// The Neon attribute [NeonIgnore] is used to tell Neon to ignore a certain property (field)
  60. /// of a Delphi object. The property is ignored both when reading JSON into Delphi objects, and
  61. /// when writing Delphi objects into JSON.
  62. /// </summary>
  63. /// <remarks>
  64. /// Read + Write Attribute
  65. /// </remarks>
  66. NeonIgnoreAttribute = class(NeonAttribute);
  67. /// <summary>
  68. /// The Neon annotation NeonInclude tells Neon to include the property (or field)
  69. /// based on the value
  70. /// </summary>
  71. /// <remarks>
  72. /// Write Attribute
  73. /// </remarks>
  74. IncludeIf = (
  75. /// <summary>
  76. /// Include the member if it's not nil
  77. /// </summary>
  78. NotNull,
  79. /// <summary>
  80. /// Include the member if the value it's not empty
  81. /// </summary>
  82. NotEmpty,
  83. /// <summary>
  84. /// Include the member if it's value it's not the default value
  85. /// </summary>
  86. NotDefault,
  87. /// <summary>
  88. /// Include the member always
  89. /// </summary>
  90. Always,
  91. /// <summary>
  92. /// Include the member based on the result of the function specified as string
  93. /// (default function is ShouldInclude)
  94. /// </summary>
  95. CustomFunction);
  96. TIncludeValue = record
  97. Present: Boolean;
  98. Value: IncludeIf;
  99. IncludeFunction: string;
  100. end;
  101. /// <summary>
  102. /// The Neon annotation NeonInclude tells Neon to include the property (or field)
  103. /// based on the value of the enumeration Include
  104. /// </summary>
  105. NeonIncludeAttribute = class(TCustomAttribute)
  106. private
  107. FIncludeValue: TIncludeValue;
  108. public
  109. constructor Create(AIncludeValue: IncludeIf = IncludeIf.Always; const AIncludeFunction: string = 'ShouldInclude');
  110. property IncludeValue: TIncludeValue read FIncludeValue write FIncludeValue;
  111. end;
  112. /// <summary>
  113. /// The NeonIgnoreProperties Neon annotation is used to specify a list of properties
  114. /// of a class to ignore. The NeonIgnoreProperties annotation is placed above the
  115. /// class declaration instead of above the individual properties (fields) to ignore.
  116. /// </summary>
  117. /// <remarks>
  118. /// Read + Write Attribute
  119. /// </remarks>
  120. NeonIgnorePropertiesAttribute = class(NeonAttribute);
  121. /// <summary>
  122. /// The NeonIgnoreType Neon annotation is used to mark a whole type (class) to be
  123. /// ignored everywhere that type is used.
  124. /// </summary>
  125. /// <remarks>
  126. /// Read + Write Attribute
  127. /// </remarks>
  128. NeonIgnoreTypeAttribute = class(NeonAttribute);
  129. /// <summary>
  130. /// The Neon attribute NeonMembers is used to tell Neon to change the Members
  131. /// when reading/writing a specific record/object
  132. /// </summary>
  133. /// <remarks>
  134. /// Read + Write Attribute
  135. /// </remarks>
  136. NeonMembersSetAttribute = class(NeonAttribute)
  137. private
  138. FValue: TNeonMembersSet;
  139. public
  140. constructor Create(const AValue: TNeonMembersSet);
  141. property Value: TNeonMembersSet read FValue write FValue;
  142. end;
  143. /// <summary>
  144. /// The Neon attribute NeonVisibilityAttribute is used to tell Neon to change the Visibility
  145. /// when reading/writing a specific record/object
  146. /// </summary>
  147. /// <remarks>
  148. /// Read + Write Attribute
  149. /// </remarks>
  150. NeonVisibilityAttribute = class(NeonAttribute)
  151. private
  152. FValue: TNeonVisibility;
  153. public
  154. constructor Create(const AValue: TNeonVisibility);
  155. property Value: TNeonVisibility read FValue write FValue;
  156. end;
  157. /// <summary>
  158. /// The NeonSerialize Neon annotation is used to specify a custom serializer for a
  159. /// field in a Delphi object.
  160. /// </summary>
  161. NeonSerializeAttribute = class(NeonAttribute)
  162. private
  163. FClazz: TClass;
  164. FName: string;
  165. public
  166. constructor Create(const AClass: TClass); overload;
  167. constructor Create(const AName: string); overload;
  168. property Clazz: TClass read FClazz write FClazz;
  169. property Name: string read FName write FName;
  170. end;
  171. /// <summary>
  172. /// The Neon annotation NeonDeserialize is used to specify a custom de-serializer
  173. /// class for a given field in a Delphi object.
  174. /// </summary>
  175. NeonDeserializeAttribute = class(NeonSerializeAttribute);
  176. /// <summary>
  177. /// The Neon annotation NeonValue tells Neon that Neon should not attempt to
  178. /// serialize the object itself, but rather call a method on the object which
  179. /// serializes the object to a TJSONValue.
  180. /// </summary>
  181. NeonValueAttribute = class(NeonAttribute);
  182. NeonMethodAttribute = class(NeonAttribute);
  183. NeonSerializerMethodAttribute = class(NeonAttribute);
  184. /// <summary>
  185. /// The NeonRawValue annotation tells Neon that this property value should written
  186. /// directly as it is to the JSON output. If the property is a String Neon would
  187. /// normally have enclosed the value in quotation marks, but if annotated with the
  188. /// NeonRawValue property Neon won't do that.
  189. /// </summary>
  190. NeonRawValueAttribute = class(NeonAttribute);
  191. {
  192. //Read Annotations
  193. NeonSetterAttribute = class(NeonAttribute);
  194. NeonAnySetterAttribute = class(NeonAttribute);
  195. NeonCreatorAttribute = class(NeonAttribute);
  196. NeonInjectAttribute = class(NeonAttribute);
  197. //Write Annotations
  198. NeonGetterAttribute = class(NeonAttribute);
  199. NeonAnyGetterAttribute = class(NeonAttribute);
  200. }
  201. implementation
  202. uses
  203. System.StrUtils, System.DateUtils;
  204. { NeonNamedAttribute }
  205. constructor NeonNamedAttribute.Create(const AValue: string);
  206. begin
  207. FValue := AValue;
  208. end;
  209. { NeonMembersTypeAttribute }
  210. constructor NeonMembersSetAttribute.Create(const AValue: TNeonMembersSet);
  211. begin
  212. FValue := AValue;
  213. end;
  214. { NeonVisibilityAttribute }
  215. constructor NeonVisibilityAttribute.Create(const AValue: TNeonVisibility);
  216. begin
  217. FValue := AValue;
  218. end;
  219. constructor NeonSerializeAttribute.Create(const AClass: TClass);
  220. begin
  221. FClazz := AClass;
  222. end;
  223. constructor NeonSerializeAttribute.Create(const AName: string);
  224. begin
  225. FName := AName;
  226. end;
  227. { NeonIncludeAttribute }
  228. constructor NeonIncludeAttribute.Create(AIncludeValue: IncludeIf; const AIncludeFunction: string);
  229. begin
  230. FIncludeValue.Present := True;
  231. FIncludeValue.Value := AIncludeValue;
  232. FIncludeValue.IncludeFunction := AIncludeFunction;
  233. end;
  234. { NeonEnumNamesAttribute }
  235. constructor NeonEnumNamesAttribute.Create(const ANames: string);
  236. begin
  237. FNames := ANames.Split([',']);
  238. end;
  239. end.