2 * Tests if casting within the json_object_get_* functions work correctly.
3 * Also checks the json_object_get_type and json_object_is_type functions.
11 #include "json_inttypes.h"
12 #include "json_object.h"
13 #include "json_tokener.h"
14 #include "json_util.h"
16 static void getit(struct json_object
*new_obj
, const char *field
);
17 static void checktype_header(void);
18 static void checktype(struct json_object
*new_obj
, const char *field
);
20 int main(int argc
, char **argv
)
22 const char *input
= "{\n\
23 \"string_of_digits\": \"123\",\n\
24 \"regular_number\": 222,\n\
25 \"decimal_number\": 99.55,\n\
26 \"boolean_true\": true,\n\
27 \"boolean_false\": false,\n\
28 \"big_number\": 2147483649,\n\
31 /* Note: 2147483649 = INT_MAX + 2 */
33 struct json_object
*new_obj
;
35 new_obj
= json_tokener_parse(input
);
36 printf("Parsed input: %s\n", input
);
37 printf("Result is %s\n", (new_obj
== NULL
) ? "NULL (error!)" : "not NULL");
39 return 1; // oops, we failed.
41 getit(new_obj
, "string_of_digits");
42 getit(new_obj
, "regular_number");
43 getit(new_obj
, "decimal_number");
44 getit(new_obj
, "boolean_true");
45 getit(new_obj
, "boolean_false");
46 getit(new_obj
, "big_number");
47 getit(new_obj
, "a_null");
49 // Now check the behaviour of the json_object_is_type() function.
50 printf("\n================================\n");
52 checktype(new_obj
, NULL
);
53 checktype(new_obj
, "string_of_digits");
54 checktype(new_obj
, "regular_number");
55 checktype(new_obj
, "decimal_number");
56 checktype(new_obj
, "boolean_true");
57 checktype(new_obj
, "boolean_false");
58 checktype(new_obj
, "big_number");
59 checktype(new_obj
, "a_null");
61 json_object_put(new_obj
);
66 static void getit(struct json_object
*new_obj
, const char *field
)
68 struct json_object
*o
= json_object_object_get(new_obj
, field
);
70 enum json_type o_type
= json_object_get_type(o
);
71 printf("new_obj.%s json_object_get_type()=%s\n", field
,
72 json_type_to_name(o_type
));
73 printf("new_obj.%s json_object_get_int()=%d\n", field
,
74 json_object_get_int(o
));
75 printf("new_obj.%s json_object_get_int64()=%" PRId64
"\n", field
,
76 json_object_get_int64(o
));
77 printf("new_obj.%s json_object_get_boolean()=%d\n", field
,
78 json_object_get_boolean(o
));
79 printf("new_obj.%s json_object_get_double()=%f\n", field
,
80 json_object_get_double(o
));
83 static void checktype_header()
85 printf("json_object_is_type: %s,%s,%s,%s,%s,%s,%s\n",
86 json_type_to_name(json_type_null
),
87 json_type_to_name(json_type_boolean
),
88 json_type_to_name(json_type_double
),
89 json_type_to_name(json_type_int
),
90 json_type_to_name(json_type_object
),
91 json_type_to_name(json_type_array
),
92 json_type_to_name(json_type_string
));
94 static void checktype(struct json_object
*new_obj
, const char *field
)
96 struct json_object
*o
= field
? json_object_object_get(new_obj
, field
) : new_obj
;
97 printf("new_obj%s%-18s: %d,%d,%d,%d,%d,%d,%d\n",
98 field
? "." : " ", field
? field
: "",
99 json_object_is_type(o
, json_type_null
),
100 json_object_is_type(o
, json_type_boolean
),
101 json_object_is_type(o
, json_type_double
),
102 json_object_is_type(o
, json_type_int
),
103 json_object_is_type(o
, json_type_object
),
104 json_object_is_type(o
, json_type_array
),
105 json_object_is_type(o
, json_type_string
));