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_double_serializer.c 3.7 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. char udata[] = "test";
  12. printf("Test default serializer:\n");
  13. printf("obj.to_string(standard)=%s\n", json_object_to_json_string(obj));
  14. printf("Test default serializer with custom userdata:\n");
  15. obj->_userdata = udata;
  16. printf("obj.to_string(userdata)=%s\n", json_object_to_json_string(obj));
  17. printf("Test explicit serializer with custom userdata:\n");
  18. json_object_set_serializer(obj, json_object_double_to_json_string, udata, NULL);
  19. printf("obj.to_string(custom)=%s\n", json_object_to_json_string(obj));
  20. printf("Test reset serializer:\n");
  21. json_object_set_serializer(obj, NULL, NULL, NULL);
  22. printf("obj.to_string(reset)=%s\n", json_object_to_json_string(obj));
  23. json_object_put(obj);
  24. obj = json_object_new_double(0.52381);
  25. printf("obj.to_string(default format)=%s\n", json_object_to_json_string(obj));
  26. if (json_c_set_serialization_double_format("x%0.3fy", JSON_C_OPTION_GLOBAL) < 0)
  27. printf("ERROR: json_c_set_serialization_double_format() failed");
  28. printf("obj.to_string(with global format)=%s\n", json_object_to_json_string(obj));
  29. #ifdef HAVE___THREAD
  30. if (json_c_set_serialization_double_format("T%0.2fX", JSON_C_OPTION_THREAD) < 0)
  31. printf("ERROR: json_c_set_serialization_double_format() failed");
  32. printf("obj.to_string(with thread format)=%s\n", json_object_to_json_string(obj));
  33. if (json_c_set_serialization_double_format("Ttttttttttttt%0.2fxxxxxxxxxxxxxxxxxxX", JSON_C_OPTION_THREAD) < 0)
  34. printf("ERROR: json_c_set_serialization_double_format() failed");
  35. printf("obj.to_string(long thread format)=%s\n", json_object_to_json_string(obj));
  36. if (json_c_set_serialization_double_format(NULL, JSON_C_OPTION_THREAD) < 0)
  37. printf("ERROR: json_c_set_serialization_double_format() failed");
  38. printf("obj.to_string(back to global format)=%s\n", json_object_to_json_string(obj));
  39. #else
  40. // Just fake it up, so the output matches.
  41. printf("obj.to_string(with thread format)=%s\n", "T0.52X");
  42. printf("obj.to_string(back to global format)=%s\n", "x0.524y");
  43. #endif
  44. if (json_c_set_serialization_double_format(NULL, JSON_C_OPTION_GLOBAL) < 0)
  45. printf("ERROR: json_c_set_serialization_double_format() failed");
  46. printf("obj.to_string(back to default format)=%s\n", json_object_to_json_string(obj));
  47. json_object_put(obj);
  48. obj = json_object_new_double(12.0);
  49. printf("obj(12.0).to_string(default format)=%s\n", json_object_to_json_string(obj));
  50. if (json_c_set_serialization_double_format("%.0f", JSON_C_OPTION_GLOBAL) < 0)
  51. printf("ERROR: json_c_set_serialization_double_format() failed");
  52. printf("obj(12.0).to_string(%%.0f)=%s\n", json_object_to_json_string(obj));
  53. if (json_c_set_serialization_double_format("%.0g", JSON_C_OPTION_GLOBAL) < 0)
  54. printf("ERROR: json_c_set_serialization_double_format() failed");
  55. printf("obj(12.0).to_string(%%.0g)=%s\n", json_object_to_json_string(obj));
  56. if (json_c_set_serialization_double_format("%.2g", JSON_C_OPTION_GLOBAL) < 0)
  57. printf("ERROR: json_c_set_serialization_double_format() failed");
  58. printf("obj(12.0).to_string(%%.1g)=%s\n", json_object_to_json_string(obj));
  59. // Reset to default to free memory
  60. if (json_c_set_serialization_double_format(NULL, JSON_C_OPTION_GLOBAL) < 0)
  61. printf("ERROR: json_c_set_serialization_double_format() failed");
  62. json_object_put(obj);
  63. obj = json_object_new_double(-12.0);
  64. printf("obj(-12.0).to_string(default format)=%s\n", json_object_to_json_string(obj));
  65. json_object_put(obj);
  66. }