summaryrefslogtreecommitdiff
path: root/ext
diff options
context:
space:
mode:
authoreno <[email protected]>2025-03-19 22:48:08 +0100
committerHiroshi SHIBATA <[email protected]>2025-03-24 14:35:04 +0900
commit528c08cc5fe8f8c990442885c0a30c95d8798e55 (patch)
tree18cd9ec53e3e7384e78644cb0cca334326b28bc1 /ext
parenta59333c58bbe0df0baaada6d3cc4a64b0aaf911e (diff)
[ruby/json] Adjust fpconv to add ".0" to integers
Adds a test case fix https://github.com/ruby/json/commit/fa5bdf87cb
Diffstat (limited to 'ext')
-rw-r--r--ext/json/ext/vendor/fpconv/src/fpconv.c10
-rw-r--r--ext/json/generator/generator.c13
2 files changed, 10 insertions, 13 deletions
diff --git a/ext/json/ext/vendor/fpconv/src/fpconv.c b/ext/json/ext/vendor/fpconv/src/fpconv.c
index 56ebb8944a..8ad80d9c6b 100644
--- a/ext/json/ext/vendor/fpconv/src/fpconv.c
+++ b/ext/json/ext/vendor/fpconv/src/fpconv.c
@@ -222,7 +222,11 @@ static int emit_digits(char* digits, int ndigits, char* dest, int K, bool neg)
memcpy(dest, digits, ndigits);
memset(dest + ndigits, '0', K);
- return ndigits + K;
+ /* add a .0 to mark this as a float. */
+ dest[ndigits + K] = '.';
+ dest[ndigits + K + 1] = '0';
+
+ return ndigits + K + 2;
}
/* write decimal w/o scientific notation */
@@ -290,7 +294,9 @@ static int filter_special(double fp, char* dest)
{
if(fp == 0.0) {
dest[0] = '0';
- return 1;
+ dest[1] = '.';
+ dest[2] = '0';
+ return 3;
}
uint64_t bits = get_dbits(fp);
diff --git a/ext/json/generator/generator.c b/ext/json/generator/generator.c
index 0f028fe072..6ef16ced8f 100644
--- a/ext/json/generator/generator.c
+++ b/ext/json/generator/generator.c
@@ -1084,18 +1084,9 @@ static void generate_json_float(FBuffer *buffer, struct generate_json_data *data
char* d = buffer->ptr + buffer->len;
int len = fpconv_dtoa(value, d);
- /* fpconv_dtoa converts a float to its shorted string representation. When
- * converting a float that is exactly an integer (e.g. `Float(2)`) this
- * returns in a string that looks like an integer. This is correct, since
- * JSON treats ints and floats the same. However, to not break integrations
- * that expect a string representation looking like a float, we append a
- * "." in that case.
+ /* fpconv_dtoa converts a float to its shortest string representation,
+ * but it adds a ".0" if this is a plain integer.
*/
- if(!memchr(d, '.', len) && !memchr(d, 'e', len)) {
- d[len] = '.';
- d[len+1] = '0';
- len += 2;
- }
buffer->len += len;
}