test_cast.c 3.4 KB

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