[英]Deserializing an IEnumerable<T> with [DataContract] applied does not work
Rather new to Json.net and tried the following simple example serializing and then deserialing an object getting the error below: 并不是Json.net的新手,而是尝试了以下简单示例,将其序列化然后反序列化,得到以下错误:
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Collections;
namespace Timehunter.Base.ServicesTests
{
/// <summary>
/// Summary description for JsonError
/// </summary>
[TestClass]
public class JsonError
{
[TestMethod]
public void TestMethod1()
{
JsonSerializerSettings serializerSettings = new JsonSerializerSettings()
{
DateFormatHandling = DateFormatHandling.IsoDateFormat,
DateParseHandling = Newtonsoft.Json.DateParseHandling.DateTimeOffset
};
Act.Activities acts = new Act.Activities();
acts.Add(new Act.Activity() { Id = 1, Name = "test1" });
acts.Add(new Act.Activity() { Id = 2, Name = "test2" });
string json = Newtonsoft.Json.JsonConvert.SerializeObject(acts, serializerSettings);
Timehunter.Base.Act.Activities target = Newtonsoft.Json.JsonConvert.DeserializeObject<Timehunter.Base.Act.Activities>(json, serializerSettings);
Assert.AreEqual("test1", target.List[0].Name, "Name of first activity");
}
}
}
namespace Timehunter.Base
{
[DataContract]
public class Activity
{
private int _id;
private string _name;
[DataMember]
public int Id
{
get { return this._id; }
set { this._id = value; }
}
[DataMember]
public string Name
{
get { return this._name; }
set { this._name = value; }
}
public Activity()
{
this._id = new int();
this._name = string.Empty;
}
}
[DataContract]
public class Activities : IEnumerable<Activity>
{
private List<Activity> _list;
[DataMember]
public List<Activity> List
{
get { return this._list; }
set { this._list = value; }
}
public Activities()
{
this._list = new List<Activity>();
}
public void Add(Activity item)
{ this._list.Add(item); }
public bool Remove(Activity item)
{ return this._list.Remove(item); }
public int Count()
{ return this._list.Count; }
public IEnumerator<Activity> GetEnumerator()
{
return this._list.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}
And then I get the following error: 然后我得到以下错误:
Test Name: TestMethod1
Test FullName: Timehunter.Base.ServicesTests.JsonError.TestMethod1
Test Source: C:\Users\hawi.HAWCONS\Documents\Visual Studio 2015\Projects\Timehunter.Data\Timehunter.Base.ServicesTests\JsonError.cs : line 67
Test Outcome: Failed
Test Duration: 0:00:00,2038359
Result StackTrace:
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateNewList(JsonReader reader, JsonArrayContract contract, Boolean& createdFromNonDefaultCreator)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateList(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, Object existingValue, String id)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)
at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings)
at Timehunter.Base.ServicesTests.JsonError.TestMethod1() in C:\Users\hawi.HAWCONS\Documents\Visual Studio 2015\Projects\Timehunter.Data\Timehunter.Base.ServicesTests\JsonError.cs:line 79
Result Message:
Test method Timehunter.Base.ServicesTests.JsonError.TestMethod1 threw exception:
Newtonsoft.Json.JsonSerializationException: Cannot create and populate list type Timehunter.Base.Act.Activities. Path '', line 1, position 1.
What am I doing wrong? 我究竟做错了什么?
Update 2 更新2
This is getting reverted back in 11.0.2 for backwards compatibility. 为了向后兼容,在11.0.2中将其还原。 Refer to the original answer for a solution. 请参阅原始答案以获取解决方案。
Update 更新资料
Reported as Issue #1598: DataContractAttribute does not cause JSon object serialization for IEnumerable and fixed in commit e9e2d00
. 报告为问题#1598:DataContractAttribute不会导致IEnumerable的JSon对象序列化,并已在commit e9e2d00
。 It should be in the next release after 10.0.3 which will probably be Json.NET version 11. 它应该在10.0.3之后的下一发行版中 ,它可能是Json.NET版本11。
Original answer 原始答案
I notice you have marked your Activities
class with [DataContract]
and [DataMember]
: 我注意到您已经用[DataContract]
和[DataMember]
标记了Activities
类:
[DataContract]
public class Activities : IEnumerable<Activity>
{
private List<Activity> _list;
[DataMember]
public List<Activity> List
{
get { return this._list; }
set { this._list = value; }
}
// ...
}
Applying [DataContact]
will cause DataContractJsonSerializer
to serialize an IEnumerable<T>
as a JSON object with properties, rather than as a JSON array. 应用[DataContact]
将导致DataContractJsonSerializer
将IEnumerable<T>
序列化为具有属性的JSON对象,而不是JSON数组。 Since Json.NET supports data contract attributes when applied to non-enumerables, you might be thinking that it will respect them on enumerables and collections as well. 由于Json.NET在应用于非枚举时支持数据协定属性 ,因此您可能会认为它也会在枚举和集合中尊重它们。
However, it appears this is not implemented. 但是,看来这没有实现。 If I serialize your class with DataContractJsonSerializer
, I see 如果我使用DataContractJsonSerializer
序列化您的类,我会看到
{"List":[{"Id":1,"Name":"test1"},{"Id":2,"Name":"test2"}]}
But if I serialize with Json.NET, I see that the [DataContract]
was ignored: 但是,如果我使用Json.NET进行序列化,则会发现[DataContract]
被忽略了:
[{"Id":1,"Name":"test1"},{"Id":2,"Name":"test2"}]
Then later it throws an exception during deserialization because it doesn't know how to add members to your IEnumerable<Activity>
class. 然后,稍后它将在反序列化期间引发异常,因为它不知道如何将成员添加到IEnumerable<Activity>
类。 (It would have been able to add members if your class implemented ICollection<Activity>
, or had a constructor with an IEnumerable<Activity>
argument .) (如果您的类实现了ICollection<Activity>
或具有带有IEnumerable<Activity>
参数的构造函数,则可以添加成员。)
So, should this work? 那么,这应该工作吗? The documentation page Serialization Attributes states: 文档页面“ 序列化属性”指出:
The DataContractAttribute can be used as substitute for JsonObjectAttribute. DataContractAttribute可以替代JsonObjectAttribute。 The DataContractAttribute will default member serialization to opt-in. DataContractAttribute将默认成员序列化以选择加入。
Which implies that Json.NET ought to work the way you expect. 这意味着Json.NET应该按照您期望的方式工作。 You could report an issue about it if you want -- at least the documentation should be clarified. 如果需要,可以报告有关此问题的信息-至少应弄清楚文档。
As a workaround , if you want to force Json.NET to serialize a collection as an object, you need to use [JsonObject]
instead: 解决方法是 ,如果要强制Json.NET将集合序列化为对象,则需要使用[JsonObject]
:
[DataContract]
[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
public class Activities : IEnumerable<Activity>
{
private List<Activity> _list;
[DataMember]
[JsonProperty]
public List<Activity> List
{
get { return this._list; }
set { this._list = value; }
}
// Remainder unchanged.
}
If you have many enumerable classes with [DataContract]
applied, or cannot add a dependency on Json.NET to your models, you can create a custom ContractResolver
that checks for the presence of [DataContract]
on enumerable classes and serializes them as objects: 如果您有许多应用了[DataContract]
可枚举类,或者无法在模型中添加对Json.NET的依赖关系,则可以创建一个自定义ContractResolver
来检查可枚举类上是否存在[DataContract]
并将其序列化为对象:
public class DataContractForCollectionsResolver : DefaultContractResolver
{
// As of 7.0.1, Json.NET suggests using a static instance for "stateless" contract resolvers, for performance reasons.
// http://www.newtonsoft.com/json/help/html/ContractResolver.htm
// http://www.newtonsoft.com/json/help/html/M_Newtonsoft_Json_Serialization_DefaultContractResolver__ctor_1.htm
// "Use the parameterless constructor and cache instances of the contract resolver within your application for optimal performance."
static DataContractForCollectionsResolver instance;
static DataContractForCollectionsResolver() { instance = new DataContractForCollectionsResolver(); }
public static DataContractForCollectionsResolver Instance { get { return instance; } }
protected DataContractForCollectionsResolver() : base() { }
protected override JsonContract CreateContract(Type objectType)
{
var t = (Nullable.GetUnderlyingType(objectType) ?? objectType);
if (!t.IsPrimitive
&& t != typeof(string)
&& !t.IsArray
&& typeof(IEnumerable).IsAssignableFrom(t)
&& !t.GetCustomAttributes(typeof(JsonContainerAttribute),true).Any())
{
if (t.GetCustomAttributes(typeof(DataContractAttribute),true).Any())
return base.CreateObjectContract(objectType);
}
return base.CreateContract(objectType);
}
}
Then use the following settings: 然后使用以下设置:
var serializerSettings = new JsonSerializerSettings()
{
DateFormatHandling = DateFormatHandling.IsoDateFormat,
DateParseHandling = Newtonsoft.Json.DateParseHandling.DateTimeOffset,
ContractResolver = DataContractForCollectionsResolver.Instance
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.