1 module hunt.serialization.Common; 2 3 import std.typecons : Flag; 4 5 6 /** 7 * 8 */ 9 struct SerializationOptions { 10 enum Default = SerializationOptions(); 11 enum Full = SerializationOptions().includeMeta(true); 12 enum Lite = SerializationOptions().traverseBase(false).ignoreNull(true).depth(0); 13 enum Normal = SerializationOptions().ignoreNull(true); 14 15 enum OnlyPublicWithNull = SerializationOptions().onlyPublic(true).traverseBase(false).depth(0); 16 enum OnlyPublicLite = OnlyPublicWithNull.ignoreNull(true); 17 18 private bool _onlyPublic = false; 19 20 private bool _traverseBase = true; 21 22 private bool _includeMeta = false; 23 24 private bool _ignoreNull = false; 25 26 private int _depth = -1; 27 28 /* --------------------------------------------------- properties --------------------------------------------------- */ 29 30 bool onlyPublic() { return _onlyPublic; } 31 32 SerializationOptions onlyPublic(bool flag) { 33 SerializationOptions r = this; 34 r._onlyPublic = flag; 35 return r; 36 } 37 38 bool traverseBase() { return _traverseBase; } 39 40 SerializationOptions traverseBase(bool flag) { 41 SerializationOptions r = this; 42 r._traverseBase = flag; 43 return r; 44 } 45 46 bool includeMeta() { return _includeMeta; } 47 48 SerializationOptions includeMeta(bool flag) { 49 SerializationOptions r = this; 50 r._includeMeta = flag; 51 return r; 52 } 53 54 bool ignoreNull() { return _ignoreNull; } 55 56 SerializationOptions ignoreNull(bool flag) { 57 SerializationOptions r = this; 58 r._ignoreNull = flag; 59 return r; 60 } 61 62 int depth() { return _depth; } 63 64 SerializationOptions depth(int depth) { 65 SerializationOptions r = this; 66 r._depth = depth; 67 return r; 68 } 69 } 70 71 /** 72 Flag indicating whether to traverse the base class. 73 */ 74 alias TraverseBase = Flag!"traverseBase"; 75 76 /** 77 Flag indicating whether to allow the public member only. 78 */ 79 alias OnlyPublic = Flag!"onlyPublic"; 80 81 /** 82 Flag indicating whether to include the meta data (especially for a class or an interface). 83 */ 84 alias IncludeMeta = Flag!"includeMeta"; 85 86 /** 87 Flag indicating whether to ignore the null member. 88 */ 89 alias IgnoreNull = Flag!"ignoreNull"; 90 91 92 /// attributes for json 93 /// https://dzone.com/articles/jackson-annotations-for-json-part-2-serialization 94 95 /** 96 * Excludes the field from both encoding and decoding. 97 */ 98 enum Ignore; 99 100 deprecated("Using Ignore instead.") 101 alias Exclude = Ignore; 102 103 /** 104 * Includes this even if it would otherwise be excluded. 105 * If Exclude (or other UDA(@)) and Include are present value will be included. 106 * Can also be used on @property methods to include them. (Be sure both the setter and getter exist!) 107 * If used on a value of a base class value will be included. 108 */ 109 enum Include; 110 111 /** 112 * Excludes the field from decoding, encode only. 113 */ 114 enum EncodeOnly; 115 116 /** 117 * Excludes the field from encoding, decode only. 118 */ 119 enum DecodeOnly;