summaryrefslogtreecommitdiff
path: root/src/qcommon/json.h
blob: cfc5b3cac6897be557fd591cf279be1b000cffcd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*
===========================================================================
Copyright (C) 2016 James Canete

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
===========================================================================
*/

#ifndef JSON_H
#define JSON_H

enum
{
	JSONTYPE_STRING, // string
	JSONTYPE_OBJECT, // object
	JSONTYPE_ARRAY,  // array
	JSONTYPE_VALUE,  // number, true, false, or null
	JSONTYPE_ERROR   // out of data
};

// --------------------------------------------------------------------------
//   Array Functions
// --------------------------------------------------------------------------

// Get pointer to first value in array
// When given pointer to an array, returns pointer to the first
// returns NULL if array is empty or not an array.
const char *JSON_ArrayGetFirstValue(const char *json, const char *jsonEnd);

// Get pointer to next value in array
// When given pointer to a value, returns pointer to the next value
// returns NULL when no next value.
const char *JSON_ArrayGetNextValue(const char *json, const char *jsonEnd);

// Get pointers to values in an array
// returns 0 if not an array, array is empty, or out of data
// returns number of values in the array and copies into index if successful
unsigned int JSON_ArrayGetIndex(const char *json, const char *jsonEnd, const char **indexes, unsigned int numIndexes);

// Get pointer to indexed value from array
// returns NULL if not an array, no index, or out of data
const char *JSON_ArrayGetValue(const char *json, const char *jsonEnd, unsigned int index);

// --------------------------------------------------------------------------
//   Object Functions
// --------------------------------------------------------------------------

// Get pointer to named value from object
// returns NULL if not an object, name not found, or out of data
const char *JSON_ObjectGetNamedValue(const char *json, const char *jsonEnd, const char *name);

// --------------------------------------------------------------------------
//   Value Functions
// --------------------------------------------------------------------------

// Get type of value
// returns JSONTYPE_ERROR if out of data
unsigned int JSON_ValueGetType(const char *json, const char *jsonEnd);

// Get value as string
// returns 0 if out of data
// returns length and copies into string if successful, including terminating nul.
// string values are stripped of enclosing quotes but not escaped
unsigned int JSON_ValueGetString(const char *json, const char *jsonEnd, char *outString, unsigned int stringLen);

// Get value as appropriate type
// returns 0 if value is false, value is null, or out of data
// returns 1 if value is true
// returns value otherwise
double JSON_ValueGetDouble(const char *json, const char *jsonEnd);
float JSON_ValueGetFloat(const char *json, const char *jsonEnd);
int JSON_ValueGetInt(const char *json, const char *jsonEnd);

#endif

#ifdef JSON_IMPLEMENTATION
#include <stdio.h>

// --------------------------------------------------------------------------
//   Internal Functions
// --------------------------------------------------------------------------

static const char *JSON_SkipSeparators(const char *json, const char *jsonEnd);
static const char *JSON_SkipString(const char *json, const char *jsonEnd);
static const char *JSON_SkipStruct(const char *json, const char *jsonEnd);
static const char *JSON_SkipValue(const char *json, const char *jsonEnd);
static const char *JSON_SkipValueAndSeparators(const char *json, const char *jsonEnd);

#define IS_SEPARATOR(x)    ((x) == ' ' || (x) == '\t' || (x) == '\n' || (x) == '\r' || (x) == ',' || (x) == ':')
#define IS_STRUCT_OPEN(x)  ((x) == '{' || (x) == '[')
#define IS_STRUCT_CLOSE(x) ((x) == '}' || (x) == ']')

static const char *JSON_SkipSeparators(const char *json, const char *jsonEnd)
{
	while (json < jsonEnd && IS_SEPARATOR(*json))
		json++;

	return json;
}

static const char *JSON_SkipString(const char *json, const char *jsonEnd)
{
	for (json++; json < jsonEnd && *json != '"'; json++)
		if (*json == '\\')
			json++;

	return (json + 1 > jsonEnd) ? jsonEnd : json + 1;
}

static const char *JSON_SkipStruct(const char *json, const char *jsonEnd)
{
	json = JSON_SkipSeparators(json + 1, jsonEnd);
	while (json < jsonEnd && !IS_STRUCT_CLOSE(*json))
		json = JSON_SkipValueAndSeparators(json, jsonEnd);

	return (json + 1 > jsonEnd) ? jsonEnd : json + 1;
}

static const char *JSON_SkipValue(const char *json, const char *jsonEnd)
{
	if (json >= jsonEnd)
		return jsonEnd;
	else if (*json == '"')
		json = JSON_SkipString(json, jsonEnd);
	else if (IS_STRUCT_OPEN(*json))
		json = JSON_SkipStruct(json, jsonEnd);
	else
	{
		while (json < jsonEnd && !IS_SEPARATOR(*json) && !IS_STRUCT_CLOSE(*json))
			json++;
	}

	return json;
}

static const char *JSON_SkipValueAndSeparators(const char *json, const char *jsonEnd)
{
	json = JSON_SkipValue(json, jsonEnd);
	return JSON_SkipSeparators(json, jsonEnd);
}

// returns 0 if value requires more parsing, 1 if no more data/false/null, 2 if true
static unsigned int JSON_NoParse(const char *json, const char *jsonEnd)
{
	if (!json || json >= jsonEnd || *json == 'f' || *json == 'n')
		return 1;

	if (*json == 't')
		return 2;

	return 0;
}

