blob: 72c8cc4a21403b86c505ab70343d35435c2f6a27 [file] [log] [blame]
Eric Haszlakiewicz41e67d02011-05-03 20:42:25 +00001/*
2 * Tests if casting within the json_object_get_* functions work correctly.
Eric Haszlakiewiczaef439a2012-03-31 13:47:28 -05003 * Also checks the json_object_get_type and json_object_is_type functions.
Eric Haszlakiewicz41e67d02011-05-03 20:42:25 +00004 */
5
6#include <stdio.h>
7#include <string.h>
8#include <stdlib.h>
9#include "config.h"
10
11#include "json_inttypes.h"
12#include "json_object.h"
13#include "json_tokener.h"
14#include "json_util.h"
15
16static void getit(struct json_object *new_obj, const char *field);
Eric Haszlakiewiczaef439a2012-03-31 13:47:28 -050017static void checktype_header(void);
18static void checktype(struct json_object *new_obj, const char *field);
Eric Haszlakiewicz41e67d02011-05-03 20:42:25 +000019
20int main(int argc, char **argv)
21{
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\
Eric Haszlakiewiczaef439a2012-03-31 13:47:28 -050029 \"a_null\": null,\n\
Eric Haszlakiewicz41e67d02011-05-03 20:42:25 +000030 }";
31 /* Note: 2147483649 = INT_MAX + 2 */
32
33 struct json_object *new_obj;
34
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");
38 if (!new_obj)
39 return 1; // oops, we failed.
40
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");
Eric Haszlakiewiczaef439a2012-03-31 13:47:28 -050047 getit(new_obj, "a_null");
48
49 // Now check the behaviour of the json_object_is_type() function.
50 printf("\n================================\n");
51 checktype_header();
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");
Eric Haszlakiewicz41e67d02011-05-03 20:42:25 +000060
Eric Haszlakiewiczc3068bf2012-09-16 20:43:29 -050061 json_object_put(new_obj);
Eric Haszlakiewicz41e67d02011-05-03 20:42:25 +000062
Eric Haszlakiewiczc3068bf2012-09-16 20:43:29 -050063 return 0;
Eric Haszlakiewicz41e67d02011-05-03 20:42:25 +000064}
65
66static void getit(struct json_object *new_obj, const char *field)
67{
68 struct json_object *o = json_object_object_get(new_obj, field);
69
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));
81}
Eric Haszlakiewiczaef439a2012-03-31 13:47:28 -050082
83static void checktype_header()
84{
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));
93}
94static void checktype(struct json_object *new_obj, const char *field)
95{
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));
106}