在项目的模型中,我使用JsonConverter
属性来帮助对这些模型进行(反序列化)。
转换器当前如下所示:
public class CustomJsonConverter : Newtonsoft.Json.JsonConverter
{
bool _canWrite = true;
public override bool CanWrite
{
get { return _canWrite; }
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
serializer.PreserveReferencesHandling = PreserveReferencesHandling.Objects;
serializer.DefaultValueHandling = DefaultValueHandling.Ignore;
serializer.NullValueHandling = NullValueHandling.Ignore;
_canWrite = false;
var jObject = JObject.FromObject(value, serializer);
_canWrite = true;
jObject.WriteTo(writer);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
serializer.PreserveReferencesHandling = PreserveReferencesHandling.Objects;
if (reader.TokenType == JsonToken.StartObject)
{
existingValue = existingValue ?? serializer.ContractResolver.ResolveContract(objectType).DefaultCreator();
serializer.Populate(reader, existingValue);
return existingValue;
}
else if (reader.TokenType == JsonToken.Null)
{
return null;
}
else
{
throw new JsonSerializationException();
}
}
public override bool CanConvert(Type objectType)
{
return typeof(IModelBase).IsAssignableFrom(objectType);
}
}
模型具有一个如下所示的基类:
[JsonConverter(typeof(CustomJsonConverter))]
public abstract class ModelBase : IModelBase
{
public string ID { get; set; }
public DateTime CreatedAt { get; set; }
public DateTime ModifiedAt { get; set; }
}
ModelBase
类的派生类具有其类型也从ModelBase
派生的属性。例如:
public class CustomerModel : ModelBase
{
public string Name { get; set; }
public UserModel CreatedBy { get; set; }
public UserModel ModifiedBy { get; set; }
}
public class UserModel : ModelBase
{
public string Name { get; set; }
public UserModel CreatedBy { get; set; }
public UserModel ModifiedBy { get; set; }
}
我在ASP.NET Web API 2应用程序(服务器端)和C#应用程序(客户端)中使用这些模型。对于大多数API调用,将返回模型数组。序列化模型时,一切按预期进行。但是,在反序列化时,只有每个引用的第一次出现都会填充信息。
[
{
"$id": "1",
"Name": "Customer1",
"CreatedBy": {
"$id": "2",
"ID": "1",
"Name": "User1"
},
"ModifiedBy": {
"$id": "3",
"ID": "3",
"Name": "User3"
},
"ID": "1",
"CreatedAt": "2019-02-06T00:00:04",
"ModifiedAt": "2019-02-06T00:20:12"
},
{
"$id": "4",
"Name": "Customer2",
"CreatedBy": {
"$ref": "2"
},
"ModifiedBy": {
"$ref": "2"
},
"ID": "2",
"CreatedAt": "2019-02-06T00:10:00",
"ModifiedAt": "2019-02-06T00:10:00"
}
]
当尝试反序列化由Web API返回的JSON对象时,第一个CreatedBy
对象的ModifiedBy
和CustomerModel
属性将是正确的。但是,对于第二个CustomerModel
对象,这些属性将是未设置任何属性的新UserModel
实例。
要反序列化JSON字符串,我使用以下代码:
using (var sr = new StreamReader(streamFromWebAPICall))
{
using (var jtr = new JsonTextReader(sr))
{
var js = new JsonSerializer();
return js.Deserialize(jtr, objectType);
}
}
如何正确设置所有反序列化对象的属性?
问题似乎出在serializer.Populate(reader, existingValue)
上,那里的参考文献不记得了。
答案 0 :(得分:2)
您的基本问题是,您为想要启用custom JsonConverter
的类型提供了PreserveReferencesHandling
。但是无论何时应用自定义转换器,它都必须手动处理一切,包括解析和生成"$ref"
和"$id"
属性。您的转换器不执行此操作,因此反序列化代码无法正确反序列化对象图。
从accepted answer到 Custom object serialization vs PreserveReferencesHandling 的模板转换器显示了如何处理这些属性。但是,由于您的转换器似乎只在(反)序列化之前切换了一些序列化程序设置,因此您可以简单地进行调用以递归(反)对对象进行序列化,从而在持续时间内禁用转换器,如下所示:
public class CustomJsonConverter : Newtonsoft.Json.JsonConverter
{
[ThreadStatic]
static bool disabled;
// Disables the converter in a thread-safe manner.
bool Disabled { get { return disabled; } set { disabled = value; } }
public override bool CanWrite { get { return !Disabled; } }
public override bool CanRead { get { return !Disabled; } }
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
using (new PushValue<bool>(true, () => Disabled, val => Disabled = val))
using (new PushValue<PreserveReferencesHandling>(PreserveReferencesHandling.Objects, () => serializer.PreserveReferencesHandling, val => serializer.PreserveReferencesHandling = val))
using (new PushValue<DefaultValueHandling>(DefaultValueHandling.Ignore, () => serializer.DefaultValueHandling, val => serializer.DefaultValueHandling = val))
using (new PushValue<NullValueHandling>(NullValueHandling.Ignore, () => serializer.NullValueHandling, val => serializer.NullValueHandling = val))
{
serializer.Serialize(writer, value);
}
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.Null)
{
return null;
}
using (new PushValue<bool>(true, () => Disabled, val => Disabled = val))
using (new PushValue<PreserveReferencesHandling>(PreserveReferencesHandling.Objects, () => serializer.PreserveReferencesHandling, val => serializer.PreserveReferencesHandling = val))
using (new PushValue<DefaultValueHandling>(DefaultValueHandling.Ignore, () => serializer.DefaultValueHandling, val => serializer.DefaultValueHandling = val))
using (new PushValue<NullValueHandling>(NullValueHandling.Ignore, () => serializer.NullValueHandling, val => serializer.NullValueHandling = val))
{
return serializer.Deserialize(reader, objectType);
}
}
public override bool CanConvert(Type objectType)
{
throw new NotImplementedException();
}
}
public struct PushValue<T> : IDisposable
{
Action<T> setValue;
T oldValue;
public PushValue(T value, Func<T> getValue, Action<T> setValue)
{
if (getValue == null || setValue == null)
throw new ArgumentNullException();
this.setValue = setValue;
this.oldValue = getValue();
setValue(value);
}
// By using a disposable struct we avoid the overhead of allocating and freeing an instance of a finalizable class.
public void Dispose()
{
if (setValue != null)
setValue(oldValue);
}
}
请注意,由于Json.NET将跨线程共享合约和转换器,因此禁用转换器的逻辑必须具有线程安全性。
演示小提琴#1 here。
作为替代方案,您可以完全消除转换器,并将[JsonObject(IsReference = true)]
直接应用于ModelBase
:
[JsonObject(IsReference = true)]
public abstract class ModelBase : IModelBase
{
public string ID { get; set; }
public DateTime CreatedAt { get; set; }
public DateTime ModifiedAt { get; set; }
}
然后使用在这样的设置中指定的DefaultValueHandling.Ignore
和NullValueHandling.Ignore
进行序列化和反序列化:
static object Deserialize(Stream streamFromWebAPICall, Type objectType)
{
using (var sr = new StreamReader(streamFromWebAPICall))
{
using (var jtr = new JsonTextReader(sr))
{
var settings = new JsonSerializerSettings
{
DefaultValueHandling = DefaultValueHandling.Ignore,
NullValueHandling = NullValueHandling.Ignore,
};
var js = JsonSerializer.CreateDefault(settings);
return js.Deserialize(jtr, objectType);
}
}
}
演示小提琴#2 here。
请注意,您也可以从[JsonObject(ItemNullValueHandling = NullValueHandling.Ignore)]
开始设置Json.NET 11.0.1,但是ItemDefaultValueHandling
上似乎没有JsonObjectAttribute
设置,因此请将其添加到序列化程序设置中(或必须将nullable用于可选的值类型值,例如CreatedAt
)。