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 bool _canThrow = true; 27 28 private bool _canCircularDetect = true; // Circular Reference Detect 29 30 private int _depth = -1; 31 32 /* --------------------------------------------------- properties --------------------------------------------------- */ 33 34 bool onlyPublic() { return _onlyPublic; } 35 36 SerializationOptions onlyPublic(bool flag) { 37 SerializationOptions r = this; 38 r._onlyPublic = flag; 39 return r; 40 } 41 42 bool traverseBase() { return _traverseBase; } 43 44 SerializationOptions traverseBase(bool flag) { 45 SerializationOptions r = this; 46 r._traverseBase = flag; 47 return r; 48 } 49 50 bool includeMeta() { return _includeMeta; } 51 52 SerializationOptions includeMeta(bool flag) { 53 SerializationOptions r = this; 54 r._includeMeta = flag; 55 return r; 56 } 57 58 bool ignoreNull() { return _ignoreNull; } 59 60 SerializationOptions ignoreNull(bool flag) { 61 SerializationOptions r = this; 62 r._ignoreNull = flag; 63 return r; 64 } 65 66 bool canThrow() { return _canThrow; } 67 68 SerializationOptions canThrow(bool flag) { 69 SerializationOptions r = this; 70 r._canThrow = flag; 71 return r; 72 } 73 74 bool canCircularDetect() { return _canCircularDetect; } 75 76 SerializationOptions canCircularDetect(bool flag) { 77 SerializationOptions r = this; 78 r._canCircularDetect = flag; 79 return r; 80 } 81 82 int depth() { return _depth; } 83 84 SerializationOptions depth(int depth) { 85 SerializationOptions r = this; 86 r._depth = depth; 87 return r; 88 } 89 } 90 91 /** 92 Flag indicating whether to traverse the base class. 93 */ 94 alias TraverseBase = Flag!"traverseBase"; 95 96 /** 97 Flag indicating whether to allow the public member only. 98 */ 99 alias OnlyPublic = Flag!"onlyPublic"; 100 101 /** 102 Flag indicating whether to include the meta data (especially for a class or an interface). 103 */ 104 alias IncludeMeta = Flag!"includeMeta"; 105 106 /** 107 Flag indicating whether to ignore the null member. 108 */ 109 alias IgnoreNull = Flag!"ignoreNull"; 110 111 112 /// attributes for json 113 /// https://dzone.com/articles/jackson-annotations-for-json-part-2-serialization 114 115 /** 116 * Excludes the field from both encoding and decoding. 117 */ 118 enum Ignore; 119 120 deprecated("Using Ignore instead.") 121 alias Exclude = Ignore; 122 123 /** 124 * Includes this even if it would otherwise be excluded. 125 * If Exclude (or other UDA(@)) and Include are present value will be included. 126 * Can also be used on @property methods to include them. (Be sure both the setter and getter exist!) 127 * If used on a value of a base class value will be included. 128 */ 129 enum Include; 130 131 /** 132 * Excludes the field from decoding, encode only. 133 */ 134 enum EncodeOnly; 135 136 /** 137 * Excludes the field from encoding, decode only. 138 */ 139 enum DecodeOnly;