| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
#ifndef JSON_H |
| 22 |
#define JSON_H |
| 23 |
|
| 24 |
enum |
| 25 |
{ |
| 26 |
JSONTYPE_STRING, |
| 27 |
JSONTYPE_OBJECT, |
| 28 |
JSONTYPE_ARRAY, |
| 29 |
JSONTYPE_VALUE, |
| 30 |
JSONTYPE_ERROR |
| 31 |
}; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
const char *JSON_ArrayGetFirstValue(const char *json, const char *jsonEnd); |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
const char *JSON_ArrayGetNextValue(const char *json, const char *jsonEnd); |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
unsigned int JSON_ArrayGetIndex(const char *json, const char *jsonEnd, const char **indexes, unsigned int numIndexes); |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
const char *JSON_ArrayGetValue(const char *json, const char *jsonEnd, unsigned int index); |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
const char *JSON_ObjectGetNamedValue(const char *json, const char *jsonEnd, const char *name); |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
unsigned int JSON_ValueGetType(const char *json, const char *jsonEnd); |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
unsigned int JSON_ValueGetString(const char *json, const char *jsonEnd, char *outString, unsigned int stringLen); |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
double JSON_ValueGetDouble(const char *json, const char *jsonEnd); |
| 83 |
float JSON_ValueGetFloat(const char *json, const char *jsonEnd); |
| 84 |
int JSON_ValueGetInt(const char *json, const char *jsonEnd); |
| 85 |
|
| 86 |
#endif |
| 87 |
|
| 88 |
#ifdef JSON_IMPLEMENTATION |
| 89 |
#include <stdio.h> |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
static const char *JSON_SkipSeparators(const char *json, const char *jsonEnd); |
| 96 |
static const char *JSON_SkipString(const char *json, const char *jsonEnd); |
| 97 |
static const char *JSON_SkipStruct(const char *json, const char *jsonEnd); |
| 98 |
static const char *JSON_SkipValue(const char *json, const char *jsonEnd); |
| 99 |
static const char *JSON_SkipValueAndSeparators(const char *json, const char *jsonEnd); |
| 100 |
|
| 101 |
#define IS_SEPARATOR(x) ((x) == ' ' || (x) == '\t' || (x) == '\n' || (x) == '\r' || (x) == ',' || (x) == ':') |
| 102 |
#define IS_STRUCT_OPEN(x) ((x) == '{' || (x) == '[') |
| 103 |
#define IS_STRUCT_CLOSE(x) ((x) == '}' || (x) == ']') |
| 104 |
|
| 105 |
static const char *JSON_SkipSeparators(const char *json, const char *jsonEnd) |
| 106 |
{ |
| 107 |
while (json < jsonEnd && IS_SEPARATOR(*json)) |
| 108 |
json++; |
| 109 |
|
| 110 |
return json; |
| 111 |
} |
| 112 |
|
| 113 |
static const char *JSON_SkipString(const char *json, const char *jsonEnd) |
| 114 |
{ |
| 115 |
for (json++; json < jsonEnd && *json != '"'; json++) |
| 116 |
if (*json == '\\') |
| 117 |
json++; |
| 118 |
|
| 119 |
return (json + 1 > jsonEnd) ? jsonEnd : json + 1; |
| 120 |
} |
| 121 |
|
| 122 |
static const char *JSON_SkipStruct(const char *json, const char *jsonEnd) |
| 123 |
{ |
| 124 |
json = JSON_SkipSeparators(json + 1, jsonEnd); |
| 125 |
while (json < jsonEnd && !IS_STRUCT_CLOSE(*json)) |
| 126 |
json = JSON_SkipValueAndSeparators(json, jsonEnd); |
| 127 |
|
| 128 |
return (json + 1 > jsonEnd) ? jsonEnd : json + 1; |
| 129 |
} |
| 130 |
|
| 131 |
static const char *JSON_SkipValue(const char *json, const char *jsonEnd) |
| 132 |
{ |
| 133 |
if (json >= jsonEnd) |
| 134 |
return jsonEnd; |
| 135 |
else if (*json == '"') |
| 136 |
json = JSON_SkipString(json, jsonEnd); |
| 137 |
else if (IS_STRUCT_OPEN(*json)) |
| 138 |
json = JSON_SkipStruct(json, jsonEnd); |
| 139 |
else |
| 140 |
{ |
| 141 |
while (json < jsonEnd && !IS_SEPARATOR(*json) && !IS_STRUCT_CLOSE(*json)) |
| 142 |
json++; |
| 143 |
} |
| 144 |
|
| 145 |
return json; |
| 146 |
} |
| 147 |
|
| 148 |
static const char *JSON_SkipValueAndSeparators(const char *json, const char *jsonEnd) |
| 149 |
{ |
| 150 |
json = JSON_SkipValue(json, jsonEnd); |
| 151 |
return JSON_SkipSeparators(json, jsonEnd); |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
static unsigned int JSON_NoParse(const char *json, const char *jsonEnd) |
| 156 |
{ |
| 157 |
if (!json || json >= jsonEnd || *json == 'f' || *json == 'n') |
| 158 |
return 1; |
| 159 |
|
| 160 |
if (*json == 't') |
| 161 |
return 2; |
| 162 |
|
| 163 |
return 0; |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
const char *JSON_ArrayGetFirstValue(const char *json, const char *jsonEnd) |
| 171 |
{ |
| 172 |
if (!json || json >= jsonEnd || !IS_STRUCT_OPEN(*json)) |
| 173 |
return NULL; |
| 174 |
|
| 175 |
json = JSON_SkipSeparators(json + 1, jsonEnd); |
| 176 |
|
| 177 |
return (json >= jsonEnd || IS_STRUCT_CLOSE(*json)) ? NULL : json; |
| 178 |
} |
| 179 |
|
| 180 |
const char *JSON_ArrayGetNextValue(const char *json, const char *jsonEnd) |
| 181 |
{ |
| 182 |
if (!json || json >= jsonEnd || IS_STRUCT_CLOSE(*json)) |
| 183 |
return NULL; |
| 184 |
|
| 185 |
json = JSON_SkipValueAndSeparators(json, jsonEnd); |
| 186 |
|
| 187 |
return (json >= jsonEnd || IS_STRUCT_CLOSE(*json)) ? NULL : json; |
| 188 |
} |
| 189 |
|
| 190 |
unsigned int JSON_ArrayGetIndex(const char *json, const char *jsonEnd, const char **indexes, unsigned int numIndexes) |
| 191 |
{ |
| 192 |
unsigned int length = 0; |
| 193 |
|
| 194 |
for (json = JSON_ArrayGetFirstValue(json, jsonEnd); json; json = JSON_ArrayGetNextValue(json, jsonEnd)) |
| 195 |
{ |
| 196 |
if (indexes && numIndexes) |
| 197 |
{ |
| 198 |
*indexes++ = json; |
| 199 |
numIndexes--; |
| 200 |
} |
| 201 |
length++; |
| 202 |
} |
| 203 |
|
| 204 |
return length; |
| 205 |
} |
| 206 |
|
| 207 |
const char *JSON_ArrayGetValue(const char *json, const char *jsonEnd, unsigned int index) |
| 208 |
{ |
| 209 |
for (json = JSON_ArrayGetFirstValue(json, jsonEnd); json && index; json = JSON_ArrayGetNextValue(json, jsonEnd)) |
| 210 |
index--; |
| 211 |
|
| 212 |
return json; |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
const char *JSON_ObjectGetNamedValue(const char *json, const char *jsonEnd, const char *name) |
| 220 |
{ |
| 221 |
unsigned int nameLen = strlen(name); |
| 222 |
|
| 223 |
for (json = JSON_ArrayGetFirstValue(json, jsonEnd); json; json = JSON_ArrayGetNextValue(json, jsonEnd)) |
| 224 |
{ |
| 225 |
if (*json == '"') |
| 226 |
{ |
| 227 |
const char *thisNameStart, *thisNameEnd; |
| 228 |
|
| 229 |
thisNameStart = json + 1; |
| 230 |
json = JSON_SkipString(json, jsonEnd); |
| 231 |
thisNameEnd = json - 1; |
| 232 |
json = JSON_SkipSeparators(json, jsonEnd); |
| 233 |
|
| 234 |
if ((unsigned int)(thisNameEnd - thisNameStart) == nameLen) |
| 235 |
if (strncmp(thisNameStart, name, nameLen) == 0) |
| 236 |
return json; |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
return NULL; |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
unsigned int JSON_ValueGetType(const char *json, const char *jsonEnd) |
| 248 |
{ |
| 249 |
if (!json || json >= jsonEnd) |
| 250 |
return JSONTYPE_ERROR; |
| 251 |
else if (*json == '"') |
| 252 |
return JSONTYPE_STRING; |
| 253 |
else if (*json == '{') |
| 254 |
return JSONTYPE_OBJECT; |
| 255 |
else if (*json == '[') |
| 256 |
return JSONTYPE_ARRAY; |
| 257 |
|
| 258 |
return JSONTYPE_VALUE; |
| 259 |
} |
| 260 |
|
| 261 |
unsigned int JSON_ValueGetString(const char *json, const char *jsonEnd, char *outString, unsigned int stringLen) |
| 262 |
{ |
| 263 |
const char *stringEnd, *stringStart; |
| 264 |
|
| 265 |
if (!json) |
| 266 |
{ |
| 267 |
*outString = '\0'; |
| 268 |
return 0; |
| 269 |
} |
| 270 |
|
| 271 |
stringStart = json; |
| 272 |
stringEnd = JSON_SkipValue(stringStart, jsonEnd); |
| 273 |
if (stringEnd >= jsonEnd) |
| 274 |
{ |
| 275 |
*outString = '\0'; |
| 276 |
return 0; |
| 277 |
} |
| 278 |
|
| 279 |
|
| 280 |
if (*stringStart == '"') |
| 281 |
stringStart++; |
| 282 |
|
| 283 |
if (*(stringEnd - 1) == '"') |
| 284 |
stringEnd--; |
| 285 |
|
| 286 |
stringLen--; |
| 287 |
if (stringLen > stringEnd - stringStart) |
| 288 |
stringLen = stringEnd - stringStart; |
| 289 |
|
| 290 |
json = stringStart; |
| 291 |
while (stringLen--) |
| 292 |
*outString++ = *json++; |
| 293 |
*outString = '\0'; |
| 294 |
|
| 295 |
return stringEnd - stringStart; |
| 296 |
} |
| 297 |
|
| 298 |
double JSON_ValueGetDouble(const char *json, const char *jsonEnd) |
| 299 |
{ |
| 300 |
char cValue[256]; |
| 301 |
double dValue = 0.0; |
| 302 |
unsigned int np = JSON_NoParse(json, jsonEnd); |
| 303 |
|
| 304 |
if (np) |
| 305 |
return (double)(np - 1); |
| 306 |
|
| 307 |
if (!JSON_ValueGetString(json, jsonEnd, cValue, 256)) |
| 308 |
return 0.0; |
| 309 |
|
| 310 |
sscanf(cValue, "%lf", &dValue); |
| 311 |
|
| 312 |
return dValue; |
| 313 |
} |
| 314 |
|
| 315 |
float JSON_ValueGetFloat(const char *json, const char *jsonEnd) |
| 316 |
{ |
| 317 |
char cValue[256]; |
| 318 |
float fValue = 0.0f; |
| 319 |
unsigned int np = JSON_NoParse(json, jsonEnd); |
| 320 |
|
| 321 |
if (np) |
| 322 |
return (float)(np - 1); |
| 323 |
|
| 324 |
if (!JSON_ValueGetString(json, jsonEnd, cValue, 256)) |
| 325 |
return 0.0f; |
| 326 |
|
| 327 |
sscanf(cValue, "%f", &fValue); |
| 328 |
|
| 329 |
return fValue; |
| 330 |
} |
| 331 |
|
| 332 |
int JSON_ValueGetInt(const char *json, const char *jsonEnd) |
| 333 |
{ |
| 334 |
char cValue[256]; |
| 335 |
int iValue = 0; |
| 336 |
unsigned int np = JSON_NoParse(json, jsonEnd); |
| 337 |
|
| 338 |
if (np) |
| 339 |
return np - 1; |
| 340 |
|
| 341 |
if (!JSON_ValueGetString(json, jsonEnd, cValue, 256)) |
| 342 |
return 0; |
| 343 |
|
| 344 |
sscanf(cValue, "%d", &iValue); |
| 345 |
|
| 346 |
return iValue; |
| 347 |
} |
| 348 |
|
| 349 |
#undef IS_SEPARATOR |
| 350 |
#undef IS_STRUCT_OPEN |
| 351 |
#undef IS_STRUCT_CLOSE |
| 352 |
|
| 353 |
#endif |