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 "config.h" 7 #include <stdio.h> 8 #include <stdlib.h> 9 #include <string.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 \"int64_number\": 2147483649,\n\ 29 \"negative_number\": -321321321,\n\ 30 \"a_null\": null,\n\ 31 }"; 32 /* Note: 2147483649 = INT_MAX + 2 */ 33 /* Note: 9223372036854775809 = INT64_MAX + 2 */ 34 /* Note: 18446744073709551617 = UINT64_MAX + 2 */ 35 36 struct json_object *new_obj; 37 38 new_obj = json_tokener_parse(input); 39 printf("Parsed input: %s\n", input); 40 printf("Result is %s\n", (new_obj == NULL) ? "NULL (error!)" : "not NULL"); 41 if (!new_obj) 42 return 1; // oops, we failed. 43 44 getit(new_obj, "string_of_digits"); 45 getit(new_obj, "regular_number"); 46 getit(new_obj, "decimal_number"); 47 getit(new_obj, "boolean_true"); 48 getit(new_obj, "boolean_false"); 49 getit(new_obj, "int64_number"); 50 getit(new_obj, "negative_number"); 51 getit(new_obj, "a_null"); 52 53 // Now check the behaviour of the json_object_is_type() function. 54 printf("\n================================\n"); 55 checktype_header(); 56 checktype(new_obj, NULL); 57 checktype(new_obj, "string_of_digits"); 58 checktype(new_obj, "regular_number"); 59 checktype(new_obj, "decimal_number"); 60 checktype(new_obj, "boolean_true"); 61 checktype(new_obj, "boolean_false"); 62 checktype(new_obj, "int64_number"); 63 checktype(new_obj, "negative_number"); 64 checktype(new_obj, "a_null"); 65 66 json_object_put(new_obj); 67 68 return 0; 69 } 70 71 static void getit(struct json_object *new_obj, const char *field) 72 { 73 struct json_object *o = NULL; 74 if (!json_object_object_get_ex(new_obj, field, &o)) 75 printf("Field %s does not exist\n", field); 76 77 enum json_type o_type = json_object_get_type(o); 78 printf("new_obj.%s json_object_get_type()=%s\n", field, json_type_to_name(o_type)); 79 printf("new_obj.%s json_object_get_int()=%d\n", field, json_object_get_int(o)); 80 printf("new_obj.%s json_object_get_int64()=%" PRId64 "\n", field, json_object_get_int64(o)); 81 printf("new_obj.%s json_object_get_uint64()=%" PRIu64 "\n", field, 82 json_object_get_uint64(o)); 83 printf("new_obj.%s json_object_get_boolean()=%d\n", field, json_object_get_boolean(o)); 84 printf("new_obj.%s json_object_get_double()=%f\n", field, json_object_get_double(o)); 85 } 86 87 static void checktype_header() 88 { 89 printf("json_object_is_type: %s,%s,%s,%s,%s,%s,%s\n", json_type_to_name(json_type_null), 90 json_type_to_name(json_type_boolean), json_type_to_name(json_type_double), 91 json_type_to_name(json_type_int), json_type_to_name(json_type_object), 92 json_type_to_name(json_type_array), json_type_to_name(json_type_string)); 93 } 94 static void checktype(struct json_object *new_obj, const char *field) 95 { 96 struct json_object *o = new_obj; 97 if (field && !json_object_object_get_ex(new_obj, field, &o)) 98 printf("Field %s does not exist\n", field); 99 100 printf("new_obj%s%-18s: %d,%d,%d,%d,%d,%d,%d\n", field ? "." : " ", field ? field : "", 101 json_object_is_type(o, json_type_null), json_object_is_type(o, json_type_boolean), 102 json_object_is_type(o, json_type_double), json_object_is_type(o, json_type_int), 103 json_object_is_type(o, json_type_object), json_object_is_type(o, json_type_array), 104 json_object_is_type(o, json_type_string)); 105 } 106
This page was automatically generated by LXR 0.3.1. • OpenWrt