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_property_reader.core.inspector;
33 import aermicioi.aedi_property_reader.core.type_guesser;
34 import std.string;
35 import std.conv;
36 import std.json;
37 import std.range;
38 import std.algorithm;
39 
40 /**
41 Inspector for json values.
42 **/
43 class JsonInspector : Inspector!JSONValue {
44 
45     /**
46     Identify the type of child field of component.
47 
48     Params:
49         component = a composite component (class, struct, assoc array etc.) containing some fields
50 
51     Returns:
52         Type of field, or typeid(void) if field is not present in component
53     **/
54     TypeInfo typeOf(JSONValue component, in string property) const nothrow {
55         try {
56 
57             switch (component.type) {
58                 case JSON_TYPE.ARRAY: {
59                     if (property.isNumeric) {
60                         return this.typeOf(component.array[property.to!size_t]);
61                     }
62 
63                     break;
64                 }
65 
66                 case JSON_TYPE.OBJECT: {
67                     return this.typeOf(component.object[property]);
68                 }
69 
70                 default: {
71                     return typeid(void);
72                 }
73             }
74         } catch (Exception e) {
75 
76         }
77 
78         return typeid(void);
79     }
80 
81     /**
82     Identify the type of component itself.
83 
84     Identify the type of component itself. It will inspect the component and will return accurate
85     type info that the component represents.
86 
87     Params:
88         component = component which should be identified.
89 
90     Returns:
91         Type info of component, or typeid(void) if component cannot be identified by inspector
92     **/
93     TypeInfo typeOf(JSONValue component) const nothrow {
94         return typeid(component);
95     }
96 
97     /**
98     Check if component has a field or a property.
99 
100     Params:
101         component = component with fields
102         property = component property that is tested for existence
103 
104     Returns:
105         true if field is present either in readonly, or writeonly form (has getters and setters).
106     **/
107     bool has(JSONValue component, in string property) const nothrow {
108         try {
109 
110             switch (component.type) {
111                 case JSON_TYPE.ARRAY: {
112                     if (property.isNumeric) {
113                         return component.array.length > property.to!size_t;
114                     }
115                     break;
116                 }
117 
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.ARRAY: {
147                     return component.array.length.iota.map!(to!string).array;
148                 }
149 
150                 case JSON_TYPE.OBJECT: {
151                     return component.object.byKey.array;
152                 }
153 
154                 default: {
155                     return [];
156                 }
157             }
158         } catch (Exception e) {
159 
160         }
161 
162         return [];
163     }
164 }