通过Json.Net反序列化F#Map

use*_*808 5 serialization f# json json.net

我有一个简单的问题:是否有可能从解析F#Map类型?因为当我尝试它(With F# Map<string, string>)时,它很容易序列化并且它看起来如何,但是当我尝试反序列化时它会抛出异常.

Newtonsoft.Json.JsonSerializationException: Unable to find a default constructor to use for type Microsoft.FSharp.Collections.FSharpMap`2[System.Int32,System.String]. Path '1', line 2, position 7.
  at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateNewDictionary (Newtonsoft.Json.JsonReader reader, Newtonsoft.Json.Serialization.JsonDictionaryContract contract, System.Boolean& createdFromNonDefaultConstructor) [0x00000] in <filename unknown>:0 
  at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject (Newtonsoft.Json.JsonReader reader, System.Type objectType, Newtonsoft.Json.Serialization.JsonContract contract, Newtonsoft.Json.Serialization.JsonProperty member, Newtonsoft.Json.Serialization.JsonContainerContract containerContract, Newtonsoft.Json.Serialization.JsonProperty containerMember, System.Object existingValue) [0x00000] in <filename unknown>:0 
  at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal (Newtonsoft.Json.JsonReader reader, System.Type objectType, Newtonsoft.Json.Serialization.JsonContract contract, Newtonsoft.Json.Serialization.JsonProperty member, Newtonsoft.Json.Serialization.JsonContainerContract containerContract, Newtonsoft.Json.Serialization.JsonProperty containerMember, System.Object existingValue) [0x00000] in <filename unknown>:0 
  at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize (Newtonsoft.Json.JsonReader reader, System.Type objectType, Boolean checkAdditionalContent) [0x00000] in <filename unknown>:0
Run Code Online (Sandbox Code Playgroud)

它是经典的反序列化:

Map.ofList [ ("1", "one"); ("2", "two"); ("3", "three") ]
Run Code Online (Sandbox Code Playgroud)

生成的JSON看起来像C#字典

{
  "1": "one",
  "2": "two",
  "3": "three"
}
Run Code Online (Sandbox Code Playgroud)

它没有设置序列化(只有缩进).那么可以序列化这个,还是有一些可行的解决方法?

谢谢你的回答

Sim*_*son 1

问题是 json.net 无法构造Map<int,string>. 但是,如果您反序列化为常规 .net,Dictionary<int,string>它将起作用,因为 json 是相同的。