From c904ce3fdc37597741fe99b8eb28169974790ce4 Mon Sep 17 00:00:00 2001 From: nitely Date: Wed, 8 Oct 2025 16:29:03 -0300 Subject: [PATCH 1/2] Avoid write float alloc --- json_serialization/writer.nim | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/json_serialization/writer.nim b/json_serialization/writer.nim index 4256771..5c23d9b 100644 --- a/json_serialization/writer.nim +++ b/json_serialization/writer.nim @@ -527,9 +527,7 @@ proc writeValue*[V: not void](w: var JsonWriter, value: V) {.raises: [IOError].} elif value is SomeFloat: autoSerializeCheck(Flavor, SomeFloat, typeof(value)): w.streamElement(s): - # TODO Implement writeText for floats - # to avoid the allocation here: - s.write $value + s.writeText value elif value is seq or(value is distinct and distinctBase(value) is seq): autoSerializeCheck(Flavor, seq, typeof(value)): From 92a24e8023202f9cc640f80f8bf55a010ea0ee8b Mon Sep 17 00:00:00 2001 From: nitely Date: Wed, 8 Oct 2025 19:14:16 -0300 Subject: [PATCH 2/2] test rountrip float --- tests/test_serialization.nim | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/test_serialization.nim b/tests/test_serialization.nim index 52d4a80..1400255 100644 --- a/tests/test_serialization.nim +++ b/tests/test_serialization.nim @@ -376,6 +376,11 @@ suite "toJson tests": "".toJson == "\"\"" "abc".toJson == "\"abc\"" + test "float": + Json.roundtripTest 1.23, "1.23" + Json.roundtripTest 1.23'f32, "1.23" + Json.roundtripTest 1.23'f64, "1.23" + test "enums": Json.roundtripTest x0, "\"x0\"" Json.roundtripTest x1, "\"x1\""