我对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();
}
}
}
然后我遇到了以下错误:
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.
我做错了什么?
DataContract
视为继承,尽管数据合同序列化程序并不是这样处理的。(http://json.codeplex.com/discussions/357850) - dbc