question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Serializing int64 inside discriminated unions generates irreversable JSON representation

See original GitHub issue

Description

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:closed
  • Created 5 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
MangelMaximecommented, Jun 2, 2018

Thank you 😃

I think it will not pass the test suite yet. I need to had more primitive type like Dates for example 😃

1reaction
Zaid-Ajajcommented, Jun 2, 2018

@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 😂 😜

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found