0005-libjson-c-Add-support-for-custom-format-strings-for-doubles.patch 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. From: Jan-Philipp Litza <janphilipp@litza.de>
  2. Date: Fri, 6 May 2016 16:44:29 +0200
  3. Subject: libjson-c: Add support for custom format strings for doubles
  4. diff --git a/package/libs/libjson-c/patches/002-custom-format-string.patch b/package/libs/libjson-c/patches/002-custom-format-string.patch
  5. new file mode 100644
  6. index 0000000000000000000000000000000000000000..2f454c560ff78c1edd4654b9651f0e6299bd5e6f
  7. --- /dev/null
  8. +++ b/package/libs/libjson-c/patches/002-custom-format-string.patch
  9. @@ -0,0 +1,98 @@
  10. +From 21dc5dc92bd56f5f4dc2c90b9ea6bf1e1407714e Mon Sep 17 00:00:00 2001
  11. +From: Jan-Philipp Litza <janphilipp@litza.de>
  12. +Date: Fri, 6 May 2016 16:12:44 +0200
  13. +Subject: [PATCH] Export json_object_double_to_json_string() and use custom
  14. + format string
  15. +BCC: janphilipp@litza.de
  16. +
  17. +---
  18. + json_object.c | 12 ++++++------
  19. + json_object.h | 28 ++++++++++++++++++++++++++++
  20. + 2 files changed, 34 insertions(+), 6 deletions(-)
  21. +
  22. +diff --git a/json_object.c b/json_object.c
  23. +index 7d60884..46701e7 100644
  24. +--- a/json_object.c
  25. ++++ b/json_object.c
  26. +@@ -55,7 +55,6 @@ static struct json_object* json_object_new(enum json_type o_type);
  27. + static json_object_to_json_string_fn json_object_object_to_json_string;
  28. + static json_object_to_json_string_fn json_object_boolean_to_json_string;
  29. + static json_object_to_json_string_fn json_object_int_to_json_string;
  30. +-static json_object_to_json_string_fn json_object_double_to_json_string;
  31. + static json_object_to_json_string_fn json_object_string_to_json_string;
  32. + static json_object_to_json_string_fn json_object_array_to_json_string;
  33. +
  34. +@@ -644,10 +643,10 @@ int64_t json_object_get_int64(const struct json_object *jso)
  35. +
  36. + /* json_object_double */
  37. +
  38. +-static int json_object_double_to_json_string(struct json_object* jso,
  39. +- struct printbuf *pb,
  40. +- int level,
  41. +- int flags)
  42. ++int json_object_double_to_json_string(struct json_object* jso,
  43. ++ struct printbuf *pb,
  44. ++ int level,
  45. ++ int flags)
  46. + {
  47. + char buf[128], *p, *q;
  48. + int size;
  49. +@@ -663,7 +662,8 @@ static int json_object_double_to_json_string(struct json_object* jso,
  50. + else
  51. + size = snprintf(buf, sizeof(buf), "-Infinity");
  52. + else
  53. +- size = snprintf(buf, sizeof(buf), "%.17g", jso->o.c_double);
  54. ++ size = snprintf(buf, sizeof(buf),
  55. ++ jso->_userdata ? (const char*) jso->_userdata : "%.17g", jso->o.c_double);
  56. +
  57. + p = strchr(buf, ',');
  58. + if (p) {
  59. +diff --git a/json_object.h b/json_object.h
  60. +index 2bce454..a89de44 100644
  61. +--- a/json_object.h
  62. ++++ b/json_object.h
  63. +@@ -614,6 +614,9 @@ extern int64_t json_object_get_int64(const struct json_object *obj);
  64. + /* double type methods */
  65. +
  66. + /** Create a new empty json_object of type json_type_double
  67. ++ *
  68. ++ * @see json_object_double_to_json_string() for how to set a custom format string.
  69. ++ *
  70. + * @param d the double
  71. + * @returns a json_object of type json_type_double
  72. + */
  73. +@@ -642,6 +645,31 @@ extern struct json_object* json_object_new_double(double d);
  74. + */
  75. + extern struct json_object* json_object_new_double_s(double d, const char *ds);
  76. +
  77. ++
  78. ++/** Serialize a json_object of type json_type_double to a string.
  79. ++ *
  80. ++ * This function isn't meant to be called directly. Instead, you can set a
  81. ++ * custom format string for the serialization of this double using the
  82. ++ * following call (where "%.17g" actually is the default):
  83. ++ *
  84. ++ * @code
  85. ++ * jso = json_object_new_double(d);
  86. ++ * json_object_set_serializer(jso, json_object_double_to_json_string,
  87. ++ * "%.17g", NULL);
  88. ++ * @endcode
  89. ++ *
  90. ++ * @see printf(3) man page for format strings
  91. ++ *
  92. ++ * @param jso The json_type_double object that is serialized.
  93. ++ * @param pb The destination buffer.
  94. ++ * @param level Ignored.
  95. ++ * @param flags Ignored.
  96. ++ */
  97. ++extern int json_object_double_to_json_string(struct json_object* jso,
  98. ++ struct printbuf *pb,
  99. ++ int level,
  100. ++ int flags);
  101. ++
  102. + /** Get the double floating point value of a json_object
  103. + *
  104. + * The type is coerced to a double if the passed object is not a double.
  105. +--
  106. +2.7.4
  107. +