Fleece icon indicating copy to clipboard operation
Fleece copied to clipboard

Parsing an integer value when JSON contains a decimal point

Open giedriusbruzas opened this issue 9 years ago • 9 comments

let result : int ParseResult = parseJSON "1.1"
let (Choice1Of2 value) = result
//val value : int = 1

I feel this is incorrect and should instead result in a failure. There's possibly other similar edge cases like this one.

giedriusbruzas avatar Oct 05 '16 17:10 giedriusbruzas

This also causes trouble when trying to do lensing.

wallymathieu avatar Jul 21 '18 13:07 wallymathieu

JNumber s implies that s is JsonPrimitive ...

wallymathieu avatar Jul 21 '18 13:07 wallymathieu

Seems like F# Data us fixing this as well: https://www.nuget.org/packages/FSharp.Data/3.0.0-beta4 :

(Breaking Change) Don't silently convert decimals and floats to integers in JsonProvider.

wallymathieu avatar Jul 23 '18 12:07 wallymathieu

https://github.com/fsharp/FSharp.Data/pull/1185

wallymathieu avatar Aug 16 '18 17:08 wallymathieu

I think this behavior is not correct at all. Anyway, I also think this should be corrected in the original library (as FSharp.Data just did). We should forward this issue to Newtonsoft and System.Json Also we should note that Fleece is a Json mapper for different libraries, since different libraries have different behavior (there is no agreed standard for Json <-> .NET) it's not up to Fleece to try to fix them.

gusty avatar Sep 22 '18 06:09 gusty

I agree. It only makes sense to align with the libraries. Right now JNumber active pattern has some interesting behavior.

wallymathieu avatar Sep 22 '18 07:09 wallymathieu

I don't know if this is related:

1 |> toJson |> string;;
val it : string = "1"

but

(2,1) |> toJson |> string;;
val it : string = "[
  2.0,
  1.0
]"

gusty avatar Sep 22 '18 18:09 gusty

We also see this kind of behavior: https://github.com/JamesNK/Newtonsoft.Json/issues/1400 So I guess it's a tradeoff what Json parser you use.

wallymathieu avatar Sep 25 '18 06:09 wallymathieu

I've added a PR that documents the behavior. Note that Fsharp.Data works the way you want.

wallymathieu avatar Sep 25 '18 19:09 wallymathieu