Adjust json_object_is_type and json_object_get_type so they return json_type_null for NULL objects.
This commit is contained in:
@@ -166,11 +166,15 @@ static struct json_object* json_object_new(enum json_type o_type)
|
|||||||
|
|
||||||
int json_object_is_type(struct json_object *jso, enum json_type type)
|
int json_object_is_type(struct json_object *jso, enum json_type type)
|
||||||
{
|
{
|
||||||
|
if (!jso)
|
||||||
|
return (type == json_type_null);
|
||||||
return (jso->o_type == type);
|
return (jso->o_type == type);
|
||||||
}
|
}
|
||||||
|
|
||||||
enum json_type json_object_get_type(struct json_object *jso)
|
enum json_type json_object_get_type(struct json_object *jso)
|
||||||
{
|
{
|
||||||
|
if (!jso)
|
||||||
|
return json_type_null;
|
||||||
return jso->o_type;
|
return jso->o_type;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -79,6 +79,7 @@ extern void json_object_put(struct json_object *obj);
|
|||||||
* Check if the json_object is of a given type
|
* Check if the json_object is of a given type
|
||||||
* @param obj the json_object instance
|
* @param obj the json_object instance
|
||||||
* @param type one of:
|
* @param type one of:
|
||||||
|
json_type_null (i.e. obj == NULL),
|
||||||
json_type_boolean,
|
json_type_boolean,
|
||||||
json_type_double,
|
json_type_double,
|
||||||
json_type_int,
|
json_type_int,
|
||||||
@@ -92,6 +93,7 @@ extern int json_object_is_type(struct json_object *obj, enum json_type type);
|
|||||||
* Get the type of the json_object
|
* Get the type of the json_object
|
||||||
* @param obj the json_object instance
|
* @param obj the json_object instance
|
||||||
* @returns type being one of:
|
* @returns type being one of:
|
||||||
|
json_type_null (i.e. obj == NULL),
|
||||||
json_type_boolean,
|
json_type_boolean,
|
||||||
json_type_double,
|
json_type_double,
|
||||||
json_type_int,
|
json_type_int,
|
||||||
|
|||||||
42
test_cast.c
42
test_cast.c
@@ -1,5 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
* Tests if casting within the json_object_get_* functions work correctly.
|
* Tests if casting within the json_object_get_* functions work correctly.
|
||||||
|
* Also checks the json_object_get_type and json_object_is_type functions.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
@@ -13,6 +14,8 @@
|
|||||||
#include "json_util.h"
|
#include "json_util.h"
|
||||||
|
|
||||||
static void getit(struct json_object *new_obj, const char *field);
|
static void getit(struct json_object *new_obj, const char *field);
|
||||||
|
static void checktype_header(void);
|
||||||
|
static void checktype(struct json_object *new_obj, const char *field);
|
||||||
|
|
||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
@@ -23,6 +26,7 @@ int main(int argc, char **argv)
|
|||||||
\"boolean_true\": true,\n\
|
\"boolean_true\": true,\n\
|
||||||
\"boolean_false\": false,\n\
|
\"boolean_false\": false,\n\
|
||||||
\"big_number\": 2147483649,\n\
|
\"big_number\": 2147483649,\n\
|
||||||
|
\"a_null\": null,\n\
|
||||||
}";
|
}";
|
||||||
/* Note: 2147483649 = INT_MAX + 2 */
|
/* Note: 2147483649 = INT_MAX + 2 */
|
||||||
|
|
||||||
@@ -40,6 +44,19 @@ int main(int argc, char **argv)
|
|||||||
getit(new_obj, "boolean_true");
|
getit(new_obj, "boolean_true");
|
||||||
getit(new_obj, "boolean_false");
|
getit(new_obj, "boolean_false");
|
||||||
getit(new_obj, "big_number");
|
getit(new_obj, "big_number");
|
||||||
|
getit(new_obj, "a_null");
|
||||||
|
|
||||||
|
// Now check the behaviour of the json_object_is_type() function.
|
||||||
|
printf("\n================================\n");
|
||||||
|
checktype_header();
|
||||||
|
checktype(new_obj, NULL);
|
||||||
|
checktype(new_obj, "string_of_digits");
|
||||||
|
checktype(new_obj, "regular_number");
|
||||||
|
checktype(new_obj, "decimal_number");
|
||||||
|
checktype(new_obj, "boolean_true");
|
||||||
|
checktype(new_obj, "boolean_false");
|
||||||
|
checktype(new_obj, "big_number");
|
||||||
|
checktype(new_obj, "a_null");
|
||||||
|
|
||||||
json_object_put(new_obj);
|
json_object_put(new_obj);
|
||||||
|
|
||||||
@@ -62,3 +79,28 @@ static void getit(struct json_object *new_obj, const char *field)
|
|||||||
printf("new_obj.%s json_object_get_double()=%f\n", field,
|
printf("new_obj.%s json_object_get_double()=%f\n", field,
|
||||||
json_object_get_double(o));
|
json_object_get_double(o));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void checktype_header()
|
||||||
|
{
|
||||||
|
printf("json_object_is_type: %s,%s,%s,%s,%s,%s,%s\n",
|
||||||
|
json_type_to_name(json_type_null),
|
||||||
|
json_type_to_name(json_type_boolean),
|
||||||
|
json_type_to_name(json_type_double),
|
||||||
|
json_type_to_name(json_type_int),
|
||||||
|
json_type_to_name(json_type_object),
|
||||||
|
json_type_to_name(json_type_array),
|
||||||
|
json_type_to_name(json_type_string));
|
||||||
|
}
|
||||||
|
static void checktype(struct json_object *new_obj, const char *field)
|
||||||
|
{
|
||||||
|
struct json_object *o = field ? json_object_object_get(new_obj, field) : new_obj;
|
||||||
|
printf("new_obj%s%-18s: %d,%d,%d,%d,%d,%d,%d\n",
|
||||||
|
field ? "." : " ", field ? field : "",
|
||||||
|
json_object_is_type(o, json_type_null),
|
||||||
|
json_object_is_type(o, json_type_boolean),
|
||||||
|
json_object_is_type(o, json_type_double),
|
||||||
|
json_object_is_type(o, json_type_int),
|
||||||
|
json_object_is_type(o, json_type_object),
|
||||||
|
json_object_is_type(o, json_type_array),
|
||||||
|
json_object_is_type(o, json_type_string));
|
||||||
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ Parsed input: {
|
|||||||
"boolean_true": true,
|
"boolean_true": true,
|
||||||
"boolean_false": false,
|
"boolean_false": false,
|
||||||
"big_number": 2147483649,
|
"big_number": 2147483649,
|
||||||
|
"a_null": null,
|
||||||
}
|
}
|
||||||
Result is not NULL
|
Result is not NULL
|
||||||
new_obj.string_of_digits json_object_get_type()=string
|
new_obj.string_of_digits json_object_get_type()=string
|
||||||
@@ -37,3 +38,19 @@ new_obj.big_number json_object_get_int()=2147483647
|
|||||||
new_obj.big_number json_object_get_int64()=2147483649
|
new_obj.big_number json_object_get_int64()=2147483649
|
||||||
new_obj.big_number json_object_get_boolean()=1
|
new_obj.big_number json_object_get_boolean()=1
|
||||||
new_obj.big_number json_object_get_double()=2147483649.000000
|
new_obj.big_number json_object_get_double()=2147483649.000000
|
||||||
|
new_obj.a_null json_object_get_type()=null
|
||||||
|
new_obj.a_null json_object_get_int()=0
|
||||||
|
new_obj.a_null json_object_get_int64()=0
|
||||||
|
new_obj.a_null json_object_get_boolean()=0
|
||||||
|
new_obj.a_null json_object_get_double()=0.000000
|
||||||
|
|
||||||
|
================================
|
||||||
|
json_object_is_type: null,boolean,double,int,object,array,string
|
||||||
|
new_obj : 0,0,0,0,1,0,0
|
||||||
|
new_obj.string_of_digits : 0,0,0,0,0,0,1
|
||||||
|
new_obj.regular_number : 0,0,0,1,0,0,0
|
||||||
|
new_obj.decimal_number : 0,0,1,0,0,0,0
|
||||||
|
new_obj.boolean_true : 0,1,0,0,0,0,0
|
||||||
|
new_obj.boolean_false : 0,1,0,0,0,0,0
|
||||||
|
new_obj.big_number : 0,0,0,1,0,0,0
|
||||||
|
new_obj.a_null : 1,0,0,0,0,0,0
|
||||||
|
|||||||
Reference in New Issue
Block a user