使用JsonWriter时,WriteStartConstructor的目的是什么? [英] When using a JsonWriter, what's the purpose of WriteStartConstructor?

查看:121
本文介绍了使用JsonWriter时,WriteStartConstructor的目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标题说明了一切.我看到它(及其相应的一端)吐出了以下内容……

Title says it all. I see it (with its corresponding end) spits out the following...

new Foo(
)

...但是反序列化时我不明白new的实际作用.文档只是说它编写了一个Json构造函数,而不是Json构造函数

...but I don't understand what the new actually does when deserializing. The docs just say it writes a Json constructor, but not what a Json constructor is!

推荐答案

此方法是作为增强功能的一部分引入的,该功能允许Json.NET在以JavaScript构造函数表示时解析日期文字,例如:在JSON中序列化日期.即使根据 JSON标准严格严格地说,该语法无效,但显然有足够的需求来支持它,Newtonsoft表示支持

This method was introduced as part of an enhancement to allow Json.NET to parse date literals when represented with JavaScript constructors like so: new Date(1234656000000). For details, see Serializing Dates in JSON. Even though this syntax is not strictly valid according to the JSON standard, apparently there was enough demand to support it that Newtonsoft put it in.

因此,有必要将低级方法添加到 JsonWriter JsonReader 来编写和读取这种格式的文字. (严格来说,我想应该可以将文字写到 WriteRaw ,这本来是一种hackey,无论如何都没有方法ReadRaw) WriteStartConstructor 是这些低级实用程序方法之一.

Thus it was necessary to add low-level methods to JsonWriter and JsonReader to write and read literals in this format. (Strictly speaking I suppose writing could have been tacked on to WriteRaw, this would have been kind of hackey, and anyway there is no method ReadRaw) WriteStartConstructor is one of these low-level utility methods.

还有一个高级Linq-to-JSON类 JConstructor 代表这种语法.您可以根据需要使用它来支持其他特定的JavaScript构造函数,例如,参见

There is also a high-level Linq-to-JSON class JConstructor to represent this syntax. You can use it to support other specific JavaScript constructors as needed, for instance see How to convert new Date(year, month, day) overload with JSON.Net.

这篇关于使用JsonWriter时,WriteStartConstructor的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