Code Coverage Statistics for Source File
Newtonsoft.Json.Tests\Serialization\NullValueHandlingTests.cs
Symbol Coverage: 100.00% (21 of 21)
Branch Coverage: 100.00% (3 of 3)
Cyclomatic Complexity Avg: 1.00 Max:1
Code Lines: 35
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.IO; |
|
29 |
using System.Linq; |
|
30 |
using System.Text; |
|
31 |
using Newtonsoft.Json.Tests.TestObjects; |
|
32 |
using NUnit.Framework; |
|
33 |
||
34 |
namespace Newtonsoft.Json.Tests.Serialization |
|
35 |
{ |
|
36 |
public class NullValueHandlingTests : TestFixtureBase |
|
37 |
{ |
|
38 |
[Test] |
|
39 |
public void NullValueHandlingSerialization() |
|
40 |
{ |
|
41 |
1 |
Store s1 = new Store();
|
42 |
||
43 |
1 |
JsonSerializer jsonSerializer = new JsonSerializer();
|
44 |
1 |
jsonSerializer.NullValueHandling = NullValueHandling.Ignore;
|
45 |
||
46 |
1 |
StringWriter sw = new StringWriter();
|
47 |
1 |
jsonSerializer.Serialize(sw, s1);
|
48 |
||
49 |
//JsonConvert.ConvertDateTimeToJavaScriptTicks(s1.Establised.DateTime) |
|
50 |
||
51 |
1 |
Assert.AreEqual(@"{""Color"":4,""Establised"":""\/Date(1264122061000)\/"",""Width"":1.1,""Employees"":999,""RoomsPerFloor"":[1,2,3,4,5,6,7,8,9],""Open"":false,""Symbol"":""@"",""Mottos"":[""Hello World"",""öäüÖÄÜ\\'{new Date(12345);}[222]_µ@²³~"",null,"" ""],""Cost"":100980.1,""Escape"":""\r\n\t\f\b?{\\r\\n\""'"",""product"":[{""Name"":""Rocket"",""ExpiryDate"":""\/Date(949532490000)\/"",""Price"":0.0},{""Name"":""Alien"",""ExpiryDate"":""\/Date(946684800000)\/"",""Price"":0.0}]}", sw.GetStringBuilder().ToString());
|
52 |
||
53 |
1 |
Store s2 = (Store)jsonSerializer.Deserialize(new JsonTextReader(new StringReader("{}")), typeof(Store));
|
54 |
1 |
Assert.AreEqual("\r\n\t\f\b?{\\r\\n\"\'", s2.Escape);
|
55 |
||
56 |
1 |
Store s3 = (Store)jsonSerializer.Deserialize(new JsonTextReader(new StringReader(@"{""Escape"":null}")), typeof(Store));
|
57 |
1 |
Assert.AreEqual("\r\n\t\f\b?{\\r\\n\"\'", s3.Escape);
|
58 |
||
59 |
1 |
Store s4 = (Store)jsonSerializer.Deserialize(new JsonTextReader(new StringReader(@"{""Color"":2,""Establised"":""\/Date(1264071600000+1300)\/"",""Width"":1.1,""Employees"":999,""RoomsPerFloor"":[1,2,3,4,5,6,7,8,9],""Open"":false,""Symbol"":""@"",""Mottos"":[""Hello World"",""öäüÖÄÜ\\'{new Date(12345);}[222]_µ@²³~"",null,"" ""],""Cost"":100980.1,""Escape"":""\r\n\t\f\b?{\\r\\n\""'"",""product"":[{""Name"":""Rocket"",""ExpiryDate"":""\/Date(949485690000+1300)\/"",""Price"":0},{""Name"":""Alien"",""ExpiryDate"":""\/Date(946638000000)\/"",""Price"":0.0}]}")), typeof(Store));
|
60 |
1 |
Assert.AreEqual(s1.Establised, s3.Establised);
|
61 |
1 |
}
|
62 |
||
63 |
[Test] |
|
64 |
public void NullValueHandlingBlogPost() |
|
65 |
{ |
|
66 |
1 |
Movie movie = new Movie();
|
67 |
1 |
movie.Name = "Bad Boys III";
|
68 |
1 |
movie.Description = "It's no Bad Boys";
|
69 |
||
70 |
1 |
string included = JsonConvert.SerializeObject(movie,
|
71 |
1 |
Formatting.Indented,
|
72 |
1 |
new JsonSerializerSettings { });
|
73 |
||
74 |
// { |
|
75 |
// "Name": "Bad Boys III", |
|
76 |
// "Description": "It's no Bad Boys", |
|
77 |
// "Classification": null, |
|
78 |
// "Studio": null, |
|
79 |
// "ReleaseDate": null, |
|
80 |
// "ReleaseCountries": null |
|
81 |
// } |
|
82 |
||
83 |
1 |
string ignored = JsonConvert.SerializeObject(movie,
|
84 |
1 |
Formatting.Indented,
|
85 |
1 |
new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
|
86 |
||
87 |
// { |
|
88 |
// "Name": "Bad Boys III", |
|
89 |
// "Description": "It's no Bad Boys" |
|
90 |
// } |
|
91 |
||
92 |
1 |
Assert.AreEqual(@"{
|
93 |
1 |
""Name"": ""Bad Boys III"",
|
94 |
1 |
""Description"": ""It's no Bad Boys"",
|
95 |
1 |
""Classification"": null,
|
96 |
1 |
""Studio"": null,
|
97 |
1 |
""ReleaseDate"": null,
|
98 |
1 |
""ReleaseCountries"": null
|
99 |
1 |
}", included);
|
100 |
||
101 |
1 |
Assert.AreEqual(@"{
|
102 |
1 |
""Name"": ""Bad Boys III"",
|
103 |
1 |
""Description"": ""It's no Bad Boys""
|
104 |
1 |
}", ignored);
|
105 |
1 |
}
|
106 |
} |
|
107 |
} |