40const char *JSON_ArrayGetFirstValue(
const char *json,
const char *jsonEnd);
45const char *JSON_ArrayGetNextValue(
const char *json,
const char *jsonEnd);
50unsigned int JSON_ArrayGetIndex(
const char *json,
const char *jsonEnd,
const char **indexes,
unsigned int numIndexes);
54const char *JSON_ArrayGetValue(
const char *json,
const char *jsonEnd,
unsigned int index);
62const char *JSON_ObjectGetNamedValue(
const char *json,
const char *jsonEnd,
const char *name);
70unsigned int JSON_ValueGetType(
const char *json,
const char *jsonEnd);
76unsigned int JSON_ValueGetString(
const char *json,
const char *jsonEnd,
char *outString,
unsigned int stringLen);
82double JSON_ValueGetDouble(
const char *json,
const char *jsonEnd);
83float JSON_ValueGetFloat(
const char *json,
const char *jsonEnd);
84int JSON_ValueGetInt(
const char *json,
const char *jsonEnd);
88#ifdef JSON_IMPLEMENTATION
95static const char *JSON_SkipSeparators(
const char *json,
const char *jsonEnd);
96static const char *JSON_SkipString(
const char *json,
const char *jsonEnd);
97static const char *JSON_SkipStruct(
const char *json,
const char *jsonEnd);
98static const char *JSON_SkipValue(
const char *json,
const char *jsonEnd);
99static const char *JSON_SkipValueAndSeparators(
const char *json,
const char *jsonEnd);
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) == ']')
105static const char *JSON_SkipSeparators(
const char *json,
const char *jsonEnd)
107 while (json < jsonEnd && IS_SEPARATOR(*json))
113static const char *JSON_SkipString(
const char *json,
const char *jsonEnd)
115 for (json++; json < jsonEnd && *json !=
'"'; json++)
119 return (json + 1 > jsonEnd) ? jsonEnd : json + 1;
122static const char *JSON_SkipStruct(
const char *json,
const char *jsonEnd)
124 json = JSON_SkipSeparators(json + 1, jsonEnd);
125 while (json < jsonEnd && !IS_STRUCT_CLOSE(*json))
126 json = JSON_SkipValueAndSeparators(json, jsonEnd);
128 return (json + 1 > jsonEnd) ? jsonEnd : json + 1;
131static const char *JSON_SkipValue(
const char *json,
const char *jsonEnd)
135 else if (*json ==
'"')
136 json = JSON_SkipString(json, jsonEnd);
137 else if (IS_STRUCT_OPEN(*json))
138 json = JSON_SkipStruct(json, jsonEnd);
141 while (json < jsonEnd && !IS_SEPARATOR(*json) && !IS_STRUCT_CLOSE(*json))
148static const char *JSON_SkipValueAndSeparators(
const char *json,
const char *jsonEnd)
150 json = JSON_SkipValue(json, jsonEnd);
151 return JSON_SkipSeparators(json, jsonEnd);
155static unsigned int JSON_NoParse(
const char *json,
const char *jsonEnd)
157 if (!json || json >= jsonEnd || *json ==
'f' || *json ==
'n')
170const char *JSON_ArrayGetFirstValue(
const char *json,
const char *jsonEnd)
172 if (!json || json >= jsonEnd || !IS_STRUCT_OPEN(*json))
175 json = JSON_SkipSeparators(json + 1, jsonEnd);
177 return (json >= jsonEnd || IS_STRUCT_CLOSE(*json)) ? NULL : json;
180const char *JSON_ArrayGetNextValue(
const char *json,
const char *jsonEnd)
182 if (!json || json >= jsonEnd || IS_STRUCT_CLOSE(*json))
185 json = JSON_SkipValueAndSeparators(json, jsonEnd);
187 return (json >= jsonEnd || IS_STRUCT_CLOSE(*json)) ? NULL : json;
190unsigned int JSON_ArrayGetIndex(
const char *json,
const char *jsonEnd,
const char **indexes,
unsigned int numIndexes)
192 unsigned int length = 0;
194 for (json = JSON_ArrayGetFirstValue(json, jsonEnd); json; json = JSON_ArrayGetNextValue(json, jsonEnd))
196 if (indexes && numIndexes)
207const char *JSON_ArrayGetValue(
const char *json,
const char *jsonEnd,
unsigned int index)
209 for (json = JSON_ArrayGetFirstValue(json, jsonEnd); json && index; json = JSON_ArrayGetNextValue(json, jsonEnd))
219const char *JSON_ObjectGetNamedValue(
const char *json,
const char *jsonEnd,
const char *name)
221 unsigned int nameLen = strlen(name);
223 for (json = JSON_ArrayGetFirstValue(json, jsonEnd); json; json = JSON_ArrayGetNextValue(json, jsonEnd))
227 const char *thisNameStart, *thisNameEnd;
229 thisNameStart = json + 1;
230 json = JSON_SkipString(json, jsonEnd);
231 thisNameEnd = json - 1;
232 json = JSON_SkipSeparators(json, jsonEnd);
234 if ((
unsigned int)(thisNameEnd - thisNameStart) == nameLen)
235 if (strncmp(thisNameStart, name, nameLen) == 0)
247unsigned int JSON_ValueGetType(
const char *json,
const char *jsonEnd)
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;
258 return JSONTYPE_VALUE;
261unsigned int JSON_ValueGetString(
const char *json,
const char *jsonEnd,
char *outString,
unsigned int stringLen)
263 const char *stringEnd, *stringStart;
272 stringEnd = JSON_SkipValue(stringStart, jsonEnd);
273 if (stringEnd >= jsonEnd)
280 if (*stringStart ==
'"')
283 if (*(stringEnd - 1) ==
'"')
287 if (stringLen > stringEnd - stringStart)
288 stringLen = stringEnd - stringStart;
292 *outString++ = *json++;
295 return stringEnd - stringStart;
298double JSON_ValueGetDouble(
const char *json,
const char *jsonEnd)
302 unsigned int np = JSON_NoParse(json, jsonEnd);
305 return (
double)(np - 1);
307 if (!JSON_ValueGetString(json, jsonEnd, cValue, 256))
310 sscanf(cValue,
"%lf", &dValue);
315float JSON_ValueGetFloat(
const char *json,
const char *jsonEnd)
319 unsigned int np = JSON_NoParse(json, jsonEnd);
322 return (
float)(np - 1);
324 if (!JSON_ValueGetString(json, jsonEnd, cValue, 256))
327 sscanf(cValue,
"%f", &fValue);
332int JSON_ValueGetInt(
const char *json,
const char *jsonEnd)
336 unsigned int np = JSON_NoParse(json, jsonEnd);
341 if (!JSON_ValueGetString(json, jsonEnd, cValue, 256))
344 sscanf(cValue,
"%d", &iValue);
351#undef IS_STRUCT_CLOSE