Code Coverage Statistics for Source File
Newtonsoft.Json.Tests\Serialization\SerializationErrorHandlingTests.cs
Symbol Coverage: 98.63% (72 of 73)
Branch Coverage: 100.00% (11 of 11)
Cyclomatic Complexity Avg: 1.15 Max:2
Code Lines: 223
Symbol Coverage Trend
View:
L | V | Source |
---|---|---|
1 |
#region License |
|
2 |
// Copyright (c) 2007 James Newton-King |
|
3 |
// |
|
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 |
|
11 |
// conditions: |
|
12 |
// |
|
13 |
// The above copyright notice and this permission notice shall be |
|
14 |
// included in all copies or substantial portions of the Software. |
|
15 |
// |
|
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. |
|
24 |
#endregion |
|
25 |
||
26 |
using System; |
|
27 |
using System.Collections.Generic; |
|
28 |
using System.Linq; |
|
29 |
using System.Text; |
|
30 |
using Newtonsoft.Json.Converters; |
|
31 |
using Newtonsoft.Json.Tests.TestObjects; |
|
32 |
using NUnit.Framework; |
|
33 |
using Newtonsoft.Json.Serialization; |
|
34 |
using System.IO; |
|
35 |
using ErrorEventArgs=Newtonsoft.Json.Serialization.ErrorEventArgs; |
|
36 |
||
37 |
namespace Newtonsoft.Json.Tests.Serialization |
|
38 |
{ |
|
39 |
public class SerializationErrorHandlingTests : TestFixtureBase |
|
40 |
{ |
|
41 |
[Test] |
|
42 |
public void ErrorDeserializingListHandled() |
|
43 |
{ |
|
44 |
1 |
string json = @"[ |
45 |
1 |
{
|
46 |
1 |
""Name"": ""Jim"",
|
47 |
1 |
""BirthDate"": ""\/Date(978048000000)\/"",
|
48 |
1 |
""LastModified"": ""\/Date(978048000000)\/""
|
49 |
1 |
},
|
50 |
1 |
{
|
51 |
1 |
""Name"": ""Jim"",
|
52 |
1 |
""BirthDate"": ""\/Date(978048000000)\/"",
|
53 |
1 |
""LastModified"": ""\/Date(978048000000)\/""
|
54 |
1 |
}
|
55 |
1 |
]";
|
56 |
||
57 |
1 |
VersionKeyedCollection c = JsonConvert.DeserializeObject<VersionKeyedCollection>(json);
|
58 |
1 |
Assert.AreEqual(1, c.Count);
|
59 |
1 |
Assert.AreEqual(1, c.Messages.Count);
|
60 |
1 |
Assert.AreEqual("Error message for member 1 = An item with the same key has already been added.", c.Messages[0]);
|
61 |
1 |
}
|
62 |
||
63 |
[Test] |
|
64 |
public void ErrorSerializingListHandled() |
|
65 |
{ |
|
66 |
1 |
VersionKeyedCollection c = new VersionKeyedCollection();
|
67 |
1 |
c.Add(new Person
|
68 |
1 |
{
|
69 |
1 |
Name = "Jim",
|
70 |
1 |
BirthDate = new DateTime(2000, 12, 29, 0, 0, 0, DateTimeKind.Utc),
|
71 |
1 |
LastModified = new DateTime(2000, 12, 29, 0, 0, 0, DateTimeKind.Utc)
|
72 |
1 |
});
|
73 |
1 |
c.Add(new Person
|
74 |
1 |
{
|
75 |
1 |
Name = "Jimbo",
|
76 |
1 |
BirthDate = new DateTime(2000, 12, 29, 0, 0, 0, DateTimeKind.Utc),
|
77 |
1 |
LastModified = new DateTime(2000, 12, 29, 0, 0, 0, DateTimeKind.Utc)
|
78 |
1 |
});
|
79 |
1 |
c.Add(new Person
|
80 |
1 |
{
|
81 |
1 |
Name = "Jimmy",
|
82 |
1 |
BirthDate = new DateTime(2000, 12, 29, 0, 0, 0, DateTimeKind.Utc),
|
83 |
1 |
LastModified = new DateTime(2000, 12, 29, 0, 0, 0, DateTimeKind.Utc)
|
84 |
1 |
});
|
85 |
1 |
c.Add(new Person
|
86 |
1 |
{
|
87 |
1 |
Name = "Jim Bean",
|
88 |
1 |
BirthDate = new DateTime(2000, 12, 29, 0, 0, 0, DateTimeKind.Utc),
|
89 |
1 |
LastModified = new DateTime(2000, 12, 29, 0, 0, 0, DateTimeKind.Utc)
|
90 |
1 |
});
|
91 |
||
92 |
1 |
string json = JsonConvert.SerializeObject(c, Formatting.Indented);
|
93 |
||
94 |
1 |
Assert.AreEqual(@"[
|
95 |
1 |
{
|
96 |
1 |
""Name"": ""Jimbo"",
|
97 |
1 |
""BirthDate"": ""\/Date(978048000000)\/"",
|
98 |
1 |
""LastModified"": ""\/Date(978048000000)\/""
|
99 |
1 |
},
|
100 |
1 |
{
|
101 |
1 |
""Name"": ""Jim Bean"",
|
102 |
1 |
""BirthDate"": ""\/Date(978048000000)\/"",
|
103 |
1 |
""LastModified"": ""\/Date(978048000000)\/""
|
104 |
1 |
}
|
105 |
1 |
]", json);
|
106 |
||
107 |
1 |
Assert.AreEqual(2, c.Messages.Count);
|
108 |
1 |
Assert.AreEqual("Error message for member 0 = Index even: 0", c.Messages[0]);
|
109 |
1 |
Assert.AreEqual("Error message for member 2 = Index even: 2", c.Messages[1]);
|
110 |
1 |
}
|
111 |
||
112 |
[Test] |
|
113 |
public void DeserializingErrorInChildObject() |
|
114 |
{ |
|
115 |
1 |
ListErrorObjectCollection c = JsonConvert.DeserializeObject<ListErrorObjectCollection>(@"[ |
116 |
1 |
{
|
117 |
1 |
""Member"": ""Value1"",
|
118 |
1 |
""Member2"": null
|
119 |
1 |
},
|
120 |
1 |
{
|
121 |
1 |
""Member"": ""Value2""
|
122 |
1 |
},
|
123 |
1 |
{
|
124 |
1 |
""ThrowError"": ""Value"",
|
125 |
1 |
""Object"": {
|
126 |
1 |
""Array"": [
|
127 |
1 |
1,
|
128 |
1 |
2
|
129 |
1 |
]
|
130 |
1 |
}
|
131 |
1 |
},
|
132 |
1 |
{
|
133 |
1 |
""ThrowError"": ""Handle this!"",
|
134 |
1 |
""Member"": ""Value3""
|
135 |
1 |
}
|
136 |
1 |
]");
|
137 |
||
138 |
1 |
Assert.AreEqual(3, c.Count);
|
139 |
1 |
Assert.AreEqual("Value1", c[0].Member);
|
140 |
1 |
Assert.AreEqual("Value2", c[1].Member);
|
141 |
1 |
Assert.AreEqual("Value3", c[2].Member);
|
142 |
1 |
Assert.AreEqual("Handle this!", c[2].ThrowError);
|
143 |
1 |
}
|
144 |
||
145 |
[Test] |
|
146 |
public void SerializingErrorInChildObject() |
|
147 |
{ |
|
148 |
1 |
ListErrorObjectCollection c = new ListErrorObjectCollection
|
149 |
1 |
{
|
150 |
1 |
new ListErrorObject
|
151 |
1 |
{
|
152 |
1 |
Member = "Value1",
|
153 |
1 |
ThrowError = "Handle this!",
|
154 |
1 |
Member2 = "Member1"
|
155 |
1 |
},
|
156 |
1 |
new ListErrorObject
|
157 |
1 |
{
|
158 |
1 |
Member = "Value2",
|
159 |
1 |
Member2 = "Member2"
|
160 |
1 |
},
|
161 |
1 |
new ListErrorObject
|
162 |
1 |
{
|
163 |
1 |
Member = "Value3",
|
164 |
1 |
ThrowError = "Handle that!",
|
165 |
1 |
Member2 = "Member3"
|
166 |
1 |
}
|
167 |
1 |
};
|
168 |
||
169 |
1 |
string json = JsonConvert.SerializeObject(c, Formatting.Indented);
|
170 |
||
171 |
1 |
Assert.AreEqual(@"[
|
172 |
1 |
{
|
173 |
1 |
""Member"": ""Value1"",
|
174 |
1 |
""ThrowError"": ""Handle this!"",
|
175 |
1 |
""Member2"": ""Member1""
|
176 |
1 |
},
|
177 |
1 |
{
|
178 |
1 |
""Member"": ""Value2""
|
179 |
1 |
},
|
180 |
1 |
{
|
181 |
1 |
""Member"": ""Value3"",
|
182 |
1 |
""ThrowError"": ""Handle that!"",
|
183 |
1 |
""Member2"": ""Member3""
|
184 |
1 |
}
|
185 |
1 |
]", json);
|
186 |
1 |
}
|
187 |
||
188 |
[Test] |
|
189 |
public void DeserializingErrorInDateTimeCollection() |
|
190 |
{ |
|
191 |
1 |
DateTimeErrorObjectCollection c = JsonConvert.DeserializeObject<DateTimeErrorObjectCollection>(@"[
|
192 |
1 |
""2009-09-09T00:00:00Z"",
|
193 |
1 |
""kjhkjhkjhkjh"",
|
194 |
1 |
[
|
195 |
1 |
1
|
196 |
1 |
],
|
197 |
1 |
""1977-02-20T00:00:00Z"",
|
198 |
1 |
null,
|
199 |
1 |
""2000-12-01T00:00:00Z""
|
200 |
1 |
]", new IsoDateTimeConverter());
|
201 |
||
202 |
1 |
Assert.AreEqual(3, c.Count);
|
203 |
1 |
Assert.AreEqual(new DateTime(2009, 9, 9, 0, 0, 0, DateTimeKind.Utc), c[0]);
|
204 |
1 |
Assert.AreEqual(new DateTime(1977, 2, 20, 0, 0, 0, DateTimeKind.Utc), c[1]);
|
205 |
1 |
Assert.AreEqual(new DateTime(2000, 12, 1, 0, 0, 0, DateTimeKind.Utc), c[2]);
|
206 |
1 |
}
|
207 |
||
208 |
[Test] |
|
209 |
public void DeserializingErrorHandlingUsingEvent() |
|
210 |
{ |
|
211 |
1 |
List<string> errors = new List<string>();
|
212 |
||
213 |
1 |
List<DateTime> c = JsonConvert.DeserializeObject<List<DateTime>>(@"[
|
214 |
1 |
""2009-09-09T00:00:00Z"",
|
215 |
1 |
""I am not a date and will error!"",
|
216 |
1 |
[
|
217 |
1 |
1
|
218 |
1 |
],
|
219 |
1 |
""1977-02-20T00:00:00Z"",
|
220 |
1 |
null,
|
221 |
1 |
""2000-12-01T00:00:00Z""
|
222 |
1 |
]",
|
223 |
1 |
new JsonSerializerSettings
|
224 |
1 |
{
|
225 |
1 |
Error = delegate(object sender, ErrorEventArgs args)
|
226 |
1 |
{
|
227 |
1 |
errors.Add(args.ErrorContext.Error.Message);
|
228 |
1 |
args.ErrorContext.Handled = true;
|
229 |
1 |
},
|
230 |
1 |
Converters = { new IsoDateTimeConverter() }
|
231 |
1 |
});
|
232 |
||
233 |
// 2009-09-09T00:00:00Z |
|
234 |
// 1977-02-20T00:00:00Z |
|
235 |
// 2000-12-01T00:00:00Z |
|
236 |
||
237 |
// The string was not recognized as a valid DateTime. There is a unknown word starting at index 0. |
|
238 |
// Unexpected token parsing date. Expected String, got StartArray. |
|
239 |
// Cannot convert null value to System.DateTime. |
|
240 |
||
241 |
1 |
Assert.AreEqual(3, c.Count);
|
242 |
1 |
Assert.AreEqual(new DateTime(2009, 9, 9, 0, 0, 0, DateTimeKind.Utc), c[0]);
|
243 |
1 |
Assert.AreEqual(new DateTime(1977, 2, 20, 0, 0, 0, DateTimeKind.Utc), c[1]);
|
244 |
1 |
Assert.AreEqual(new DateTime(2000, 12, 1, 0, 0, 0, DateTimeKind.Utc), c[2]);
|
245 |
||
246 |
1 |
Assert.AreEqual(3, errors.Count);
|
247 |
1 |
Assert.AreEqual("The string was not recognized as a valid DateTime. There is a unknown word starting at index 0.", errors[0]);
|
248 |
1 |
Assert.AreEqual("Unexpected token parsing date. Expected String, got StartArray.", errors[1]);
|
249 |
1 |
Assert.AreEqual("Cannot convert null value to System.DateTime.", errors[2]);
|
250 |
1 |
}
|
251 |
||
252 |
[Test] |
|
253 |
public void DeserializingErrorInDateTimeCollectionWithAttributeWithEventNotCalled() |
|
254 |
{ |
|
255 |
1 |
bool eventErrorHandlerCalled = false;
|
256 |
||
257 |
1 |
DateTimeErrorObjectCollection c = JsonConvert.DeserializeObject<DateTimeErrorObjectCollection>(@"[
|
258 |
1 |
""2009-09-09T00:00:00Z"",
|
259 |
1 |
""kjhkjhkjhkjh"",
|
260 |
1 |
[
|
261 |
1 |
1
|
262 |
1 |
],
|
263 |
1 |
""1977-02-20T00:00:00Z"",
|
264 |
1 |
null,
|
265 |
1 |
""2000-12-01T00:00:00Z""
|
266 |
1 |
]",
|
267 |
1 |
new JsonSerializerSettings
|
268 |
1 |
{
|
269 |
0 |
Error = (s, a) => eventErrorHandlerCalled = true,
|
270 |
1 |
Converters =
|
271 |
1 |
{
|
272 |
1 |
new IsoDateTimeConverter()
|
273 |
1 |
}
|
274 |
1 |
});
|
275 |
||
276 |
1 |
Assert.AreEqual(3, c.Count);
|
277 |
1 |
Assert.AreEqual(new DateTime(2009, 9, 9, 0, 0, 0, DateTimeKind.Utc), c[0]);
|
278 |
1 |
Assert.AreEqual(new DateTime(1977, 2, 20, 0, 0, 0, DateTimeKind.Utc), c[1]);
|
279 |
1 |
Assert.AreEqual(new DateTime(2000, 12, 1, 0, 0, 0, DateTimeKind.Utc), c[2]);
|
280 |
||
281 |
1 |
Assert.AreEqual(false, eventErrorHandlerCalled);
|
282 |
1 |
}
|
283 |
||
284 |
[Test] |
|
285 |
public void SerializePerson() |
|
286 |
{ |
|
287 |
1 |
PersonError person = new PersonError
|
288 |
1 |
{
|
289 |
1 |
Name = "George Michael Bluth",
|
290 |
1 |
Age = 16,
|
291 |
1 |
Roles = null,
|
292 |
1 |
Title = "Mister Manager"
|
293 |
1 |
};
|
294 |
||
295 |
1 |
string json = JsonConvert.SerializeObject(person, Formatting.Indented);
|
296 |
||
297 |
1 |
Console.WriteLine(json);
|
298 |
//{ |
|
299 |
// "Name": "George Michael Bluth", |
|
300 |
// "Age": 16, |
|
301 |
// "Title": "Mister Manager" |
|
302 |
//} |
|
303 |
||
304 |
1 |
Assert.AreEqual(@"{
|
305 |
1 |
""Name"": ""George Michael Bluth"",
|
306 |
1 |
""Age"": 16,
|
307 |
1 |
""Title"": ""Mister Manager""
|
308 |
1 |
}", json);
|
309 |
1 |
}
|
310 |
||
311 |
[Test] |
|
312 |
public void DeserializeNestedUnhandled() |
|
313 |
{ |
|
314 |
1 |
List<string> errors = new List<string>();
|
315 |
||
316 |
1 |
string json = @"[[""kjhkjhkjhkjh""]]";
|
317 |
||
318 |
try |
|
319 |
{ |
|
320 |
1 |
JsonSerializer serializer = new JsonSerializer();
|
321 |
1 |
serializer.Error += delegate(object sender, ErrorEventArgs args)
|
322 |
1 |
{
|
323 |
1 |
// only log an error once
|
324 |
1 |
if (args.CurrentObject == args.ErrorContext.OriginalObject)
|
325 |
1 |
errors.Add(args.ErrorContext.Error.Message);
|
326 |
1 |
};
|
327 |
||
328 |
1 |
serializer.Deserialize(new StringReader(json), typeof(List<List<DateTime>>));
|
329 |
} |
|
330 |
1 |
catch (Exception ex)
|
331 |
{ |
|
332 |
1 |
Console.WriteLine(ex.Message);
|
333 |
} |
|
334 |
||
335 |
1 |
Assert.AreEqual(1, errors.Count);
|
336 |
1 |
Assert.AreEqual(@"Error converting value ""kjhkjhkjhkjh"" to type 'System.DateTime'.", errors[0]);
|
337 |
1 |
}
|
338 |
} |
|
339 |
} |