Json.NET中的JConstructor和JRaw [英] JConstructor and JRaw in Json.NET
问题描述
根据在StackOverflow上的此答案:
Json.NET包含许多功能,这些功能不是JSON规范的一部分.特别是,它允许解析正式"无效的一些JSON文件.这包括未引用的属性,注释,构造函数等.
Json.NET includes many features which are not part of JSON specification. In particular, it allows parsing some JSON files which are "officially" invalid. This includes unquoted properties, comments, constructors etc.
这些都是可以从JToken
分配的所有类型:
These are all the types assignable from JToken
:
JArray
JConstructor
JContainer
JObject
JProperty
JRaw
JValue
请告知以下内容是否正确:
Please tell if the following are true:
-
在正式"有效json上的
JToken.Parse(json)
不可能在其后代中包含JConstructor
或JRaw
.
It is not possible that
JToken.Parse(json)
on an "officially" valid json will in its descendants containJConstructor
orJRaw
.
假设json是官方"有效的,则在这些后代中只能看到以下类型:JArray
,JObject
,JProperty
,JValue
.
Provided that a json is "officially" valid, one can expect only the following types to see in those descendants: JArray
, JObject
, JProperty
, JValue
.
推荐答案
您的陈述是正确的.
-
JConstructor
旨在实现对日期以 JavaScript日期格式,例如:new Date(1234656000000)
.如在JSON中序列化日期:
JConstructor
is designed to enable capture of dates in JavaScript Date format, e.g.:new Date(1234656000000)
. As noted in Serializing Dates in JSON:
从技术上讲,根据规范,这是无效 JSON,但是所有浏览器和某些JSON框架(包括Json.NET)都支持它.
Technically this is invalid JSON according to the spec, but all browsers and some JSON frameworks, including Json.NET, support it.
因此,当解析严格符合当前IETF提议的标准的JSON时,JConstructor
将不会出现.或原始JSON提案.
Thus JConstructor
will not appear when parsing JSON that conforms strictly to the current IETF proposed standard or the original JSON proposal.
JRaw
不会 使用 JToken.Parse(string)
解析JSON时出现.它主要用于帮助编写来自JToken
层次结构的预格式化JSON文字,这很有用.通过使用JRaw
,可以避免仅仅为了发出它而对已格式化的JSON进行解析,例如:
JRaw
will never appear when parsing JSON using JToken.Parse(string)
. It is useful mainly to facilitate writing of pre-formatted JSON literals from a JToken
hierarchy. By using JRaw
, one can avoid parsing the already-formatted JSON simply in order to emit it, e.g.:
var root = new JObject(new JProperty("response", new JRaw(jsonLiteral)));
var rootJson = root.ToString();
可以代替效率较低的方法:
can be done instead of the less-efficient:
var root = new JObject(new JProperty("response", JToken.Parse(jsonLiteral)));
也可以反序列化为JRaw
以将JSON层次结构捕获为单个字符串文字,尽管我认为这样做没有太大用处.例如,给定类:
It's also possible to deserialize to JRaw
to capture a JSON hierarchy as a single string literal, though I don't see much use in doing so. For instance, given the class:
public class RootObject
{
public JRaw response { get; set; }
}
一个人可以做:
One can do:
var rootDeserialized = JsonConvert.DeserializeObject<RootObject>(rootJson);
var jsonLiteralDeserialized = (string)rootDeserialized.response;
但是,这不一定比反序列化为JToken
更有效.
However, this is not necessarily more efficient than deserializing to a JToken
.
您猜测,在解析严格有效的JSON时,只会出现JArray
,JObject
,JProperty
和JValue
.
As you surmise, only JArray
, JObject
, JProperty
and JValue
will appear when parsing strictly valid JSON.
这篇关于Json.NET中的JConstructor和JRaw的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!