-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:
@@ -332,26 +332,6 @@ keyword such as type of business.""
|
||||
Assert.AreEqual(nestedTorrentsArray.Children().Count(), 19);
|
||||
}
|
||||
|
||||
public class JsonPropertyClass
|
||||
{
|
||||
[JsonProperty("pie")]
|
||||
public string Pie = "Yum";
|
||||
|
||||
[JsonIgnore]
|
||||
public string pie = "No pie for you!";
|
||||
|
||||
public string pie1 = "PieChart!";
|
||||
|
||||
private int _sweetCakesCount;
|
||||
|
||||
[JsonProperty("sweet_cakes_count")]
|
||||
public int SweetCakesCount
|
||||
{
|
||||
get { return _sweetCakesCount; }
|
||||
set { _sweetCakesCount = value; }
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void JsonPropertyClassSerialize()
|
||||
{
|
||||
@@ -1079,72 +1059,6 @@ keyword such as type of business.""
|
||||
Assert.AreEqual(json, after);
|
||||
}
|
||||
|
||||
public class GoogleMapGeocoderStructure
|
||||
{
|
||||
public string Name;
|
||||
public Status Status;
|
||||
public List<Placemark> Placemark;
|
||||
}
|
||||
|
||||
public class Status
|
||||
{
|
||||
public string Request;
|
||||
public string Code;
|
||||
}
|
||||
|
||||
public class Placemark
|
||||
{
|
||||
public string Address;
|
||||
public AddressDetails AddressDetails;
|
||||
public Point Point;
|
||||
}
|
||||
|
||||
public class AddressDetails
|
||||
{
|
||||
public int Accuracy;
|
||||
public Country Country;
|
||||
}
|
||||
|
||||
public class Country
|
||||
{
|
||||
public string CountryNameCode;
|
||||
public AdministrativeArea AdministrativeArea;
|
||||
}
|
||||
|
||||
public class AdministrativeArea
|
||||
{
|
||||
public string AdministrativeAreaName;
|
||||
public SubAdministrativeArea SubAdministrativeArea;
|
||||
}
|
||||
|
||||
public class SubAdministrativeArea
|
||||
{
|
||||
public string SubAdministrativeAreaName;
|
||||
public Locality Locality;
|
||||
}
|
||||
|
||||
public class Locality
|
||||
{
|
||||
public string LocalityName;
|
||||
public Thoroughfare Thoroughfare;
|
||||
public PostalCode PostalCode;
|
||||
}
|
||||
|
||||
public class Thoroughfare
|
||||
{
|
||||
public string ThoroughfareName;
|
||||
}
|
||||
|
||||
public class PostalCode
|
||||
{
|
||||
public string PostalCodeNumber;
|
||||
}
|
||||
|
||||
public class Point
|
||||
{
|
||||
public List<decimal> Coordinates;
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeserializeGoogleGeoCode()
|
||||
{
|
||||
@@ -1191,38 +1105,14 @@ keyword such as type of business.""
|
||||
GoogleMapGeocoderStructure jsonGoogleMapGeocoder = JsonConvert.DeserializeObject<GoogleMapGeocoderStructure>(json);
|
||||
}
|
||||
|
||||
public class Co : ICo
|
||||
{
|
||||
public String Name { get; set; }
|
||||
}
|
||||
|
||||
public interface ICo
|
||||
{
|
||||
String Name { get; set; }
|
||||
}
|
||||
|
||||
public interface ITest
|
||||
{
|
||||
ICo co { get; set; }
|
||||
}
|
||||
|
||||
public class Test : ITest
|
||||
{
|
||||
public ICo co { get; set; }
|
||||
|
||||
public Test()
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(JsonSerializationException), ExpectedMessage = @"Could not create an instance of type Newtonsoft.Json.Tests.Serialization.JsonSerializerTest+ICo. Type is an interface or abstract class and cannot be instantated.")]
|
||||
[ExpectedException(typeof(JsonSerializationException), ExpectedMessage = @"Could not create an instance of type Newtonsoft.Json.Tests.TestObjects.ICo. Type is an interface or abstract class and cannot be instantated.")]
|
||||
public void DeserializeInterfaceProperty()
|
||||
{
|
||||
Test testClass = new Test();
|
||||
InterfacePropertyTestClass testClass = new InterfacePropertyTestClass();
|
||||
testClass.co = new Co();
|
||||
String strFromTest = JsonConvert.SerializeObject(testClass);
|
||||
Test testFromDe = (Test)JsonConvert.DeserializeObject(strFromTest, typeof(Test));
|
||||
InterfacePropertyTestClass testFromDe = (InterfacePropertyTestClass)JsonConvert.DeserializeObject(strFromTest, typeof(InterfacePropertyTestClass));
|
||||
}
|
||||
|
||||
private Person GetPerson()
|
||||
@@ -1259,12 +1149,6 @@ keyword such as type of business.""
|
||||
}
|
||||
}
|
||||
|
||||
public class LogEntry
|
||||
{
|
||||
public string Details { get; set; }
|
||||
public DateTime LogDate { get; set; }
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WriteJsonDates()
|
||||
{
|
||||
@@ -1334,14 +1218,6 @@ keyword such as type of business.""
|
||||
CollectionAssert.AreEqual(o.IDictionaryProperty.ToArray(), deserializedObject.IDictionaryProperty.ToArray());
|
||||
}
|
||||
|
||||
public class PropertyCase
|
||||
{
|
||||
public string firstName { get; set; }
|
||||
public string FirstName { get; set; }
|
||||
public string LastName { get; set; }
|
||||
public string lastName { get; set; }
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeserializeBestMatchPropertyCase()
|
||||
{
|
||||
@@ -1361,18 +1237,6 @@ keyword such as type of business.""
|
||||
Assert.AreEqual("lastName", o.lastName);
|
||||
}
|
||||
|
||||
public class SuperKlass
|
||||
{
|
||||
public string SuperProp { get; set; }
|
||||
public SuperKlass() { SuperProp = "default superprop"; }
|
||||
}
|
||||
|
||||
public class SubKlass : SuperKlass
|
||||
{
|
||||
public string SubProp { get; set; }
|
||||
public SubKlass(string subprop) { SubProp = subprop; }
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeserializePropertiesOnToNonDefaultConstructor()
|
||||
{
|
||||
@@ -1423,18 +1287,6 @@ keyword such as type of business.""
|
||||
JsonConvert.SerializeObject(o, Formatting.Indented, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore });
|
||||
}
|
||||
|
||||
public class RequestOnly
|
||||
{
|
||||
public string Request { get; set; }
|
||||
}
|
||||
|
||||
public class NonRequest
|
||||
{
|
||||
public Guid Sid { get; set; }
|
||||
public Guid Uid { get; set; }
|
||||
public IList<string> FidOrder { get; set; }
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void PartialClassDeserialize()
|
||||
{
|
||||
@@ -1565,33 +1417,6 @@ keyword such as type of business.""
|
||||
}
|
||||
#endif
|
||||
|
||||
public class DictionaryInterfaceClass
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public IDictionary<string, int> Dictionary { get; set; }
|
||||
public ICollection<int> Collection { get; set; }
|
||||
public EmployeeReference Employee { get; set; }
|
||||
public object Random { get; set; }
|
||||
|
||||
public DictionaryInterfaceClass()
|
||||
{
|
||||
Dictionary = new Dictionary<string, int>
|
||||
{
|
||||
{ "existing", 1 }
|
||||
};
|
||||
Collection = new List<int>
|
||||
{
|
||||
1,
|
||||
2,
|
||||
3
|
||||
};
|
||||
Employee = new EmployeeReference
|
||||
{
|
||||
Name = "EmployeeName!"
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeserializeDictionaryInterface()
|
||||
{
|
||||
@@ -1720,25 +1545,6 @@ keyword such as type of business.""
|
||||
Assert.AreEqual(@"{""bar"":{""baz"":13}}", json);
|
||||
}
|
||||
|
||||
[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
|
||||
public class Content : IEnumerable<Content>
|
||||
{
|
||||
[JsonProperty]
|
||||
public List<Content> Children;
|
||||
[JsonProperty]
|
||||
public string Text;
|
||||
|
||||
public IEnumerator GetEnumerator()
|
||||
{
|
||||
return Children.GetEnumerator();
|
||||
}
|
||||
|
||||
IEnumerator<Content> IEnumerable<Content>.GetEnumerator()
|
||||
{
|
||||
return Children.GetEnumerator();
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SerializeEnumerableAsObject()
|
||||
{
|
||||
@@ -1794,25 +1600,6 @@ keyword such as type of business.""
|
||||
Assert.AreEqual("Second", content.Children[1].Text);
|
||||
}
|
||||
|
||||
public enum RoleTransferOperation
|
||||
{
|
||||
First,
|
||||
Second
|
||||
}
|
||||
|
||||
public enum RoleTransferDirection
|
||||
{
|
||||
First,
|
||||
Second
|
||||
}
|
||||
|
||||
public class RoleTransfer
|
||||
{
|
||||
public RoleTransferOperation Operation { get; set; } //This is enum type
|
||||
public string RoleName { get; set; }
|
||||
public RoleTransferDirection Direction { get; set; } //This is enum type
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void RoleTransferTest()
|
||||
{
|
||||
@@ -1825,13 +1612,6 @@ keyword such as type of business.""
|
||||
Assert.AreEqual(RoleTransferDirection.First, r.Direction);
|
||||
}
|
||||
|
||||
public class ObjectArrayPropertyTest
|
||||
{
|
||||
public string Action { get; set; }
|
||||
public string Method { get; set; }
|
||||
public object[] Data { get; set; }
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void PrimitiveValuesInObjectArray()
|
||||
{
|
||||
@@ -1975,127 +1755,8 @@ keyword such as type of business.""
|
||||
Assert.AreEqual(null, c.DateTimeField);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// What types of events are there? Just sticking to a basic set of four for now.
|
||||
/// </summary>
|
||||
/// <remarks></remarks>
|
||||
public enum EventType
|
||||
{
|
||||
Debug = 0,
|
||||
Info = 1,
|
||||
Warning = 2,
|
||||
Error = 3
|
||||
}
|
||||
|
||||
public sealed class Event
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// If no current user is specified, returns Nothing (0 from VB)
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
/// <remarks></remarks>
|
||||
private static int GetCurrentUserId()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets either the application path or the current stack trace.
|
||||
/// NOTE: You MUST call this from the top level entry point. Otherwise,
|
||||
/// the stack trace will be buried in Logger itself.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
/// <remarks></remarks>
|
||||
private static string GetCurrentSubLocation()
|
||||
{
|
||||
return "";
|
||||
}
|
||||
|
||||
private string _sublocation;
|
||||
private int _userId;
|
||||
private EventType _type;
|
||||
private string _summary;
|
||||
private string _details;
|
||||
private string _stackTrace;
|
||||
private string _tag;
|
||||
private DateTime _time;
|
||||
|
||||
public Event(string summary)
|
||||
{
|
||||
_summary = summary;
|
||||
_time = DateTime.Now;
|
||||
|
||||
if (_userId == 0) _userId = GetCurrentUserId();
|
||||
//This call only works at top level for now.
|
||||
//If _stackTrace = Nothing Then _stackTrace = Environment.StackTrace
|
||||
if (_sublocation == null) _sublocation = GetCurrentSubLocation();
|
||||
}
|
||||
|
||||
public Event(string sublocation, int userId, EventType type, string summary, string details, string stackTrace, string tag)
|
||||
{
|
||||
_sublocation = sublocation;
|
||||
_userId = userId;
|
||||
_type = type;
|
||||
_summary = summary;
|
||||
_details = details;
|
||||
_stackTrace = stackTrace;
|
||||
_tag = tag;
|
||||
_time = DateTime.Now;
|
||||
|
||||
if (_userId == 0) _userId = GetCurrentUserId();
|
||||
//If _stackTrace = Nothing Then _stackTrace = Environment.StackTrace
|
||||
if (_sublocation == null) _sublocation = GetCurrentSubLocation();
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return string.Format("{{ sublocation = {0}, userId = {1}, type = {2}, summary = {3}, details = {4}, stackTrace = {5}, tag = {6} }}", _sublocation, _userId, _type, _summary, _details, _stackTrace, _tag);
|
||||
}
|
||||
|
||||
public string sublocation
|
||||
{
|
||||
get { return _sublocation; }
|
||||
set { _sublocation = value; }
|
||||
}
|
||||
public int userId
|
||||
{
|
||||
get { return _userId; }
|
||||
set { _userId = value; }
|
||||
}
|
||||
public EventType type
|
||||
{
|
||||
get { return _type; }
|
||||
set { _type = value; }
|
||||
}
|
||||
public string summary
|
||||
{
|
||||
get { return _summary; }
|
||||
set { _summary = value; }
|
||||
}
|
||||
public string details
|
||||
{
|
||||
get { return _details; }
|
||||
set { _details = value; }
|
||||
}
|
||||
public string stackTrace
|
||||
{
|
||||
get { return _stackTrace; }
|
||||
set { _stackTrace = value; }
|
||||
}
|
||||
public string tag
|
||||
{
|
||||
get { return _tag; }
|
||||
set { _tag = value; }
|
||||
}
|
||||
public DateTime time
|
||||
{
|
||||
get { return _time; }
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(JsonSerializationException), ExpectedMessage = @"Unable to determine which constructor to use for type Newtonsoft.Json.Tests.Serialization.JsonSerializerTest+Event. A class with no default constructor should have only one constructor with arguments.")]
|
||||
[ExpectedException(typeof(JsonSerializationException), ExpectedMessage = @"Unable to determine which constructor to use for type Newtonsoft.Json.Tests.TestObjects.Event. A class with no default constructor should have only one constructor with arguments.")]
|
||||
public void FailWhenClassWithNoDefaultConstructorHasMultipleConstructorsWithArguments()
|
||||
{
|
||||
string json = @"{""sublocation"":""AlertEmailSender.Program.Main"",""userId"":0,""type"":0,""summary"":""Loading settings variables"",""details"":null,""stackTrace"":"" at System.Environment.GetStackTrace(Exception e, Boolean needFileInfo)\r\n at System.Environment.get_StackTrace()\r\n at mr.Logging.Event..ctor(String summary) in C:\\Projects\\MRUtils\\Logging\\Event.vb:line 71\r\n at AlertEmailSender.Program.Main(String[] args) in C:\\Projects\\AlertEmailSender\\AlertEmailSender\\Program.cs:line 25"",""tag"":null,""time"":""\/Date(1249591032026-0400)\/""}";
|
||||
@@ -2103,19 +1764,6 @@ keyword such as type of business.""
|
||||
Event e = JsonConvert.DeserializeObject<Event>(json);
|
||||
}
|
||||
|
||||
public class SetOnlyPropertyClass2
|
||||
{
|
||||
private object _value;
|
||||
public object SetOnlyProperty
|
||||
{
|
||||
set { _value = value; }
|
||||
}
|
||||
public object GetValue()
|
||||
{
|
||||
return _value;
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeserializeObjectSetOnlyProperty()
|
||||
{
|
||||
@@ -2128,29 +1776,12 @@ keyword such as type of business.""
|
||||
Assert.AreEqual(5, (int)a[a.Count - 1]);
|
||||
}
|
||||
|
||||
[JsonObject(MemberSerialization.OptIn)]
|
||||
public class list
|
||||
{
|
||||
[JsonProperty]
|
||||
public string id { get; set; }
|
||||
[JsonProperty]
|
||||
public List<ListItem> items { get; set; }
|
||||
|
||||
}
|
||||
|
||||
[JsonObject(MemberSerialization.OptIn)]
|
||||
public class ListItem
|
||||
{
|
||||
[JsonProperty]
|
||||
public string id { get; set; }
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeserializeOptInClasses()
|
||||
{
|
||||
string json = @"{id: ""12"", name: ""test"", items: [{id: ""112"", name: ""testing""}]}";
|
||||
|
||||
list l = JsonConvert.DeserializeObject<list>(json);
|
||||
ListTestClass l = JsonConvert.DeserializeObject<ListTestClass>(json);
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -2163,5 +1794,22 @@ keyword such as type of business.""
|
||||
Assert.AreEqual(null, l[1]);
|
||||
Assert.AreEqual(1.1m, l[2]);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeserializeNullableListWithNullss()
|
||||
{
|
||||
//JavaScriptSerializer s = new JavaScriptSerializer();
|
||||
//s.Deserialize<List<decimal?>>("[ 3.3, null, 1.1 ] ");
|
||||
|
||||
//DataContractJsonSerializer s = new DataContractJsonSerializer(typeof(List<decimal?>));
|
||||
//s.ReadObject(new MemoryStream(Encoding.UTF8.GetBytes("[ 3.3, null, 1.1 ] ")));
|
||||
|
||||
List<decimal?> l = JsonConvert.DeserializeObject<List<decimal?>>("[ 3.3, null, 1.1 ] ");
|
||||
Assert.AreEqual(3, l.Count);
|
||||
|
||||
Assert.AreEqual(3.3m, l[0]);
|
||||
Assert.AreEqual(null, l[1]);
|
||||
Assert.AreEqual(1.1m, l[2]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,205 @@
|
||||
#region License
|
||||
// Copyright (c) 2007 James Newton-King
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person
|
||||
// obtaining a copy of this software and associated documentation
|
||||
// files (the "Software"), to deal in the Software without
|
||||
// restriction, including without limitation the rights to use,
|
||||
// copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the
|
||||
// Software is furnished to do so, subject to the following
|
||||
// conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
||||
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||||
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
// OTHER DEALINGS IN THE SOFTWARE.
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Newtonsoft.Json.Converters;
|
||||
using Newtonsoft.Json.Tests.TestObjects;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace Newtonsoft.Json.Tests.Serialization
|
||||
{
|
||||
public class SerializationErrorHandlingTests : TestFixtureBase
|
||||
{
|
||||
[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]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user