Json.NET
Code Coverage Statistics for Source File

Newtonsoft.Json.Tests\Serialization\ContractResolverTests.cs

Symbol Coverage: 100.00% (14 of 14)

Branch Coverage: 100.00% (4 of 4)

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 NUnit.Framework;
6
using Newtonsoft.Json.Serialization;
7

  
8
namespace Newtonsoft.Json.Tests.Serialization
9
{
10
  public class DynamicContractResolver : DefaultContractResolver
11
  {
12
    private readonly char _startingWithChar;
13
 2
    public DynamicContractResolver(char startingWithChar)
14
 2
      : base(false)
15
    {
16
 2
      _startingWithChar = startingWithChar;
17
 2
    }
18

  
19
    protected override IList<JsonProperty> CreateProperties(JsonObjectContract contract)
20
    {
21
 2
      IList<JsonProperty> properties = base.CreateProperties(contract);
22

  
23
      // only serializer properties that start with the specified character
24
 2
      properties = 
25
 2
        properties.Where(p => p.PropertyName.StartsWith(_startingWithChar.ToString())).ToList();
26

  
27
 2
      return properties;
28
 2
    }
29
  }
30

  
31
  public class Book
32
  {
33
    public string BookName { get; set; }
34
    public decimal BookPrice { get; set; }
35
    public string AuthorName { get; set; }
36
    public int AuthorAge { get; set; }
37
    public string AuthorCountry { get; set; }
38
  }
39

  
40
  public class ContractResolverTests : TestFixtureBase
41
  {
42
    [Test]
43
    public void SingleTypeWithMultipleContractResolvers()
44
    {
45
 1
      Book book = new Book
46
 1
                    {
47
 1
                      BookName = "The Gathering Storm",
48
 1
                      BookPrice = 16.19m,
49
 1
                      AuthorName = "Brandon Sanderson",
50
 1
                      AuthorAge = 34,
51
 1
                      AuthorCountry = "United States of America"
52
 1
                    };
53

  
54
 1
      string startingWithA = JsonConvert.SerializeObject(book, Formatting.Indented,
55
 1
        new JsonSerializerSettings { ContractResolver = new DynamicContractResolver('A') });
56

  
57
      // {
58
      //   "AuthorName": "Brandon Sanderson",
59
      //   "AuthorAge": 34,
60
      //   "AuthorCountry": "United States of America"
61
      // }
62

  
63
 1
      string startingWithB = JsonConvert.SerializeObject(book, Formatting.Indented,
64
 1
        new JsonSerializerSettings { ContractResolver = new DynamicContractResolver('B') });
65

  
66
      // {
67
      //   "BookName": "The Gathering Storm",
68
      //   "BookPrice": 16.19
69
      // }
70

  
71
 1
      Assert.AreEqual(@"{
72
 1
  ""AuthorName"": ""Brandon Sanderson"",
73
 1
  ""AuthorAge"": 34,
74
 1
  ""AuthorCountry"": ""United States of America""
75
 1
}", startingWithA);
76

  
77
 1
      Assert.AreEqual(@"{
78
 1
  ""BookName"": ""The Gathering Storm"",
79
 1
  ""BookPrice"": 16.19
80
 1
}", startingWithB);
81
 1
    }
82
  }
83
}