test_double_serializer.c 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. * Tests if the format string for double serialization is handled correctly
  3. */
  4. #include <stdio.h>
  5. #include "config.h"
  6. #include "json_object.h"
  7. #include "json_object_private.h"
  8. int main()
  9. {
  10. struct json_object *obj = json_object_new_double(0.5);
  11. printf("Test default serializer:\n");
  12. printf("obj.to_string(standard)=%s\n", json_object_to_json_string(obj));
  13. printf("Test default serializer with custom userdata:\n");
  14. obj->_userdata = "test";
  15. printf("obj.to_string(userdata)=%s\n", json_object_to_json_string(obj));
  16. printf("Test explicit serializer with custom userdata:\n");
  17. json_object_set_serializer(obj, json_object_double_to_json_string, "test", NULL);
  18. printf("obj.to_string(custom)=%s\n", json_object_to_json_string(obj));
  19. printf("Test reset serializer:\n");
  20. json_object_set_serializer(obj, NULL, NULL, NULL);
  21. printf("obj.to_string(reset)=%s\n", json_object_to_json_string(obj));
  22. json_object_put(obj);
  23. obj = json_object_new_double(0.52381);
  24. printf("obj.to_string(default format)=%s\n", json_object_to_json_string(obj));
  25. if (json_c_set_serialization_double_format("x%0.3fy", JSON_C_OPTION_GLOBAL) < 0)
  26. printf("ERROR: json_c_set_serialization_double_format() failed");
  27. printf("obj.to_string(with global format)=%s\n", json_object_to_json_string(obj));
  28. #ifdef HAVE___THREAD
  29. if (json_c_set_serialization_double_format("T%0.2fX", JSON_C_OPTION_THREAD) < 0)
  30. printf("ERROR: json_c_set_serialization_double_format() failed");
  31. printf("obj.to_string(with thread format)=%s\n", json_object_to_json_string(obj));
  32. if (json_c_set_serialization_double_format("Ttttttttttttt%0.2fxxxxxxxxxxxxxxxxxxX", JSON_C_OPTION_THREAD) < 0)
  33. printf("ERROR: json_c_set_serialization_double_format() failed");
  34. printf("obj.to_string(long thread format)=%s\n", json_object_to_json_string(obj));
  35. if (json_c_set_serialization_double_format(NULL, JSON_C_OPTION_THREAD) < 0)
  36. printf("ERROR: json_c_set_serialization_double_format() failed");
  37. printf("obj.to_string(back to global format)=%s\n", json_object_to_json_string(obj));
  38. #else
  39. // Just fake it up, so the output matches.
  40. printf("obj.to_string(with thread format)=%s\n", "T0.52X");
  41. printf("obj.to_string(back to global format)=%s\n", "x0.524y");
  42. #endif
  43. if (json_c_set_serialization_double_format(NULL, JSON_C_OPTION_GLOBAL) < 0)
  44. printf("ERROR: json_c_set_serialization_double_format() failed");
  45. printf("obj.to_string(back to default format)=%s\n", json_object_to_json_string(obj));
  46. json_object_put(obj);
  47. obj = json_object_new_double(12.0);
  48. printf("obj(12.0).to_string(default format)=%s\n", json_object_to_json_string(obj));
  49. if (json_c_set_serialization_double_format("%.0f", JSON_C_OPTION_GLOBAL) < 0)
  50. printf("ERROR: json_c_set_serialization_double_format() failed");
  51. printf("obj(12.0).to_string(%%.0f)=%s\n", json_object_to_json_string(obj));
  52. if (json_c_set_serialization_double_format("%.0g", JSON_C_OPTION_GLOBAL) < 0)
  53. printf("ERROR: json_c_set_serialization_double_format() failed");
  54. printf("obj(12.0).to_string(%%.0g)=%s\n", json_object_to_json_string(obj));
  55. if (json_c_set_serialization_double_format("%.2g", JSON_C_OPTION_GLOBAL) < 0)
  56. printf("ERROR: json_c_set_serialization_double_format() failed");
  57. printf("obj(12.0).to_string(%%.1g)=%s\n", json_object_to_json_string(obj));
  58. // Reset to default to free memory
  59. if (json_c_set_serialization_double_format(NULL, JSON_C_OPTION_GLOBAL) < 0)
  60. printf("ERROR: json_c_set_serialization_double_format() failed");
  61. json_object_put(obj);
  62. }