File tree Expand file tree Collapse file tree
src/FSharp.Data.Json.Core
tests/FSharp.Data.Core.Tests Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -86,7 +86,13 @@ type JsonValue =
8686 | Boolean b -> w.Write( if b then " true" else " false" )
8787 | Number number -> w.Write number
8888 | Float v when Double.IsInfinity v || Double.IsNaN v -> w.Write " null"
89- | Float number -> w.Write number
89+ | Float number ->
90+ let s = number.ToString( " R" , CultureInfo.InvariantCulture)
91+ w.Write s
92+ // Ensure the output looks like a float (has a decimal point or exponent),
93+ // so that round-tripping through JSON preserves the Float type.
94+ if s.IndexOfAny([| '.' ; 'E' ; 'e' |]) = - 1 then
95+ w.Write " .0"
9096 | String s ->
9197 w.Write " \" "
9298 JsonValue.JsonStringEncodeTo w s
Original file line number Diff line number Diff line change @@ -293,6 +293,20 @@ let ``Serializes special float value as null`` v =
293293 let json = JsonValue.Float v
294294 json.ToString( JsonSaveOptions.DisableFormatting) |> should equal " null"
295295
296+ [<Test>]
297+ let ``Float value 100.0 serializes with decimal point`` () =
298+ // Regression test for https://github.com/fsprojects/FSharp.Data/issues/1356
299+ // JsonValue.Float(100.0) should serialize as "100.0", not "100"
300+ JsonValue.Float( 100.0 ) .ToString( JsonSaveOptions.DisableFormatting) |> should equal " 100.0"
301+
302+ [<Test>]
303+ let ``Float value with fractional part serializes correctly`` () =
304+ JsonValue.Float( 100.5 ) .ToString( JsonSaveOptions.DisableFormatting) |> should equal " 100.5"
305+
306+ [<Test>]
307+ let ``Float value in scientific notation serializes correctly`` () =
308+ JsonValue.Float( 1e20 ) .ToString( JsonSaveOptions.DisableFormatting) |> should equal " 1E+20"
309+
296310let normalize ( str : string ) =
297311 str.Replace( " \r\n " , " \n " )
298312 .Replace( " \r " , " \n " )
You can’t perform that action at this time.
0 commit comments