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 module aermicioi.aedi_property_reader.json.convertor; 31 32 import aermicioi.aedi_property_reader.core.convertor; 33 import aermicioi.aedi_property_reader.core.accessor; 34 import aermicioi.aedi_property_reader.core.setter; 35 import aermicioi.aedi_property_reader.core.inspector; 36 import aermicioi.aedi_property_reader.json.accessor; 37 import aermicioi.aedi_property_reader.json.inspector; 38 import aermicioi.aedi.factory; 39 import aermicioi.aedi.storage.locator; 40 import aermicioi.aedi.storage.wrapper; 41 import aermicioi.aedi.storage.decorator; 42 import aermicioi.aedi.exception; 43 import std.json; 44 import std.traits; 45 import std.conv : to, text; 46 import std.experimental.allocator; 47 import std.exception : enforce; 48 49 private { 50 enum JAccessor(From : JSONValue) = () => new RuntimeFieldAccessor!JSONValue(dsl(new JsonPropertyAccessor, new JsonIndexAccessor)); 51 enum JInspector(From : JSONValue) = () => new JsonInspector; 52 enum CSetter(To) = () => new CompositeSetter!To(); 53 enum CInspector(To) = () => new CompositeInspector!To(); 54 } 55 56 alias JsonConvertor = ChainedAdvisedConvertor!( 57 AdvisedConvertor!(convert, destruct), 58 AdvisedConvertor!( 59 JAccessor, 60 CSetter, 61 CInspector, 62 JInspector 63 ) 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 void convert(To, From : JSONValue)(in From json, ref To value, RCIAllocator allocator = theAllocator) 78 if (isFloatingPoint!To && !is(To == enum)) { 79 80 if (json.type != JSON_TYPE.FLOAT) { 81 throw new InvalidCastException("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 void convert(To, From : JSONValue)(in From json, ref To value, RCIAllocator allocator = 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 throw new InvalidCastException("Could not convert json " ~ json.toString() ~ " value to type " ~ fullyQualifiedName!To); 104 } 105 106 /** 107 ditto 108 **/ 109 void convert(To, From : JSONValue)(in From json, ref To value, RCIAllocator allocator = 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 throw new InvalidCastException("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 void convert(To, From : JSONValue)(in From json, ref To value, RCIAllocator allocator = 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 void convert(To, From : JSONValue)(in From json, ref To value, RCIAllocator allocator = 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 void convert(To, From : JSONValue)(in From json, ref To value, RCIAllocator allocator = theAllocator) 149 if (isSomeString!To && !is(To == enum)) { 150 151 if (json.type != JSON_TYPE.STRING) { 152 throw new InvalidCastException("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 void convert(To : Z[], From : JSONValue, Z)(in From json, ref To value, RCIAllocator allocator = theAllocator) 162 if (!isSomeString!To && !is(To == enum)) { 163 164 if (json.type != JSON_TYPE.ARRAY) { 165 throw new InvalidCastException("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, ref el; value) { 171 convert!Z(json.array[index], el, allocator); 172 } 173 } 174 175 /** 176 ditto 177 **/ 178 void convert(To : Z[string], From : JSONValue, Z)(in From json, ref To value, RCIAllocator allocator = theAllocator) if (!is(To == enum)) { 179 180 if (json.type != JSON_TYPE.OBJECT) { 181 throw new InvalidCastException("Could not convert json " ~ json.toString() ~ " value to type " ~ fullyQualifiedName!To); 182 } 183 184 auto jsonAssociativeArray = json.object; 185 186 foreach (key, ref el; jsonAssociativeArray) { 187 188 Z temp; 189 convert!Z(el, temp, allocator); 190 value[key] = temp; 191 } 192 } 193 194 /** 195 ditto 196 **/ 197 void convert(To, From : JSONValue)(in From json, ref To value, RCIAllocator allocator = theAllocator) if (is(To == enum)) { 198 199 string temp; 200 json.convert!string(temp, allocator); 201 value = temp.to!To; 202 temp.destruct(allocator); 203 } 204 205 void destruct(To)(ref To to, RCIAllocator allocator = theAllocator) { 206 destroy(to); 207 to = to.init; 208 } 209 210 void destruct(To : Z[], Z)(ref To to, RCIAllocator allocator = theAllocator) 211 if (!isSomeString!To) { 212 allocator.dispose(to); 213 to = To.init; 214 }