Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decode.float fails to decode large floats #212

Open
ajuch opened this issue Dec 20, 2024 · 0 comments
Open

Decode.float fails to decode large floats #212

ajuch opened this issue Dec 20, 2024 · 0 comments

Comments

@ajuch
Copy link

ajuch commented Dec 20, 2024

Serializing and deserializing Double.MaxValue works in general:

open System
open Thoth.Json.Net
Encode.float Double.MaxValue |> Encode.toString 0 |> Decode.fromString Decode.float
val it: Result<float,string> = Ok 1.797693135e+308

match it with Ok f -> f = System.Double.MaxValue | Error e -> failwith "error"
val it: bool = true

But in our case we are storing the JSON in a Postgres database in a JSONB-column. Postgres uses the numeric type for numeric JSON values. Fetching the JSON from the database and deserializing it with Thoth.Json leads to a problem:

select (jsonb('1.7976931348623157E+308'))::text
-- 179769313486231570000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
let maxFloat = "179769313486231570000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
val maxFloat: string =
  "1797693134862315700000000000000000000000000000000000000000000"+[248 chars]

match Decode.fromString Decode.float maxFloat with Ok f -> f = Double.MaxValue | Error e -> failwith "error"
System.InvalidCastException: Object must implement IConvertible.
   at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)
   at Newtonsoft.Json.Linq.Extensions.Convert[T,U](T token)
   at Newtonsoft.Json.Linq.Extensions.Value[T,U](IEnumerable`1 value)
   at Newtonsoft.Json.Linq.Extensions.Value[U](IEnumerable`1 value)
   at Thoth.Json.Net.Decode.float(String path, JToken token)
   at Thoth.Json.Net.Decode.fromValue[T](String path, FSharpFunc`2 decoder, JToken value)
   at Thoth.Json.Net.Decode.fromString[T](FSharpFunc`2 decoder, String value)
   at <StartupCode$FSI_0026>.$FSI_0026.main@() in C:\stdin:line 3
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
   at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)
Aufgrund eines Fehlers beendet

Several other methods to deserialize this value to float worked:

let parsed = System.Double.Parse "179769313486231570000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
val parsed: float = 1.797693135e+308

parsed = System.Double.MaxValue
val it: bool = true
let parsed = Newtonsoft.Json.JsonConvert.DeserializeObject<float>("179769313486231570000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")
val parsed: float = 1.797693135e+308

parsed = System.Double.MaxValue
val it: bool = true
System.Text.Json.JsonSerializer.Deserialize<float>("179769313486231570000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")
val it: float = 1.797693135e+308

parsed = System.Double.MaxValue
val it: bool = true

So I consider this a bug, although I would be happier if postgres just returned the value in exponential notation. Please note that the value doesn't need to be that high, it fails also for smaller inputs. It seems somewhere in the loop the value is deserialized as BigInteger, which doesn't implement IConvertible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant