json.net中的字符串行为:为什么将空JToken替换为非空值? [英] string behavior in json.net: why is a null JToken replaced with a non-null value?
问题描述
使用json.net(C#).不知道为什么JToken不为null.
This using json.net (C#). Not sure why JToken is not null.
var obj = new JObject();
obj["field1"] = null;
var token = obj["field1"];
Console.WriteLine(token == null); // false
Console.WriteLine(token.ToString() == ""); // true
Console.WriteLine(token.Type == JTokenType.Null); // true
推荐答案
这是设计使然.
Json.NET决不允许在JToken
层次结构中以JArray
成员或JProperty
值的形式出现实际的空JToken
值.相反,如果应用程序代码尝试设置或添加null
令牌,则会将其替换为带有JValue
. json/help/html/P_Newtonsoft_Json_Linq_JValue_Type.htm"rel =" nofollow noreferrer> JValue.Type
等于 JContainer.EnsureParentToken(JToken item, bool skipParentCheck)
:
Json.NET never allows an actual null JToken
value to appear in a JToken
hierarchy, either as an JArray
member or a JProperty
value. Instead, if applications code attempts to set or add a null
token, it gets replaced with a non-null JValue
with JValue.Type
equal to JTokenType.Null
. This replacement occurs in, e.g., JContainer.EnsureParentToken(JToken item, bool skipParentCheck)
:
internal JToken EnsureParentToken(JToken item, bool skipParentCheck)
{
if (item == null)
{
return JValue.CreateNull();
}
以及 JProperty.Value
:
public JToken Value
{
set
{
CheckReentrancy();
JToken newValue = value ?? JValue.CreateNull();
我相信Newtonsoft这样做是为了捕获以下两个JSON对象之间的差异:
I believe Newtonsoft does this to capture the difference between the following two JSON objects:
{
"field1": null
}
还有空对象:
{ }
在第一种情况下,属性"field1"
带有null
JSON值.在第二种情况下,属性"field1"
不存在. Linq-to-JSON用空类型JValue
而不是JProperty.Value
实际上为空表示第一种情况.可能这样做是因为,如果不这样做,在两种情况下object["field1"]
都将返回null
,这使得它们更难区分. (与Dictionary<TKey, TValue>
不同,JObject
在尝试访问不存在的属性的值时不会抛出KeyNotFoundException
.相反,
In the first case, the property "field1"
is present with a null
JSON value. In the second case, the property "field1"
is not present. Linq-to-JSON represents the first case with a null-type JValue
rather than having JProperty.Value
actually be null. It likely does this because if it did not, object["field1"]
would return null
in both cases, making them harder to distinguish. (Unlike Dictionary<TKey, TValue>
, JObject
does not throw a KeyNotFoundException
when attempting to access the value of a non-existent property. Instead, JObject.Item[String]
returns a null JValue
for the value of a missing key.)
您的代码obj["field1"] = null;
创建第一种形式的JSON对象,如果您检查obj.ToString()
的值,则可以看到该对象.因此obj["field1"]
返回非null
Your code, obj["field1"] = null;
, creates a JSON object of the first form, which you can see if you examine the value of obj.ToString()
. Thus obj["field1"]
returns non-null
如果不需要区分缺失值和空值属性,则可以引入扩展方法来检查空值,例如 在Newtonsoft中使用JSON空处理的问题 .
If you do not need to distinguish between missing and null-valued properties, you could introduce an extension method to check for a null value such as the one of the ones from Checking for empty/null JToken in a JObject or Issue with JSON null handling in Newtonsoft.
这篇关于json.net中的字符串行为:为什么将空JToken替换为非空值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!