1 /**
2 License:
3 Boost Software License - Version 1.0 - August 17th, 2003
4 5 Permission is hereby granted, free of charge, to any person or organization
6 obtaining a copy of the software and accompanying documentation covered by
7 this license (the "Software") to use, reproduce, display, distribute,
8 execute, and transmit the Software, and to prepare derivative works of the
9 Software, and to permit third-parties to whom the Software is furnished to
10 do so, all subject to the following:
11 12 The copyright notices in the Software and this entire statement, including
13 the above license grant, this restriction and the following disclaimer,
14 must be included in all copies of the Software, in whole or in part, and
15 all derivative works of the Software, unless such copies or derivative
16 works are solely in the form of machine-executable object code generated by
17 a source language processor.
18 19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
22 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
23 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
24 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 DEALINGS IN THE SOFTWARE.
26 27 Authors:
28 aermicioi
29 **/30 moduleaermicioi.aedi_property_reader.json.convertor;
31 32 importaermicioi.aedi_property_reader.core.convertor;
33 importaermicioi.aedi_property_reader.core.accessor;
34 importaermicioi.aedi_property_reader.core.setter;
35 importaermicioi.aedi_property_reader.core.inspector;
36 importaermicioi.aedi_property_reader.json.accessor;
37 importaermicioi.aedi_property_reader.json.inspector;
38 importaermicioi.aedi.factory;
39 importaermicioi.aedi.storage.locator;
40 importaermicioi.aedi.storage.wrapper;
41 importaermicioi.aedi.storage.decorator;
42 importaermicioi.aedi.exception;
43 importstd.json;
44 importstd.traits;
45 importstd.conv : to, text;
46 importstd.experimental.allocator;
47 importstd.exception : enforce;
48 49 private {
50 enumJAccessor(From : JSONValue) = () => newRuntimeFieldAccessor!JSONValue(dsl(newJsonPropertyAccessor, newJsonIndexAccessor));
51 enumJInspector(From : JSONValue) = () => newJsonInspector;
52 enumCSetter(To) = () => newCompositeSetter!To();
53 enumCInspector(To) = () => newCompositeInspector!To();
54 }
55 56 aliasJsonConvertor = ChainedAdvisedConvertor!(
57 AdvisedConvertor!(convert, destruct),
58 AdvisedConvertor!(
59 JAccessor,
60 CSetter,
61 CInspector,
62 JInspector63 )
64 ).AdvisedConvertorImplementation;
65 66 /**
67 Convert JSONValue into T scalar/array/assocarray value.
68 69 Params:
70 value = storage where to put converted JSONValue
71 json = the data that is to be converted.
72 Throws:
73 InvalidCastException when the type of value does not match stored data.
74 Returns:
75 value
76 **/77 voidconvert(To, From : JSONValue)(inFromjson, refTovalue, RCIAllocatorallocator = theAllocator)
78 if (isFloatingPoint!To && !is(To == enum)) {
79 80 if (json.type != JSON_TYPE.FLOAT) {
81 thrownewInvalidCastException("Could not convert json " ~ json.toString() ~ " value to type " ~ fullyQualifiedName!To);
82 }
83 84 value = json.floating.to!To;
85 }
86 87 /**
88 ditto
89 **/90 voidconvert(To, From : JSONValue)(inFromjson, refTovalue, RCIAllocatorallocator = theAllocator)
91 if (is(To : bool) && !is(To == enum)) {
92 93 if (json.type == JSON_TYPE.TRUE) {
94 value = true;
95 return;
96 }
97 98 if (json.type == JSON_TYPE.FALSE) {
99 value = false;
100 return;
101 }
102 103 thrownewInvalidCastException("Could not convert json " ~ json.toString() ~ " value to type " ~ fullyQualifiedName!To);
104 }
105 106 /**
107 ditto
108 **/109 voidconvert(To, From : JSONValue)(inFromjson, refTovalue, RCIAllocatorallocator = theAllocator)
110 if (isUnsigned!To && isIntegral!To && !is(To == enum)) {
111 112 if ((json.type == JSON_TYPE.INTEGER) && (json.integer >= 0)) {
113 value = json.integer.to!To;
114 return;
115 }
116 117 if (json.type != JSON_TYPE.UINTEGER) {
118 thrownewInvalidCastException("Could not convert json " ~ json.toString() ~ " value to type " ~ fullyQualifiedName!To);
119 }
120 121 value = json.uinteger.to!To;
122 }
123 124 /**
125 ditto
126 **/127 voidconvert(To, From : JSONValue)(inFromjson, refTovalue, RCIAllocatorallocator = theAllocator)
128 if (!isUnsigned!To && isIntegral!To && !is(To == enum)) {
129 enforce!InvalidCastException(json.type == JSON_TYPE.INTEGER,
130 text("Could not convert json " ~ json.toString() ~ " value to type " ~ fullyQualifiedName!To));
131 132 value = json.integer.to!To;
133 }
134 135 /**
136 ditto
137 **/138 voidconvert(To, From : JSONValue)(inFromjson, refTovalue, RCIAllocatorallocator = theAllocator)
139 if (isSomeChar!To && !is(To == enum)) {
140 enforce!InvalidCastException(json.type == JSON_TYPE.STRING, text("Could not convert json ", json, " value to type ", fullyQualifiedName!To));
141 142 value = json.str.to!To;
143 }
144 145 /**
146 ditto
147 **/148 voidconvert(To, From : JSONValue)(inFromjson, refTovalue, RCIAllocatorallocator = theAllocator)
149 if (isSomeString!To && !is(To == enum)) {
150 151 if (json.type != JSON_TYPE.STRING) {
152 thrownewInvalidCastException("Could not convert json " ~ json.toString() ~ " value to type " ~ fullyQualifiedName!To);
153 }
154 155 value = json.str.to!To;
156 }
157 158 /**
159 ditto
160 **/161 voidconvert(To : Z[], From : JSONValue, Z)(inFromjson, refTovalue, RCIAllocatorallocator = theAllocator)
162 if (!isSomeString!To && !is(To == enum)) {
163 164 if (json.type != JSON_TYPE.ARRAY) {
165 thrownewInvalidCastException("Could not convert json " ~ json.toString() ~ " value to type " ~ fullyQualifiedName!To);
166 }
167 168 value = allocator.makeArray!Z(json.array.length);
169 170 foreach (index, refel; value) {
171 convert!Z(json.array[index], el, allocator);
172 }
173 }
174 175 /**
176 ditto
177 **/178 voidconvert(To : Z[string], From : JSONValue, Z)(inFromjson, refTovalue, RCIAllocatorallocator = theAllocator) if (!is(To == enum)) {
179 180 if (json.type != JSON_TYPE.OBJECT) {
181 thrownewInvalidCastException("Could not convert json " ~ json.toString() ~ " value to type " ~ fullyQualifiedName!To);
182 }
183 184 autojsonAssociativeArray = json.object;
185 186 foreach (key, refel; jsonAssociativeArray) {
187 188 Ztemp;
189 convert!Z(el, temp, allocator);
190 value[key] = temp;
191 }
192 }
193 194 /**
195 ditto
196 **/197 voidconvert(To, From : JSONValue)(inFromjson, refTovalue, RCIAllocatorallocator = theAllocator) if (is(To == enum)) {
198 199 stringtemp;
200 json.convert!string(temp, allocator);
201 value = temp.to!To;
202 temp.destruct(allocator);
203 }
204 205 voiddestruct(To)(refToto, RCIAllocatorallocator = theAllocator) {
206 destroy(to);
207 to = to.init;
208 }
209 210 voiddestruct(To : Z[], Z)(refToto, RCIAllocatorallocator = theAllocator)
211 if (!isSomeString!To) {
212 allocator.dispose(to);
213 to = To.init;
214 }