JsonConverter的ReadJson方法中使用的现存参数是什么? [英] What is the existingValue parameter used for in the ReadJson method of a JsonConverter?
问题描述
创建自定义Json Converter时,需要重写的方法之一是:
When creating a custom Json Converter one of the methods which needs to be overridden is:
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
"existingValue"参数的用途是什么?在这种情况下,变量名"existingValue"是什么意思?
What is "existingValue" parameter used for? What does the variable name "existingValue" mean in this context?
推荐答案
简单地说,existingValue
参数为您提供对象的现有值或默认值,该值最终将由ReadJson
方法返回的值替换.这使ReadJson
方法有机会在确定要返回的值时评估现有值.例如,该方法可以决定保留默认值,或者根据需要以某种方式将其与来自读取器的反序列化值组合.
Simply put, the existingValue
parameter gives you the existing or default value of the object that will ultimately be replaced with the value returned from the ReadJson
method. This gives the ReadJson
method the chance to evaluate the existing value when determining what to return. The method could, for example, decide to keep the default, or combine it in some way with the deserialized value from the reader if desired.
请考虑以下示例.该转换器将从JSON反序列化一个整数值,并返回该值与要反序列化到的字段的现有值之和.
Consider the following example. This converter will deserialize an integer value from the JSON and return the sum of that value and the existing value for the field being deserialized to.
class AdditiveIntConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return (objectType == typeof(int));
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
JToken token = JToken.Load(reader);
return (int)existingValue + token.ToObject<int>();
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
}
现在,我们有一个类Foo
,它具有两个int
属性,分别为Value1
和Value2
,这两个属性均使用此转换器.在构造函数中为Value1
分配了默认值42
,而Value2
的常规默认值为零.
Now let's say we have a class Foo
which has two int
properties, Value1
and Value2
, both of which use this converter. Value1
is assigned a default value of 42
in the constructor, while Value2
has the usual default value of zero.
class Foo
{
[JsonConverter(typeof(AdditiveIntConverter))]
public int Value1 { get; set; }
[JsonConverter(typeof(AdditiveIntConverter))]
public int Value2 { get; set; }
public Foo()
{
Value1 = 42;
}
}
如果我们将一些数据反序列化到此类中...
If we deserialize some data into this class...
class Program
{
static void Main(string[] args)
{
string json = @"{ ""Value1"" : 18, ""Value2"" : 33 }";
Foo foo = JsonConvert.DeserializeObject<Foo>(json);
Console.WriteLine("Value1: " + foo.Value1);
Console.WriteLine("Value2: " + foo.Value2);
}
}
...我们得到以下结果:
...we get the following result:
Value1: 60
Value2: 33
当然,这只是一个人为的例子.实际上,在实现JsonConverter时并不需要使用existingValue
参数,并且在大多数情况下,其值将为null或0.您可以放心地忽略它.
Of course, this is just a contrived example. In practice, there is not much of a need to use the existingValue
parameter when implementing a JsonConverter, and most of the time its value will be either null or zero. You can safely ignore it.
这篇关于JsonConverter的ReadJson方法中使用的现存参数是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!