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     Alexandru Ermicioi
29 **/
30 module aermicioi.aedi_property_reader.json.setter;
31 
32 import aermicioi.aedi.configurer.annotation.annotation;
33 import aermicioi.aedi_property_reader.convertor.setter;
34 import aermicioi.aedi_property_reader.convertor.exception;
35 import std.json;
36 import std.exception;
37 import std.conv : text;
38 import std.experimental.allocator;
39 
40 @component
41 class JsonObjectSetter : PropertySetter!(JSONValue, JSONValue) {
42 
43     /**
44     Set a field or property of CompositeType.
45 
46     Params:
47         composite = composite that will store value
48         value = actual value that is assigned to a field in composite
49         property = the identity of field in composite
50     Throws:
51         InvalidArgumentException when value or composite is not what was expected
52     **/
53     void set(ref JSONValue composite, JSONValue value, string property, RCIAllocator allocator = theAllocator) const {
54         enforce!ConvertorException(composite.type == JSON_TYPE.object, text("The passed json is not of object type but ", composite.type));
55 
56         composite.object["property"] = value;
57     }
58 
59     /**
60      Identify the type of supported component.
61 
62      Identify the type of supported component. It returns type info of component
63      if it is supported by accessor, otherwise it will return typeid(void) denoting that
64      the type isn't supported by accessor. The accessor is not limited to returning the type
65      info of passed component, it can actually return type info of super type or any type
66      given the returned type is implicitly convertible or castable to ComponentType.
67 
68      Params:
69          component = the component for which accessor should identify the underlying type
70 
71      Returns:
72          TypeInfo type information about passed component, or typeid(void) if component is not supported.
73      **/
74     TypeInfo componentType(ref JSONValue composite) const nothrow {
75         return typeid(JSONValue);
76     }
77 }
78 
79 @component
80 class JsonArraySetter : PropertySetter!(JSONValue, JSONValue, size_t) {
81 
82     /**
83     Set a field or property of CompositeType.
84 
85     Params:
86         composite = composite that will store value
87         value = actual value that is assigned to a field in composite
88         property = the identity of field in composite
89     Throws:
90         InvalidArgumentException when value or composite is not what was expected
91     **/
92     void set(ref JSONValue composite, JSONValue value, size_t property, RCIAllocator allocator = theAllocator) const {
93         enforce!ConvertorException(composite.type == JSON_TYPE.ARRAY, text("The passed json is not of array type but ", composite.type));
94 
95         if (composite.array.length <= property) {
96             composite.array.length = property + 1;
97         }
98 
99         composite.array[property] = value;
100     }
101 
102     /**
103      Identify the type of supported component.
104 
105      Identify the type of supported component. It returns type info of component
106      if it is supported by accessor, otherwise it will return typeid(void) denoting that
107      the type isn't supported by accessor. The accessor is not limited to returning the type
108      info of passed component, it can actually return type info of super type or any type
109      given the returned type is implicitly convertible or castable to ComponentType.
110 
111      Params:
112          component = the component for which accessor should identify the underlying type
113 
114      Returns:
115          TypeInfo type information about passed component, or typeid(void) if component is not supported.
116      **/
117     TypeInfo componentType(ref JSONValue composite) const nothrow {
118         return typeid(JSONValue);
119     }
120 }