mirror of
https://github.com/DaveGamble/cJSON.git
synced 2023-08-10 21:13:26 +03:00
Merge e6178a0e23a0adc69733d981f5c6840b9be0f04f into cb8693b058ba302f4829ec6d03f609ac6f848546
This commit is contained in:
commit
713b0a07ff
42
cJSON.c
42
cJSON.c
@ -568,14 +568,20 @@ static cJSON_bool print_number(const cJSON * const item, printbuffer * const out
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
/* Try 15 decimal places of precision to avoid nonsignificant nonzero digits */
|
/* If decimal places are present and value is not an integer type */
|
||||||
length = sprintf((char*)number_buffer, "%1.15g", d);
|
if (item->decimal_places && (ceil(d) != d)) {
|
||||||
|
/* Then convert it to string */
|
||||||
|
length = sprintf((char*) number_buffer, "%.*f", item->decimal_places, d);
|
||||||
|
} else {
|
||||||
|
/* Try 15 decimal places of precision to avoid nonsignificant nonzero digits */
|
||||||
|
length = sprintf((char*)number_buffer, "%1.15g", d);
|
||||||
|
|
||||||
/* Check whether the original double can be recovered */
|
/* Check whether the original double can be recovered */
|
||||||
if ((sscanf((char*)number_buffer, "%lg", &test) != 1) || !compare_double((double)test, d))
|
if ((sscanf((char*)number_buffer, "%lg", &test) != 1) || !compare_double((double)test, d))
|
||||||
{
|
{
|
||||||
/* If not, print with 17 decimal places of precision */
|
/* If not, print with 17 decimal places of precision */
|
||||||
length = sprintf((char*)number_buffer, "%1.17g", d);
|
length = sprintf((char*)number_buffer, "%1.17g", d);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -2136,6 +2142,28 @@ CJSON_PUBLIC(cJSON*) cJSON_AddNumberToObject(cJSON * const object, const char *
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
CJSON_PUBLIC(cJSON*) cJSON_AddNumberWithPrecisionToObject(cJSON * const object, const char * const name, const double n, int decimal_places)
|
||||||
|
{
|
||||||
|
cJSON *number_item;
|
||||||
|
|
||||||
|
if (decimal_places <= 0 || decimal_places > 14)
|
||||||
|
{
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
number_item = cJSON_CreateNumber(n);
|
||||||
|
|
||||||
|
number_item->decimal_places = decimal_places;
|
||||||
|
|
||||||
|
if (add_item_to_object(object, name, number_item, &global_hooks, false))
|
||||||
|
{
|
||||||
|
return number_item;
|
||||||
|
}
|
||||||
|
|
||||||
|
cJSON_Delete(number_item);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
CJSON_PUBLIC(cJSON*) cJSON_AddStringToObject(cJSON * const object, const char * const name, const char * const string)
|
CJSON_PUBLIC(cJSON*) cJSON_AddStringToObject(cJSON * const object, const char * const name, const char * const string)
|
||||||
{
|
{
|
||||||
cJSON *string_item = cJSON_CreateString(string);
|
cJSON *string_item = cJSON_CreateString(string);
|
||||||
|
3
cJSON.h
3
cJSON.h
@ -117,6 +117,8 @@ typedef struct cJSON
|
|||||||
int valueint;
|
int valueint;
|
||||||
/* The item's number, if type==cJSON_Number */
|
/* The item's number, if type==cJSON_Number */
|
||||||
double valuedouble;
|
double valuedouble;
|
||||||
|
/* Decimal places */
|
||||||
|
int decimal_places;
|
||||||
|
|
||||||
/* The item's name string, if this item is the child of, or is in the list of subitems of an object. */
|
/* The item's name string, if this item is the child of, or is in the list of subitems of an object. */
|
||||||
char *string;
|
char *string;
|
||||||
@ -266,6 +268,7 @@ CJSON_PUBLIC(cJSON*) cJSON_AddTrueToObject(cJSON * const object, const char * co
|
|||||||
CJSON_PUBLIC(cJSON*) cJSON_AddFalseToObject(cJSON * const object, const char * const name);
|
CJSON_PUBLIC(cJSON*) cJSON_AddFalseToObject(cJSON * const object, const char * const name);
|
||||||
CJSON_PUBLIC(cJSON*) cJSON_AddBoolToObject(cJSON * const object, const char * const name, const cJSON_bool boolean);
|
CJSON_PUBLIC(cJSON*) cJSON_AddBoolToObject(cJSON * const object, const char * const name, const cJSON_bool boolean);
|
||||||
CJSON_PUBLIC(cJSON*) cJSON_AddNumberToObject(cJSON * const object, const char * const name, const double number);
|
CJSON_PUBLIC(cJSON*) cJSON_AddNumberToObject(cJSON * const object, const char * const name, const double number);
|
||||||
|
CJSON_PUBLIC(cJSON*) cJSON_AddNumberWithPrecisionToObject(cJSON * const object, const char * const name, const double n, int decimal_places);
|
||||||
CJSON_PUBLIC(cJSON*) cJSON_AddStringToObject(cJSON * const object, const char * const name, const char * const string);
|
CJSON_PUBLIC(cJSON*) cJSON_AddStringToObject(cJSON * const object, const char * const name, const char * const string);
|
||||||
CJSON_PUBLIC(cJSON*) cJSON_AddRawToObject(cJSON * const object, const char * const name, const char * const raw);
|
CJSON_PUBLIC(cJSON*) cJSON_AddRawToObject(cJSON * const object, const char * const name, const char * const raw);
|
||||||
CJSON_PUBLIC(cJSON*) cJSON_AddObjectToObject(cJSON * const object, const char * const name);
|
CJSON_PUBLIC(cJSON*) cJSON_AddObjectToObject(cJSON * const object, const char * const name);
|
||||||
|
@ -840,7 +840,7 @@ static int apply_patch(cJSON *object, const cJSON *patch, const cJSON_bool case_
|
|||||||
{
|
{
|
||||||
if (opcode == REMOVE)
|
if (opcode == REMOVE)
|
||||||
{
|
{
|
||||||
static const cJSON invalid = { NULL, NULL, NULL, cJSON_Invalid, NULL, 0, 0, NULL};
|
static const cJSON invalid = { NULL, NULL, NULL, cJSON_Invalid, NULL, 0, 0, 0, NULL};
|
||||||
|
|
||||||
overwrite_item(object, invalid);
|
overwrite_item(object, invalid);
|
||||||
|
|
||||||
|
@ -221,7 +221,7 @@ static void cjson_should_not_parse_to_deeply_nested_jsons(void)
|
|||||||
|
|
||||||
static void cjson_set_number_value_should_set_numbers(void)
|
static void cjson_set_number_value_should_set_numbers(void)
|
||||||
{
|
{
|
||||||
cJSON number[1] = {{NULL, NULL, NULL, cJSON_Number, NULL, 0, 0, NULL}};
|
cJSON number[1] = {{NULL, NULL, NULL, cJSON_Number, NULL, 0, 0, 0, NULL}};
|
||||||
|
|
||||||
cJSON_SetNumberValue(number, 1.5);
|
cJSON_SetNumberValue(number, 1.5);
|
||||||
TEST_ASSERT_EQUAL(1, number->valueint);
|
TEST_ASSERT_EQUAL(1, number->valueint);
|
||||||
@ -328,7 +328,7 @@ static void cjson_replace_item_via_pointer_should_replace_items(void)
|
|||||||
|
|
||||||
static void cjson_replace_item_in_object_should_preserve_name(void)
|
static void cjson_replace_item_in_object_should_preserve_name(void)
|
||||||
{
|
{
|
||||||
cJSON root[1] = {{NULL, NULL, NULL, 0, NULL, 0, 0, NULL}};
|
cJSON root[1] = {{NULL, NULL, NULL, 0, NULL, 0, 0, 0, NULL}};
|
||||||
cJSON *child = NULL;
|
cJSON *child = NULL;
|
||||||
cJSON *replacement = NULL;
|
cJSON *replacement = NULL;
|
||||||
cJSON_bool flag = false;
|
cJSON_bool flag = false;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user