MACSio  0.9
Multi-purpose, Application-Centric, Scalable I/O Proxy App
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
test_cast.c
Go to the documentation of this file.
1 /*
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.
4  */
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 
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);
19 
20 int 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\
29  \"a_null\": null,\n\
30  }";
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");
47  getit(new_obj, "a_null");
48 
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");
60 
61  json_object_put(new_obj);
62 
63  return 0;
64 }
65 
66 static void getit(struct json_object *new_obj, const char *field)
67 {
68  struct json_object *o = json_object_object_get(new_obj, field);
69 
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,
75  printf("new_obj.%s json_object_get_int64()=%" PRId64 "\n", field,
77  printf("new_obj.%s json_object_get_boolean()=%d\n", field,
79  printf("new_obj.%s json_object_get_double()=%f\n", field,
81 }
82 
83 static void checktype_header()
84 {
85  printf("json_object_is_type: %s,%s,%s,%s,%s,%s,%s\n",
93 }
94 static 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 : "",
106 }
#define PRId64
Definition: json_inttypes.h:23
enum json_type json_object_get_type(struct json_object *obj)
Definition: json_object.c:211
union json_object::data o
int json_object_is_type(struct json_object *obj, enum json_type type)
Definition: json_object.c:204
json_bool json_object_get_boolean(struct json_object *obj)
Definition: json_object.c:467
const char * json_type_to_name(enum json_type o_type)
Definition: json_util.c:308
static void getit(struct json_object *new_obj, const char *field)
Definition: test_cast.c:66
static void checktype(struct json_object *new_obj, const char *field)
Definition: test_cast.c:94
int main(int argc, char **argv)
Definition: test_cast.c:20
struct json_object * json_object_object_get(struct json_object *jso, const char *key)
Definition: json_object.c:415
static void checktype_header(void)
Definition: test_cast.c:83
enum json_type o_type
double json_object_get_double(struct json_object *obj)
Definition: json_object.c:656
int json_object_put(struct json_object *obj)
Definition: json_object.c:155
int64_t json_object_get_int64(struct json_object *obj)
Definition: json_object.c:559
int32_t json_object_get_int(struct json_object *obj)
Definition: json_object.c:504
struct json_object * json_tokener_parse(const char *str)
Definition: json_tokener.c:158
json_type
Definition: json_object.h:107