反序列化选项
当尝试反序列化从 JSON 字符串返回的 Header option
类型的记录成员时,出现以下异常:
数据契约类型 'Microsoft.FSharp.Core.FSharpOption`1[[MyWeb.Controllers.Header, MyWeb.Controllers,版本=0.0.0.0,文化=中性, PublicKeyToken=null]]' 无法反序列化,因为需要 未找到数据成员“值”。
我正在序列化/反序列化 Message
记录:
[<DataContract>]
type Header =
{ [<DataMember>] mutable ID : int
[<DataMember>] mutable Description : string }
[<DataContract>]
type Message =
{ [<DataMember>] mutable ID : int
[<DataMember>] mutable Header : Header option
[<DataMember>] mutable SenderID : string
[<DataMember>] mutable ReceiverID : string }
我用来反序列化 JSON 的代码:
let deserializeJson<'a> (s:string) =
use ms = new MemoryStream(ASCIIEncoding.ASCII.GetBytes s)
let serialize = DataContractJsonSerializer(typeof<'a>)
serialize.ReadObject ms :?> 'a
以及实际的原始 JSON 结果:
"Message":
{
"ID":13,
"Header": { "Value":{"ID":21,"Description":"some"}},
"SenderID":"312345332423",
"ReceiverID":"16564543423"
}
问题:如何反序列化 'a 选项
?
更新
ASP.NET MVC 默认使用 JavaScriptSerializer
来序列化对象,而我使用 DataContractJsonSerializer
来反序列化。
由于某种原因,似乎 DataContractJsonSerializer 无法读取 JSON 字符串,除非该选项的 Value 属性为小写(正如 @svick 所指出的)。一个肮脏的修复方法是在返回的 JSON 字符串中将“Value”替换为“value”,但我选择采用 Roberts 的建议。
When trying to deserialize a record member of type Header option
returned from a JSON string, I get the following exception:
The data contract type
'Microsoft.FSharp.Core.FSharpOption`1[[MyWeb.Controllers.Header,
MyWeb.Controllers, Version=0.0.0.0, Culture=neutral,
PublicKeyToken=null]]' cannot be deserialized because the required
data member 'value' was not found.
I'm serializing/deserializing a Message
record:
[<DataContract>]
type Header =
{ [<DataMember>] mutable ID : int
[<DataMember>] mutable Description : string }
[<DataContract>]
type Message =
{ [<DataMember>] mutable ID : int
[<DataMember>] mutable Header : Header option
[<DataMember>] mutable SenderID : string
[<DataMember>] mutable ReceiverID : string }
The code I use to deserialize the JSON:
let deserializeJson<'a> (s:string) =
use ms = new MemoryStream(ASCIIEncoding.ASCII.GetBytes s)
let serialize = DataContractJsonSerializer(typeof<'a>)
serialize.ReadObject ms :?> 'a
And the actual raw JSON result:
"Message":
{
"ID":13,
"Header": { "Value":{"ID":21,"Description":"some"}},
"SenderID":"312345332423",
"ReceiverID":"16564543423"
}
The question: how do I deserialize a 'a option
?
Update
ASP.NET MVC uses JavaScriptSerializer
by default to serialize objects and I'm using DataContractJsonSerializer
to deserialize.
For some reason it seems DataContractJsonSerializer
can't read the JSON string unless the Value
property for the option is in lowercase (as pointed out by @svick). A dirty fix would be to replace "Value" with "value" in the returned JSON string, but I've chosen to go with Roberts' suggestion.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您要跳到使用 json.net (又名 Newtonsoft.Json)而不是附带的 json 序列化器使用 .NET 框架,那么您可以使用 选项序列化器 是为了让我能够更有效地使用 ravendb。应该只是用序列化器注册转换并调用反序列化的问题。
If you were to hop over to using json.net (a.k.a Newtonsoft.Json) instead of the json serializer that comes with the .NET framework, then you could use the option serializer I built to allow me to work more effectively with ravendb. Should just be a matter of registering the convert with the serializer and calling Deserialize.