// --------------------------------------------------------------------------
//   Array Functions
// --------------------------------------------------------------------------

const char *JSON_ArrayGetFirstValue(const char *json, const char *jsonEnd)
{
	if (!json || json >= jsonEnd || !IS_STRUCT_OPEN(*json))
		return NULL;

	json = JSON_SkipSeparators(json + 1, jsonEnd);

	return (json >= jsonEnd || IS_STRUCT_CLOSE(*json)) ? NULL : json;
}

const char *JSON_ArrayGetNextValue(const char *json, const char *jsonEnd)
{
	if (!json || json >= jsonEnd || IS_STRUCT_CLOSE(*json))
		return NULL;

	json = JSON_SkipValueAndSeparators(json, jsonEnd);

	return (json >= jsonEnd || IS_STRUCT_CLOSE(*json)) ? NULL : json;
}

unsigned int JSON_ArrayGetIndex(const char *json, const char *jsonEnd, const char **indexes, unsigned int numIndexes)
{
	unsigned int length = 0;

	for (json = JSON_ArrayGetFirstValue(json, jsonEnd); json; json = JSON_ArrayGetNextValue(json, jsonEnd))
	{
		if (indexes && numIndexes)
		{
			*indexes++ = json;
			numIndexes--;
		}
		length++;
	}

	return length;
}

const char *JSON_ArrayGetValue(const char *json, const char *jsonEnd, unsigned int index)
{
	for (json = JSON_ArrayGetFirstValue(json, jsonEnd); json && index; json = JSON_ArrayGetNextValue(json, jsonEnd))
		index--;

	return json;
}

// --------------------------------------------------------------------------
//   Object Functions
// --------------------------------------------------------------------------

const char *JSON_ObjectGetNamedValue(const char *json, const char *jsonEnd, const char *name)
{
	unsigned int nameLen = strlen(name);

	for (json = JSON_ArrayGetFirstValue(json, jsonEnd); json; json = JSON_ArrayGetNextValue(json, jsonEnd))
	{
		if (*json == '"')
		{
			const char *thisNameStart, *thisNameEnd;

			thisNameStart = json + 1;
			json = JSON_SkipString(json, jsonEnd);
			thisNameEnd = json - 1;
			json = JSON_SkipSeparators(json, jsonEnd);

			if ((unsigned int)(thisNameEnd - thisNameStart) == nameLen)
				if (strncmp(thisNameStart, name, nameLen) == 0)
					return json;
		}
	}

	return NULL;
}

// --------------------------------------------------------------------------
//   Value Functions
// --------------------------------------------------------------------------

unsigned int JSON_ValueGetType(const char *json, const char *jsonEnd)
{
	if (!json || json >= jsonEnd)
		return JSONTYPE_ERROR;
	else if (*json == '"')
		return JSONTYPE_STRING;
	else if (*json == '{')
		return JSONTYPE_OBJECT;
	else if (*json == '[')
		return JSONTYPE_ARRAY;

	return JSONTYPE_VALUE;
}

unsigned int JSON_ValueGetString(const char *json, const char *jsonEnd, char *outString, unsigned int stringLen)
{
	const char *stringEnd, *stringStart;

	if (!json)
	{
		*outString = '\0';
		return 0;
	}

	stringStart = json;
	stringEnd = JSON_SkipValue(stringStart, jsonEnd);
	if (stringEnd >= jsonEnd)
	{
		*outString = '\0';
		return 0;
	}

	// skip enclosing quotes if they exist
	if (*stringStart == '"')
		stringStart++;

	if (*(stringEnd - 1) == '"')
		stringEnd--;

	stringLen--;
	if (stringLen > stringEnd - stringStart)
		stringLen = stringEnd - stringStart;

	json = stringStart;
	while (stringLen--)
		*outString++ = *json++;
	*outString = '\0';

	return stringEnd - stringStart;
}

double JSON_ValueGetDouble(const char *json, const char *jsonEnd)
{
	char cValue[256];
	double dValue = 0.0;
	unsigned int np = JSON_NoParse(json, jsonEnd);

	if (np)
		return (double)(np - 1);

	if (!JSON_ValueGetString(json, jsonEnd, cValue, 256))
		return 0.0;

	sscanf(cValue, "%lf", &dValue);

	return dValue;
}

float JSON_ValueGetFloat(const char *json, const char *jsonEnd)
{
	char cValue[256];
	float fValue = 0.0f;
	unsigned int np = JSON_NoParse(json, jsonEnd);

	if (np)
		return (float)(np - 1);

	if (!JSON_ValueGetString(json, jsonEnd, cValue, 256))
		return 0.0f;

	sscanf(cValue, "%f", &fValue);

	return fValue;
}

int JSON_ValueGetInt(const char *json, const char *jsonEnd)
{
	char cValue[256];
	int iValue = 0;
	unsigned int np = JSON_NoParse(json, jsonEnd);

	if (np)
		return np - 1;

	if (!JSON_ValueGetString(json, jsonEnd, cValue, 256))
		return 0;

	sscanf(cValue, "%d", &iValue);

	return iValue;
}

#undef IS_SEPARATOR
#undef IS_STRUCT_OPEN
#undef IS_STRUCT_CLOSE

#endif