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 4.1 kB

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