2 // Copyright (c) 2007 James Newton-King
4 // Permission is hereby granted, free of charge, to any person
5 // obtaining a copy of this software and associated documentation
6 // files (the "Software"), to deal in the Software without
7 // restriction, including without limitation the rights to use,
8 // copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the
10 // Software is furnished to do so, subject to the following
13 // The above copyright notice and this permission notice shall be
14 // included in all copies or substantial portions of the Software.
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18 // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 // OTHER DEALINGS IN THE SOFTWARE.
27 using System.Collections.Generic;
30 using Newtonsoft.Json.Converters;
31 using Newtonsoft.Json.Tests.TestObjects;
32 using NUnit.Framework;
33 using Newtonsoft.Json.Serialization;
35 using ErrorEventArgs=Newtonsoft.Json.Serialization.ErrorEventArgs;
37 namespace Newtonsoft.Json.Tests.Serialization
39 public class SerializationErrorHandlingTests : TestFixtureBase
42 public void ErrorDeserializingListHandled()
47 ""BirthDate"": ""\/Date(978048000000)\/"",
48 ""LastModified"": ""\/Date(978048000000)\/""
52 ""BirthDate"": ""\/Date(978048000000)\/"",
53 ""LastModified"": ""\/Date(978048000000)\/""
57 VersionKeyedCollection c = JsonConvert.DeserializeObject<VersionKeyedCollection>(json);
58 Assert.AreEqual(1, c.Count);
59 Assert.AreEqual(1, c.Messages.Count);
60 Assert.AreEqual("Error message for member 1 = An item with the same key has already been added.", c.Messages[0]);
64 public void DeserializingErrorInChildObject()
66 ListErrorObjectCollection c = JsonConvert.DeserializeObject<ListErrorObjectCollection>(@"[
68 ""Member"": ""Value1"",
72 ""Member"": ""Value2""
75 ""ThrowError"": ""Value"",
84 ""ThrowError"": ""Handle this!"",
85 ""Member"": ""Value3""
89 Assert.AreEqual(3, c.Count);
90 Assert.AreEqual("Value1", c[0].Member);
91 Assert.AreEqual("Value2", c[1].Member);
92 Assert.AreEqual("Value3", c[2].Member);
93 Assert.AreEqual("Handle this!", c[2].ThrowError);
97 public void SerializingErrorInChildObject()
99 ListErrorObjectCollection c = new ListErrorObjectCollection
104 ThrowError = "Handle this!",
115 ThrowError = "Handle that!",
120 string json = JsonConvert.SerializeObject(c, Formatting.Indented);
124 ""Member"": ""Value1"",
125 ""ThrowError"": ""Handle this!"",
126 ""Member2"": ""Member1""
129 ""Member"": ""Value2""
132 ""Member"": ""Value3"",
133 ""ThrowError"": ""Handle that!"",
134 ""Member2"": ""Member3""
140 public void DeserializingErrorInDateTimeCollection()
142 DateTimeErrorObjectCollection c = JsonConvert.DeserializeObject<DateTimeErrorObjectCollection>(@"[
143 ""2009-09-09T00:00:00Z"",
148 ""1977-02-20T00:00:00Z"",
150 ""2000-12-01T00:00:00Z""
151 ]", new IsoDateTimeConverter());
153 Assert.AreEqual(3, c.Count);
154 Assert.AreEqual(new DateTime(2009, 9, 9, 0, 0, 0, DateTimeKind.Utc), c[0]);
155 Assert.AreEqual(new DateTime(1977, 2, 20, 0, 0, 0, DateTimeKind.Utc), c[1]);
156 Assert.AreEqual(new DateTime(2000, 12, 1, 0, 0, 0, DateTimeKind.Utc), c[2]);
160 public void DeserializingErrorHandlingUsingEvent()
162 List<string> errors = new List<string>();
164 List<DateTime> c = JsonConvert.DeserializeObject<List<DateTime>>(@"[
165 ""2009-09-09T00:00:00Z"",
166 ""I am not a date and will error!"",
170 ""1977-02-20T00:00:00Z"",
172 ""2000-12-01T00:00:00Z""
174 new JsonSerializerSettings
176 Error = delegate(object sender, ErrorEventArgs args)
178 errors.Add(args.ErrorContext.Error.Message);
179 args.ErrorContext.Handled = true;
181 Converters = { new IsoDateTimeConverter() }
184 // 2009-09-09T00:00:00Z
185 // 1977-02-20T00:00:00Z
186 // 2000-12-01T00:00:00Z
188 // The string was not recognized as a valid DateTime. There is a unknown word starting at index 0.
189 // Unexpected token parsing date. Expected String, got StartArray.
190 // Cannot convert null value to System.DateTime.
192 Assert.AreEqual(3, c.Count);
193 Assert.AreEqual(new DateTime(2009, 9, 9, 0, 0, 0, DateTimeKind.Utc), c[0]);
194 Assert.AreEqual(new DateTime(1977, 2, 20, 0, 0, 0, DateTimeKind.Utc), c[1]);
195 Assert.AreEqual(new DateTime(2000, 12, 1, 0, 0, 0, DateTimeKind.Utc), c[2]);
197 Assert.AreEqual(3, errors.Count);
198 #if !(NET20 || NET35 || WINDOWS_PHONE)
199 Assert.AreEqual("The string was not recognized as a valid DateTime. There is an unknown word starting at index 0.", errors[0]);
201 Assert.AreEqual("The string was not recognized as a valid DateTime. There is a unknown word starting at index 0.", errors[0]);
203 Assert.AreEqual("Unexpected token parsing date. Expected String, got StartArray.", errors[1]);
204 Assert.AreEqual("Cannot convert null value to System.DateTime.", errors[2]);
208 public void DeserializingErrorInDateTimeCollectionWithAttributeWithEventNotCalled()
210 bool eventErrorHandlerCalled = false;
212 DateTimeErrorObjectCollection c = JsonConvert.DeserializeObject<DateTimeErrorObjectCollection>(@"[
213 ""2009-09-09T00:00:00Z"",
218 ""1977-02-20T00:00:00Z"",
220 ""2000-12-01T00:00:00Z""
222 new JsonSerializerSettings
224 Error = (s, a) => eventErrorHandlerCalled = true,
227 new IsoDateTimeConverter()
231 Assert.AreEqual(3, c.Count);
232 Assert.AreEqual(new DateTime(2009, 9, 9, 0, 0, 0, DateTimeKind.Utc), c[0]);
233 Assert.AreEqual(new DateTime(1977, 2, 20, 0, 0, 0, DateTimeKind.Utc), c[1]);
234 Assert.AreEqual(new DateTime(2000, 12, 1, 0, 0, 0, DateTimeKind.Utc), c[2]);
236 Assert.AreEqual(false, eventErrorHandlerCalled);
240 public void SerializePerson()
242 PersonError person = new PersonError
244 Name = "George Michael Bluth",
247 Title = "Mister Manager"
250 string json = JsonConvert.SerializeObject(person, Formatting.Indented);
252 Console.WriteLine(json);
254 // "Name": "George Michael Bluth",
256 // "Title": "Mister Manager"
260 ""Name"": ""George Michael Bluth"",
262 ""Title"": ""Mister Manager""
267 public void DeserializeNestedUnhandled()
269 List<string> errors = new List<string>();
271 string json = @"[[""kjhkjhkjhkjh""]]";
275 JsonSerializer serializer = new JsonSerializer();
276 serializer.Error += delegate(object sender, ErrorEventArgs args)
278 // only log an error once
279 if (args.CurrentObject == args.ErrorContext.OriginalObject)
280 errors.Add(args.ErrorContext.Error.Message);
283 serializer.Deserialize(new StringReader(json), typeof(List<List<DateTime>>));
287 Console.WriteLine(ex.Message);
290 Assert.AreEqual(1, errors.Count);
291 Assert.AreEqual(@"Error converting value ""kjhkjhkjhkjh"" to type 'System.DateTime'.", errors[0]);