Fable: Quotes added to keys in serialisation of Map<string,'T>

Created on 19 Jan 2017  路  4Comments  路  Source: fable-compiler/Fable

Description

Serialising a value of type Map<string,'T> adds quotes around string values. E.g.,

(Map.ofList ["foo", 1] |> toJson) = """{"\"foo\"" : 1}""""     // evaluates to true

(Note that the key is serialised not as the string foo but as the string "foo".)

Repro code

Compile the following; the console will show the actual and expected serialisation.

#r "node_modules/fable-core/Fable.Core.dll"
open Fable.Core
open Fable.Core.JsInterop

let v = [ "foo", 1 ] |> Map.ofList 
let serialised = toJson v
let expected = """{"foo":1}"""

if expected <> serialised then  
  printfn "Unexpected deserialisation."
  printfn "value (%%A):    %A" v
  printfn "serialisation: %s" serialised
  printfn "expected:      %s" expected

Expected and actual results

Expected: no output.
Actual output:

Unexpected deserialisation. 
value (%A):    [["foo",1]]
serialisation: {"\"foo\"":1}
expected:      {"foo": 1}

Related information

  • fable-compiler version: 0.7.32
  • fable-core version: 0.7.23
  • Operating system: Mac OS 10.11.5

All 4 comments

Thanks for reporting this. I guess this is because you can have any object as map key, so the keys are being serialized to string as well, including string keys. In actuality, it's working as is, but I agree is nicer to have the strings without quotes, so I'll try to check if the keys are string first :+1:

Thanks!

I wouldn't say its working, though: If I round-trip a Map<string,'T> from server to client to server, the keys have quotes on them when they get back. That means that Map.find "foo" m works before the round-trip, but breaks afterwards.

Btw, thanks for an exciting compiler/framework!

You're right. It was an easy fix anyways, can you try with latest fable-core version?

Works as expected, both in the small example above and in my larger prototype app. Thanks a bunch!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

nozzlegear picture nozzlegear  路  3Comments

theprash picture theprash  路  3Comments

jwosty picture jwosty  路  3Comments

ncave picture ncave  路  3Comments

et1975 picture et1975  路  3Comments