1 /** 2 Grimoire 3 Copyright (c) 2017 Enalye 4 5 This software is provided 'as-is', without any express or implied warranty. 6 In no event will the authors be held liable for any damages arising 7 from the use of this software. 8 9 Permission is granted to anyone to use this software for any purpose, 10 including commercial applications, and to alter it and redistribute 11 it freely, subject to the following restrictions: 12 13 1. The origin of this software must not be misrepresented; 14 you must not claim that you wrote the original software. 15 If you use this software in a product, an acknowledgment 16 in the product documentation would be appreciated but 17 is not required. 18 19 2. Altered source versions must be plainly marked as such, 20 and must not be misrepresented as being the original software. 21 22 3. This notice may not be removed or altered from any source distribution. 23 */ 24 25 module core.json; 26 27 public import std.json; 28 import std.conv; 29 30 bool hasJson(JSONValue json, string tag) { 31 return ((tag in json.object) !is null); 32 } 33 34 JSONValue getJson(JSONValue json, string tag) { 35 if((tag in json.object) is null) 36 throw new Exception("JSON: \'" ~ tag ~ "\'' does not exist in JSON."); 37 return json.object[tag]; 38 } 39 40 JSONValue[] getJsonArray(JSONValue json, string tag) { 41 if((tag in json.object) is null) 42 throw new Exception("JSON: \'" ~ tag ~ "\'' does not exist in JSON."); 43 return json.object[tag].array; 44 } 45 46 string[] getJsonArrayStr(JSONValue json, string tag) { 47 if((tag in json.object) is null) 48 throw new Exception("JSON: \'" ~ tag ~ "\'' does not exist in JSON."); 49 string[] array; 50 foreach(JSONValue value; json.object[tag].array) 51 array ~= value.str; 52 return array; 53 } 54 55 string[] getJsonArrayStr(JSONValue json, string tag, string[] defValue) { 56 if((tag in json.object) is null) 57 return defValue; 58 string[] array; 59 foreach(JSONValue value; json.object[tag].array) 60 array ~= value.str; 61 return array; 62 } 63 64 int[] getJsonArrayInt(JSONValue json, string tag) { 65 if((tag in json.object) is null) 66 throw new Exception("JSON: \'" ~ tag ~ "\'' does not exist in JSON."); 67 int[] array; 68 foreach(JSONValue value; json.object[tag].array) { 69 if(value.type() == JSON_TYPE.INTEGER) 70 array ~= cast(int)value.integer; 71 else 72 array ~= to!int(value.str); 73 } 74 return array; 75 } 76 77 int[] getJsonArrayInt(JSONValue json, string tag, int[] defValue) { 78 if((tag in json.object) is null) 79 return defValue; 80 int[] array; 81 foreach(JSONValue value; json.object[tag].array) { 82 if(value.type() == JSON_TYPE.INTEGER) 83 array ~= cast(int)value.integer; 84 else 85 array ~= to!int(value.str); 86 } 87 return array; 88 } 89 90 float[] getJsonArrayFloat(JSONValue json, string tag) { 91 if((tag in json.object) is null) 92 throw new Exception("JSON: \'" ~ tag ~ "\'' does not exist in JSON."); 93 float[] array; 94 foreach(JSONValue value; json.object[tag].array) { 95 if(value.type() == JSON_TYPE.INTEGER) 96 array ~= value.floating; 97 else 98 array ~= to!float(value.str); 99 } 100 return array; 101 } 102 103 float[] getJsonArrayFloat(JSONValue json, string tag, float[] defValue) { 104 if((tag in json.object) is null) 105 return defValue; 106 float[] array; 107 foreach(JSONValue value; json.object[tag].array) { 108 if(value.type() == JSON_TYPE.INTEGER) 109 array ~= value.floating; 110 else 111 array ~= to!float(value.str); 112 } 113 return array; 114 } 115 116 string getJsonStr(JSONValue json, string tag) { 117 if((tag in json.object) is null) 118 throw new Exception("JSON: \'" ~ tag ~ "\'' does not exist in JSON."); 119 return json.object[tag].str; 120 } 121 122 string getJsonStr(JSONValue json, string tag, string defValue) { 123 if((tag in json.object) is null) 124 return defValue; 125 return json.object[tag].str; 126 } 127 128 int getJsonInt(JSONValue json, string tag) { 129 if((tag in json.object) is null) 130 throw new Exception("JSON: \'" ~ tag ~ "\'' does not exist in JSON."); 131 JSONValue value = json.object[tag]; 132 switch(value.type()) with(JSON_TYPE) { 133 case INTEGER: 134 return cast(int)value.integer; 135 case UINTEGER: 136 return cast(int)value.uinteger; 137 case FLOAT: 138 return cast(int)value.floating; 139 case STRING: 140 return to!int(value.str); 141 default: 142 throw new Exception("JSON: No integer value in \'" ~ tag ~ "\'."); 143 } 144 } 145 146 int getJsonInt(JSONValue json, string tag, int defValue) { 147 if((tag in json.object) is null) 148 return defValue; 149 JSONValue value = json.object[tag]; 150 151 switch(value.type()) with(JSON_TYPE) { 152 case INTEGER: 153 return cast(int)value.integer; 154 case UINTEGER: 155 return cast(int)value.uinteger; 156 case FLOAT: 157 return cast(int)value.floating; 158 case STRING: 159 return to!int(value.str); 160 default: 161 throw new Exception("JSON: No integer value in \'" ~ tag ~ "\'."); 162 } 163 } 164 165 float getJsonFloat(JSONValue json, string tag) { 166 if((tag in json.object) is null) 167 throw new Exception("JSON: \'" ~ tag ~ "\'' does not exist in JSON."); 168 JSONValue value = json.object[tag]; 169 switch(value.type()) with(JSON_TYPE) { 170 case INTEGER: 171 return cast(float)value.integer; 172 case UINTEGER: 173 return cast(float)value.uinteger; 174 case FLOAT: 175 return value.floating; 176 case STRING: 177 return to!float(value.str); 178 default: 179 throw new Exception("JSON: No floating value in \'" ~ tag ~ "\'."); 180 } 181 } 182 183 float getJsonFloat(JSONValue json, string tag, float defValue) { 184 if((tag in json.object) is null) 185 return defValue; 186 JSONValue value = json.object[tag]; 187 switch(value.type()) with(JSON_TYPE) { 188 case INTEGER: 189 return cast(float)value.integer; 190 case UINTEGER: 191 return cast(float)value.uinteger; 192 case FLOAT: 193 return value.floating; 194 case STRING: 195 return to!float(value.str); 196 default: 197 throw new Exception("JSON: No floating value in \'" ~ tag ~ "\'."); 198 } 199 } 200 201 bool getJsonBool(JSONValue json, string tag) { 202 if((tag in json.object) is null) 203 throw new Exception("JSON: \'" ~ tag ~ "\'' does not exist in JSON."); 204 JSONValue value = json.object[tag]; 205 if(value.type() == JSON_TYPE.TRUE) 206 return true; 207 else if(value.type() == JSON_TYPE.FALSE) 208 return false; 209 else 210 throw new Exception("JSON: \'" ~ tag ~ "\' is not a boolean value."); 211 } 212 213 bool getJsonBool(JSONValue json, string tag, bool defValue) { 214 if((tag in json.object) is null) 215 return defValue; 216 JSONValue value = json.object[tag]; 217 if(value.type() == JSON_TYPE.TRUE) 218 return true; 219 else if(value.type() == JSON_TYPE.FALSE) 220 return false; 221 else 222 throw new Exception("JSON: \'" ~ tag ~ "\' is not a boolean value."); 223 }