2

场景是 - 从网络接收 JSON 字符串并将其反序列化为正确的相应记录类型。

JSON 字符串可以是:

(1)"{"a":"some text"}"

(2)"{"b":1}"

值可能不同,但字段的格式将对应于 Type1 或 Type2:

type Type1 = {a:string}
type Type2 = {b:int}

收到未知字符串时,我正在尝试获取正确记录类型的实例:

// Contents of a string might be like (1) or like (2), but we don't know which one
let someJsonString = "..."

let obj = JsonConvert.DeserializeObject(someJsonString)

最后一行返回一个 Object 类型的对象。

在其上使用模式匹配并不能确定类型:

match obj with
| :? Type1 as t1 -> printfn "Type1: a = %A" t1.a
| :? Type2 as t2 -> printfn "Type2: b = %A" t2.b
| _ -> printfn "None of above"

这里打印了“以上都不是”。

当我使用指示某种类型来反序列化对象时:

JsonConvert.DeserializeObject<Type1>(someJsonString)

模式匹配正在工作和打印:

Type1: a = <the value of a>

但是,这在我的情况下不起作用,因为我无法提前知道未知 JSON 字符串将具有什么类型的内容。

有没有办法根据字符串内容将 JSON 字符串反序列化为正确的记录类型?

注意:如有必要,当字符串在发送端被序列化时,类型的名称可以作为该字符串的一部分发送。但是,如何获得一个 Type 类型的实例,具有一个类型的名称,如“Type1”或“Type2”?

该类型的完全限定名称在不同的机器上会有所不同,所以我不确定它是否可能。即一台机器将 Type1 指定为:

"FSI_0059+Test1, FSI-ASSEMBLY, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"

另一个是:

"FSI_0047+Test1, FSI-ASSEMBLY, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"
4

1 回答 1

2

如果没有其他类型信息,您通常无法执行此操作。序列化时需要指定类型,以便在反序列化时可以读回。

Newtonsoft.Json有一个TypeNameHandling 选项,您可以在序列化时设置,以便生成的 JSON 反序列化为正确的类型。

这是一个完整的例子:

let instance = { a = 10 }
let settings = new JsonSerializerSettings(TypeNameHandling = TypeNameHandling.All)
let json = JsonConvert.SerializeObject(instance, settings)
let retrieved = JsonConvert.DeserializeObject(json, settings)
于 2017-01-22T06:52:51.757 回答