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