Skip to content

Commit

Permalink
Update cJSON from 1.7.13 to 1.7.14 (released 3 September 2020). The s…
Browse files Browse the repository at this point in the history
…tructure of linkedlist has been changed to improve the efficiency of adding items to arrays/objects.

Signed-off-by: DL6ER <[email protected]>
  • Loading branch information
DL6ER committed Sep 3, 2020
1 parent 3f918fe commit fd2f8d7
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 16 deletions.
47 changes: 34 additions & 13 deletions src/cJSON/cJSON.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ CJSON_PUBLIC(const char *) cJSON_GetErrorPtr(void)
return (const char*) (global_error.json + global_error.position);
}

CJSON_PUBLIC(char *) cJSON_GetStringValue(cJSON *item)
CJSON_PUBLIC(char *) cJSON_GetStringValue(const cJSON * const item)
{
if (!cJSON_IsString(item))
{
Expand All @@ -102,18 +102,18 @@ CJSON_PUBLIC(char *) cJSON_GetStringValue(cJSON *item)
return item->valuestring;
}

CJSON_PUBLIC(double) cJSON_GetNumberValue(cJSON *item)
CJSON_PUBLIC(double) cJSON_GetNumberValue(const cJSON * const item)
{
if (!cJSON_IsNumber(item))
{
return NAN;
return (double) NAN;
}

return item->valuedouble;
}

/* This is a safeguard to prevent copy-pasters from using incompatible C and header files */
#if (CJSON_VERSION_MAJOR != 1) || (CJSON_VERSION_MINOR != 7) || (CJSON_VERSION_PATCH != 13)
#if (CJSON_VERSION_MAJOR != 1) || (CJSON_VERSION_MINOR != 7) || (CJSON_VERSION_PATCH != 14)
#error cJSON.h and cJSON.c have different versions. Make sure that both have the same.
#endif

Expand Down Expand Up @@ -1509,6 +1509,10 @@ static cJSON_bool parse_array(cJSON * const item, parse_buffer * const input_buf
success:
input_buffer->depth--;

if (head != NULL) {
head->prev = current_item;
}

item->type = cJSON_Array;
item->child = head;

Expand Down Expand Up @@ -1681,6 +1685,10 @@ static cJSON_bool parse_object(cJSON * const item, parse_buffer * const input_bu
success:
input_buffer->depth--;

if (head != NULL) {
head->prev = current_item;
}

item->type = cJSON_Object;
item->child = head;

Expand Down Expand Up @@ -1967,15 +1975,6 @@ static cJSON_bool add_item_to_array(cJSON *array, cJSON *item)
suffix_object(child->prev, item);
array->child->prev = item;
}
else
{
while (child->next)
{
child = child->next;
}
suffix_object(child, item);
array->child->prev = item;
}
}

return true;
Expand Down Expand Up @@ -2202,6 +2201,12 @@ CJSON_PUBLIC(cJSON *) cJSON_DetachItemViaPointer(cJSON *parent, cJSON * const it
/* first element */
parent->child = item->next;
}
else if (item->next == NULL)
{
/* last element */
parent->child->prev = item->prev;
}

/* make sure the detached item doesn't point anywhere anymore */
item->prev = NULL;
item->next = NULL;
Expand Down Expand Up @@ -2299,6 +2304,10 @@ CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemViaPointer(cJSON * const parent, cJSON
}
if (parent->child == item)
{
if (parent->child->prev == parent->child)
{
replacement->prev = replacement;
}
parent->child = replacement;
}
else
Expand All @@ -2310,6 +2319,10 @@ CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemViaPointer(cJSON * const parent, cJSON
{
replacement->prev->next = replacement;
}
if (replacement->next == NULL)
{
parent->child->prev = replacement;
}
}

item->next = NULL;
Expand Down Expand Up @@ -2549,6 +2562,7 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateIntArray(const int *numbers, int count)
}
p = n;
}
a->child->prev = n;

return a;
}
Expand Down Expand Up @@ -2585,6 +2599,7 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateFloatArray(const float *numbers, int count)
}
p = n;
}
a->child->prev = n;

return a;
}
Expand Down Expand Up @@ -2621,6 +2636,7 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateDoubleArray(const double *numbers, int count)
}
p = n;
}
a->child->prev = n;

return a;
}
Expand Down Expand Up @@ -2657,6 +2673,7 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateStringArray(const char *const *strings, int co
}
p = n;
}
a->child->prev = n;

return a;
}
Expand Down Expand Up @@ -2729,6 +2746,10 @@ CJSON_PUBLIC(cJSON *) cJSON_Duplicate(const cJSON *item, cJSON_bool recurse)
}
child = child->next;
}
if (newitem && newitem->child)
{
newitem->child->prev = newchild;
}

return newitem;

Expand Down
6 changes: 3 additions & 3 deletions src/cJSON/cJSON.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ then using the CJSON_API_VISIBILITY flag to "export" the same symbols the way CJ
/* project version */
#define CJSON_VERSION_MAJOR 1
#define CJSON_VERSION_MINOR 7
#define CJSON_VERSION_PATCH 13
#define CJSON_VERSION_PATCH 14

#include <stddef.h>

Expand Down Expand Up @@ -176,8 +176,8 @@ CJSON_PUBLIC(cJSON_bool) cJSON_HasObjectItem(const cJSON *object, const char *st
CJSON_PUBLIC(const char *) cJSON_GetErrorPtr(void);

/* Check item type and return its value */
CJSON_PUBLIC(char *) cJSON_GetStringValue(cJSON *item);
CJSON_PUBLIC(double) cJSON_GetNumberValue(cJSON *item);
CJSON_PUBLIC(char *) cJSON_GetStringValue(const cJSON * const item);
CJSON_PUBLIC(double) cJSON_GetNumberValue(const cJSON * const item);

/* These functions check the type of an item */
CJSON_PUBLIC(cJSON_bool) cJSON_IsInvalid(const cJSON * const item);
Expand Down

0 comments on commit fd2f8d7

Please sign in to comment.