【发布时间】:2014-02-13 07:19:59
【问题描述】:
我有一个简单的问题:是否可以从json 解析 F# Map 类型?因为当我尝试它时(使用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
它是从经典反序列化的:
Map.ofList [ ("1", "one"); ("2", "two"); ("3", "three") ]
生成的 JSON 看起来像 C# 字典
{
"1": "one",
"2": "two",
"3": "three"
}
它在没有设置的情况下进行序列化(只有缩进)。那么是否可以序列化呢,还是有一些解决方法?
感谢回答
【问题讨论】:
-
我认为存在一个重大问题,因为 F# 映射是不可变的。
标签: json json serialization f# json.net