Renamed bool to boolean.

Naming variables bool is a poor idea, even in C.
pull/1/head
Krzysztof Gabis 12 years ago
parent cad9bda1e4
commit 2740213c5c
  1. 38
      parson.c
  2. 10
      parson.h
  3. 12
      tests.c
  4. 4
      tests/test_2.txt

@ -35,7 +35,7 @@ union json_value_value {
double number; double number;
JSON_Object *object; JSON_Object *object;
JSON_Array *array; JSON_Array *array;
int bool; int boolean;
int null; 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_array();
static JSON_Value * json_value_init_string(const char *string); 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_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(); static JSON_Value * json_value_init_null();
/* Parser */ /* 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_object_value(const char **string);
static JSON_Value * parse_array_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_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_number_value(const char **string);
static JSON_Value * parse_null_value(const char **string); static JSON_Value * parse_null_value(const char **string);
static JSON_Value * parse_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; 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)); JSON_Value *new_value = (JSON_Value*)malloc(sizeof(JSON_Value));
new_value->type = JSONBool; new_value->type = JSONBoolean;
new_value->value.bool = bool; new_value->value.boolean = boolean;
return new_value; return new_value;
} }
@ -329,7 +329,7 @@ static JSON_Value * parse_value(const char **string) {
break; break;
case 'f': case 'f':
case 't': case 't':
output_value = parse_bool_value(string); output_value = parse_boolean_value(string);
break; break;
case '-': case '-':
case '0': case '1': case '2': case '3': case '4': 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); 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 true_token_size = sizeof_token("true");
size_t false_token_size = sizeof_token("false"); size_t false_token_size = sizeof_token("false");
if (strncmp("true", *string, true_token_size) == 0) { if (strncmp("true", *string, true_token_size) == 0) {
*string += true_token_size; *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) { } else if (strncmp("false", *string, false_token_size) == 0) {
*string += false_token_size; *string += false_token_size;
return json_value_init_bool(0); return json_value_init_boolean(0);
} }
return NULL; 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)); return json_value_get_array(json_object_get_value(object, 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) {
return json_value_get_bool(json_object_get_value(object, 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) { 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)); return json_value_get_array(json_object_dotget_value(object, 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) {
return json_value_get_bool(json_object_dotget_value(object, name)); return json_value_get_boolean(json_object_dotget_value(object, name));
} }
/* JSON Array API */ /* 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)); return json_value_get_array(json_array_get_value(array, 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) {
return json_value_get_bool(json_array_get_value(array, index)); return json_value_get_boolean(json_array_get_value(array, index));
} }
size_t json_array_get_count(const JSON_Array *array) { 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; return value->value.number;
} }
int json_value_get_bool(const JSON_Value *value) { int json_value_get_boolean(const JSON_Value *value) {
if (value == NULL || value->type != JSONBool) { return -1; } if (value == NULL || value->type != JSONBoolean) { return -1; }
return value->value.bool; return value->value.boolean;
} }
void json_value_free(JSON_Value *value) { void json_value_free(JSON_Value *value) {

@ -41,7 +41,7 @@ enum json_value_type {
JSONNumber = 3, JSONNumber = 3,
JSONObject = 4, JSONObject = 4,
JSONArray = 5, JSONArray = 5,
JSONBool = 6 JSONBoolean = 6
}; };
/* Parses first JSON value in a file, returns NULL in case of error */ /* 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); 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_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); 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, /* 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). 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); 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_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); 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 Array */
JSON_Value * json_array_get_value(const JSON_Array *array, size_t index); 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); 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_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); 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); size_t json_array_get_count(const JSON_Array *array);
/* JSON Value */ /* 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); JSON_Array * json_value_get_array(const JSON_Value *value);
const char * json_value_get_string(const JSON_Value *value); const char * json_value_get_string(const JSON_Value *value);
double json_value_get_number(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); void json_value_free(JSON_Value *value);
#ifdef __cplusplus #ifdef __cplusplus

@ -27,7 +27,7 @@
#define TEST(A) printf("%-72s-",#A); \ #define TEST(A) printf("%-72s-",#A); \
if(A){puts(" OK");tests_passed++;} \ if(A){puts(" OK");tests_passed++;} \
else{puts(" FAIL");tests_failed++;} 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_1();
void test_suite_2(); 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, "positive one") == 1.0);
TEST(json_object_get_number(object, "negative 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_number(object, "hard to parse number") == -0.000314);
TEST(json_object_get_bool(object, "bool true")); TEST(json_object_get_boolean(object, "boolean true") == 1);
TEST(!json_object_get_bool(object, "bool false")); TEST(json_object_get_boolean(object, "boolean false") == 0);
TEST(json_value_get_type(json_object_get_value(object, "null")) == JSONNull); TEST(json_value_get_type(json_object_get_value(object, "null")) == JSONNull);
array = json_object_get_array(object, "string array"); 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(json_object_get_array(object, "non existent array") == NULL);
TEST(STREQ(json_object_dotget_string(object, "object.nested string"), "str")); TEST(STREQ(json_object_dotget_string(object, "object.nested string"), "str"));
TEST(json_object_dotget_bool(object, "object.nested true")); TEST(json_object_dotget_boolean(object, "object.nested true"));
TEST(!json_object_dotget_bool(object, "object.nested false")); TEST(!json_object_dotget_boolean(object, "object.nested false"));
TEST(json_object_dotget_value(object, "object.nested null") != NULL); TEST(json_object_dotget_value(object, "object.nested null") != NULL);
TEST(json_object_dotget_number(object, "object.nested number") == 123); TEST(json_object_dotget_number(object, "object.nested number") == 123);
@ -132,7 +132,7 @@ void test_suite_2() {
} else { } else {
tests_failed++; tests_failed++;
} }
TEST(json_object_dotget_bool(object, "nested true")); TEST(json_object_dotget_boolean(object, "nested true"));
json_value_free(root_value); json_value_free(root_value);
} }

@ -5,8 +5,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,
"bool true" : true, "boolean true" : true,
"bool false" : false, "boolean false" : false,
"null" : null, "null" : null,
"string array" : ["lorem", "ipsum"], "string array" : ["lorem", "ipsum"],
"x^2 array" : [0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100], "x^2 array" : [0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100],

Loading…
Cancel
Save