mirror of
https://github.com/kgabis/parson.git
synced 2025-02-05 17:05:29 +00:00
Changes float print format, removes array/object capacity limit, doesn't accept inf/nan numbers.
This commit is contained in:
parent
e18751499d
commit
4e8a901242
28
parson.c
28
parson.c
@ -39,11 +39,9 @@
|
|||||||
* don't have to. */
|
* don't have to. */
|
||||||
#define sscanf THINK_TWICE_ABOUT_USING_SSCANF
|
#define sscanf THINK_TWICE_ABOUT_USING_SSCANF
|
||||||
|
|
||||||
#define STARTING_CAPACITY 15
|
#define STARTING_CAPACITY 16
|
||||||
#define ARRAY_MAX_CAPACITY 122880 /* 15*(2^13) */
|
|
||||||
#define OBJECT_MAX_CAPACITY 960 /* 15*(2^6) */
|
|
||||||
#define MAX_NESTING 2048
|
#define MAX_NESTING 2048
|
||||||
#define DOUBLE_SERIALIZATION_FORMAT "%f"
|
#define FLOAT_FORMAT "%1.17g"
|
||||||
|
|
||||||
#define SIZEOF_TOKEN(a) (sizeof(a) - 1)
|
#define SIZEOF_TOKEN(a) (sizeof(a) - 1)
|
||||||
#define SKIP_CHAR(str) ((*str)++)
|
#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) {
|
if (object->count >= object->capacity) {
|
||||||
size_t new_capacity = MAX(object->capacity * 2, STARTING_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) {
|
if (json_object_resize(object, new_capacity) == JSONFailure) {
|
||||||
return 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) {
|
static JSON_Status json_array_add(JSON_Array *array, JSON_Value *value) {
|
||||||
if (array->count >= array->capacity) {
|
if (array->count >= array->capacity) {
|
||||||
size_t new_capacity = MAX(array->capacity * 2, STARTING_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) {
|
if (json_array_resize(array, new_capacity) == JSONFailure) {
|
||||||
return 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) {
|
if (buf != NULL) {
|
||||||
num_buf = buf;
|
num_buf = buf;
|
||||||
}
|
}
|
||||||
if (num == ((double)(int)num)) { /* check if num is integer */
|
written = sprintf(num_buf, FLOAT_FORMAT, num);
|
||||||
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);
|
|
||||||
}
|
|
||||||
if (written < 0) {
|
if (written < 0) {
|
||||||
return -1;
|
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 * json_value_init_number(double number) {
|
||||||
JSON_Value *new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value));
|
JSON_Value *new_value = NULL;
|
||||||
if (!new_value) {
|
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;
|
return NULL;
|
||||||
}
|
}
|
||||||
new_value->parent = 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]")));
|
TEST(json_value_equals(remove_test_val, json_parse_string("[2, 4, 5]")));
|
||||||
json_array_remove(remove_test_arr, 2);
|
json_array_remove(remove_test_arr, 2);
|
||||||
TEST(json_value_equals(remove_test_val, json_parse_string("[2, 4]")));
|
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) {
|
void test_suite_6(void) {
|
||||||
@ -505,28 +509,29 @@ void test_suite_9(void) {
|
|||||||
TEST((strlen(serialized)+1) == serialization_size);
|
TEST((strlen(serialized)+1) == serialization_size);
|
||||||
|
|
||||||
file_contents = read_file(filename);
|
file_contents = read_file(filename);
|
||||||
|
|
||||||
TEST(STREQ(file_contents, serialized));
|
TEST(STREQ(file_contents, serialized));
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_suite_10(void) {
|
void test_suite_10(void) {
|
||||||
JSON_Value *val1;
|
JSON_Value *val;
|
||||||
char *serialized;
|
char *serialized;
|
||||||
|
|
||||||
malloc_count = 0;
|
malloc_count = 0;
|
||||||
|
|
||||||
val1 = json_parse_file("tests/test_1_1.txt");
|
val = json_parse_file("tests/test_1_1.txt");
|
||||||
json_value_free(val1);
|
json_value_free(val);
|
||||||
|
|
||||||
val1 = json_parse_file("tests/test_1_3.txt");
|
val = json_parse_file("tests/test_1_3.txt");
|
||||||
json_value_free(val1);
|
json_value_free(val);
|
||||||
|
|
||||||
val1 = json_parse_file("tests/test_2.txt");
|
val = json_parse_file("tests/test_2.txt");
|
||||||
serialized = json_serialize_to_string_pretty(val1);
|
serialized = json_serialize_to_string_pretty(val);
|
||||||
json_free_serialized_string(serialized);
|
json_free_serialized_string(serialized);
|
||||||
json_value_free(val1);
|
json_value_free(val);
|
||||||
|
|
||||||
val1 = json_parse_file("tests/test_2_pretty.txt");
|
val = json_parse_file("tests/test_2_pretty.txt");
|
||||||
json_value_free(val1);
|
json_value_free(val);
|
||||||
|
|
||||||
TEST(malloc_count == 0);
|
TEST(malloc_count == 0);
|
||||||
}
|
}
|
||||||
|
@ -7,6 +7,8 @@
|
|||||||
"negative one" : -1,
|
"negative one" : -1,
|
||||||
"pi" : 3.14,
|
"pi" : 3.14,
|
||||||
"hard to parse number" : -3.14e-4,
|
"hard to parse number" : -3.14e-4,
|
||||||
|
"big int": 2147483647,
|
||||||
|
"big uint": 4294967295,
|
||||||
"boolean true" : true,
|
"boolean true" : true,
|
||||||
"boolean false" : false,
|
"boolean false" : false,
|
||||||
"null" : null,
|
"null" : null,
|
||||||
|
@ -13,6 +13,8 @@
|
|||||||
"negative one" : -1,
|
"negative one" : -1,
|
||||||
"pi" : 3.14,
|
"pi" : 3.14,
|
||||||
"hard to parse number" : -3.14e-4,
|
"hard to parse number" : -3.14e-4,
|
||||||
|
"big int": 2147483647,
|
||||||
|
"big uint": 4294967295,
|
||||||
"boolean true" : true,
|
"boolean true" : true,
|
||||||
"boolean false" : false,
|
"boolean false" : false,
|
||||||
"null" : null,
|
"null" : null,
|
||||||
|
@ -5,8 +5,10 @@
|
|||||||
"surrogate string": "lorem𝄞ipsum𝍧lorem",
|
"surrogate string": "lorem𝄞ipsum𝍧lorem",
|
||||||
"positive one": 1,
|
"positive one": 1,
|
||||||
"negative one": -1,
|
"negative one": -1,
|
||||||
"pi": 3.140000,
|
"pi": 3.1400000000000001,
|
||||||
"hard to parse number": -0.000314,
|
"hard to parse number": -0.00031399999999999999,
|
||||||
|
"big int": 2147483647,
|
||||||
|
"big uint": 4294967295,
|
||||||
"boolean true": true,
|
"boolean true": true,
|
||||||
"boolean false": false,
|
"boolean false": false,
|
||||||
"null": null,
|
"null": null,
|
||||||
|
Loading…
Reference in New Issue
Block a user