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.inspector;
31 
32 import aermicioi.aedi.configurer.annotation.annotation;
33 import aermicioi.aedi_property_reader.convertor.inspector;
34 import aermicioi.aedi_property_reader.convertor.type_guesser;
35 import std..string;
36 import std.conv;
37 import std.json;
38 import std.range;
39 import std.algorithm;
40 
41 @component
42 auto runtimeJsonInspector(JsonInspector inspector) {
43     return new RuntimeInspector!JSONValue(inspector);
44 }
45 
46 /**
47 Inspector for json values.
48 **/
49 @component
50 class JsonInspector : Inspector!JSONValue {
51 
52     /**
53     Identify the type of child field of component.
54 
55     Params:
56         component = a composite component (class, struct, assoc array etc.) containing some fields
57 
58     Returns:
59         Type of field, or typeid(void) if field is not present in component
60     **/
61     TypeInfo typeOf(JSONValue component, in string property) const nothrow {
62         try {
63 
64             switch (component.type) {
65                 case JSON_TYPE.ARRAY: {
66                     if (property.isNumeric) {
67                         return this.typeOf(component.array[property.to!size_t]);
68                     }
69 
70                     break;
71                 }
72 
73                 case JSON_TYPE.OBJECT: {
74                     return this.typeOf(component.object[property]);
75                 }
76 
77                 default: {
78                     return typeid(void);
79                 }
80             }
81         } catch (Exception e) {
82 
83         }
84 
85         return typeid(void);
86     }
87 
88     /**
89     Identify the type of component itself.
90 
91     Identify the type of component itself. It will inspect the component and will return accurate
92     type info that the component represents.
93 
94     Params:
95         component = component which should be identified.
96 
97     Returns:
98         Type info of component, or typeid(void) if component cannot be identified by inspector
99     **/
100     TypeInfo typeOf(JSONValue component) const nothrow {
101         return typeid(component);
102     }
103 
104     /**
105     Check if component has a field or a property.
106 
107     Params:
108         component = component with fields
109         property = component property that is tested for existence
110 
111     Returns:
112         true if field is present either in readonly, or writeonly form (has getters and setters).
113     **/
114     bool has(JSONValue component, in string property) const nothrow {
115         try {
116 
117             switch (component.type) {
118                 case JSON_TYPE.OBJECT: {
119                     return (property in component.object) !is null;
120                 }
121 
122                 default: {
123                     return false;
124                 }
125             }
126         } catch (Exception e) {
127 
128         }
129 
130         return false;
131     }
132 
133     /**
134     Return a list of properties that component holds.
135 
136     Params:
137         component = the component with fields
138 
139     Returns:
140         an arary of property identities.
141     **/
142     string[] properties(JSONValue component) const nothrow {
143         try {
144 
145             switch (component.type) {
146                 case JSON_TYPE.OBJECT: {
147                     return component.object.byKey.array;
148                 }
149 
150                 default: {
151                     return [];
152                 }
153             }
154         } catch (Exception e) {
155 
156         }
157 
158         return [];
159     }
160 }