Json.NET
Code Coverage Statistics for Source File

Newtonsoft.Json.Tests\Serialization\DefaultValueHandlingTests.cs

Symbol Coverage: 100.00% (15 of 15)

Branch Coverage: 100.00% (3 of 3)

Cyclomatic Complexity Avg: 1.00 Max:1

Code Lines: 31


L V Source
1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Text;
5
using Newtonsoft.Json.Tests.TestObjects;
6
using NUnit.Framework;
7

  
8
namespace Newtonsoft.Json.Tests.Serialization
9
{
10
  public class DefaultValueHandlingTests : TestFixtureBase
11
  {
12
    [Test]
13
    public void SerializeInvoice()
14
    {
15
 1
      Invoice invoice = new Invoice
16
 1
      {
17
 1
        Company = "Acme Ltd.",
18
 1
        Amount = 50.0m,
19
 1
        Paid = false,
20
 1
        FollowUpDays = 30,
21
 1
        FollowUpEmailAddress = string.Empty,
22
 1
        PaidDate = null
23
 1
      };
24

  
25
 1
      string included = JsonConvert.SerializeObject(invoice,
26
 1
        Formatting.Indented,
27
 1
        new JsonSerializerSettings { });
28

  
29
      // {
30
      //   "Company": "Acme Ltd.",
31
      //   "Amount": 50.0,
32
      //   "Paid": false,
33
      //   "PaidDate": null,
34
      //   "FollowUpDays": 30,
35
      //   "FollowUpEmailAddress": ""
36
      // }
37

  
38
 1
      string ignored = JsonConvert.SerializeObject(invoice,
39
 1
        Formatting.Indented,
40
 1
        new JsonSerializerSettings { DefaultValueHandling = DefaultValueHandling.Ignore });
41

  
42
      // {
43
      //   "Company": "Acme Ltd.",
44
      //   "Amount": 50.0
45
      // }
46

  
47
 1
      Console.WriteLine(included);
48
 1
      Console.WriteLine(ignored);
49
 1
    }
50

  
51
    [Test]
52
    public void DefaultValueAttributeTest()
53
    {
54
 1
      string json = JsonConvert.SerializeObject(new DefaultValueAttributeTestClass(),
55
 1
        Formatting.None, new JsonSerializerSettings { DefaultValueHandling = DefaultValueHandling.Ignore });
56
 1
      Assert.AreEqual(@"{""TestField1"":0,""TestProperty1"":null}", json);
57

  
58
 1
      json = JsonConvert.SerializeObject(new DefaultValueAttributeTestClass { TestField1 = int.MinValue, TestProperty1 = "NotDefault" },
59
 1
        Formatting.None, new JsonSerializerSettings { DefaultValueHandling = DefaultValueHandling.Ignore });
60
 1
      Assert.AreEqual(@"{""TestField1"":-2147483648,""TestProperty1"":""NotDefault""}", json);
61

  
62
 1
      json = JsonConvert.SerializeObject(new DefaultValueAttributeTestClass { TestField1 = 21, TestProperty1 = "NotDefault" },
63
 1
        Formatting.None, new JsonSerializerSettings { DefaultValueHandling = DefaultValueHandling.Ignore });
64
 1
      Assert.AreEqual(@"{""TestProperty1"":""NotDefault""}", json);
65

  
66
 1
      json = JsonConvert.SerializeObject(new DefaultValueAttributeTestClass { TestField1 = 21, TestProperty1 = "TestProperty1Value" },
67
 1
        Formatting.None, new JsonSerializerSettings { DefaultValueHandling = DefaultValueHandling.Ignore });
68
 1
      Assert.AreEqual(@"{}", json);
69
 1
    }
70
  }
71
}