Skip to content

Commit

Permalink
Update json
Browse files Browse the repository at this point in the history
  • Loading branch information
hung-eoh committed Apr 4, 2024
1 parent 57c0faa commit 619c52a
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 28 deletions.
128 changes: 108 additions & 20 deletions src/Utility/cJSON.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,63 @@ static cJSON *cJSON_New_Item(const internal_hooks * const hooks)
return node;
}

CJSON_PUBLIC(cJSON_Int_t) cJSON_SetIntHelper(cJSON *object, cJSON_Int_t number)
{
/* return CJSON_INT_MIN if the object is corrupted */
if (object == NULL)
{
return CJSON_INT_MIN;
}

object->valuedouble = (double)number;
return object->valueint = number;
}

CJSON_PUBLIC(char*) cJSON_SetValueString(cJSON *object, const char *valuestring)
{
char *copy = NULL;
/* if object's type is not cJSON_String or is cJSON_IsReference, it should not set valuestring */
if ((object == NULL) || !(object->type & cJSON_String) || (object->type & cJSON_IsReference))
{
return NULL;
}
/* return NULL if the object is corrupted */
if (object->valuestring == NULL)
{
return NULL;
}
if (strlen(valuestring) <= strlen(object->valuestring))
{
strcpy(object->valuestring, valuestring);
return object->valuestring;
}
copy = (char*) cJSON_strdup((const unsigned char*)valuestring, &global_hooks);
if (copy == NULL)
{
return NULL;
}
if (object->valuestring != NULL)
{
cJSON_free(object->valuestring);
}
object->valuestring = copy;

return copy;
}

CJSON_PUBLIC(int) cJSON_SetBoolHelper(cJSON *object, const cJSON_bool boolean)
{
/* if object's type is not cJSON_False or is cJSON_True, it should not set type, valueint and valuedouble */
if ((object == NULL) || !(object->type & (cJSON_False | cJSON_True)))
{
return cJSON_Invalid;
}

object->valueint = boolean ? 1 : 0;
object->valuedouble = boolean ? 1 : 0;
return object->type = ((object->type & (~(cJSON_False | cJSON_True))) | (boolean ? cJSON_True : cJSON_False));
}

