Skip to content

Commit 00ec268

Browse files
authored
Avoid write float alloc (#134)
1 parent 572e2f8 commit 00ec268

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed

json_serialization/writer.nim

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -527,9 +527,7 @@ proc writeValue*[V: not void](w: var JsonWriter, value: V) {.raises: [IOError].}
527527
elif value is SomeFloat:
528528
autoSerializeCheck(Flavor, SomeFloat, typeof(value)):
529529
w.streamElement(s):
530-
# TODO Implement writeText for floats
531-
# to avoid the allocation here:
532-
s.write $value
530+
s.writeText value
533531

534532
elif value is seq or(value is distinct and distinctBase(value) is seq):
535533
autoSerializeCheck(Flavor, seq, typeof(value)):

tests/test_serialization.nim

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,11 @@ suite "toJson tests":
376376
"".toJson == "\"\""
377377
"abc".toJson == "\"abc\""
378378

379+
test "float":
380+
Json.roundtripTest 1.23, "1.23"
381+
Json.roundtripTest 1.23'f32, "1.23"
382+
Json.roundtripTest 1.23'f64, "1.23"
383+
379384
test "enums":
380385
Json.roundtripTest x0, "\"x0\""
381386
Json.roundtripTest x1, "\"x1\""

0 commit comments

Comments
 (0)