Changes float print format, removes array/object capacity limit, doesn't accept inf/nan numbers.
This commit is contained in:
Родитель
e18751499d
Коммит
4e8a901242
30
parson.c
30
parson.c
|
@ -39,11 +39,9 @@
|
|||
* don't have to. */
|
||||
#define sscanf THINK_TWICE_ABOUT_USING_SSCANF
|
||||
|
||||
#define STARTING_CAPACITY 15
|
||||
#define ARRAY_MAX_CAPACITY 122880 /* 15*(2^13) */
|
||||
#define OBJECT_MAX_CAPACITY 960 /* 15*(2^6) */
|
||||
#define MAX_NESTING 2048
|
||||
#define DOUBLE_SERIALIZATION_FORMAT "%f"
|
||||
#define STARTING_CAPACITY 16
|
||||
#define MAX_NESTING 2048
|
||||
#define FLOAT_FORMAT "%1.17g"
|
||||
|
||||
#define SIZEOF_TOKEN(a) (sizeof(a) - 1)
|
||||
#define SKIP_CHAR(str) ((*str)++)
|
||||
|
@ -354,9 +352,6 @@ static JSON_Status json_object_add(JSON_Object *object, const char *name, JSON_V
|
|||
}
|
||||
if (object->count >= object->capacity) {
|
||||
size_t new_capacity = MAX(object->capacity * 2, STARTING_CAPACITY);
|
||||
if (new_capacity > OBJECT_MAX_CAPACITY) {
|
||||
return JSONFailure;
|
||||
}
|
||||
if (json_object_resize(object, new_capacity) == JSONFailure) {
|
||||
return JSONFailure;
|
||||
}
|
||||
|
@ -443,9 +438,6 @@ static JSON_Array * json_array_init(JSON_Value *wrapping_value) {
|
|||
static JSON_Status json_array_add(JSON_Array *array, JSON_Value *value) {
|
||||
if (array->count >= array->capacity) {
|
||||
size_t new_capacity = MAX(array->capacity * 2, STARTING_CAPACITY);
|
||||
if (new_capacity > ARRAY_MAX_CAPACITY) {
|
||||
return JSONFailure;
|
||||
}
|
||||
if (json_array_resize(array, new_capacity) == JSONFailure) {
|
||||
return JSONFailure;
|
||||
}
|
||||
|
@ -929,13 +921,7 @@ static int json_serialize_to_buffer_r(const JSON_Value *value, char *buf, int le
|
|||
if (buf != NULL) {
|
||||
num_buf = buf;
|
||||
}
|
||||
if (num == ((double)(int)num)) { /* check if num is integer */
|
||||
written = sprintf(num_buf, "%d", (int)num);
|
||||
} else if (num == ((double)(unsigned int)num)) {
|
||||
written = sprintf(num_buf, "%u", (unsigned int)num);
|
||||
} else {
|
||||
written = sprintf(num_buf, DOUBLE_SERIALIZATION_FORMAT, num);
|
||||
}
|
||||
written = sprintf(num_buf, FLOAT_FORMAT, num);
|
||||
if (written < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
@ -1315,8 +1301,12 @@ JSON_Value * json_value_init_string(const char *string) {
|
|||
}
|
||||
|
||||
JSON_Value * json_value_init_number(double number) {
|
||||
JSON_Value *new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value));
|
||||
if (!new_value) {
|
||||
JSON_Value *new_value = NULL;
|
||||
if ((number * 0.0) != 0.0) { /* nan and inf test */
|
||||
return NULL;
|
||||
}
|
||||
new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value));
|
||||
if (new_value == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
new_value->parent = NULL;
|
||||
|
|
25
tests.c
25
tests.c
|
@ -436,6 +436,10 @@ void test_suite_5(void) {
|
|||
TEST(json_value_equals(remove_test_val, json_parse_string("[2, 4, 5]")));
|
||||
json_array_remove(remove_test_arr, 2);
|
||||
TEST(json_value_equals(remove_test_val, json_parse_string("[2, 4]")));
|
||||
|
||||
/* Testing nan and inf */
|
||||
TEST(json_object_set_number(obj, "num", 0.0 / 0.0) == JSONFailure);
|
||||
TEST(json_object_set_number(obj, "num", 1.0 / 0.0) == JSONFailure);
|
||||
}
|
||||
|
||||
void test_suite_6(void) {
|
||||
|
@ -505,28 +509,29 @@ void test_suite_9(void) {
|
|||
TEST((strlen(serialized)+1) == serialization_size);
|
||||
|
||||
file_contents = read_file(filename);
|
||||
|
||||
TEST(STREQ(file_contents, serialized));
|
||||
}
|
||||
|
||||
void test_suite_10(void) {
|
||||
JSON_Value *val1;
|
||||
JSON_Value *val;
|
||||
char *serialized;
|
||||
|
||||
malloc_count = 0;
|
||||
|
||||
val1 = json_parse_file("tests/test_1_1.txt");
|
||||
json_value_free(val1);
|
||||
val = json_parse_file("tests/test_1_1.txt");
|
||||
json_value_free(val);
|
||||
|
||||
val1 = json_parse_file("tests/test_1_3.txt");
|
||||
json_value_free(val1);
|
||||
val = json_parse_file("tests/test_1_3.txt");
|
||||
json_value_free(val);
|
||||
|
||||
val1 = json_parse_file("tests/test_2.txt");
|
||||
serialized = json_serialize_to_string_pretty(val1);
|
||||
val = json_parse_file("tests/test_2.txt");
|
||||
serialized = json_serialize_to_string_pretty(val);
|
||||
json_free_serialized_string(serialized);
|
||||
json_value_free(val1);
|
||||
json_value_free(val);
|
||||
|
||||
val1 = json_parse_file("tests/test_2_pretty.txt");
|
||||
json_value_free(val1);
|
||||
val = json_parse_file("tests/test_2_pretty.txt");
|
||||
json_value_free(val);
|
||||
|
||||
TEST(malloc_count == 0);
|
||||
}
|
||||
|
|
|
@ -7,6 +7,8 @@
|
|||
"negative one" : -1,
|
||||
"pi" : 3.14,
|
||||
"hard to parse number" : -3.14e-4,
|
||||
"big int": 2147483647,
|
||||
"big uint": 4294967295,
|
||||
"boolean true" : true,
|
||||
"boolean false" : false,
|
||||
"null" : null,
|
||||
|
|
|
@ -13,6 +13,8 @@
|
|||
"negative one" : -1,
|
||||
"pi" : 3.14,
|
||||
"hard to parse number" : -3.14e-4,
|
||||
"big int": 2147483647,
|
||||
"big uint": 4294967295,
|
||||
"boolean true" : true,
|
||||
"boolean false" : false,
|
||||
"null" : null,
|
||||
|
|
|
@ -5,8 +5,10 @@
|
|||
"surrogate string": "lorem𝄞ipsum𝍧lorem",
|
||||
"positive one": 1,
|
||||
"negative one": -1,
|
||||
"pi": 3.140000,
|
||||
"hard to parse number": -0.000314,
|
||||
"pi": 3.1400000000000001,
|
||||
"hard to parse number": -0.00031399999999999999,
|
||||
"big int": 2147483647,
|
||||
"big uint": 4294967295,
|
||||
"boolean true": true,
|
||||
"boolean false": false,
|
||||
"null": null,
|
||||
|
|
Загрузка…
Ссылка в новой задаче