2015-06-29 17:25:34 +00:00
|
|
|
/*
|
|
|
|
Copyright (c) 2009 Dave Gamble
|
2021-09-04 03:55:09 +00:00
|
|
|
Copyright (c) 2015-2021 The Khronos Group Inc.
|
|
|
|
Copyright (c) 2015-2021 Valve Corporation
|
|
|
|
Copyright (c) 2015-2021 LunarG, Inc.
|
2015-06-29 17:25:34 +00:00
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* cJSON */
|
|
|
|
/* JSON parser in C. */
|
|
|
|
|
2021-09-05 05:23:28 +00:00
|
|
|
#include <ctype.h>
|
2015-06-29 17:25:34 +00:00
|
|
|
#include <float.h>
|
|
|
|
#include <limits.h>
|
2021-09-05 05:23:28 +00:00
|
|
|
#include <math.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2015-06-29 17:25:34 +00:00
|
|
|
#include "cJSON.h"
|
|
|
|
|
2021-09-04 21:38:53 +00:00
|
|
|
#include "allocation.h"
|
2021-09-04 19:33:38 +00:00
|
|
|
|
2022-05-21 01:40:53 +00:00
|
|
|
void *cJSON_malloc(const VkAllocationCallbacks *pAllocator, size_t size) {
|
|
|
|
return loader_alloc(pAllocator, size, VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
|
2021-09-04 19:33:38 +00:00
|
|
|
}
|
|
|
|
|
2022-05-21 01:40:53 +00:00
|
|
|
void cJSON_Free(const VkAllocationCallbacks *pAllocator, void *pMemory) { loader_free(pAllocator, pMemory); }
|
2021-09-04 19:33:38 +00:00
|
|
|
|
2015-06-29 17:25:34 +00:00
|
|
|
static const char *ep;
|
|
|
|
|
2016-02-03 00:47:28 +00:00
|
|
|
const char *cJSON_GetErrorPtr(void) { return ep; }
|
2015-06-29 17:25:34 +00:00
|
|
|
|
2022-05-21 01:40:53 +00:00
|
|
|
char *cJSON_strdup(const VkAllocationCallbacks *pAllocator, const char *str) {
|
2016-02-03 00:47:28 +00:00
|
|
|
size_t len;
|
|
|
|
char *copy;
|
2015-06-29 17:25:34 +00:00
|
|
|
|
2016-02-03 00:47:28 +00:00
|
|
|
len = strlen(str) + 1;
|
2022-05-21 01:40:53 +00:00
|
|
|
copy = (char *)cJSON_malloc(pAllocator, len);
|
2022-03-29 05:50:33 +00:00
|
|
|
if (!copy) return 0;
|
2016-02-03 00:47:28 +00:00
|
|
|
memcpy(copy, str, len);
|
|
|
|
return copy;
|
2015-06-29 17:25:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Internal constructor. */
|
2022-05-21 01:40:53 +00:00
|
|
|
cJSON *cJSON_New_Item(const VkAllocationCallbacks *pAllocator) {
|
|
|
|
cJSON *node = (cJSON *)cJSON_malloc(pAllocator, sizeof(cJSON));
|
|
|
|
if (node) {
|
|
|
|
memset(node, 0, sizeof(cJSON));
|
|
|
|
node->pAllocator = (VkAllocationCallbacks *)pAllocator;
|
|
|
|
}
|
2016-02-03 00:47:28 +00:00
|
|
|
return node;
|
2015-06-29 17:25:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Delete a cJSON structure. */
|
2022-05-21 01:40:53 +00:00
|
|
|
void cJSON_Delete(cJSON *c) {
|
2016-02-03 00:47:28 +00:00
|
|
|
cJSON *next;
|
|
|
|
while (c) {
|
|
|
|
next = c->next;
|
2022-05-21 01:40:53 +00:00
|
|
|
if (!(c->type & cJSON_IsReference) && c->child) cJSON_Delete(c->child);
|
|
|
|
if (!(c->type & cJSON_IsReference) && c->valuestring) cJSON_Free(c->pAllocator, c->valuestring);
|
|
|
|
if (!(c->type & cJSON_StringIsConst) && c->string) cJSON_Free(c->pAllocator, c->string);
|
|
|
|
cJSON_Free(c->pAllocator, c);
|
2016-02-03 00:47:28 +00:00
|
|
|
c = next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Parse the input text to generate a number, and populate the result into item.
|
|
|
|
*/
|
2021-09-04 19:33:38 +00:00
|
|
|
const char *parse_number(cJSON *item, const char *num) {
|
2016-02-03 00:47:28 +00:00
|
|
|
double n = 0, sign = 1, scale = 0;
|
|
|
|
int subscale = 0, signsubscale = 1;
|
|
|
|
|
2017-01-26 20:34:13 +00:00
|
|
|
if (*num == '-') sign = -1, num++; /* Has sign? */
|
|
|
|
if (*num == '0') num++; /* is zero */
|
|
|
|
if (*num >= '1' && *num <= '9') do
|
2016-02-03 00:47:28 +00:00
|
|
|
n = (n * 10.0) + (*num++ - '0');
|
|
|
|
while (*num >= '0' && *num <= '9'); /* Number? */
|
|
|
|
if (*num == '.' && num[1] >= '0' && num[1] <= '9') {
|
|
|
|
num++;
|
2021-08-10 20:35:29 +00:00
|
|
|
do n = (n * 10.0) + (*num++ - '0'), scale--;
|
2016-02-03 00:47:28 +00:00
|
|
|
while (*num >= '0' && *num <= '9');
|
|
|
|
} /* Fractional part? */
|
|
|
|
if (*num == 'e' || *num == 'E') /* Exponent? */
|
|
|
|
{
|
|
|
|
num++;
|
|
|
|
if (*num == '+')
|
|
|
|
num++;
|
|
|
|
else if (*num == '-')
|
2017-01-26 20:34:13 +00:00
|
|
|
signsubscale = -1, num++; /* With sign? */
|
|
|
|
while (*num >= '0' && *num <= '9') subscale = (subscale * 10) + (*num++ - '0'); /* Number? */
|
2016-02-03 00:47:28 +00:00
|
|
|
}
|
|
|
|
|
2017-01-26 19:16:30 +00:00
|
|
|
n = sign * n * pow(10.0, (scale + subscale * signsubscale)); /* number = +/-
|
|
|
|
number.fraction *
|
|
|
|
10^+/- exponent */
|
2016-02-03 00:47:28 +00:00
|
|
|
|
|
|
|
item->valuedouble = n;
|
|
|
|
item->valueint = (int)n;
|
|
|
|
item->type = cJSON_Number;
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
2021-09-04 19:33:38 +00:00
|
|
|
size_t pow2gt(size_t x) {
|
2016-02-03 00:47:28 +00:00
|
|
|
--x;
|
|
|
|
x |= x >> 1;
|
|
|
|
x |= x >> 2;
|
|
|
|
x |= x >> 4;
|
|
|
|
x |= x >> 8;
|
|
|
|
x |= x >> 16;
|
|
|
|
return x + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
char *buffer;
|
|
|
|
size_t length;
|
|
|
|
size_t offset;
|
|
|
|
} printbuffer;
|
|
|
|
|
2022-05-21 01:40:53 +00:00
|
|
|
char *ensure(const VkAllocationCallbacks *pAllocator, printbuffer *p, size_t needed) {
|
2016-02-03 00:47:28 +00:00
|
|
|
char *newbuffer;
|
|
|
|
size_t newsize;
|
2017-01-26 20:34:13 +00:00
|
|
|
if (!p || !p->buffer) return 0;
|
2016-02-03 00:47:28 +00:00
|
|
|
needed += p->offset;
|
2017-01-26 20:34:13 +00:00
|
|
|
if (needed <= p->length) return p->buffer + p->offset;
|
2016-02-03 00:47:28 +00:00
|
|
|
|
|
|
|
newsize = pow2gt(needed);
|
2022-05-21 01:40:53 +00:00
|
|
|
newbuffer = (char *)cJSON_malloc(pAllocator, newsize);
|
2016-02-03 00:47:28 +00:00
|
|
|
if (!newbuffer) {
|
2022-05-21 01:40:53 +00:00
|
|
|
cJSON_Free(pAllocator, p->buffer);
|
2016-02-03 00:47:28 +00:00
|
|
|
p->length = 0, p->buffer = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
2017-01-26 20:34:13 +00:00
|
|
|
if (newbuffer) memcpy(newbuffer, p->buffer, p->length);
|
2022-05-21 01:40:53 +00:00
|
|
|
cJSON_Free(pAllocator, p->buffer);
|
2016-02-03 00:47:28 +00:00
|
|
|
p->length = newsize;
|
|
|
|
p->buffer = newbuffer;
|
|
|
|
return newbuffer + p->offset;
|
|
|
|
}
|
|
|
|
|
2021-09-16 20:39:05 +00:00
|
|
|
size_t cJSON_update(printbuffer *p) {
|
2016-02-03 00:47:28 +00:00
|
|
|
char *str;
|
2017-01-26 20:34:13 +00:00
|
|
|
if (!p || !p->buffer) return 0;
|
2016-02-03 00:47:28 +00:00
|
|
|
str = p->buffer + p->offset;
|
|
|
|
return p->offset + strlen(str);
|
2015-06-29 17:25:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Render the number nicely from the given item into a string. */
|
2022-05-21 01:40:53 +00:00
|
|
|
char *print_number(cJSON *item, printbuffer *p) {
|
2016-02-03 00:47:28 +00:00
|
|
|
char *str = 0;
|
|
|
|
double d = item->valuedouble;
|
|
|
|
if (d == 0) {
|
|
|
|
if (p)
|
2022-05-21 01:40:53 +00:00
|
|
|
str = ensure(item->pAllocator, p, 2);
|
2016-02-03 00:47:28 +00:00
|
|
|
else
|
2022-05-21 01:40:53 +00:00
|
|
|
str = (char *)cJSON_malloc(item->pAllocator, 2); /* special case for 0. */
|
2017-01-26 20:34:13 +00:00
|
|
|
if (str) strcpy(str, "0");
|
2017-01-26 19:16:30 +00:00
|
|
|
} else if (fabs(((double)item->valueint) - d) <= DBL_EPSILON && d <= INT_MAX && d >= INT_MIN) {
|
2016-02-03 00:47:28 +00:00
|
|
|
if (p)
|
2022-05-21 01:40:53 +00:00
|
|
|
str = ensure(item->pAllocator, p, 21);
|
2016-02-03 00:47:28 +00:00
|
|
|
else
|
2022-05-21 01:40:53 +00:00
|
|
|
str = (char *)cJSON_malloc(item->pAllocator, 21); /* 2^64+1 can be represented in 21 chars. */
|
2017-01-26 20:34:13 +00:00
|
|
|
if (str) sprintf(str, "%d", item->valueint);
|
2016-02-03 00:47:28 +00:00
|
|
|
} else {
|
|
|
|
if (p)
|
2022-05-21 01:40:53 +00:00
|
|
|
str = ensure(item->pAllocator, p, 64);
|
2016-02-03 00:47:28 +00:00
|
|
|
else
|
2022-05-21 01:40:53 +00:00
|
|
|
str = (char *)cJSON_malloc(item->pAllocator, 64); /* This is a nice tradeoff. */
|
2016-02-03 00:47:28 +00:00
|
|
|
if (str) {
|
|
|
|
if (fabs(floor(d) - d) <= DBL_EPSILON && fabs(d) < 1.0e60)
|
|
|
|
sprintf(str, "%.0f", d);
|
|
|
|
else if (fabs(d) < 1.0e-6 || fabs(d) > 1.0e9)
|
|
|
|
sprintf(str, "%e", d);
|
|
|
|
else
|
|
|
|
sprintf(str, "%f", d);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2021-09-04 19:33:38 +00:00
|
|
|
unsigned parse_hex4(const char *str) {
|
2016-02-03 00:47:28 +00:00
|
|
|
unsigned h = 0;
|
|
|
|
if (*str >= '0' && *str <= '9')
|
|
|
|
h += (*str) - '0';
|
|
|
|
else if (*str >= 'A' && *str <= 'F')
|
|
|
|
h += 10 + (*str) - 'A';
|
|
|
|
else if (*str >= 'a' && *str <= 'f')
|
|
|
|
h += 10 + (*str) - 'a';
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
h = h << 4;
|
|
|
|
str++;
|
|
|
|
if (*str >= '0' && *str <= '9')
|
|
|
|
h += (*str) - '0';
|
|
|
|
else if (*str >= 'A' && *str <= 'F')
|
|
|
|
h += 10 + (*str) - 'A';
|
|
|
|
else if (*str >= 'a' && *str <= 'f')
|
|
|
|
h += 10 + (*str) - 'a';
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
h = h << 4;
|
|
|
|
str++;
|
|
|
|
if (*str >= '0' && *str <= '9')
|
|
|
|
h += (*str) - '0';
|
|
|
|
else if (*str >= 'A' && *str <= 'F')
|
|
|
|
h += 10 + (*str) - 'A';
|
|
|
|
else if (*str >= 'a' && *str <= 'f')
|
|
|
|
h += 10 + (*str) - 'a';
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
h = h << 4;
|
|
|
|
str++;
|
|
|
|
if (*str >= '0' && *str <= '9')
|
|
|
|
h += (*str) - '0';
|
|
|
|
else if (*str >= 'A' && *str <= 'F')
|
|
|
|
h += 10 + (*str) - 'A';
|
|
|
|
else if (*str >= 'a' && *str <= 'f')
|
|
|
|
h += 10 + (*str) - 'a';
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
return h;
|
2015-06-29 17:25:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Parse the input text into an unescaped cstring, and populate item. */
|
2017-01-26 19:16:30 +00:00
|
|
|
static const unsigned char firstByteMark[7] = {0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC};
|
2022-05-21 01:40:53 +00:00
|
|
|
const char *parse_string(cJSON *item, const char *str) {
|
2016-02-03 00:47:28 +00:00
|
|
|
const char *ptr = str + 1;
|
|
|
|
char *ptr2;
|
|
|
|
char *out;
|
|
|
|
int len = 0;
|
|
|
|
unsigned uc, uc2;
|
|
|
|
if (*str != '\"') {
|
|
|
|
ep = str;
|
|
|
|
return 0;
|
|
|
|
} /* not a string! */
|
|
|
|
|
|
|
|
while (*ptr != '\"' && *ptr && ++len)
|
2017-01-26 20:34:13 +00:00
|
|
|
if (*ptr++ == '\\') ptr++; /* Skip escaped quotes. */
|
2016-02-03 00:47:28 +00:00
|
|
|
|
2022-05-21 01:40:53 +00:00
|
|
|
out = (char *)cJSON_malloc(item->pAllocator, len + 1); /* This is how long we need for the string, roughly. */
|
2017-01-26 20:34:13 +00:00
|
|
|
if (!out) return 0;
|
2016-02-03 00:47:28 +00:00
|
|
|
|
|
|
|
ptr = str + 1;
|
|
|
|
ptr2 = out;
|
|
|
|
while (*ptr != '\"' && *ptr) {
|
|
|
|
if (*ptr != '\\')
|
|
|
|
*ptr2++ = *ptr++;
|
|
|
|
else {
|
|
|
|
ptr++;
|
|
|
|
switch (*ptr) {
|
2017-01-26 20:34:13 +00:00
|
|
|
case 'b':
|
|
|
|
*ptr2++ = '\b';
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
*ptr2++ = '\f';
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
*ptr2++ = '\n';
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
*ptr2++ = '\r';
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
*ptr2++ = '\t';
|
|
|
|
break;
|
|
|
|
case 'u': /* transcode utf16 to utf8. */
|
|
|
|
uc = parse_hex4(ptr + 1);
|
|
|
|
ptr += 4; /* get the unicode char. */
|
|
|
|
|
|
|
|
if ((uc >= 0xDC00 && uc <= 0xDFFF) || uc == 0) break; /* check for invalid. */
|
|
|
|
|
|
|
|
if (uc >= 0xD800 && uc <= 0xDBFF) /* UTF16 surrogate pairs. */
|
|
|
|
{
|
|
|
|
if (ptr[1] != '\\' || ptr[2] != 'u') break; /* missing second-half of surrogate. */
|
|
|
|
uc2 = parse_hex4(ptr + 3);
|
|
|
|
ptr += 6;
|
|
|
|
if (uc2 < 0xDC00 || uc2 > 0xDFFF) break; /* invalid second-half of surrogate. */
|
|
|
|
uc = 0x10000 + (((uc & 0x3FF) << 10) | (uc2 & 0x3FF));
|
|
|
|
}
|
|
|
|
|
|
|
|
len = 4;
|
|
|
|
if (uc < 0x80)
|
|
|
|
len = 1;
|
|
|
|
else if (uc < 0x800)
|
|
|
|
len = 2;
|
|
|
|
else if (uc < 0x10000)
|
|
|
|
len = 3;
|
|
|
|
ptr2 += len;
|
|
|
|
|
|
|
|
switch (len) {
|
|
|
|
case 4:
|
|
|
|
*--ptr2 = ((uc | 0x80) & 0xBF);
|
|
|
|
uc >>= 6;
|
2018-04-17 16:05:20 +00:00
|
|
|
// fall through
|
2017-01-26 20:34:13 +00:00
|
|
|
case 3:
|
|
|
|
*--ptr2 = ((uc | 0x80) & 0xBF);
|
|
|
|
uc >>= 6;
|
2018-04-17 16:05:20 +00:00
|
|
|
// fall through
|
2017-01-26 20:34:13 +00:00
|
|
|
case 2:
|
|
|
|
*--ptr2 = ((uc | 0x80) & 0xBF);
|
|
|
|
uc >>= 6;
|
2018-04-17 16:05:20 +00:00
|
|
|
// fall through
|
2017-01-26 20:34:13 +00:00
|
|
|
case 1:
|
|
|
|
*--ptr2 = ((unsigned char)uc | firstByteMark[len]);
|
|
|
|
}
|
|
|
|
ptr2 += len;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
*ptr2++ = *ptr;
|
|
|
|
break;
|
2016-02-03 00:47:28 +00:00
|
|
|
}
|
|
|
|
ptr++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*ptr2 = 0;
|
2017-01-26 20:34:13 +00:00
|
|
|
if (*ptr == '\"') ptr++;
|
2016-02-03 00:47:28 +00:00
|
|
|
item->valuestring = out;
|
|
|
|
item->type = cJSON_String;
|
|
|
|
return ptr;
|
2015-06-29 17:25:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Render the cstring provided to an escaped version that can be printed. */
|
2022-05-21 01:40:53 +00:00
|
|
|
char *print_string_ptr(const VkAllocationCallbacks *pAllocator, const char *str, printbuffer *p) {
|
2016-02-03 00:47:28 +00:00
|
|
|
const char *ptr;
|
|
|
|
char *ptr2;
|
|
|
|
char *out;
|
|
|
|
size_t len = 0, flag = 0;
|
|
|
|
unsigned char token;
|
|
|
|
|
2017-01-26 20:34:13 +00:00
|
|
|
for (ptr = str; *ptr; ptr++) flag |= ((*ptr > 0 && *ptr < 32) || (*ptr == '\"') || (*ptr == '\\')) ? 1 : 0;
|
2016-02-03 00:47:28 +00:00
|
|
|
if (!flag) {
|
|
|
|
len = ptr - str;
|
|
|
|
if (p)
|
2022-05-21 01:40:53 +00:00
|
|
|
out = ensure(pAllocator, p, len + 3);
|
2016-02-03 00:47:28 +00:00
|
|
|
else
|
2022-05-21 01:40:53 +00:00
|
|
|
out = (char *)cJSON_malloc(pAllocator, len + 3);
|
2017-01-26 20:34:13 +00:00
|
|
|
if (!out) return 0;
|
2016-02-03 00:47:28 +00:00
|
|
|
ptr2 = out;
|
|
|
|
*ptr2++ = '\"';
|
|
|
|
strcpy(ptr2, str);
|
|
|
|
ptr2[len] = '\"';
|
|
|
|
ptr2[len + 1] = 0;
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!str) {
|
|
|
|
if (p)
|
2022-05-21 01:40:53 +00:00
|
|
|
out = ensure(pAllocator, p, 3);
|
2016-02-03 00:47:28 +00:00
|
|
|
else
|
2022-05-21 01:40:53 +00:00
|
|
|
out = (char *)cJSON_malloc(pAllocator, 3);
|
2017-01-26 20:34:13 +00:00
|
|
|
if (!out) return 0;
|
2016-02-03 00:47:28 +00:00
|
|
|
strcpy(out, "\"\"");
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
ptr = str;
|
2022-03-29 05:50:33 +00:00
|
|
|
token = *ptr;
|
|
|
|
while (token && ++len) {
|
2016-02-03 00:47:28 +00:00
|
|
|
if (strchr("\"\\\b\f\n\r\t", token))
|
|
|
|
len++;
|
|
|
|
else if (token < 32)
|
|
|
|
len += 5;
|
|
|
|
ptr++;
|
2022-03-29 05:50:33 +00:00
|
|
|
token = *ptr;
|
2016-02-03 00:47:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (p)
|
2022-05-21 01:40:53 +00:00
|
|
|
out = ensure(pAllocator, p, len + 3);
|
2016-02-03 00:47:28 +00:00
|
|
|
else
|
2022-05-21 01:40:53 +00:00
|
|
|
out = (char *)cJSON_malloc(pAllocator, len + 3);
|
2017-01-26 20:34:13 +00:00
|
|
|
if (!out) return 0;
|
2016-02-03 00:47:28 +00:00
|
|
|
|
|
|
|
ptr2 = out;
|
|
|
|
ptr = str;
|
|
|
|
*ptr2++ = '\"';
|
|
|
|
while (*ptr) {
|
|
|
|
if ((unsigned char)*ptr > 31 && *ptr != '\"' && *ptr != '\\')
|
|
|
|
*ptr2++ = *ptr++;
|
|
|
|
else {
|
|
|
|
switch (token = *ptr++) {
|
2017-01-26 20:34:13 +00:00
|
|
|
case '\\':
|
|
|
|
*ptr2++ = '\\';
|
|
|
|
break;
|
|
|
|
case '\"':
|
|
|
|
*ptr2++ = '\"';
|
|
|
|
break;
|
|
|
|
case '\b':
|
|
|
|
*ptr2++ = '\b';
|
|
|
|
break;
|
|
|
|
case '\f':
|
|
|
|
*ptr2++ = '\f';
|
|
|
|
break;
|
|
|
|
case '\n':
|
|
|
|
*ptr2++ = '\n';
|
|
|
|
break;
|
|
|
|
case '\r':
|
|
|
|
*ptr2++ = '\r';
|
|
|
|
break;
|
|
|
|
case '\t':
|
|
|
|
*ptr2++ = '\t';
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
sprintf(ptr2, "u%04x", token);
|
|
|
|
ptr2 += 5;
|
|
|
|
break; /* escape and print */
|
2016-02-03 00:47:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*ptr2++ = '\"';
|
|
|
|
*ptr2++ = 0;
|
|
|
|
return out;
|
2015-06-29 17:25:34 +00:00
|
|
|
}
|
2022-04-07 17:39:01 +00:00
|
|
|
/* Invoke print_string_ptr (which is useful) on an item. */
|
2022-05-21 01:40:53 +00:00
|
|
|
char *print_string(cJSON *item, printbuffer *p) { return print_string_ptr(item->pAllocator, item->valuestring, p); }
|
2015-06-29 17:25:34 +00:00
|
|
|
|
|
|
|
/* Predeclare these prototypes. */
|
2022-05-21 01:40:53 +00:00
|
|
|
const char *parse_value(cJSON *item, const char *value);
|
|
|
|
char *print_value(cJSON *item, int depth, int fmt, printbuffer *p);
|
|
|
|
const char *parse_array(cJSON *item, const char *value);
|
|
|
|
char *print_array(cJSON *item, int depth, int fmt, printbuffer *p);
|
|
|
|
const char *parse_object(cJSON *item, const char *value);
|
|
|
|
char *print_object(cJSON *item, int depth, int fmt, printbuffer *p);
|
2015-06-29 17:25:34 +00:00
|
|
|
|
|
|
|
/* Utility to jump whitespace and cr/lf */
|
2021-09-04 19:33:38 +00:00
|
|
|
const char *skip(const char *in) {
|
2017-01-26 20:34:13 +00:00
|
|
|
while (in && *in && (unsigned char)*in <= 32) in++;
|
2016-02-03 00:47:28 +00:00
|
|
|
return in;
|
|
|
|
}
|
2015-06-29 17:25:34 +00:00
|
|
|
|
|
|
|
/* Parse an object - create a new root, and populate. */
|
2022-05-21 01:40:53 +00:00
|
|
|
cJSON *cJSON_ParseWithOpts(const VkAllocationCallbacks *pAllocator, const char *value, const char **return_parse_end,
|
2021-09-04 19:33:38 +00:00
|
|
|
int require_null_terminated) {
|
2016-02-03 00:47:28 +00:00
|
|
|
const char *end = 0;
|
2022-05-21 01:40:53 +00:00
|
|
|
cJSON *c = cJSON_New_Item(pAllocator);
|
2016-02-03 00:47:28 +00:00
|
|
|
ep = 0;
|
2017-01-26 20:34:13 +00:00
|
|
|
if (!c) return 0; /* memory fail */
|
2015-06-29 17:25:34 +00:00
|
|
|
|
2022-05-21 01:40:53 +00:00
|
|
|
end = parse_value(c, skip(value));
|
2016-02-03 00:47:28 +00:00
|
|
|
if (!end) {
|
2022-05-21 01:40:53 +00:00
|
|
|
cJSON_Delete(c);
|
2016-02-03 00:47:28 +00:00
|
|
|
return 0;
|
|
|
|
} /* parse failure. ep is set. */
|
2015-06-29 17:25:34 +00:00
|
|
|
|
2016-02-03 00:47:28 +00:00
|
|
|
/* if we require null-terminated JSON without appended garbage, skip and
|
|
|
|
* then check for a null terminator */
|
|
|
|
if (require_null_terminated) {
|
|
|
|
end = skip(end);
|
|
|
|
if (*end) {
|
2022-05-21 01:40:53 +00:00
|
|
|
cJSON_Delete(c);
|
2016-02-03 00:47:28 +00:00
|
|
|
ep = end;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2017-01-26 20:34:13 +00:00
|
|
|
if (return_parse_end) *return_parse_end = end;
|
2016-02-03 00:47:28 +00:00
|
|
|
return c;
|
2015-06-29 17:25:34 +00:00
|
|
|
}
|
|
|
|
/* Default options for cJSON_Parse */
|
2022-05-21 01:40:53 +00:00
|
|
|
cJSON *cJSON_Parse(const VkAllocationCallbacks *pAllocator, const char *value) {
|
|
|
|
return cJSON_ParseWithOpts(pAllocator, value, 0, 0);
|
|
|
|
}
|
2015-06-29 17:25:34 +00:00
|
|
|
|
|
|
|
/* Render a cJSON item/entity/structure to text. */
|
2022-05-21 01:40:53 +00:00
|
|
|
char *cJSON_Print(cJSON *item) { return print_value(item, 0, 1, 0); }
|
|
|
|
char *cJSON_PrintUnformatted(cJSON *item) { return print_value(item, 0, 0, 0); }
|
2015-06-29 17:25:34 +00:00
|
|
|
|
2022-05-21 01:40:53 +00:00
|
|
|
char *cJSON_PrintBuffered(cJSON *item, int prebuffer, int fmt) {
|
2016-02-03 00:47:28 +00:00
|
|
|
printbuffer p;
|
2022-05-21 01:40:53 +00:00
|
|
|
p.buffer = (char *)cJSON_malloc(item->pAllocator, prebuffer);
|
2016-02-03 00:47:28 +00:00
|
|
|
p.length = prebuffer;
|
|
|
|
p.offset = 0;
|
2022-05-21 01:40:53 +00:00
|
|
|
return print_value(item, 0, fmt, &p);
|
2015-06-29 17:25:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Parser core - when encountering text, process appropriately. */
|
2022-05-21 01:40:53 +00:00
|
|
|
const char *parse_value(cJSON *item, const char *value) {
|
2017-01-26 20:34:13 +00:00
|
|
|
if (!value) return 0; /* Fail on null. */
|
2016-02-03 00:47:28 +00:00
|
|
|
if (!strncmp(value, "null", 4)) {
|
|
|
|
item->type = cJSON_NULL;
|
|
|
|
return value + 4;
|
|
|
|
}
|
|
|
|
if (!strncmp(value, "false", 5)) {
|
|
|
|
item->type = cJSON_False;
|
|
|
|
return value + 5;
|
|
|
|
}
|
|
|
|
if (!strncmp(value, "true", 4)) {
|
|
|
|
item->type = cJSON_True;
|
|
|
|
item->valueint = 1;
|
|
|
|
return value + 4;
|
|
|
|
}
|
|
|
|
if (*value == '\"') {
|
2022-05-21 01:40:53 +00:00
|
|
|
return parse_string(item, value);
|
2016-02-03 00:47:28 +00:00
|
|
|
}
|
|
|
|
if (*value == '-' || (*value >= '0' && *value <= '9')) {
|
|
|
|
return parse_number(item, value);
|
|
|
|
}
|
|
|
|
if (*value == '[') {
|
2022-05-21 01:40:53 +00:00
|
|
|
return parse_array(item, value);
|
2016-02-03 00:47:28 +00:00
|
|
|
}
|
|
|
|
if (*value == '{') {
|
2022-05-21 01:40:53 +00:00
|
|
|
return parse_object(item, value);
|
2016-02-03 00:47:28 +00:00
|
|
|
}
|
2015-06-29 17:25:34 +00:00
|
|
|
|
2016-02-03 00:47:28 +00:00
|
|
|
ep = value;
|
|
|
|
return 0; /* failure. */
|
2015-06-29 17:25:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Render a value to text. */
|
2022-05-21 01:40:53 +00:00
|
|
|
char *print_value(cJSON *item, int depth, int fmt, printbuffer *p) {
|
2016-02-03 00:47:28 +00:00
|
|
|
char *out = 0;
|
2017-01-26 20:34:13 +00:00
|
|
|
if (!item) return 0;
|
2016-02-03 00:47:28 +00:00
|
|
|
if (p) {
|
|
|
|
switch ((item->type) & 255) {
|
2017-01-26 20:34:13 +00:00
|
|
|
case cJSON_NULL: {
|
2022-05-21 01:40:53 +00:00
|
|
|
out = ensure(item->pAllocator, p, 5);
|
2017-01-26 20:34:13 +00:00
|
|
|
if (out) strcpy(out, "null");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case cJSON_False: {
|
2022-05-21 01:40:53 +00:00
|
|
|
out = ensure(item->pAllocator, p, 6);
|
2017-01-26 20:34:13 +00:00
|
|
|
if (out) strcpy(out, "false");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case cJSON_True: {
|
2022-05-21 01:40:53 +00:00
|
|
|
out = ensure(item->pAllocator, p, 5);
|
2017-01-26 20:34:13 +00:00
|
|
|
if (out) strcpy(out, "true");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case cJSON_Number:
|
2022-05-21 01:40:53 +00:00
|
|
|
out = print_number(item, p);
|
2017-01-26 20:34:13 +00:00
|
|
|
break;
|
|
|
|
case cJSON_String:
|
2022-05-21 01:40:53 +00:00
|
|
|
out = print_string(item, p);
|
2017-01-26 20:34:13 +00:00
|
|
|
break;
|
|
|
|
case cJSON_Array:
|
2022-05-21 01:40:53 +00:00
|
|
|
out = print_array(item, depth, fmt, p);
|
2017-01-26 20:34:13 +00:00
|
|
|
break;
|
|
|
|
case cJSON_Object:
|
2022-05-21 01:40:53 +00:00
|
|
|
out = print_object(item, depth, fmt, p);
|
2017-01-26 20:34:13 +00:00
|
|
|
break;
|
2016-02-03 00:47:28 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
switch ((item->type) & 255) {
|
2017-01-26 20:34:13 +00:00
|
|
|
case cJSON_NULL:
|
2022-05-21 01:40:53 +00:00
|
|
|
out = cJSON_strdup(item->pAllocator, "null");
|
2017-01-26 20:34:13 +00:00
|
|
|
break;
|
|
|
|
case cJSON_False:
|
2022-05-21 01:40:53 +00:00
|
|
|
out = cJSON_strdup(item->pAllocator, "false");
|
2017-01-26 20:34:13 +00:00
|
|
|
break;
|
|
|
|
case cJSON_True:
|
2022-05-21 01:40:53 +00:00
|
|
|
out = cJSON_strdup(item->pAllocator, "true");
|
2017-01-26 20:34:13 +00:00
|
|
|
break;
|
|
|
|
case cJSON_Number:
|
2022-05-21 01:40:53 +00:00
|
|
|
out = print_number(item, 0);
|
2017-01-26 20:34:13 +00:00
|
|
|
break;
|
|
|
|
case cJSON_String:
|
2022-05-21 01:40:53 +00:00
|
|
|
out = print_string(item, 0);
|
2017-01-26 20:34:13 +00:00
|
|
|
break;
|
|
|
|
case cJSON_Array:
|
2022-05-21 01:40:53 +00:00
|
|
|
out = print_array(item, depth, fmt, 0);
|
2017-01-26 20:34:13 +00:00
|
|
|
break;
|
|
|
|
case cJSON_Object:
|
2022-05-21 01:40:53 +00:00
|
|
|
out = print_object(item, depth, fmt, 0);
|
2017-01-26 20:34:13 +00:00
|
|
|
break;
|
2016-02-03 00:47:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return out;
|
2015-06-29 17:25:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Build an array from input text. */
|
2022-05-21 01:40:53 +00:00
|
|
|
const char *parse_array(cJSON *item, const char *value) {
|
2016-02-03 00:47:28 +00:00
|
|
|
cJSON *child;
|
|
|
|
if (*value != '[') {
|
|
|
|
ep = value;
|
|
|
|
return 0;
|
|
|
|
} /* not an array! */
|
2015-06-29 17:25:34 +00:00
|
|
|
|
2016-02-03 00:47:28 +00:00
|
|
|
item->type = cJSON_Array;
|
|
|
|
value = skip(value + 1);
|
2017-01-26 20:34:13 +00:00
|
|
|
if (*value == ']') return value + 1; /* empty array. */
|
2015-06-29 17:25:34 +00:00
|
|
|
|
2022-05-21 01:40:53 +00:00
|
|
|
item->child = child = cJSON_New_Item(item->pAllocator);
|
|
|
|
if (!item->child) return 0; /* memory fail */
|
|
|
|
value = skip(parse_value(child, skip(value))); /* skip any spacing, get the value. */
|
2017-01-26 20:34:13 +00:00
|
|
|
if (!value) return 0;
|
2015-06-29 17:25:34 +00:00
|
|
|
|
2016-02-03 00:47:28 +00:00
|
|
|
while (*value == ',') {
|
|
|
|
cJSON *new_item;
|
2022-05-21 01:40:53 +00:00
|
|
|
new_item = cJSON_New_Item(item->pAllocator);
|
2022-03-29 05:50:33 +00:00
|
|
|
if (!new_item) return 0; /* memory fail */
|
2016-02-03 00:47:28 +00:00
|
|
|
child->next = new_item;
|
|
|
|
new_item->prev = child;
|
|
|
|
child = new_item;
|
2022-05-21 01:40:53 +00:00
|
|
|
value = skip(parse_value(child, skip(value + 1)));
|
2017-01-26 20:34:13 +00:00
|
|
|
if (!value) return 0; /* memory fail */
|
2016-02-03 00:47:28 +00:00
|
|
|
}
|
2015-06-29 17:25:34 +00:00
|
|
|
|
2017-01-26 20:34:13 +00:00
|
|
|
if (*value == ']') return value + 1; /* end of array */
|
2016-02-03 00:47:28 +00:00
|
|
|
ep = value;
|
|
|
|
return 0; /* malformed. */
|
2015-06-29 17:25:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Render an array to text */
|
2022-05-21 01:40:53 +00:00
|
|
|
char *print_array(cJSON *item, int depth, int fmt, printbuffer *p) {
|
2016-02-03 00:47:28 +00:00
|
|
|
char **entries;
|
|
|
|
char *out = 0, *ptr, *ret;
|
|
|
|
size_t len = 5;
|
|
|
|
cJSON *child = item->child;
|
|
|
|
int numentries = 0, fail = 0, j = 0;
|
|
|
|
size_t tmplen = 0, i = 0;
|
|
|
|
|
|
|
|
/* How many entries in the array? */
|
2017-01-26 20:34:13 +00:00
|
|
|
while (child) numentries++, child = child->next;
|
2016-02-03 00:47:28 +00:00
|
|
|
/* Explicitly handle numentries==0 */
|
|
|
|
if (!numentries) {
|
|
|
|
if (p)
|
2022-05-21 01:40:53 +00:00
|
|
|
out = ensure(item->pAllocator, p, 3);
|
2016-02-03 00:47:28 +00:00
|
|
|
else
|
2022-05-21 01:40:53 +00:00
|
|
|
out = (char *)cJSON_malloc(item->pAllocator, 3);
|
2017-01-26 20:34:13 +00:00
|
|
|
if (out) strcpy(out, "[]");
|
2016-02-03 00:47:28 +00:00
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p) {
|
|
|
|
/* Compose the output array. */
|
|
|
|
i = p->offset;
|
2022-05-21 01:40:53 +00:00
|
|
|
ptr = ensure(item->pAllocator, p, 1);
|
2017-01-26 20:34:13 +00:00
|
|
|
if (!ptr) return 0;
|
2016-02-03 00:47:28 +00:00
|
|
|
*ptr = '[';
|
|
|
|
p->offset++;
|
|
|
|
child = item->child;
|
|
|
|
while (child && !fail) {
|
2022-05-21 01:40:53 +00:00
|
|
|
print_value(child, depth + 1, fmt, p);
|
2021-09-16 20:39:05 +00:00
|
|
|
p->offset = cJSON_update(p);
|
2016-02-03 00:47:28 +00:00
|
|
|
if (child->next) {
|
|
|
|
len = fmt ? 2 : 1;
|
2022-05-21 01:40:53 +00:00
|
|
|
ptr = ensure(item->pAllocator, p, len + 1);
|
2017-01-26 20:34:13 +00:00
|
|
|
if (!ptr) return 0;
|
2016-02-03 00:47:28 +00:00
|
|
|
*ptr++ = ',';
|
2017-01-26 20:34:13 +00:00
|
|
|
if (fmt) *ptr++ = ' ';
|
2016-02-03 00:47:28 +00:00
|
|
|
*ptr = 0;
|
|
|
|
p->offset += len;
|
|
|
|
}
|
|
|
|
child = child->next;
|
|
|
|
}
|
2022-05-21 01:40:53 +00:00
|
|
|
ptr = ensure(item->pAllocator, p, 2);
|
2017-01-26 20:34:13 +00:00
|
|
|
if (!ptr) return 0;
|
2016-02-03 00:47:28 +00:00
|
|
|
*ptr++ = ']';
|
|
|
|
*ptr = 0;
|
|
|
|
out = (p->buffer) + i;
|
|
|
|
} else {
|
|
|
|
/* Allocate an array to hold the values for each */
|
2022-05-21 01:40:53 +00:00
|
|
|
entries = (char **)cJSON_malloc(item->pAllocator, numentries * sizeof(char *));
|
2017-01-26 20:34:13 +00:00
|
|
|
if (!entries) return 0;
|
2016-02-03 00:47:28 +00:00
|
|
|
memset(entries, 0, numentries * sizeof(char *));
|
|
|
|
/* Retrieve all the results: */
|
|
|
|
child = item->child;
|
|
|
|
while (child && !fail) {
|
2022-05-21 01:40:53 +00:00
|
|
|
ret = print_value(child, depth + 1, fmt, 0);
|
2016-02-03 00:47:28 +00:00
|
|
|
entries[i++] = ret;
|
|
|
|
if (ret)
|
|
|
|
len += strlen(ret) + 2 + (fmt ? 1 : 0);
|
|
|
|
else
|
|
|
|
fail = 1;
|
|
|
|
child = child->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If we didn't fail, try to malloc the output string */
|
2022-05-21 01:40:53 +00:00
|
|
|
if (!fail) out = (char *)cJSON_malloc(item->pAllocator, len);
|
2016-02-03 00:47:28 +00:00
|
|
|
/* If that fails, we fail. */
|
2017-01-26 20:34:13 +00:00
|
|
|
if (!out) fail = 1;
|
2016-02-03 00:47:28 +00:00
|
|
|
|
|
|
|
/* Handle failure. */
|
|
|
|
if (fail) {
|
|
|
|
for (j = 0; j < numentries; j++)
|
2022-05-21 01:40:53 +00:00
|
|
|
if (entries[j]) cJSON_Free(item->pAllocator, entries[j]);
|
|
|
|
cJSON_Free(item->pAllocator, entries);
|
2016-02-03 00:47:28 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Compose the output array. */
|
|
|
|
*out = '[';
|
|
|
|
ptr = out + 1;
|
|
|
|
*ptr = 0;
|
|
|
|
for (j = 0; j < numentries; j++) {
|
|
|
|
tmplen = strlen(entries[j]);
|
|
|
|
memcpy(ptr, entries[j], tmplen);
|
|
|
|
ptr += tmplen;
|
|
|
|
if (j != numentries - 1) {
|
|
|
|
*ptr++ = ',';
|
2017-01-26 20:34:13 +00:00
|
|
|
if (fmt) *ptr++ = ' ';
|
2016-02-03 00:47:28 +00:00
|
|
|
*ptr = 0;
|
|
|
|
}
|
2022-05-21 01:40:53 +00:00
|
|
|
cJSON_Free(item->pAllocator, entries[j]);
|
2016-02-03 00:47:28 +00:00
|
|
|
}
|
2022-05-21 01:40:53 +00:00
|
|
|
cJSON_Free(item->pAllocator, entries);
|
2016-02-03 00:47:28 +00:00
|
|
|
*ptr++ = ']';
|
|
|
|
*ptr++ = 0;
|
|
|
|
}
|
|
|
|
return out;
|
2015-06-29 17:25:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Build an object from the text. */
|
2022-05-21 01:40:53 +00:00
|
|
|
const char *parse_object(cJSON *item, const char *value) {
|
2016-02-03 00:47:28 +00:00
|
|
|
cJSON *child;
|
|
|
|
if (*value != '{') {
|
|
|
|
ep = value;
|
|
|
|
return 0;
|
|
|
|
} /* not an object! */
|
|
|
|
|
|
|
|
item->type = cJSON_Object;
|
|
|
|
value = skip(value + 1);
|
2017-01-26 20:34:13 +00:00
|
|
|
if (*value == '}') return value + 1; /* empty array. */
|
2016-02-03 00:47:28 +00:00
|
|
|
|
2022-05-21 01:40:53 +00:00
|
|
|
item->child = child = cJSON_New_Item(item->pAllocator);
|
2017-01-26 20:34:13 +00:00
|
|
|
if (!item->child) return 0;
|
2022-05-21 01:40:53 +00:00
|
|
|
value = skip(parse_string(child, skip(value)));
|
2017-01-26 20:34:13 +00:00
|
|
|
if (!value) return 0;
|
2016-02-03 00:47:28 +00:00
|
|
|
child->string = child->valuestring;
|
|
|
|
child->valuestring = 0;
|
|
|
|
if (*value != ':') {
|
|
|
|
ep = value;
|
|
|
|
return 0;
|
2022-05-21 01:40:53 +00:00
|
|
|
} /* fail! */
|
|
|
|
value = skip(parse_value(child, skip(value + 1))); /* skip any spacing, get the value. */
|
2017-01-26 20:34:13 +00:00
|
|
|
if (!value) return 0;
|
2016-02-03 00:47:28 +00:00
|
|
|
|
|
|
|
while (*value == ',') {
|
|
|
|
cJSON *new_item;
|
2022-05-21 01:40:53 +00:00
|
|
|
new_item = cJSON_New_Item(item->pAllocator);
|
2022-03-29 05:50:33 +00:00
|
|
|
if (!new_item) return 0; /* memory fail */
|
2016-02-03 00:47:28 +00:00
|
|
|
child->next = new_item;
|
|
|
|
new_item->prev = child;
|
|
|
|
child = new_item;
|
2022-05-21 01:40:53 +00:00
|
|
|
value = skip(parse_string(child, skip(value + 1)));
|
2017-01-26 20:34:13 +00:00
|
|
|
if (!value) return 0;
|
2016-02-03 00:47:28 +00:00
|
|
|
child->string = child->valuestring;
|
|
|
|
child->valuestring = 0;
|
|
|
|
if (*value != ':') {
|
|
|
|
ep = value;
|
|
|
|
return 0;
|
2022-05-21 01:40:53 +00:00
|
|
|
} /* fail! */
|
|
|
|
value = skip(parse_value(child, skip(value + 1))); /* skip any spacing, get the value. */
|
2017-01-26 20:34:13 +00:00
|
|
|
if (!value) return 0;
|
2016-02-03 00:47:28 +00:00
|
|
|
}
|
|
|
|
|
2017-01-26 20:34:13 +00:00
|
|
|
if (*value == '}') return value + 1; /* end of array */
|
2016-02-03 00:47:28 +00:00
|
|
|
ep = value;
|
|
|
|
return 0; /* malformed. */
|
2015-06-29 17:25:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Render an object to text. */
|
2022-05-21 01:40:53 +00:00
|
|
|
char *print_object(cJSON *item, int depth, int fmt, printbuffer *p) {
|
2016-02-03 00:47:28 +00:00
|
|
|
char **entries = 0, **names = 0;
|
|
|
|
char *out = 0, *ptr, *ret, *str;
|
|
|
|
int j;
|
|
|
|
cJSON *child = item->child;
|
|
|
|
int numentries = 0, fail = 0, k;
|
|
|
|
size_t tmplen = 0, i = 0, len = 7;
|
|
|
|
/* Count the number of entries. */
|
2017-01-26 20:34:13 +00:00
|
|
|
while (child) numentries++, child = child->next;
|
2016-02-03 00:47:28 +00:00
|
|
|
/* Explicitly handle empty object case */
|
|
|
|
if (!numentries) {
|
|
|
|
if (p)
|
2022-05-21 01:40:53 +00:00
|
|
|
out = ensure(item->pAllocator, p, fmt ? depth + 4 : 3);
|
2016-02-03 00:47:28 +00:00
|
|
|
else
|
2022-05-21 01:40:53 +00:00
|
|
|
out = (char *)cJSON_malloc(item->pAllocator, fmt ? depth + 4 : 3);
|
2017-01-26 20:34:13 +00:00
|
|
|
if (!out) return 0;
|
2016-02-03 00:47:28 +00:00
|
|
|
ptr = out;
|
|
|
|
*ptr++ = '{';
|
|
|
|
if (fmt) {
|
|
|
|
*ptr++ = '\n';
|
2017-01-26 20:34:13 +00:00
|
|
|
for (j = 0; j < depth - 1; j++) *ptr++ = '\t';
|
2016-02-03 00:47:28 +00:00
|
|
|
}
|
|
|
|
*ptr++ = '}';
|
|
|
|
*ptr++ = 0;
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
if (p) {
|
|
|
|
/* Compose the output: */
|
|
|
|
i = p->offset;
|
|
|
|
len = fmt ? 2 : 1;
|
2022-05-21 01:40:53 +00:00
|
|
|
ptr = ensure(item->pAllocator, p, len + 1);
|
2017-01-26 20:34:13 +00:00
|
|
|
if (!ptr) return 0;
|
2016-02-03 00:47:28 +00:00
|
|
|
*ptr++ = '{';
|
2017-01-26 20:34:13 +00:00
|
|
|
if (fmt) *ptr++ = '\n';
|
2016-02-03 00:47:28 +00:00
|
|
|
*ptr = 0;
|
|
|
|
p->offset += len;
|
|
|
|
child = item->child;
|
|
|
|
depth++;
|
|
|
|
while (child) {
|
|
|
|
if (fmt) {
|
2022-05-21 01:40:53 +00:00
|
|
|
ptr = ensure(item->pAllocator, p, depth);
|
2017-01-26 20:34:13 +00:00
|
|
|
if (!ptr) return 0;
|
|
|
|
for (j = 0; j < depth; j++) *ptr++ = '\t';
|
2016-02-03 00:47:28 +00:00
|
|
|
p->offset += depth;
|
|
|
|
}
|
2022-05-21 01:40:53 +00:00
|
|
|
print_string_ptr(item->pAllocator, child->string, p);
|
2021-09-16 20:39:05 +00:00
|
|
|
p->offset = cJSON_update(p);
|
2016-02-03 00:47:28 +00:00
|
|
|
|
|
|
|
len = fmt ? 2 : 1;
|
2022-05-21 01:40:53 +00:00
|
|
|
ptr = ensure(item->pAllocator, p, len);
|
2017-01-26 20:34:13 +00:00
|
|
|
if (!ptr) return 0;
|
2016-02-03 00:47:28 +00:00
|
|
|
*ptr++ = ':';
|
2017-01-26 20:34:13 +00:00
|
|
|
if (fmt) *ptr++ = '\t';
|
2016-02-03 00:47:28 +00:00
|
|
|
p->offset += len;
|
|
|
|
|
2022-05-21 01:40:53 +00:00
|
|
|
print_value(child, depth, fmt, p);
|
2021-09-16 20:39:05 +00:00
|
|
|
p->offset = cJSON_update(p);
|
2016-02-03 00:47:28 +00:00
|
|
|
|
|
|
|
len = (fmt ? 1 : 0) + (child->next ? 1 : 0);
|
2022-05-21 01:40:53 +00:00
|
|
|
ptr = ensure(item->pAllocator, p, len + 1);
|
2017-01-26 20:34:13 +00:00
|
|
|
if (!ptr) return 0;
|
|
|
|
if (child->next) *ptr++ = ',';
|
|
|
|
if (fmt) *ptr++ = '\n';
|
2016-02-03 00:47:28 +00:00
|
|
|
*ptr = 0;
|
|
|
|
p->offset += len;
|
|
|
|
child = child->next;
|
|
|
|
}
|
2022-05-21 01:40:53 +00:00
|
|
|
ptr = ensure(item->pAllocator, p, fmt ? (depth + 1) : 2);
|
2017-01-26 20:34:13 +00:00
|
|
|
if (!ptr) return 0;
|
2016-02-03 00:47:28 +00:00
|
|
|
if (fmt)
|
2017-01-26 20:34:13 +00:00
|
|
|
for (j = 0; j < depth - 1; j++) *ptr++ = '\t';
|
2016-02-03 00:47:28 +00:00
|
|
|
*ptr++ = '}';
|
|
|
|
*ptr = 0;
|
|
|
|
out = (p->buffer) + i;
|
|
|
|
} else {
|
|
|
|
/* Allocate space for the names and the objects */
|
2022-05-21 01:40:53 +00:00
|
|
|
entries = (char **)cJSON_malloc(item->pAllocator, numentries * sizeof(char *));
|
2017-01-26 20:34:13 +00:00
|
|
|
if (!entries) return 0;
|
2022-05-21 01:40:53 +00:00
|
|
|
names = (char **)cJSON_malloc(item->pAllocator, numentries * sizeof(char *));
|
2016-02-03 00:47:28 +00:00
|
|
|
if (!names) {
|
2022-05-21 01:40:53 +00:00
|
|
|
cJSON_Free(item->pAllocator, entries);
|
2016-02-03 00:47:28 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
memset(entries, 0, sizeof(char *) * numentries);
|
|
|
|
memset(names, 0, sizeof(char *) * numentries);
|
|
|
|
|
|
|
|
/* Collect all the results into our arrays: */
|
|
|
|
child = item->child;
|
|
|
|
depth++;
|
2017-01-26 20:34:13 +00:00
|
|
|
if (fmt) len += depth;
|
2016-02-03 00:47:28 +00:00
|
|
|
while (child) {
|
2022-05-21 01:40:53 +00:00
|
|
|
names[i] = str = print_string_ptr(item->pAllocator, child->string, 0);
|
|
|
|
entries[i++] = ret = print_value(child, depth, fmt, 0);
|
2016-02-03 00:47:28 +00:00
|
|
|
if (str && ret)
|
|
|
|
len += strlen(ret) + strlen(str) + 2 + (fmt ? 2 + depth : 0);
|
|
|
|
else
|
|
|
|
fail = 1;
|
|
|
|
child = child->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Try to allocate the output string */
|
2022-05-21 01:40:53 +00:00
|
|
|
if (!fail) out = (char *)cJSON_malloc(item->pAllocator, len);
|
2017-01-26 20:34:13 +00:00
|
|
|
if (!out) fail = 1;
|
2016-02-03 00:47:28 +00:00
|
|
|
|
|
|
|
/* Handle failure */
|
|
|
|
if (fail) {
|
|
|
|
for (j = 0; j < numentries; j++) {
|
2022-05-21 01:40:53 +00:00
|
|
|
if (names[i]) cJSON_Free(item->pAllocator, names[j]);
|
|
|
|
if (entries[j]) cJSON_Free(item->pAllocator, entries[j]);
|
2016-02-03 00:47:28 +00:00
|
|
|
}
|
2022-05-21 01:40:53 +00:00
|
|
|
cJSON_Free(item->pAllocator, names);
|
|
|
|
cJSON_Free(item->pAllocator, entries);
|
2016-02-03 00:47:28 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Compose the output: */
|
|
|
|
*out = '{';
|
|
|
|
ptr = out + 1;
|
2017-01-26 20:34:13 +00:00
|
|
|
if (fmt) *ptr++ = '\n';
|
2016-02-03 00:47:28 +00:00
|
|
|
*ptr = 0;
|
|
|
|
for (j = 0; j < numentries; j++) {
|
|
|
|
if (fmt)
|
2017-01-26 20:34:13 +00:00
|
|
|
for (k = 0; k < depth; k++) *ptr++ = '\t';
|
2016-02-03 00:47:28 +00:00
|
|
|
tmplen = strlen(names[j]);
|
|
|
|
memcpy(ptr, names[j], tmplen);
|
|
|
|
ptr += tmplen;
|
|
|
|
*ptr++ = ':';
|
2017-01-26 20:34:13 +00:00
|
|
|
if (fmt) *ptr++ = '\t';
|
2016-02-03 00:47:28 +00:00
|
|
|
strcpy(ptr, entries[j]);
|
|
|
|
ptr += strlen(entries[j]);
|
2017-01-26 20:34:13 +00:00
|
|
|
if (j != numentries - 1) *ptr++ = ',';
|
|
|
|
if (fmt) *ptr++ = '\n';
|
2016-02-03 00:47:28 +00:00
|
|
|
*ptr = 0;
|
2022-05-21 01:40:53 +00:00
|
|
|
cJSON_Free(item->pAllocator, names[j]);
|
|
|
|
cJSON_Free(item->pAllocator, entries[j]);
|
2016-02-03 00:47:28 +00:00
|
|
|
}
|
|
|
|
|
2022-05-21 01:40:53 +00:00
|
|
|
cJSON_Free(item->pAllocator, names);
|
|
|
|
cJSON_Free(item->pAllocator, entries);
|
2016-02-03 00:47:28 +00:00
|
|
|
if (fmt)
|
2017-01-26 20:34:13 +00:00
|
|
|
for (j = 0; j < depth - 1; j++) *ptr++ = '\t';
|
2016-02-03 00:47:28 +00:00
|
|
|
*ptr++ = '}';
|
|
|
|
*ptr++ = 0;
|
|
|
|
}
|
|
|
|
return out;
|
2015-06-29 17:25:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Get Array size/item / object item. */
|
2016-02-03 00:47:28 +00:00
|
|
|
int cJSON_GetArraySize(cJSON *array) {
|
|
|
|
cJSON *c = array->child;
|
|
|
|
int i = 0;
|
2017-01-26 20:34:13 +00:00
|
|
|
while (c) i++, c = c->next;
|
2016-02-03 00:47:28 +00:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
cJSON *cJSON_GetArrayItem(cJSON *array, int item) {
|
|
|
|
cJSON *c = array->child;
|
2017-01-26 20:34:13 +00:00
|
|
|
while (c && item > 0) item--, c = c->next;
|
2016-02-03 00:47:28 +00:00
|
|
|
return c;
|
|
|
|
}
|
|
|
|
cJSON *cJSON_GetObjectItem(cJSON *object, const char *string) {
|
|
|
|
cJSON *c = object->child;
|
2017-01-26 20:34:13 +00:00
|
|
|
while (c && strcmp(c->string, string)) c = c->next;
|
2016-02-03 00:47:28 +00:00
|
|
|
return c;
|
|
|
|
}
|
2015-06-29 17:25:34 +00:00
|
|
|
|
|
|
|
/* Utility for array list handling. */
|
2021-09-04 19:33:38 +00:00
|
|
|
void suffix_object(cJSON *prev, cJSON *item) {
|
2016-02-03 00:47:28 +00:00
|
|
|
prev->next = item;
|
|
|
|
item->prev = prev;
|
|
|
|
}
|
2015-06-29 17:25:34 +00:00
|
|
|
/* Utility for handling references. */
|
2022-05-21 01:40:53 +00:00
|
|
|
cJSON *create_reference(cJSON *item) {
|
|
|
|
cJSON *ref = cJSON_New_Item(item->pAllocator);
|
2017-01-26 20:34:13 +00:00
|
|
|
if (!ref) return 0;
|
2016-02-03 00:47:28 +00:00
|
|
|
memcpy(ref, item, sizeof(cJSON));
|
|
|
|
ref->string = 0;
|
|
|
|
ref->type |= cJSON_IsReference;
|
|
|
|
ref->next = ref->prev = 0;
|
|
|
|
return ref;
|
|
|
|
}
|
2015-06-29 17:25:34 +00:00
|
|
|
|
|
|
|
/* Add item to array/object. */
|
2016-02-03 00:47:28 +00:00
|
|
|
void cJSON_AddItemToArray(cJSON *array, cJSON *item) {
|
|
|
|
cJSON *c = array->child;
|
2017-01-26 20:34:13 +00:00
|
|
|
if (!item) return;
|
2016-02-03 00:47:28 +00:00
|
|
|
if (!c) {
|
|
|
|
array->child = item;
|
|
|
|
} else {
|
2017-01-26 20:34:13 +00:00
|
|
|
while (c && c->next) c = c->next;
|
2016-02-03 00:47:28 +00:00
|
|
|
suffix_object(c, item);
|
|
|
|
}
|
|
|
|
}
|
2022-05-21 01:40:53 +00:00
|
|
|
void cJSON_AddItemToObject(cJSON *object, const char *string, cJSON *item) {
|
2017-01-26 20:34:13 +00:00
|
|
|
if (!item) return;
|
2022-05-21 01:40:53 +00:00
|
|
|
if (item->string) cJSON_Free(object->pAllocator, item->string);
|
|
|
|
item->string = cJSON_strdup(object->pAllocator, string);
|
2016-02-03 00:47:28 +00:00
|
|
|
cJSON_AddItemToArray(object, item);
|
|
|
|
}
|
2022-05-21 01:40:53 +00:00
|
|
|
void cJSON_AddItemToObjectCS(cJSON *object, const char *string, cJSON *item) {
|
2017-01-26 20:34:13 +00:00
|
|
|
if (!item) return;
|
2022-05-21 01:40:53 +00:00
|
|
|
if (!(item->type & cJSON_StringIsConst) && item->string) cJSON_Free(object->pAllocator, item->string);
|
2016-02-03 00:47:28 +00:00
|
|
|
item->string = (char *)string;
|
|
|
|
item->type |= cJSON_StringIsConst;
|
|
|
|
cJSON_AddItemToArray(object, item);
|
|
|
|
}
|
2022-05-21 01:40:53 +00:00
|
|
|
void cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item) { cJSON_AddItemToArray(array, create_reference(item)); }
|
|
|
|
void cJSON_AddItemReferenceToObject(cJSON *object, const char *string, cJSON *item) {
|
|
|
|
cJSON_AddItemToObject(object, string, create_reference(item));
|
2016-02-03 00:47:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cJSON *cJSON_DetachItemFromArray(cJSON *array, int which) {
|
|
|
|
cJSON *c = array->child;
|
2017-01-26 20:34:13 +00:00
|
|
|
while (c && which > 0) c = c->next, which--;
|
|
|
|
if (!c) return 0;
|
|
|
|
if (c->prev) c->prev->next = c->next;
|
|
|
|
if (c->next) c->next->prev = c->prev;
|
|
|
|
if (c == array->child) array->child = c->next;
|
2016-02-03 00:47:28 +00:00
|
|
|
c->prev = c->next = 0;
|
|
|
|
return c;
|
|
|
|
}
|
2022-05-21 01:40:53 +00:00
|
|
|
void cJSON_DeleteItemFromArray(cJSON *array, int which) { cJSON_Delete(cJSON_DetachItemFromArray(array, which)); }
|
2016-02-03 00:47:28 +00:00
|
|
|
cJSON *cJSON_DetachItemFromObject(cJSON *object, const char *string) {
|
|
|
|
int i = 0;
|
|
|
|
cJSON *c = object->child;
|
2017-01-26 20:34:13 +00:00
|
|
|
while (c && strcmp(c->string, string)) i++, c = c->next;
|
|
|
|
if (c) return cJSON_DetachItemFromArray(object, i);
|
2016-02-03 00:47:28 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2022-05-21 01:40:53 +00:00
|
|
|
void cJSON_DeleteItemFromObject(cJSON *object, const char *string) { cJSON_Delete(cJSON_DetachItemFromObject(object, string)); }
|
2015-06-29 17:25:34 +00:00
|
|
|
|
|
|
|
/* Replace array/object items with new ones. */
|
2016-02-03 00:47:28 +00:00
|
|
|
void cJSON_InsertItemInArray(cJSON *array, int which, cJSON *newitem) {
|
|
|
|
cJSON *c = array->child;
|
2017-01-26 20:34:13 +00:00
|
|
|
while (c && which > 0) c = c->next, which--;
|
2016-02-03 00:47:28 +00:00
|
|
|
if (!c) {
|
|
|
|
cJSON_AddItemToArray(array, newitem);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
newitem->next = c;
|
|
|
|
newitem->prev = c->prev;
|
|
|
|
c->prev = newitem;
|
|
|
|
if (c == array->child)
|
|
|
|
array->child = newitem;
|
|
|
|
else
|
|
|
|
newitem->prev->next = newitem;
|
|
|
|
}
|
2022-05-21 01:40:53 +00:00
|
|
|
void cJSON_ReplaceItemInArray(cJSON *array, int which, cJSON *newitem) {
|
2016-02-03 00:47:28 +00:00
|
|
|
cJSON *c = array->child;
|
2017-01-26 20:34:13 +00:00
|
|
|
while (c && which > 0) c = c->next, which--;
|
|
|
|
if (!c) return;
|
2016-02-03 00:47:28 +00:00
|
|
|
newitem->next = c->next;
|
|
|
|
newitem->prev = c->prev;
|
2017-01-26 20:34:13 +00:00
|
|
|
if (newitem->next) newitem->next->prev = newitem;
|
2016-02-03 00:47:28 +00:00
|
|
|
if (c == array->child)
|
|
|
|
array->child = newitem;
|
|
|
|
else
|
|
|
|
newitem->prev->next = newitem;
|
|
|
|
c->next = c->prev = 0;
|
2022-05-21 01:40:53 +00:00
|
|
|
cJSON_Delete(c);
|
2016-02-03 00:47:28 +00:00
|
|
|
}
|
2022-05-21 01:40:53 +00:00
|
|
|
void cJSON_ReplaceItemInObject(cJSON *object, const char *string, cJSON *newitem) {
|
2016-02-03 00:47:28 +00:00
|
|
|
int i = 0;
|
|
|
|
cJSON *c = object->child;
|
2017-01-26 20:34:13 +00:00
|
|
|
while (c && strcmp(c->string, string)) i++, c = c->next;
|
2016-02-03 00:47:28 +00:00
|
|
|
if (c) {
|
2022-05-21 01:40:53 +00:00
|
|
|
newitem->string = cJSON_strdup(object->pAllocator, string);
|
|
|
|
cJSON_ReplaceItemInArray(object, i, newitem);
|
2016-02-03 00:47:28 +00:00
|
|
|
}
|
|
|
|
}
|
2015-06-29 17:25:34 +00:00
|
|
|
|
|
|
|
/* Create basic types: */
|
2022-05-21 01:40:53 +00:00
|
|
|
cJSON *cJSON_CreateNull(const VkAllocationCallbacks *pAllocator) {
|
|
|
|
cJSON *item = cJSON_New_Item(pAllocator);
|
2017-01-26 20:34:13 +00:00
|
|
|
if (item) item->type = cJSON_NULL;
|
2016-02-03 00:47:28 +00:00
|
|
|
return item;
|
|
|
|
}
|
2022-05-21 01:40:53 +00:00
|
|
|
cJSON *cJSON_CreateTrue(const VkAllocationCallbacks *pAllocator) {
|
|
|
|
cJSON *item = cJSON_New_Item(pAllocator);
|
2017-01-26 20:34:13 +00:00
|
|
|
if (item) item->type = cJSON_True;
|
2016-02-03 00:47:28 +00:00
|
|
|
return item;
|
|
|
|
}
|
2022-05-21 01:40:53 +00:00
|
|
|
cJSON *cJSON_CreateFalse(const VkAllocationCallbacks *pAllocator) {
|
|
|
|
cJSON *item = cJSON_New_Item(pAllocator);
|
2017-01-26 20:34:13 +00:00
|
|
|
if (item) item->type = cJSON_False;
|
2016-02-03 00:47:28 +00:00
|
|
|
return item;
|
|
|
|
}
|
2022-05-21 01:40:53 +00:00
|
|
|
cJSON *cJSON_CreateBool(const VkAllocationCallbacks *pAllocator, int b) {
|
|
|
|
cJSON *item = cJSON_New_Item(pAllocator);
|
2017-01-26 20:34:13 +00:00
|
|
|
if (item) item->type = b ? cJSON_True : cJSON_False;
|
2016-02-03 00:47:28 +00:00
|
|
|
return item;
|
|
|
|
}
|
2022-05-21 01:40:53 +00:00
|
|
|
cJSON *cJSON_CreateNumber(const VkAllocationCallbacks *pAllocator, double num) {
|
|
|
|
cJSON *item = cJSON_New_Item(pAllocator);
|
2016-02-03 00:47:28 +00:00
|
|
|
if (item) {
|
|
|
|
item->type = cJSON_Number;
|
|
|
|
item->valuedouble = num;
|
|
|
|
item->valueint = (int)num;
|
|
|
|
}
|
|
|
|
return item;
|
|
|
|
}
|
2022-05-21 01:40:53 +00:00
|
|
|
cJSON *cJSON_CreateString(const VkAllocationCallbacks *pAllocator, const char *string) {
|
|
|
|
cJSON *item = cJSON_New_Item(pAllocator);
|
2016-02-03 00:47:28 +00:00
|
|
|
if (item) {
|
|
|
|
item->type = cJSON_String;
|
2022-05-21 01:40:53 +00:00
|
|
|
item->valuestring = cJSON_strdup(pAllocator, string);
|
2016-02-03 00:47:28 +00:00
|
|
|
}
|
|
|
|
return item;
|
|
|
|
}
|
2022-05-21 01:40:53 +00:00
|
|
|
cJSON *cJSON_CreateArray(const VkAllocationCallbacks *pAllocator) {
|
|
|
|
cJSON *item = cJSON_New_Item(pAllocator);
|
2017-01-26 20:34:13 +00:00
|
|
|
if (item) item->type = cJSON_Array;
|
2016-02-03 00:47:28 +00:00
|
|
|
return item;
|
|
|
|
}
|
2022-05-21 01:40:53 +00:00
|
|
|
cJSON *cJSON_CreateObject(const VkAllocationCallbacks *pAllocator) {
|
|
|
|
cJSON *item = cJSON_New_Item(pAllocator);
|
2017-01-26 20:34:13 +00:00
|
|
|
if (item) item->type = cJSON_Object;
|
2016-02-03 00:47:28 +00:00
|
|
|
return item;
|
|
|
|
}
|
2015-06-29 17:25:34 +00:00
|
|
|
|
|
|
|
/* Create Arrays: */
|
2022-05-21 01:40:53 +00:00
|
|
|
cJSON *cJSON_CreateIntArray(const VkAllocationCallbacks *pAllocator, const int *numbers, int count) {
|
2016-02-03 00:47:28 +00:00
|
|
|
int i;
|
2022-05-21 01:40:53 +00:00
|
|
|
cJSON *n = 0, *p = 0, *a = cJSON_CreateArray(pAllocator);
|
2016-02-03 00:47:28 +00:00
|
|
|
for (i = 0; a && i < count; i++) {
|
2022-05-21 01:40:53 +00:00
|
|
|
n = cJSON_CreateNumber(pAllocator, numbers[i]);
|
2016-02-03 00:47:28 +00:00
|
|
|
if (!i)
|
|
|
|
a->child = n;
|
|
|
|
else
|
|
|
|
suffix_object(p, n);
|
|
|
|
p = n;
|
|
|
|
}
|
|
|
|
return a;
|
|
|
|
}
|
2022-05-21 01:40:53 +00:00
|
|
|
cJSON *cJSON_CreateFloatArray(const VkAllocationCallbacks *pAllocator, const float *numbers, int count) {
|
2016-02-03 00:47:28 +00:00
|
|
|
int i;
|
2022-05-21 01:40:53 +00:00
|
|
|
cJSON *n = 0, *p = 0, *a = cJSON_CreateArray(pAllocator);
|
2016-02-03 00:47:28 +00:00
|
|
|
for (i = 0; a && i < count; i++) {
|
2022-05-21 01:40:53 +00:00
|
|
|
n = cJSON_CreateNumber(pAllocator, numbers[i]);
|
2016-02-03 00:47:28 +00:00
|
|
|
if (!i)
|
|
|
|
a->child = n;
|
|
|
|
else
|
|
|
|
suffix_object(p, n);
|
|
|
|
p = n;
|
|
|
|
}
|
|
|
|
return a;
|
|
|
|
}
|
2022-05-21 01:40:53 +00:00
|
|
|
cJSON *cJSON_CreateDoubleArray(const VkAllocationCallbacks *pAllocator, const double *numbers, int count) {
|
2016-02-03 00:47:28 +00:00
|
|
|
int i;
|
2022-05-21 01:40:53 +00:00
|
|
|
cJSON *n = 0, *p = 0, *a = cJSON_CreateArray(pAllocator);
|
2016-02-03 00:47:28 +00:00
|
|
|
for (i = 0; a && i < count; i++) {
|
2022-05-21 01:40:53 +00:00
|
|
|
n = cJSON_CreateNumber(pAllocator, numbers[i]);
|
2016-02-03 00:47:28 +00:00
|
|
|
if (!i)
|
|
|
|
a->child = n;
|
|
|
|
else
|
|
|
|
suffix_object(p, n);
|
|
|
|
p = n;
|
|
|
|
}
|
|
|
|
return a;
|
|
|
|
}
|
2022-05-21 01:40:53 +00:00
|
|
|
cJSON *cJSON_CreateStringArray(const VkAllocationCallbacks *pAllocator, const char **strings, int count) {
|
2016-02-03 00:47:28 +00:00
|
|
|
int i;
|
2022-05-21 01:40:53 +00:00
|
|
|
cJSON *n = 0, *p = 0, *a = cJSON_CreateArray(pAllocator);
|
2016-02-03 00:47:28 +00:00
|
|
|
for (i = 0; a && i < count; i++) {
|
2022-05-21 01:40:53 +00:00
|
|
|
n = cJSON_CreateString(pAllocator, strings[i]);
|
2016-02-03 00:47:28 +00:00
|
|
|
if (!i)
|
|
|
|
a->child = n;
|
|
|
|
else
|
|
|
|
suffix_object(p, n);
|
|
|
|
p = n;
|
|
|
|
}
|
|
|
|
return a;
|
|
|
|
}
|
2015-06-29 17:25:34 +00:00
|
|
|
|
|
|
|
/* Duplication */
|
2022-05-21 01:40:53 +00:00
|
|
|
cJSON *cJSON_Duplicate(cJSON *item, int recurse) {
|
2016-02-03 00:47:28 +00:00
|
|
|
cJSON *newitem, *cptr, *nptr = 0, *newchild;
|
|
|
|
/* Bail on bad ptr */
|
2017-01-26 20:34:13 +00:00
|
|
|
if (!item) return 0;
|
2016-02-03 00:47:28 +00:00
|
|
|
/* Create new item */
|
2022-05-21 01:40:53 +00:00
|
|
|
newitem = cJSON_New_Item(item->pAllocator);
|
2017-01-26 20:34:13 +00:00
|
|
|
if (!newitem) return 0;
|
2016-02-03 00:47:28 +00:00
|
|
|
/* Copy over all vars */
|
2017-01-26 19:16:30 +00:00
|
|
|
newitem->type = item->type & (~cJSON_IsReference), newitem->valueint = item->valueint, newitem->valuedouble = item->valuedouble;
|
2016-02-03 00:47:28 +00:00
|
|
|
if (item->valuestring) {
|
2022-05-21 01:40:53 +00:00
|
|
|
newitem->valuestring = cJSON_strdup(item->pAllocator, item->valuestring);
|
2016-02-03 00:47:28 +00:00
|
|
|
if (!newitem->valuestring) {
|
2022-05-21 01:40:53 +00:00
|
|
|
cJSON_Delete(newitem);
|
2016-02-03 00:47:28 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (item->string) {
|
2022-05-21 01:40:53 +00:00
|
|
|
newitem->string = cJSON_strdup(item->pAllocator, item->string);
|
2016-02-03 00:47:28 +00:00
|
|
|
if (!newitem->string) {
|
2022-05-21 01:40:53 +00:00
|
|
|
cJSON_Delete(newitem);
|
2016-02-03 00:47:28 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* If non-recursive, then we're done! */
|
2017-01-26 20:34:13 +00:00
|
|
|
if (!recurse) return newitem;
|
2016-02-03 00:47:28 +00:00
|
|
|
/* Walk the ->next chain for the child. */
|
|
|
|
cptr = item->child;
|
|
|
|
while (cptr) {
|
2022-05-21 01:40:53 +00:00
|
|
|
newchild = cJSON_Duplicate(cptr, 1); /* Duplicate (with recurse) each item in the ->next chain */
|
2016-02-03 00:47:28 +00:00
|
|
|
if (!newchild) {
|
2022-05-21 01:40:53 +00:00
|
|
|
cJSON_Delete(newitem);
|
2016-02-03 00:47:28 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (nptr) {
|
|
|
|
nptr->next = newchild, newchild->prev = nptr;
|
|
|
|
nptr = newchild;
|
|
|
|
} /* If newitem->child already set, then crosswire ->prev and ->next and
|
|
|
|
move on */
|
|
|
|
else {
|
|
|
|
newitem->child = newchild;
|
|
|
|
nptr = newchild;
|
|
|
|
} /* Set newitem->child and move to it */
|
|
|
|
cptr = cptr->next;
|
|
|
|
}
|
|
|
|
return newitem;
|
|
|
|
}
|
|
|
|
|
|
|
|
void cJSON_Minify(char *json) {
|
|
|
|
char *into = json;
|
|
|
|
while (*json) {
|
|
|
|
if (*json == ' ')
|
|
|
|
json++;
|
|
|
|
else if (*json == '\t')
|
|
|
|
json++; /* Whitespace characters. */
|
|
|
|
else if (*json == '\r')
|
|
|
|
json++;
|
|
|
|
else if (*json == '\n')
|
|
|
|
json++;
|
|
|
|
else if (*json == '/' && json[1] == '/')
|
2017-01-26 20:34:13 +00:00
|
|
|
while (*json && *json != '\n') json++; /* double-slash comments, to end of line. */
|
2016-02-03 00:47:28 +00:00
|
|
|
else if (*json == '/' && json[1] == '*') {
|
2017-01-26 20:34:13 +00:00
|
|
|
while (*json && !(*json == '*' && json[1] == '/')) json++;
|
2016-02-03 00:47:28 +00:00
|
|
|
json += 2;
|
|
|
|
} /* multiline comments. */
|
|
|
|
else if (*json == '\"') {
|
|
|
|
*into++ = *json++;
|
|
|
|
while (*json && *json != '\"') {
|
2017-01-26 20:34:13 +00:00
|
|
|
if (*json == '\\') *into++ = *json++;
|
2016-02-03 00:47:28 +00:00
|
|
|
*into++ = *json++;
|
|
|
|
}
|
|
|
|
*into++ = *json++;
|
|
|
|
} /* string literals, which are \" sensitive. */
|
|
|
|
else
|
|
|
|
*into++ = *json++; /* All other characters. */
|
|
|
|
}
|
|
|
|
*into = 0; /* and null-terminate. */
|
2015-06-29 17:25:34 +00:00
|
|
|
}
|