When using a JsonWriter, what's the purpose of WriteStartConstructor?

£可爱£侵袭症+ 提交于 2019-12-10 18:26:20

问题


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

new Foo(
)

...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!


回答1:


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.

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.

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.



来源:https://stackoverflow.com/questions/31778612/when-using-a-jsonwriter-whats-the-purpose-of-writestartconstructor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!