因某些需求,需要将指定的对象序列化放到缓存中,在使用Newtonsoft.Json序列化时候报错,异常信息清晰明显就是循环引用问题。具体错误如下:
详细异常如下:
fail: System.Exception[-2146233088]
Self referencing loop detected for property ‘Ancestor’ with type ”. Path ‘Descendants[0]’. Newtonsoft.Json.JsonSerializationException: Self referencing loop detected for property ‘Ancestor’ with type ‘*’. Path ‘Descendants[0]’.
at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.CheckForCircularReference(JsonWriter writer, Object value, JsonProperty property, JsonContract contract, JsonContainerContract containerContract, JsonProperty containerProperty)
at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.CalculatePropertyValues(JsonWriter writer, Object value, JsonContainerContract contract, JsonProperty member, JsonProperty property, JsonContract& memberContract, Object& memberValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter writer, IEnumerable values, JsonArrayContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter, Object value, Type objectType)
at Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter, Object value, Type objectType)
at Newtonsoft.Json.JsonConvert.SerializeObjectInternal(Object value, Type type, JsonSerializer jsonSerializer)
at System.Threading.Tasks.ValueTask`1.get_Result()
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeActionMethodAsync()
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeNextActionFilterAsync()
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ActionExecutedContext context)
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeInnerFilterAsync()
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextExceptionFilterAsync()
解决方法
在序列化或者反序列化指定JsonSerializerSettings即可解决:
var setting = new JsonSerializerSettings();
setting.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
var json = JsonConvert.SerializeObject(data, setting)
如果想要循环引用的数据得以保留,以便后面反序列化时能还原数据,所以将循环引用设置为序列化 ,如下方式:
var setting = new JsonSerializerSettings();
setting.PreserveReferencesHandling = PreserveReferencesHandling.Objects;
setting.ReferenceLoopHandling = ReferenceLoopHandling.Serialize;
var json = JsonConvert.SerializeObject(data, setting);
转载请注明:清风亦平凡 » Newtonsoft.Json序列化对象时循环引用异常处理