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

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