-XML documentation

-Changed OnError to be available in .NET 2.0, Silverlight and Compact Framework builds
-Added StreamingContext stand-in for frameworks where it isn't available
-Split test classes out of JsonSerializerTests
This commit is contained in:
JamesNK
2009-10-10 00:54:01 +00:00
parent e8dc0bb764
commit 75627d6292
48 changed files with 1492 additions and 672 deletions
@@ -25,19 +25,14 @@
#if !PocketPC && !SILVERLIGHT && !NET20
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text;
using Newtonsoft.Json.Serialization;
using Newtonsoft.Json.Tests;
using Newtonsoft.Json.Tests.TestObjects;
using NUnit.Framework;
using Newtonsoft.Json.Linq;
using System.Collections;
using Newtonsoft.Json.Converters;
namespace Newtonsoft.Json.Tests.Serialization
@@ -114,254 +109,6 @@ namespace Newtonsoft.Json.Tests.Serialization
Assert.AreEqual("This value was set after deserialization.", obj.Member4);
}
public class ListErrorObject
{
public string Member { get; set; }
private string _throwError;
public string ThrowError
{
get
{
if (_throwError != null)
return _throwError;
throw new Exception("ListErrorObject.ThrowError get error!");
}
set
{
if (value != null && value.StartsWith("Handle"))
{
_throwError = value;
return;
}
throw new Exception("ListErrorObject.ThrowError set error!");
}
}
public string Member2 { get; set; }
}
public class ListErrorObjectCollection : Collection<ListErrorObject>
{
[OnError]
internal void OnErrorMethod(StreamingContext context, ErrorContext errorContext)
{
errorContext.Handled = true;
}
}
public class DateTimeErrorObjectCollection : Collection<DateTime>
{
[OnError]
internal void OnErrorMethod(StreamingContext context, ErrorContext errorContext)
{
errorContext.Handled = true;
}
}
public class VersionKeyedCollection : KeyedCollection<string, Person>, IList
{
public List<string> Messages { get; set; }
public VersionKeyedCollection()
{
Messages = new List<string>();
}
protected override string GetKeyForItem(Person item)
{
return item.Name;
}
[OnError]
internal void OnErrorMethod(StreamingContext context, ErrorContext errorContext)
{
Messages.Add("Error message for member " + errorContext.Member + " = " + errorContext.Error.Message);
errorContext.Handled = true;
}
object IList.this[int index]
{
get
{
if (index % 2 == 0)
throw new Exception("Index even: " + index);
return this[index];
}
set { this[index] = (Person)value; }
}
}
[Test]
public void ErrorDeserializingListHandled()
{
string json = @"[
{
""Name"": ""Jim"",
""BirthDate"": ""\/Date(978048000000)\/"",
""LastModified"": ""\/Date(978048000000)\/""
},
{
""Name"": ""Jim"",
""BirthDate"": ""\/Date(978048000000)\/"",
""LastModified"": ""\/Date(978048000000)\/""
}
]";
VersionKeyedCollection c = JsonConvert.DeserializeObject<VersionKeyedCollection>(json);
Assert.AreEqual(1, c.Count);
Assert.AreEqual(1, c.Messages.Count);
Assert.AreEqual("Error message for member 1 = An item with the same key has already been added.", c.Messages[0]);
}
[Test]
public void ErrorSerializingListHandled()
{
VersionKeyedCollection c = new VersionKeyedCollection();
c.Add(new Person
{
Name = "Jim",
BirthDate = new DateTime(2000, 12, 29, 0, 0, 0, DateTimeKind.Utc),
LastModified = new DateTime(2000, 12, 29, 0, 0, 0, DateTimeKind.Utc)
});
c.Add(new Person
{
Name = "Jimbo",
BirthDate = new DateTime(2000, 12, 29, 0, 0, 0, DateTimeKind.Utc),
LastModified = new DateTime(2000, 12, 29, 0, 0, 0, DateTimeKind.Utc)
});
c.Add(new Person
{
Name = "Jimmy",
BirthDate = new DateTime(2000, 12, 29, 0, 0, 0, DateTimeKind.Utc),
LastModified = new DateTime(2000, 12, 29, 0, 0, 0, DateTimeKind.Utc)
});
c.Add(new Person
{
Name = "Jim Bean",
BirthDate = new DateTime(2000, 12, 29, 0, 0, 0, DateTimeKind.Utc),
LastModified = new DateTime(2000, 12, 29, 0, 0, 0, DateTimeKind.Utc)
});
string json = JsonConvert.SerializeObject(c, Formatting.Indented);
Assert.AreEqual(@"[
{
""Name"": ""Jimbo"",
""BirthDate"": ""\/Date(978048000000)\/"",
""LastModified"": ""\/Date(978048000000)\/""
},
{
""Name"": ""Jim Bean"",
""BirthDate"": ""\/Date(978048000000)\/"",
""LastModified"": ""\/Date(978048000000)\/""
}
]", json);
Assert.AreEqual(2, c.Messages.Count);
Assert.AreEqual("Error message for member 0 = Index even: 0", c.Messages[0]);
Assert.AreEqual("Error message for member 2 = Index even: 2", c.Messages[1]);
}
[Test]
public void DeserializingErrorInChildObject()
{
ListErrorObjectCollection c = JsonConvert.DeserializeObject<ListErrorObjectCollection>(@"[
{
""Member"": ""Value1"",
""Member2"": null
},
{
""Member"": ""Value2""
},
{
""ThrowError"": ""Value"",
""Object"": {
""Array"": [
1,
2
]
}
},
{
""ThrowError"": ""Handle this!"",
""Member"": ""Value3""
}
]");
Assert.AreEqual(3, c.Count);
Assert.AreEqual("Value1", c[0].Member);
Assert.AreEqual("Value2", c[1].Member);
Assert.AreEqual("Value3", c[2].Member);
Assert.AreEqual("Handle this!", c[2].ThrowError);
}
[Test]
public void SerializingErrorInChildObject()
{
ListErrorObjectCollection c = new ListErrorObjectCollection
{
new ListErrorObject
{
Member = "Value1",
ThrowError = "Handle this!",
Member2 = "Member1"
},
new ListErrorObject
{
Member = "Value2",
Member2 = "Member2"
},
new ListErrorObject
{
Member = "Value3",
ThrowError = "Handle that!",
Member2 = "Member3"
}
};
string json = JsonConvert.SerializeObject(c, Formatting.Indented);
Assert.AreEqual(@"[
{
""Member"": ""Value1"",
""ThrowError"": ""Handle this!"",
""Member2"": ""Member1""
},
{
""Member"": ""Value2""
},
{
""Member"": ""Value3"",
""ThrowError"": ""Handle that!"",
""Member2"": ""Member3""
}
]", json);
}
[Test]
public void DeserializingErrorInDateTimeCollection()
{
DateTimeErrorObjectCollection c = JsonConvert.DeserializeObject<DateTimeErrorObjectCollection>(@"[
""2009-09-09T00:00:00Z"",
""kjhkjhkjhkjh"",
[
1
],
""1977-02-20T00:00:00Z"",
null,
""2000-12-01T00:00:00Z""
]", new IsoDateTimeConverter());
Assert.AreEqual(3, c.Count);
Assert.AreEqual(new DateTime(2009, 9, 9, 0, 0, 0, DateTimeKind.Utc), c[0]);
Assert.AreEqual(new DateTime(1977, 2, 20, 0, 0, 0, DateTimeKind.Utc), c[1]);
Assert.AreEqual(new DateTime(2000, 12, 1, 0, 0, 0, DateTimeKind.Utc), c[2]);
}
[Test]
public void ListEvents()
{