-
-
Notifications
You must be signed in to change notification settings - Fork 42
Open
Description
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 = trueBut 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
-- 179769313486231570000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000let 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 beendetSeveral 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 = truelet parsed = Newtonsoft.Json.JsonConvert.DeserializeObject<float>("179769313486231570000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")
val parsed: float = 1.797693135e+308
parsed = System.Double.MaxValue
val it: bool = trueSystem.Text.Json.JsonSerializer.Deserialize<float>("179769313486231570000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")
val it: float = 1.797693135e+308
parsed = System.Double.MaxValue
val it: bool = trueSo 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.
Metadata
Metadata
Assignees
Labels
No labels