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.configurer.annotation.annotation;
33 import aermicioi.aedi_property_reader.convertor.convertor : Convertor, ConvertsFromToMixin, FromMixin, ToMixin, EqualToHashToStringOpCmpMixin;
34 import aermicioi.aedi_property_reader.convertor.mapper;
35 import aermicioi.aedi_property_reader.convertor.accessor;
36 import aermicioi.aedi_property_reader.convertor.exception : ConvertorException;
37 import aermicioi.aedi_property_reader.convertor.inspector;
38 import aermicioi.aedi_property_reader.convertor.placeholder;
39 import aermicioi.aedi_property_reader.convertor.traits : n;
40 import aermicioi.aedi_property_reader.json.accessor;
41 import aermicioi.aedi_property_reader.json.inspector;
42 import aermicioi.aedi.factory;
43 import aermicioi.aedi.storage.locator;
44 import aermicioi.aedi.storage.wrapper;
45 import aermicioi.aedi.storage.decorator;
46 import aermicioi.aedi_property_reader.convertor.exception : InvalidCastException;
47 import std.json;
48 import std.traits;
49 import std.conv : to, text;
50 import std.experimental.allocator;
51 import std.exception : enforce;
52 
53 @component
54 class JsonConvertor : Convertor {
55     import std.meta : AliasSeq;
56     import aermicioi.aedi_property_reader.convertor.type_guesser : TypeGuesser;
57 
58     alias ToTypes = AliasSeq!(double, bool, ulong, long, string, JSONValue[], JSONValue[string], JSONValue);
59 
60     mixin FromMixin!JSONValue;
61     mixin ToMixin!ToTypes;
62     mixin ConvertsFromToMixin DefaultMixin;
63     mixin EqualToHashToStringOpCmpMixin;
64 
65     private TypeGuesser!JSONValue guesser;
66 
67     @autowired
68     this(TypeGuesser!JSONValue guesser) {
69         this.guesser = guesser;
70     }
71 
72     /**
73     ditto
74     **/
75     bool converts(in Object from, const TypeInfo to) @safe const nothrow {
76         return DefaultMixin.converts(from, to) && (this.guesser.guess(from.unwrap!JSONValue).n is to);
77     }
78 
79     /**
80     ditto
81     **/
82     bool converts(in Object from, in Object to) @safe const nothrow {
83         return DefaultMixin.converts(from, to) && (this.guesser.guess(from.unwrap!JSONValue).n is to.identify);
84     }
85 
86     /**
87     Convert from component to component.
88 
89     Params:
90         from = original component that is to be converted.
91         to = destination object that will be constructed out for original one.
92         allocator = optional allocator that could be used to construct to component.
93     Throws:
94         ConvertorException when there is a converting error
95         InvalidArgumentException when arguments passed are not of right type or state
96     Returns:
97         Resulting converted component.
98     **/
99     Object convert(in Object from, const TypeInfo to, RCIAllocator allocator = theAllocator) const {
100         enforce!ConvertorException(from.identify is typeid(JSONValue), text("Cannot convert from ", from.identify, " type, expected one of ", this.from));
101         JSONValue unwrapped = from.unwrap!JSONValue;
102 
103         if (to is typeid(string)) {
104             enforce!ConvertorException(unwrapped.type is JSON_TYPE..string, text("Cannot convert to ", to, " contained value is of type ", unwrapped.type));
105             return unwrapped.str.pack(from, this, allocator);
106         }
107 
108         if (to is typeid(double)) {
109             enforce!ConvertorException(unwrapped.type is JSON_TYPE.float_, text("Cannot convert to ", to, " contained value is of type ", unwrapped.type));
110             return unwrapped.floating.pack(from, this, allocator);
111         }
112 
113         if (to is typeid(bool)) {
114             enforce!ConvertorException((unwrapped.type is JSON_TYPE.true_) || (unwrapped.type is JSON_TYPE.false_), text("Cannot convert to ", to, " contained value is of type ", unwrapped.type));
115             return unwrapped.boolean.pack(from, this, allocator);
116         }
117 
118         if (to is typeid(ulong)) {
119             enforce!ConvertorException(unwrapped.type is JSON_TYPE.uinteger, text("Cannot convert to ", to, " contained value is of type ", unwrapped.type));
120             return unwrapped.uinteger.pack(from, this, allocator);
121         }
122 
123         if (to is typeid(long)) {
124             enforce!ConvertorException(unwrapped.type is JSON_TYPE.integer, text("Cannot convert to ", to, " contained value is of type ", unwrapped.type));
125             return unwrapped.integer.pack(from, this, allocator);
126         }
127 
128         if (to is typeid(JSONValue[])) {
129             enforce!ConvertorException(unwrapped.type is JSON_TYPE.array, text("Cannot convert to ", to, " contained value is of type ", unwrapped.type));
130             return unwrapped.array.pack(from, this, allocator);
131         }
132 
133         if (to is typeid(JSONValue[string])) {
134             enforce!ConvertorException(unwrapped.type is JSON_TYPE.object, text("Cannot convert to ", to, " contained value is of type ", unwrapped.type));
135             return unwrapped.object.pack(from, this, allocator);
136         }
137 
138         if (to is typeid(JSONValue)) {
139             return from.unwrap!JSONValue.pack(from, this, allocator);
140         }
141 
142         throw new ConvertorException(text("Destination type ", to, " isn't convertable by ", this, " convertor. Expected destination types of ", this.to));
143     }
144 
145     /**
146     Destroy component created using this convertor.
147 
148     Destroy component created using this convertor.
149     Since convertor could potentially allocate memory for
150     converted component, only itself is containing history of allocation,
151     and therefore it is responsible as well to destroy and free allocated
152     memory with allocator.
153 
154     Params:
155         converted = component that should be destroyed.
156         allocator = allocator used to allocate converted component.
157     **/
158     void destruct(const TypeInfo from, ref Object converted, RCIAllocator allocator = theAllocator) const {
159         if (this.destroys(from, converted)) {
160             static foreach (ToType; ToTypes) {
161                 if (converted.identify is typeid(ToType)) {
162                     converted.unpack!ToType;
163                 }
164             }
165         }
166     }
167 }