Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/FSharp.Data.Json.Core/JsonValue.fs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,13 @@ type JsonValue =
| Boolean b -> w.Write(if b then "true" else "false")
| Number number -> w.Write number
| Float v when Double.IsInfinity v || Double.IsNaN v -> w.Write "null"
| Float number -> w.Write number
| Float number ->
let s = number.ToString("R", CultureInfo.InvariantCulture)
w.Write s
// Ensure the output looks like a float (has a decimal point or exponent),
// so that round-tripping through JSON preserves the Float type.
if s.IndexOfAny([| '.'; 'E'; 'e' |]) = -1 then
w.Write ".0"
| String s ->
w.Write "\""
JsonValue.JsonStringEncodeTo w s
Expand Down
14 changes: 14 additions & 0 deletions tests/FSharp.Data.Core.Tests/JsonValue.fs
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,20 @@ let ``Serializes special float value as null`` v =
let json = JsonValue.Float v
json.ToString(JsonSaveOptions.DisableFormatting) |> should equal "null"

[<Test>]
let ``Float value 100.0 serializes with decimal point`` () =
// Regression test for https://github.com/fsprojects/FSharp.Data/issues/1356
// JsonValue.Float(100.0) should serialize as "100.0", not "100"
JsonValue.Float(100.0).ToString(JsonSaveOptions.DisableFormatting) |> should equal "100.0"

[<Test>]
let ``Float value with fractional part serializes correctly`` () =
JsonValue.Float(100.5).ToString(JsonSaveOptions.DisableFormatting) |> should equal "100.5"

[<Test>]
let ``Float value in scientific notation serializes correctly`` () =
JsonValue.Float(1e20).ToString(JsonSaveOptions.DisableFormatting) |> should equal "1E+20"

let normalize (str:string) =
str.Replace("\r\n", "\n")
.Replace("\r", "\n")
Expand Down
Loading