27 #if !defined(_CRT_SECURE_NO_DEPRECATE) && defined(_MSC_VER)
28 #define _CRT_SECURE_NO_DEPRECATE
32 #pragma GCC visibility push(default)
35 #pragma warning (push)
37 #pragma warning (disable : 4001)
56 #pragma GCC visibility pop
65 #define true ((cJSON_bool)1)
70 #define false ((cJSON_bool)0)
74 #define isinf(d) (isnan((d - d)) && !isnan(d))
77 #define isnan(d) (d != d)
82 #define NAN sqrt (-1.0)
101 if (!cJSON_IsString(
item))
111 if (!cJSON_IsNumber(
item))
120 #if (CJSON_VERSION_MAJOR != 1) || (CJSON_VERSION_MINOR != 7) || (CJSON_VERSION_PATCH != 14)
121 #error cJSON.h and cJSON.c have different versions. Make sure that both have the same.
135 if ((string1 == NULL) || (string2 == NULL))
140 if (string1 == string2)
145 for(; tolower(*string1) == tolower(*string2); (void)string1++, string2++)
147 if (*string1 ==
'\0')
153 return tolower(*string1) - tolower(*string2);
163 #if defined(_MSC_VER)
178 #define internal_malloc malloc
179 #define internal_free free
180 #define internal_realloc realloc
184 #define static_strlen(string_literal) (sizeof(string_literal) - sizeof(""))
191 unsigned char *copy = NULL;
198 length = strlen((
const char*)
string) +
sizeof(
"");
204 memcpy(copy,
string,
length);
227 if (hooks->free_fn != NULL)
279 #ifdef ENABLE_LOCALES
280 struct lconv *lconv = localeconv();
281 return (
unsigned char) lconv->decimal_point[0];
297 #define can_read(buffer, size) ((buffer != NULL) && (((buffer)->offset + size) <= (buffer)->length))
299 #define can_access_at_index(buffer, index) ((buffer != NULL) && (((buffer)->offset + index) < (buffer)->length))
300 #define cannot_access_at_index(buffer, index) (!can_access_at_index(buffer, index))
302 #define buffer_at_offset(buffer) ((buffer)->content + (buffer)->offset)
308 unsigned char *after_end = NULL;
309 unsigned char number_c_string[64];
313 if ((input_buffer == NULL) || (input_buffer->
content == NULL))
343 number_c_string[
i] = decimal_point;
351 number_c_string[
i] =
'\0';
353 number = strtod((
const char*)number_c_string, (
char**)&after_end);
354 if (number_c_string == after_end)
366 else if (
number <= (
double)INT_MIN)
377 input_buffer->
offset += (size_t)(after_end - number_c_string);
386 object->valueint = INT_MAX;
388 else if (
number <= (
double)INT_MIN)
390 object->valueint = INT_MIN;
394 object->valueint = (int)
number;
397 return object->valuedouble =
number;
411 return object->valuestring;
422 object->valuestring = copy;
441 unsigned char *newbuffer = NULL;
444 if ((p == NULL) || (p->
buffer == NULL))
455 if (needed > INT_MAX)
472 if (needed > (INT_MAX / 2))
475 if (needed <= INT_MAX)
486 newsize = needed * 2;
493 if (newbuffer == NULL)
521 return newbuffer + p->
offset;
527 const unsigned char *buffer_pointer = NULL;
534 buffer->offset += strlen((
const char*)buffer_pointer);
541 return (
fabs(
a -
b) <= maxVal * DBL_EPSILON);
547 unsigned char *output_pointer = NULL;
551 unsigned char number_buffer[26] = {0};
555 if (output_buffer == NULL)
563 length = sprintf((
char*)number_buffer,
"null");
568 length = sprintf((
char*)number_buffer,
"%1.15g", d);
571 if ((sscanf((
char*)number_buffer,
"%lg", &test) != 1) || !
compare_double((
double)test, d))
574 length = sprintf((
char*)number_buffer,
"%1.17g", d);
579 if ((
length < 0) || (
length > (
int)(
sizeof(number_buffer) - 1)))
585 output_pointer =
ensure(output_buffer, (
size_t)
length +
sizeof(
""));
586 if (output_pointer == NULL)
595 if (number_buffer[
i] == decimal_point)
597 output_pointer[
i] =
'.';
601 output_pointer[
i] = number_buffer[
i];
603 output_pointer[
i] =
'\0';
611 static unsigned parse_hex4(
const unsigned char *
const input)
616 for (
i = 0;
i < 4;
i++)
619 if ((input[
i] >=
'0') && (input[
i] <=
'9'))
621 h += (
unsigned int) input[
i] -
'0';
623 else if ((input[
i] >=
'A') && (input[
i] <=
'F'))
625 h += (
unsigned int) 10 + input[
i] -
'A';
627 else if ((input[
i] >=
'a') && (input[
i] <=
'f'))
629 h += (
unsigned int) 10 + input[
i] -
'a';
648 static unsigned char utf16_literal_to_utf8(
const unsigned char *
const input_pointer,
const unsigned char *
const input_end,
unsigned char **output_pointer)
650 long unsigned int codepoint = 0;
651 unsigned int first_code = 0;
652 const unsigned char *first_sequence = input_pointer;
653 unsigned char utf8_length = 0;
654 unsigned char utf8_position = 0;
655 unsigned char sequence_length = 0;
656 unsigned char first_byte_mark = 0;
658 if ((input_end - first_sequence) < 6)
668 if (((first_code >= 0xDC00) && (first_code <= 0xDFFF)))
674 if ((first_code >= 0xD800) && (first_code <= 0xDBFF))
676 const unsigned char *second_sequence = first_sequence + 6;
677 unsigned int second_code = 0;
678 sequence_length = 12;
680 if ((input_end - second_sequence) < 6)
686 if ((second_sequence[0] !=
'\\') || (second_sequence[1] !=
'u'))
693 second_code =
parse_hex4(second_sequence + 2);
695 if ((second_code < 0xDC00) || (second_code > 0xDFFF))
703 codepoint = 0x10000 + (((first_code & 0x3FF) << 10) | (second_code & 0x3FF));
708 codepoint = first_code;
714 if (codepoint < 0x80)
719 else if (codepoint < 0x800)
723 first_byte_mark = 0xC0;
725 else if (codepoint < 0x10000)
729 first_byte_mark = 0xE0;
731 else if (codepoint <= 0x10FFFF)
735 first_byte_mark = 0xF0;
744 for (utf8_position = (
unsigned char)(utf8_length - 1); utf8_position > 0; utf8_position--)
747 (*output_pointer)[utf8_position] = (
unsigned char)((codepoint | 0x80) & 0xBF);
753 (*output_pointer)[0] = (
unsigned char)((codepoint | first_byte_mark) & 0xFF);
757 (*output_pointer)[0] = (
unsigned char)(codepoint & 0x7F);
760 *output_pointer += utf8_length;
762 return sequence_length;
773 unsigned char *output_pointer = NULL;
774 unsigned char *output = NULL;
784 size_t allocation_length = 0;
785 size_t skipped_bytes = 0;
786 while (((
size_t)(input_end - input_buffer->
content) < input_buffer->
length) && (*input_end !=
'\"'))
789 if (input_end[0] ==
'\\')
791 if ((
size_t)(input_end + 1 - input_buffer->
content) >= input_buffer->
length)
801 if (((
size_t)(input_end - input_buffer->
content) >= input_buffer->
length) || (*input_end !=
'\"'))
807 allocation_length = (size_t) (input_end -
buffer_at_offset(input_buffer)) - skipped_bytes;
808 output = (
unsigned char*)input_buffer->
hooks.
allocate(allocation_length +
sizeof(
""));
815 output_pointer = output;
817 while (input_pointer < input_end)
819 if (*input_pointer !=
'\\')
821 *output_pointer++ = *input_pointer++;
826 unsigned char sequence_length = 2;
827 if ((input_end - input_pointer) < 1)
832 switch (input_pointer[1])
835 *output_pointer++ =
'\b';
838 *output_pointer++ =
'\f';
841 *output_pointer++ =
'\n';
844 *output_pointer++ =
'\r';
847 *output_pointer++ =
'\t';
852 *output_pointer++ = input_pointer[1];
858 if (sequence_length == 0)
868 input_pointer += sequence_length;
873 *output_pointer =
'\0';
878 input_buffer->
offset = (size_t) (input_end - input_buffer->
content);
886 input_buffer->
hooks.deallocate(output);
889 if (input_pointer != NULL)
891 input_buffer->
offset = (size_t)(input_pointer - input_buffer->
content);
900 const unsigned char *input_pointer = NULL;
901 unsigned char *output = NULL;
902 unsigned char *output_pointer = NULL;
903 size_t output_length = 0;
905 size_t escape_characters = 0;
907 if (output_buffer == NULL)
915 output =
ensure(output_buffer,
sizeof(
"\"\""));
920 strcpy((
char*)output,
"\"\"");
926 for (input_pointer = input; *input_pointer; input_pointer++)
928 switch (*input_pointer)
941 if (*input_pointer < 32)
944 escape_characters += 5;
949 output_length = (size_t)(input_pointer - input) + escape_characters;
951 output =
ensure(output_buffer, output_length +
sizeof(
"\"\""));
958 if (escape_characters == 0)
961 memcpy(output + 1, input, output_length);
962 output[output_length + 1] =
'\"';
963 output[output_length + 2] =
'\0';
969 output_pointer = output + 1;
971 for (input_pointer = input; *input_pointer !=
'\0'; (void)input_pointer++, output_pointer++)
973 if ((*input_pointer > 31) && (*input_pointer !=
'\"') && (*input_pointer !=
'\\'))
976 *output_pointer = *input_pointer;
981 *output_pointer++ =
'\\';
982 switch (*input_pointer)
985 *output_pointer =
'\\';
988 *output_pointer =
'\"';
991 *output_pointer =
'b';
994 *output_pointer =
'f';
997 *output_pointer =
'n';
1000 *output_pointer =
'r';
1003 *output_pointer =
't';
1007 sprintf((
char*)output_pointer,
"u%04x", *input_pointer);
1008 output_pointer += 4;
1013 output[output_length + 1] =
'\"';
1014 output[output_length + 2] =
'\0';
1147 local_error.
json = (
const unsigned char*)
value;
1154 else if (
buffer.length > 0)
1173 return cJSON_ParseWithOpts(
value, 0, 0);
1181 #define cjson_min(a, b) (((a) < (b)) ? (a) : (b))
1185 static const size_t default_buffer_size = 256;
1187 unsigned char *printed = NULL;
1192 buffer->buffer = (
unsigned char*) hooks->
allocate(default_buffer_size);
1193 buffer->length = default_buffer_size;
1196 if (
buffer->buffer == NULL)
1212 if (printed == NULL) {
1220 if (printed == NULL)
1225 printed[
buffer->offset] =
'\0';
1228 hooks->deallocate(
buffer->buffer);
1234 if (
buffer->buffer != NULL)
1236 hooks->deallocate(
buffer->buffer);
1239 if (printed != NULL)
1241 hooks->deallocate(printed);
1260 printbuffer p = { 0, 0, 0, 0, 0, 0, { 0, 0, 0 } };
1290 printbuffer p = { 0, 0, 0, 0, 0, 0, { 0, 0, 0 } };
1310 if ((input_buffer == NULL) || (input_buffer->
content == NULL))
1320 input_buffer->
offset += 4;
1327 input_buffer->
offset += 5;
1335 input_buffer->
offset += 4;
1365 unsigned char *output = NULL;
1367 if ((
item == NULL) || (output_buffer == NULL))
1375 output =
ensure(output_buffer, 5);
1380 strcpy((
char*)output,
"null");
1384 output =
ensure(output_buffer, 6);
1389 strcpy((
char*)output,
"false");
1393 output =
ensure(output_buffer, 5);
1398 strcpy((
char*)output,
"true");
1406 size_t raw_length = 0;
1413 output =
ensure(output_buffer, raw_length);
1440 cJSON *current_item = NULL;
1446 input_buffer->
depth++;
1476 if (new_item == NULL)
1485 current_item = head = new_item;
1490 current_item->
next = new_item;
1491 new_item->
prev = current_item;
1492 current_item = new_item;
1512 input_buffer->
depth--;
1515 head->
prev = current_item;
1537 unsigned char *output_pointer = NULL;
1541 if (output_buffer == NULL)
1548 output_pointer =
ensure(output_buffer, 1);
1549 if (output_pointer == NULL)
1554 *output_pointer =
'[';
1556 output_buffer->
depth++;
1558 while (current_element != NULL)
1565 if (current_element->
next)
1569 if (output_pointer == NULL)
1573 *output_pointer++ =
',';
1574 if(output_buffer->
format)
1576 *output_pointer++ =
' ';
1578 *output_pointer =
'\0';
1581 current_element = current_element->
next;
1584 output_pointer =
ensure(output_buffer, 2);
1585 if (output_pointer == NULL)
1589 *output_pointer++ =
']';
1590 *output_pointer =
'\0';
1591 output_buffer->
depth--;
1600 cJSON *current_item = NULL;
1606 input_buffer->
depth++;
1634 if (new_item == NULL)
1643 current_item = head = new_item;
1648 current_item->
next = new_item;
1649 new_item->
prev = current_item;
1650 current_item = new_item;
1688 input_buffer->
depth--;
1691 head->
prev = current_item;
1712 unsigned char *output_pointer = NULL;
1716 if (output_buffer == NULL)
1724 if (output_pointer == NULL)
1729 *output_pointer++ =
'{';
1730 output_buffer->
depth++;
1731 if (output_buffer->
format)
1733 *output_pointer++ =
'\n';
1737 while (current_item)
1739 if (output_buffer->
format)
1742 output_pointer =
ensure(output_buffer, output_buffer->
depth);
1743 if (output_pointer == NULL)
1747 for (
i = 0;
i < output_buffer->
depth;
i++)
1749 *output_pointer++ =
'\t';
1763 if (output_pointer == NULL)
1767 *output_pointer++ =
':';
1768 if (output_buffer->
format)
1770 *output_pointer++ =
'\t';
1782 length = ((size_t)(output_buffer->
format ? 1 : 0) + (size_t)(current_item->
next ? 1 : 0));
1784 if (output_pointer == NULL)
1788 if (current_item->
next)
1790 *output_pointer++ =
',';
1793 if (output_buffer->
format)
1795 *output_pointer++ =
'\n';
1797 *output_pointer =
'\0';
1800 current_item = current_item->
next;
1803 output_pointer =
ensure(output_buffer, output_buffer->
format ? (output_buffer->
depth + 1) : 2);
1804 if (output_pointer == NULL)
1808 if (output_buffer->
format)
1811 for (
i = 0;
i < (output_buffer->
depth - 1);
i++)
1813 *output_pointer++ =
'\t';
1816 *output_pointer++ =
'}';
1817 *output_pointer =
'\0';
1818 output_buffer->
depth--;
1826 cJSON *child = NULL;
1834 child = array->
child;
1836 while(child != NULL)
1839 child = child->
next;
1849 cJSON *current_child = NULL;
1856 current_child = array->
child;
1857 while ((current_child != NULL) && (
index > 0))
1860 current_child = current_child->
next;
1863 return current_child;
1878 cJSON *current_element = NULL;
1880 if ((
object == NULL) || (
name == NULL))
1885 current_element =
object->
child;
1888 while ((current_element != NULL) && (current_element->
string != NULL) && (strcmp(
name, current_element->
string) != 0))
1890 current_element = current_element->
next;
1897 current_element = current_element->
next;
1901 if ((current_element == NULL) || (current_element->
string == NULL)) {
1905 return current_element;
1913 CJSON_PUBLIC(
cJSON *) cJSON_GetObjectItemCaseSensitive(
const cJSON *
const object,
const char *
const string)
1920 return cJSON_GetObjectItem(
object,
string) ? 1 : 0;
1933 cJSON *reference = NULL;
1940 if (reference == NULL)
1946 reference->
string = NULL;
1948 reference->
next = reference->
prev = NULL;
1954 cJSON *child = NULL;
1956 if ((
item == NULL) || (array == NULL) || (array ==
item))
1961 child = array->
child;
1991 #if defined(__clang__) || (defined(__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5))))
1992 #pragma GCC diagnostic push
1995 #pragma GCC diagnostic ignored "-Wcast-qual"
2002 #if defined(__clang__) || (defined(__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5))))
2003 #pragma GCC diagnostic pop
2009 char *new_key = NULL;
2012 if ((
object == NULL) || (
string == NULL) || (
item == NULL) || (
object ==
item))
2025 if (new_key == NULL)
2067 if ((
object == NULL) || (
string == NULL))
2077 cJSON *
null = cJSON_CreateNull();
2089 cJSON *true_item = cJSON_CreateTrue();
2095 cJSON_Delete(true_item);
2101 cJSON *false_item = cJSON_CreateFalse();
2107 cJSON_Delete(false_item);
2113 cJSON *bool_item = cJSON_CreateBool(
boolean);
2119 cJSON_Delete(bool_item);
2131 cJSON_Delete(number_item);
2137 cJSON *string_item = cJSON_CreateString(
string);
2143 cJSON_Delete(string_item);
2149 cJSON *raw_item = cJSON_CreateRaw(
raw);
2155 cJSON_Delete(raw_item);
2161 cJSON *object_item = cJSON_CreateObject();
2167 cJSON_Delete(object_item);
2173 cJSON *array = cJSON_CreateArray();
2179 cJSON_Delete(array);
2185 if ((parent == NULL) || (
item == NULL))
2231 cJSON_Delete(cJSON_DetachItemFromArray(array,
which));
2236 cJSON *to_detach = cJSON_GetObjectItem(
object,
string);
2238 return cJSON_DetachItemViaPointer(
object, to_detach);
2243 cJSON *to_detach = cJSON_GetObjectItemCaseSensitive(
object,
string);
2245 return cJSON_DetachItemViaPointer(
object, to_detach);
2248 CJSON_PUBLIC(
void) cJSON_DeleteItemFromObject(
cJSON *
object,
const char *
string)
2250 cJSON_Delete(cJSON_DetachItemFromObject(
object,
string));
2253 CJSON_PUBLIC(
void) cJSON_DeleteItemFromObjectCaseSensitive(
cJSON *
object,
const char *
string)
2255 cJSON_Delete(cJSON_DetachItemFromObjectCaseSensitive(
object,
string));
2261 cJSON *after_inserted = NULL;
2269 if (after_inserted == NULL)
2277 if (after_inserted == array->
child)
2433 else if (num <= (
double)INT_MIN)
2543 if ((
count < 0) || (numbers == NULL))
2548 a = cJSON_CreateArray();
2550 for(
i = 0;
a && (
i < (size_t)
count);
i++)
2552 n = cJSON_CreateNumber(numbers[
i]);
2584 if ((
count < 0) || (numbers == NULL))
2589 a = cJSON_CreateArray();
2591 for(
i = 0;
a && (
i < (size_t)
count);
i++)
2593 n = cJSON_CreateNumber((
double)numbers[
i]);
2625 if ((
count < 0) || (numbers == NULL))
2630 a = cJSON_CreateArray();
2632 for (
i = 0;
a && (
i < (size_t)
count);
i++)
2634 n = cJSON_CreateNumber(numbers[
i]);
2666 if ((
count < 0) || (strings == NULL))
2671 a = cJSON_CreateArray();
2673 for (
i = 0;
a && (
i < (size_t)
count);
i++)
2675 n = cJSON_CreateString(strings[
i]);
2704 cJSON *child = NULL;
2706 cJSON *newchild = NULL;
2746 while (child != NULL)
2748 newchild = cJSON_Duplicate(child,
true);
2756 next->next = newchild;
2766 child = child->
next;
2788 for (; (*input)[0] !=
'\0'; ++(*input))
2790 if ((*input)[0] ==
'\n') {
2801 for (; (*input)[0] !=
'\0'; ++(*input))
2803 if (((*input)[0] ==
'*') && ((*input)[1] ==
'/'))
2812 (*output)[0] = (*input)[0];
2817 for (; (*input)[0] !=
'\0'; (void)++(*input), ++(*output)) {
2818 (*output)[0] = (*input)[0];
2820 if ((*input)[0] ==
'\"') {
2821 (*output)[0] =
'\"';
2825 }
else if (((*input)[0] ==
'\\') && ((*input)[1] ==
'\"')) {
2826 (*output)[1] = (*input)[1];
2842 while (json[0] !=
'\0')
2858 else if (json[1] ==
'*')
2983 if ((
a == NULL) || (
b == NULL) || ((
a->type & 0xFF) != (
b->type & 0xFF)) || cJSON_IsInvalid(
a))
2989 switch (
a->type & 0xFF)
3011 switch (
a->type & 0xFF)
3028 if ((
a->valuestring == NULL) || (
b->valuestring == NULL))
3032 if (strcmp(
a->valuestring,
b->valuestring) == 0)
3041 cJSON *a_element =
a->child;
3042 cJSON *b_element =
b->child;
3044 for (; (a_element != NULL) && (b_element != NULL);)
3051 a_element = a_element->
next;
3052 b_element = b_element->
next;
3056 if (a_element != b_element) {
3065 cJSON *a_element = NULL;
3066 cJSON *b_element = NULL;
3071 if (b_element == NULL)
3087 if (a_element == NULL)