diff --git a/parson.c b/parson.c index 943b898..79c2511 100644 --- a/parson.c +++ b/parson.c @@ -35,7 +35,7 @@ union json_value_value { double number; JSON_Object *object; JSON_Array *array; - int bool; + int boolean; int null; }; @@ -72,7 +72,7 @@ static JSON_Value * json_value_init_object(); static JSON_Value * json_value_init_array(); static JSON_Value * json_value_init_string(const char *string); static JSON_Value * json_value_init_number(double number); -static JSON_Value * json_value_init_bool(int bool); +static JSON_Value * json_value_init_boolean(int boolean); static JSON_Value * json_value_init_null(); /* Parser */ @@ -84,7 +84,7 @@ static const char * get_string(const char **string); static JSON_Value * parse_object_value(const char **string); static JSON_Value * parse_array_value(const char **string); static JSON_Value * parse_string_value(const char **string); -static JSON_Value * parse_bool_value(const char **string); +static JSON_Value * parse_boolean_value(const char **string); static JSON_Value * parse_number_value(const char **string); static JSON_Value * parse_null_value(const char **string); static JSON_Value * parse_value(const char **string); @@ -183,10 +183,10 @@ static JSON_Value * json_value_init_number(double number) { return new_value; } -static JSON_Value * json_value_init_bool(int bool) { +static JSON_Value * json_value_init_boolean(int boolean) { JSON_Value *new_value = (JSON_Value*)malloc(sizeof(JSON_Value)); - new_value->type = JSONBool; - new_value->value.bool = bool; + new_value->type = JSONBoolean; + new_value->value.boolean = boolean; return new_value; } @@ -329,7 +329,7 @@ static JSON_Value * parse_value(const char **string) { break; case 'f': case 't': - output_value = parse_bool_value(string); + output_value = parse_boolean_value(string); break; case '-': case '0': case '1': case '2': case '3': case '4': @@ -411,15 +411,15 @@ static JSON_Value * parse_string_value(const char **string) { return json_value_init_string(new_string); } -static JSON_Value * parse_bool_value(const char **string) { +static JSON_Value * parse_boolean_value(const char **string) { size_t true_token_size = sizeof_token("true"); size_t false_token_size = sizeof_token("false"); if (strncmp("true", *string, true_token_size) == 0) { *string += true_token_size; - return json_value_init_bool(1); + return json_value_init_boolean(1); } else if (strncmp("false", *string, false_token_size) == 0) { *string += false_token_size; - return json_value_init_bool(0); + return json_value_init_boolean(0); } return NULL; } @@ -494,8 +494,8 @@ JSON_Array * json_object_get_array(const JSON_Object *object, const char *name) return json_value_get_array(json_object_get_value(object, name)); } -int json_object_get_bool(const JSON_Object *object, const char *name) { - return json_value_get_bool(json_object_get_value(object, name)); +int json_object_get_boolean(const JSON_Object *object, const char *name) { + return json_value_get_boolean(json_object_get_value(object, name)); } JSON_Value * json_object_dotget_value(const JSON_Object *object, const char *name) { @@ -525,8 +525,8 @@ JSON_Array * json_object_dotget_array(const JSON_Object *object, const char *nam return json_value_get_array(json_object_dotget_value(object, name)); } -int json_object_dotget_bool(const JSON_Object *object, const char *name) { - return json_value_get_bool(json_object_dotget_value(object, name)); +int json_object_dotget_boolean(const JSON_Object *object, const char *name) { + return json_value_get_boolean(json_object_dotget_value(object, name)); } /* JSON Array API */ @@ -551,8 +551,8 @@ JSON_Array * json_array_get_array(const JSON_Array *array, size_t index) { return json_value_get_array(json_array_get_value(array, index)); } -int json_array_get_bool(const JSON_Array *array, size_t index) { - return json_value_get_bool(json_array_get_value(array, index)); +int json_array_get_boolean(const JSON_Array *array, size_t index) { + return json_value_get_boolean(json_array_get_value(array, index)); } size_t json_array_get_count(const JSON_Array *array) { @@ -584,9 +584,9 @@ double json_value_get_number(const JSON_Value *value) { return value->value.number; } -int json_value_get_bool(const JSON_Value *value) { - if (value == NULL || value->type != JSONBool) { return -1; } - return value->value.bool; +int json_value_get_boolean(const JSON_Value *value) { + if (value == NULL || value->type != JSONBoolean) { return -1; } + return value->value.boolean; } void json_value_free(JSON_Value *value) { diff --git a/parson.h b/parson.h index bd94006..ca6d051 100644 --- a/parson.h +++ b/parson.h @@ -41,7 +41,7 @@ enum json_value_type { JSONNumber = 3, JSONObject = 4, JSONArray = 5, - JSONBool = 6 + JSONBoolean = 6 }; /* Parses first JSON value in a file, returns NULL in case of error */ @@ -56,7 +56,7 @@ const char * json_object_get_string(const JSON_Object *object, const char *name) double json_object_get_number(const JSON_Object *object, const char *name); JSON_Object * json_object_get_object(const JSON_Object *object, const char *name); JSON_Array * json_object_get_array(const JSON_Object *object, const char *name); -int json_object_get_bool(const JSON_Object *object, const char *name); +int json_object_get_boolean(const JSON_Object *object, const char *name); /* dotget functions enable addressing values with dot notation in nested objects, just like in structs or c++/java/c# objects (e.g. objectA.objectB.value). @@ -67,7 +67,7 @@ const char * json_object_dotget_string(const JSON_Object *object, const char *na double json_object_dotget_number(const JSON_Object *object, const char *name); JSON_Object * json_object_dotget_object(const JSON_Object *object, const char *name); JSON_Array * json_object_dotget_array(const JSON_Object *object, const char *name); -int json_object_dotget_bool(const JSON_Object *object, const char *name); +int json_object_dotget_boolean(const JSON_Object *object, const char *name); /* JSON Array */ JSON_Value * json_array_get_value(const JSON_Array *array, size_t index); @@ -75,7 +75,7 @@ const char * json_array_get_string(const JSON_Array *array, size_t index); double json_array_get_number(const JSON_Array *array, size_t index); JSON_Object * json_array_get_object(const JSON_Array *array, size_t index); JSON_Array * json_array_get_array(const JSON_Array *array, size_t index); -int json_array_get_bool(const JSON_Array *array, size_t index); +int json_array_get_boolean(const JSON_Array *array, size_t index); size_t json_array_get_count(const JSON_Array *array); /* JSON Value */ @@ -84,7 +84,7 @@ JSON_Object * json_value_get_object(const JSON_Value *value); JSON_Array * json_value_get_array(const JSON_Value *value); const char * json_value_get_string(const JSON_Value *value); double json_value_get_number(const JSON_Value *value); -int json_value_get_bool(const JSON_Value *value); +int json_value_get_boolean(const JSON_Value *value); void json_value_free(JSON_Value *value); #ifdef __cplusplus diff --git a/tests.c b/tests.c index 7b22ab3..9ba9285 100644 --- a/tests.c +++ b/tests.c @@ -27,7 +27,7 @@ #define TEST(A) printf("%-72s-",#A); \ if(A){puts(" OK");tests_passed++;} \ else{puts(" FAIL");tests_failed++;} -#define STREQ(A, B) (strcmp(A, B) == 0) +#define STREQ(A, B) (A && B ? strcmp(A, B) == 0 : 0) void test_suite_1(); void test_suite_2(); @@ -92,8 +92,8 @@ void test_suite_2() { TEST(json_object_get_number(object, "positive one") == 1.0); TEST(json_object_get_number(object, "negative one") == -1.0); TEST(json_object_get_number(object, "hard to parse number") == -0.000314); - TEST(json_object_get_bool(object, "bool true")); - TEST(!json_object_get_bool(object, "bool false")); + TEST(json_object_get_boolean(object, "boolean true") == 1); + TEST(json_object_get_boolean(object, "boolean false") == 0); TEST(json_value_get_type(json_object_get_value(object, "null")) == JSONNull); array = json_object_get_array(object, "string array"); @@ -115,8 +115,8 @@ void test_suite_2() { TEST(json_object_get_array(object, "non existent array") == NULL); TEST(STREQ(json_object_dotget_string(object, "object.nested string"), "str")); - TEST(json_object_dotget_bool(object, "object.nested true")); - TEST(!json_object_dotget_bool(object, "object.nested false")); + TEST(json_object_dotget_boolean(object, "object.nested true")); + TEST(!json_object_dotget_boolean(object, "object.nested false")); TEST(json_object_dotget_value(object, "object.nested null") != NULL); TEST(json_object_dotget_number(object, "object.nested number") == 123); @@ -132,7 +132,7 @@ void test_suite_2() { } else { tests_failed++; } - TEST(json_object_dotget_bool(object, "nested true")); + TEST(json_object_dotget_boolean(object, "nested true")); json_value_free(root_value); } diff --git a/tests/test_2.txt b/tests/test_2.txt index 717108b..90d53c4 100644 --- a/tests/test_2.txt +++ b/tests/test_2.txt @@ -5,8 +5,8 @@ "negative one" : -1, "pi" : 3.14, "hard to parse number" : -3.14e-4, - "bool true" : true, - "bool false" : false, + "boolean true" : true, + "boolean false" : false, "null" : null, "string array" : ["lorem", "ipsum"], "x^2 array" : [0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100],