CJSON_PUBLIC(cJSON *) cJSON_CreateRawNumber(const char *raw)
{
cJSON *item = cJSON_New_Item(&global_hooks);
Expand All @@ -249,13 +306,13 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateRawNumber(const char *raw)
item->valuedouble = num;

/* use saturation in case of overflow */
if (num >= ERA_INT_MAX)
if (num >= CJSON_INT_MAX)
{
item->valueint = ERA_INT_MAX;
item->valueint = CJSON_INT_MAX;
}
else if (num <= (double)ERA_INT_MIN)
else if (num <= (double)CJSON_INT_MIN)
{
item->valueint = ERA_INT_MIN;
item->valueint = CJSON_INT_MIN;
}
else
{
Expand Down Expand Up @@ -589,13 +646,13 @@ static cJSON_bool parse_number(cJSON * const item, parse_buffer * const input_bu
item->valuedouble = number;

/* use saturation in case of overflow */
if (number >= ERA_INT_MAX)
if (number >= CJSON_INT_MAX)
{
item->valueint = ERA_INT_MAX;
item->valueint = CJSON_INT_MAX;
}
else if (number <= (double)ERA_INT_MIN)
else if (number <= (double)CJSON_INT_MIN)
{
item->valueint = ERA_INT_MIN;
item->valueint = CJSON_INT_MIN;
}
else
{
Expand All @@ -608,16 +665,34 @@ static cJSON_bool parse_number(cJSON * const item, parse_buffer * const input_bu
return true;
}

CJSON_PUBLIC(cJSON_Int_t) cJSON_SetIntHelper(cJSON *object, cJSON_Int_t number)
{
/* return CJSON_INT_MIN if the object is corrupted */
if (object == NULL)
{
return CJSON_INT_MIN;
}

object->valuedouble = (double)number;
return object->valueint = number;
}

/* don't ask me, but the original cJSON_SetNumberValue returns an integer or double */
CJSON_PUBLIC(double) cJSON_SetNumberHelper(cJSON *object, double number)
{
if (number >= ERA_INT_MAX)
/* return NAN if the object is corrupted */
if (object == NULL)
{
return NAN;
}

if (number >= CJSON_INT_MAX)
{
object->valueint = ERA_INT_MAX;
object->valueint = CJSON_INT_MAX;
}
else if (number <= (double)ERA_INT_MIN)
else if (number <= (double)CJSON_INT_MIN)
{
object->valueint = ERA_INT_MIN;
object->valueint = CJSON_INT_MIN;
}
else
{
Expand Down Expand Up @@ -659,6 +734,19 @@ CJSON_PUBLIC(char*) cJSON_SetValueString(cJSON *object, const char *valuestring)
return copy;
}

CJSON_PUBLIC(int) cJSON_SetBoolHelper(cJSON *object, const cJSON_bool boolean)
{
/* if object's type is not cJSON_False or is cJSON_True, it should not set type, valueint and valuedouble */
if ((object == NULL) || !(object->type & (cJSON_False | cJSON_True)))
{
return cJSON_Invalid;
}

object->valueint = boolean ? 1 : 0;
object->valuedouble = boolean ? 1 : 0;
return object->type = ((object->type & (~(cJSON_False | cJSON_True))) | (boolean ? cJSON_True : cJSON_False));
}

typedef struct
{
unsigned char *buffer;
Expand Down Expand Up @@ -2726,13 +2814,13 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateNumber(double num)
item->valuedouble = num;

/* use saturation in case of overflow */
if (num >= ERA_INT_MAX)
if (num >= CJSON_INT_MAX)
{
item->valueint = ERA_INT_MAX;
item->valueint = CJSON_INT_MAX;
}
else if (num <= (double)ERA_INT_MIN)
else if (num <= (double)CJSON_INT_MIN)
{
item->valueint = ERA_INT_MIN;
item->valueint = CJSON_INT_MIN;
}
else
{
Expand Down Expand Up @@ -2820,13 +2908,13 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateRawNumber(const char *raw)
item->valuedouble = num;

/* use saturation in case of overflow */
if (num >= ERA_INT_MAX)
if (num >= CJSON_INT_MAX)
{
item->valueint = ERA_INT_MAX;
item->valueint = CJSON_INT_MAX;
}
else if (num <= (double)ERA_INT_MIN)
else if (num <= (double)CJSON_INT_MIN)
{
item->valueint = ERA_INT_MIN;
item->valueint = CJSON_INT_MIN;
}
else
{
Expand Down
16 changes: 8 additions & 8 deletions src/Utility/cJSON.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -288,19 +288,19 @@ CJSON_PUBLIC(cJSON*) cJSON_AddArrayToObject(cJSON * const object, const char * c
CJSON_PUBLIC(cJSON_bool) cJSON_Rename(cJSON * const object, const char * const name);

/* When assigning an integer value, it needs to be propagated to valuedouble too. */
#define cJSON_SetIntValue(object, number) ((object) ? (object)->valueint = (object)->valuedouble = (number) : (number))
/* helper for the cJSON_SetNumberValue macro */
/* Helper for the cJSON_SetIntValue macro */
CJSON_PUBLIC(cJSON_Int_t) cJSON_SetIntHelper(cJSON *object, cJSON_Int_t number);
#define cJSON_SetIntValue(object, number) cJSON_SetIntHelper(object, cJSON_Int_t(number))
/* Helper for the cJSON_SetNumberValue macro */
CJSON_PUBLIC(double) cJSON_SetNumberHelper(cJSON *object, double number);
#define cJSON_SetNumberValue(object, number) ((object != NULL) ? cJSON_SetNumberHelper(object, (double)number) : (number))
#define cJSON_SetNumberValue(object, number) cJSON_SetNumberHelper(object, (double)number)
/* Change the valuestring of a cJSON_String object, only takes effect when type of object is cJSON_String */
CJSON_PUBLIC(char*) cJSON_SetValueString(cJSON *object, const char *valuestring);

/* If the object is not a boolean type this does nothing and returns cJSON_Invalid else it returns the new type */
#define cJSON_SetBoolValue(object, boolean) ( \
(object != NULL && ((object)->type & (cJSON_False|cJSON_True))) ? \
(object)->type=((object)->type &(~(cJSON_False|cJSON_True)))|((boolean)?cJSON_True:cJSON_False) : \
cJSON_Invalid\
)
/* Helper for the cJSON_SetBoolValue macro */
CJSON_PUBLIC(int) cJSON_SetBoolHelper(cJSON *object, const cJSON_bool boolean);
#define cJSON_SetBoolValue(object, boolean) cJSON_SetBoolHelper(object, (cJSON_bool)boolean)

/* Macro for iterating over an array or object */
#define cJSON_ArrayForEach(element, array) for(element = (array != NULL) ? (array)->child : NULL; element != NULL; element = element->next)
Expand Down

0 comments on commit 619c52a

Please sign in to comment.