1 /**
2     Json
3 
4     Copyright: (c) Enalye 2017
5     License: Zlib
6     Authors: Enalye
7 */
8 
9 module atelier.core.json;
10 
11 public import std.json;
12 import std.conv;
13 import std.regex, std.path;
14 import std.stdio;
15 
16 /// Transform your path in a system agnostic path.
17 string convertPathToExport(string path) {
18     return replaceAll(path, regex(r"\\/|/|\\"), "/");
19 }
20 
21 /// Transform the path in your path system.
22 string convertPathToImport(string path) {
23     return replaceAll(path, regex(r"\\/|/|\\"), dirSeparator);
24 }
25 
26 /// Does the node exist ?
27 bool hasJson(JSONValue json, string tag) {
28     return ((tag in json.object) !is null);
29 }
30 
31 /// Get the node
32 JSONValue getJson(JSONValue json, string tag) {
33     if ((tag in json.object) is null)
34         throw new Exception("JSON: \'" ~ tag ~ "\'' does not exist in JSON.");
35     return json.object[tag];
36 }
37 
38 JSONValue[] getJsonArray(JSONValue json, string tag) {
39     if ((tag in json.object) is null)
40         throw new Exception("JSON: \'" ~ tag ~ "\'' does not exist in JSON.");
41     return json.object[tag].array;
42 }
43 
44 string[] getJsonArrayStr(JSONValue json, string tag) {
45     if ((tag in json.object) is null)
46         throw new Exception("JSON: \'" ~ tag ~ "\'' does not exist in JSON.");
47     string[] array;
48     foreach (JSONValue value; json.object[tag].array)
49         array ~= value.str;
50     return array;
51 }
52 
53 string[] getJsonArrayStr(JSONValue json, string tag, string[] defValue) {
54     if ((tag in json.object) is null)
55         return defValue;
56     string[] array;
57     foreach (JSONValue value; json.object[tag].array)
58         array ~= value.str;
59     return array;
60 }
61 
62 int[] getJsonArrayInt(JSONValue json, string tag) {
63     if ((tag in json.object) is null)
64         throw new Exception("JSON: \'" ~ tag ~ "\'' does not exist in JSON.");
65     int[] array;
66     foreach (JSONValue value; json.object[tag].array) {
67         if (value.type() == JSONType.integer)
68             array ~= cast(int) value.integer;
69         else
70             array ~= to!int(value.str);
71     }
72     return array;
73 }
74 
75 int[] getJsonArrayInt(JSONValue json, string tag, int[] defValue) {
76     if ((tag in json.object) is null)
77         return defValue;
78     int[] array;
79     foreach (JSONValue value; json.object[tag].array) {
80         if (value.type() == JSONType.integer)
81             array ~= cast(int) value.integer;
82         else
83             array ~= to!int(value.str);
84     }
85     return array;
86 }
87 
88 float[] getJsonArrayFloat(JSONValue json, string tag) {
89     if ((tag in json.object) is null)
90         throw new Exception("JSON: \'" ~ tag ~ "\'' does not exist in JSON.");
91     float[] array;
92     foreach (JSONValue value; json.object[tag].array) {
93         if (value.type() == JSONType.integer)
94             array ~= value.floating;
95         else
96             array ~= to!float(value.str);
97     }
98     return array;
99 }
100 
101 float[] getJsonArrayFloat(JSONValue json, string tag, float[] defValue) {
102     if ((tag in json.object) is null)
103         return defValue;
104     float[] array;
105     foreach (JSONValue value; json.object[tag].array) {
106         if (value.type() == JSONType.integer)
107             array ~= value.floating;
108         else
109             array ~= to!float(value.str);
110     }
111     return array;
112 }
113 
114 string getJsonStr(JSONValue json, string tag) {
115     if ((tag in json.object) is null)
116         throw new Exception("JSON: \'" ~ tag ~ "\'' does not exist in JSON.");
117     return json.object[tag].str;
118 }
119 
120 string getJsonStr(JSONValue json, string tag, string defValue) {
121     if ((tag in json.object) is null)
122         return defValue;
123     return json.object[tag].str;
124 }
125 
126 int getJsonInt(JSONValue json, string tag) {
127     if ((tag in json.object) is null)
128         throw new Exception("JSON: \'" ~ tag ~ "\'' does not exist in JSON.");
129     JSONValue value = json.object[tag];
130     switch (value.type()) with (JSONType) {
131     case integer:
132         return cast(int) value.integer;
133     case uinteger:
134         return cast(int) value.uinteger;
135     case float_:
136         return cast(int) value.floating;
137     case string:
138         return to!int(value.str);
139     default:
140         throw new Exception("JSON: No integer value in \'" ~ tag ~ "\'.");
141     }
142 }
143 
144 int getJsonInt(JSONValue json, string tag, int defValue) {
145     if ((tag in json.object) is null)
146         return defValue;
147     JSONValue value = json.object[tag];
148 
149     switch (value.type()) with (JSONType) {
150     case integer:
151         return cast(int) value.integer;
152     case uinteger:
153         return cast(int) value.uinteger;
154     case float_:
155         return cast(int) value.floating;
156     case string:
157         return to!int(value.str);
158     default:
159         throw new Exception("JSON: No integer value in \'" ~ tag ~ "\'.");
160     }
161 }
162 
163 float getJsonFloat(JSONValue json, string tag) {
164     if ((tag in json.object) is null)
165         throw new Exception("JSON: \'" ~ tag ~ "\'' does not exist in JSON.");
166     JSONValue value = json.object[tag];
167     switch (value.type()) with (JSONType) {
168     case integer:
169         return cast(float) value.integer;
170     case uinteger:
171         return cast(float) value.uinteger;
172     case float_:
173         return value.floating;
174     case string:
175         return to!float(value.str);
176     default:
177         throw new Exception("JSON: No floating value in \'" ~ tag ~ "\'.");
178     }
179 }
180 
181 float getJsonFloat(JSONValue json, string tag, float defValue) {
182     if ((tag in json.object) is null)
183         return defValue;
184     JSONValue value = json.object[tag];
185     switch (value.type()) with (JSONType) {
186     case integer:
187         return cast(float) value.integer;
188     case uinteger:
189         return cast(float) value.uinteger;
190     case float_:
191         return value.floating;
192     case string:
193         return to!float(value.str);
194     default:
195         throw new Exception("JSON: No floating value in \'" ~ tag ~ "\'.");
196     }
197 }
198 
199 bool getJsonBool(JSONValue json, string tag) {
200     if ((tag in json.object) is null)
201         throw new Exception("JSON: \'" ~ tag ~ "\'' does not exist in JSON.");
202     JSONValue value = json.object[tag];
203     if (value.type() == JSONType.true_)
204         return true;
205     else if (value.type() == JSONType.false_)
206         return false;
207     else
208         throw new Exception("JSON: \'" ~ tag ~ "\' is not a boolean value.");
209 }
210 
211 bool getJsonBool(JSONValue json, string tag, bool defValue) {
212     if ((tag in json.object) is null)
213         return defValue;
214     JSONValue value = json.object[tag];
215     if (value.type() == JSONType.true_)
216         return true;
217     else if (value.type() == JSONType.false_)
218         return false;
219     else
220         throw new Exception("JSON: \'" ~ tag ~ "\' is not a boolean value.");
221 }