You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_cast.c 3.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. #include "config.h"
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include "json_inttypes.h"
  10. #include "json_object.h"
  11. #include "json_tokener.h"
  12. #include "json_util.h"
  13. static void getit(struct json_object *new_obj, const char *field);
  14. static void checktype_header(void);
  15. static void checktype(struct json_object *new_obj, const char *field);
  16. int main(int argc, char **argv)
  17. {
  18. const char *input = "{\n\
  19. \"string_of_digits\": \"123\",\n\
  20. \"regular_number\": 222,\n\
  21. \"decimal_number\": 99.55,\n\
  22. \"boolean_true\": true,\n\
  23. \"boolean_false\": false,\n\
  24. \"int64_number\": 2147483649,\n\
  25. \"negative_number\": -321321321,\n\
  26. \"a_null\": null,\n\
  27. }";
  28. /* Note: 2147483649 = INT_MAX + 2 */
  29. /* Note: 9223372036854775809 = INT64_MAX + 2 */
  30. /* Note: 18446744073709551617 = UINT64_MAX + 2 */
  31. struct json_object *new_obj;
  32. new_obj = json_tokener_parse(input);
  33. printf("Parsed input: %s\n", input);
  34. printf("Result is %s\n", (new_obj == NULL) ? "NULL (error!)" : "not NULL");
  35. if (!new_obj)
  36. return 1; // oops, we failed.
  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, "int64_number");
  43. getit(new_obj, "negative_number");
  44. getit(new_obj, "a_null");
  45. // Now check the behaviour of the json_object_is_type() function.
  46. printf("\n================================\n");
  47. checktype_header();
  48. checktype(new_obj, NULL);
  49. checktype(new_obj, "string_of_digits");
  50. checktype(new_obj, "regular_number");
  51. checktype(new_obj, "decimal_number");
  52. checktype(new_obj, "boolean_true");
  53. checktype(new_obj, "boolean_false");
  54. checktype(new_obj, "int64_number");
  55. checktype(new_obj, "negative_number");
  56. checktype(new_obj, "a_null");
  57. json_object_put(new_obj);
  58. return 0;
  59. }
  60. static void getit(struct json_object *new_obj, const char *field)
  61. {
  62. struct json_object *o = NULL;
  63. if (!json_object_object_get_ex(new_obj, field, &o))
  64. printf("Field %s does not exist\n", field);
  65. enum json_type o_type = json_object_get_type(o);
  66. printf("new_obj.%s json_object_get_type()=%s\n", field, json_type_to_name(o_type));
  67. printf("new_obj.%s json_object_get_int()=%d\n", field, json_object_get_int(o));
  68. printf("new_obj.%s json_object_get_int64()=%" PRId64 "\n", field, json_object_get_int64(o));
  69. printf("new_obj.%s json_object_get_uint64()=%" PRIu64 "\n", field,
  70. json_object_get_uint64(o));
  71. printf("new_obj.%s json_object_get_boolean()=%d\n", field, json_object_get_boolean(o));
  72. printf("new_obj.%s json_object_get_double()=%f\n", field, json_object_get_double(o));
  73. }
  74. static void checktype_header()
  75. {
  76. printf("json_object_is_type: %s,%s,%s,%s,%s,%s,%s\n", json_type_to_name(json_type_null),
  77. json_type_to_name(json_type_boolean), json_type_to_name(json_type_double),
  78. json_type_to_name(json_type_int), json_type_to_name(json_type_object),
  79. json_type_to_name(json_type_array), json_type_to_name(json_type_string));
  80. }
  81. static void checktype(struct json_object *new_obj, const char *field)
  82. {
  83. struct json_object *o = new_obj;
  84. if (field && !json_object_object_get_ex(new_obj, field, &o))
  85. printf("Field %s does not exist\n", field);
  86. printf("new_obj%s%-18s: %d,%d,%d,%d,%d,%d,%d\n", field ? "." : " ", field ? field : "",
  87. json_object_is_type(o, json_type_null), json_object_is_type(o, json_type_boolean),
  88. json_object_is_type(o, json_type_double), json_object_is_type(o, json_type_int),
  89. json_object_is_type(o, json_type_object), json_object_is_type(o, json_type_array),
  90. json_object_is_type(o, json_type_string));
  91. }