Serializing int64 inside discriminated unions generates irreversable JSON representation
See original GitHub issueDescription
Serializing int64 inside discriminated unions generates incompatible JSON, in other words, the generated JSON cannot be deserialized back to original type.
After tracking down the issue at Fable.Remoting, I narrowed it down to the following code that doesn’t work:
open Fable.Core
open Fable.Core.JsInterop
type SingleCase = SingleCase of int64
let input = SingleCase 20L
printfn "%A" (ofJson<SingleCase> (toJson input))
You will get the error:
Uncaught Error: Cannot deserialize into Test.SingleCase - Error (inflate): Input string was not in a correct format. - Data: '{"SingleCase":{"low":20,"high":0,"unsigned":false}}'
Note: the same issue happens even with unions of multiple cases.
The Fable JsonConverter
on the server is also unable to deserialize the date {"low":20,"high":0,"unsigned":false}
into a proper int64
and throws this exception:
System.Exception: Expecting int64 but got StartObject.
/*
rest of irrelevant stack strace
*/
where the StartObject
indicates that the deserializer encountered an object literal where it was expecting int64
Related information
- Fable version (
dotnet fable --version
): 1.3.17 && repl - Operating system: Windows 10 && linux on Travis CI
Issue Analytics
- State:
- Created 5 years ago
- Comments:6 (6 by maintainers)
Top Results From Across the Web
Mimic Discriminated Union Types in C# with serialization via ...
Let's try to serialize an instance of this union type using System.Text.Json. In order to produce an instance of one of the Status...
Read more >Ability to JSON serialize and deserialize int64 with ...
Does anyone know of a way to accurately serialize and deserialize whole Int64 numbers to/from JSON with precision, or is a library modification ......
Read more >Trouble with JSON serialization/parsing of type ...
However, I'm having some trouble serializing/parsing the discriminating union containing either the connection or the telemetry data.
Read more >Discriminated Unions - F# | Microsoft Learn
Discriminated unions provide support for values that can be one of a number of named cases, possibly each with different values and types....
Read more >F# – Serializing F# Record and Discriminated Union types
I love using F#'s Record and Discriminated Union types, they work nicely with pattern matching inside your F# code and can often alleviate ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Thank you 😃
I think it will not pass the test suite yet. I need to had more primitive type like Dates for example 😃
@MangelMaxime I took a closer look, very interesting way for deserialization, I wonder if it can handle the exhaustive test suite of Fable.Remoting, I will try it soon and otherwise I will be swarming you with issues 😂 😜