blob: 6459250b5a48fea8b7305e5b73e5d9936310cd8e [file] [log] [blame]
Eric Haszlakiewicz41e67d02011-05-03 20:42:25 +00001/*
2 * Tests if casting within the json_object_get_* functions work correctly.
3 */
4
5#include <stdio.h>
6#include <string.h>
7#include <stdlib.h>
8#include "config.h"
9
10#include "json_inttypes.h"
11#include "json_object.h"
12#include "json_tokener.h"
13#include "json_util.h"
14
15static void getit(struct json_object *new_obj, const char *field);
16
17int main(int argc, char **argv)
18{
19 const char *input = "{\n\
20 \"string_of_digits\": \"123\",\n\
21 \"regular_number\": 222,\n\
22 \"decimal_number\": 99.55,\n\
23 \"boolean_true\": true,\n\
24 \"boolean_false\": false,\n\
25 \"big_number\": 2147483649,\n\
26 }";
27 /* Note: 2147483649 = INT_MAX + 2 */
28
29 struct json_object *new_obj;
30
31 new_obj = json_tokener_parse(input);
32 printf("Parsed input: %s\n", input);
33 printf("Result is %s\n", (new_obj == NULL) ? "NULL (error!)" : "not NULL");
34 if (!new_obj)
35 return 1; // oops, we failed.
36
37 getit(new_obj, "string_of_digits");
38 getit(new_obj, "regular_number");
39 getit(new_obj, "decimal_number");
40 getit(new_obj, "boolean_true");
41 getit(new_obj, "boolean_false");
42 getit(new_obj, "big_number");
43
44 json_object_put(new_obj);
45
46 return 0;
47}
48
49static void getit(struct json_object *new_obj, const char *field)
50{
51 struct json_object *o = json_object_object_get(new_obj, field);
52
53 enum json_type o_type = json_object_get_type(o);
54 printf("new_obj.%s json_object_get_type()=%s\n", field,
55 json_type_to_name(o_type));
56 printf("new_obj.%s json_object_get_int()=%d\n", field,
57 json_object_get_int(o));
58 printf("new_obj.%s json_object_get_int64()=%" PRId64 "\n", field,
59 json_object_get_int64(o));
60 printf("new_obj.%s json_object_get_boolean()=%d\n", field,
61 json_object_get_boolean(o));
62 printf("new_obj.%s json_object_get_double()=%f\n", field,
63 json_object_get_double(o));
64}