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

FSharp.Data emits invalid JSON for NaN, Infinity and -Infinity #1259

Open
dbarbashov opened this issue Apr 17, 2019 · 1 comment
Open

FSharp.Data emits invalid JSON for NaN, Infinity and -Infinity #1259

dbarbashov opened this issue Apr 17, 2019 · 1 comment

Comments

@dbarbashov
Copy link

dbarbashov commented Apr 17, 2019

FSharp.Data serializes NaN as just NaN, Infinity as just Infinity and -Infinity as -Infinity.
If I understand it correctly, there is no JSON representation for these values, so emitted JSON is not valid.
Repro script:

open FSharp.Data

let serializeDeserializeFloat floatValue = 
    let jsonValue = JsonValue.Array ([| JsonValue.Float floatValue |])
    
    let json = jsonValue.ToString(JsonSaveOptions.DisableFormatting)
    printfn "Serialized: %A" json
    
    let jsonValue = JsonValue.Parse json
    printfn "Deserialized: %A" jsonValue
    
serializeDeserializeFloat 42.0
// OK
serializeDeserializeFloat nan    
// Throws exception
// System.Exception: Invalid JSON starting at character 1, snippet = 
// ----
// [NaN]
// -----
// json = 
// ------
// [NaN]
// -------
serializeDeserializeFloat infinity
//System.Exception: Invalid JSON starting at character 1, snippet = 
//----
//[Infinity]
//-----
//json = 
//------
//[Infinity]
//-------
serializeDeserializeFloat -infinity
//System.Exception: Invalid JSON starting at character 2, snippet = 
//----
//[-Infinity]
//-----
//json = 
//------
//[-Infinity]
//-------

For example, Firefox console encodes these values like this:

>> JSON.stringify([NaN, Infinity, -Infinity])
<< "[null,null,null]"
@dbarbashov
Copy link
Author

For example Newtonsoft.Json serializes these values as strings. NaN is "NaN", Infinity is "Infinity" and -Infinity is "-Infinity", so it is able to deserialize it back to float and JSON is valid.

open Newtonsoft.Json

let serializeDeserializeFloatNewtonsoft floatValue =
    let json = JsonConvert.SerializeObject([| floatValue |])
    printfn "Serialized %A" json
    let deserialized = JsonConvert.DeserializeObject<float[]>(json)
    printfn "Deserialized %A" deserialized
    
serializeDeserializeFloatNewtonsoft 20.0
serializeDeserializeFloatNewtonsoft nan    
serializeDeserializeFloatNewtonsoft infinity
serializeDeserializeFloatNewtonsoft -infinity

Output:

Serialized "[20.0]"
Deserialized [|20.0|]
Serialized "["NaN"]"
Deserialized [|nan|]
Serialized "["Infinity"]"
Deserialized [|infinity|]
Serialized "["-Infinity"]"
Deserialized [|-infinity|]

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