~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/Newtonsoft.Json/Src/Newtonsoft.Json.Tests/Serialization/WebApiIntegrationTests.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
Import upstream version 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
#if !NET20
 
30
using System.Linq;
 
31
#endif
 
32
using System.Text;
 
33
#if !NETFX_CORE
 
34
using NUnit.Framework;
 
35
#else
 
36
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
 
37
using TestFixture = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute;
 
38
using Test = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAttribute;
 
39
#endif
 
40
#if !(SILVERLIGHT || NETFX_CORE || NET20)
 
41
using System.Runtime.Serialization.Json;
 
42
#endif
 
43
using Newtonsoft.Json.Serialization;
 
44
 
 
45
namespace Newtonsoft.Json.Tests.Serialization
 
46
{
 
47
  [TestFixture]
 
48
  public class WebApiIntegrationTests : TestFixtureBase
 
49
  {
 
50
    [Test]
 
51
    public void SerializeSerializableType()
 
52
    {
 
53
      SerializableType serializableType = new SerializableType("protected")
 
54
        {
 
55
          publicField = "public",
 
56
          protectedInternalField = "protected internal",
 
57
          internalField = "internal",
 
58
          PublicProperty = "private",
 
59
          nonSerializedField = "Error"
 
60
        };
 
61
 
 
62
#if !(SILVERLIGHT || NETFX_CORE || NET20 || PORTABLE)
 
63
      MemoryStream ms = new MemoryStream();
 
64
      DataContractJsonSerializer dataContractJsonSerializer = new DataContractJsonSerializer(typeof(SerializableType));
 
65
      dataContractJsonSerializer.WriteObject(ms, serializableType);
 
66
 
 
67
      string dtJson = Encoding.UTF8.GetString(ms.ToArray());
 
68
      string dtExpected = @"{""internalField"":""internal"",""privateField"":""private"",""protectedField"":""protected"",""protectedInternalField"":""protected internal"",""publicField"":""public""}";
 
69
 
 
70
      Assert.AreEqual(dtExpected, dtJson);
 
71
#endif
 
72
 
 
73
      string expected = "{\"publicField\":\"public\",\"internalField\":\"internal\",\"protectedInternalField\":\"protected internal\",\"protectedField\":\"protected\",\"privateField\":\"private\"}";
 
74
      string json = JsonConvert.SerializeObject(serializableType, new JsonSerializerSettings
 
75
        {
 
76
          ContractResolver = new DefaultContractResolver
 
77
            {
 
78
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
 
79
              IgnoreSerializableAttribute = false
 
80
#endif
 
81
            }
 
82
        });
 
83
 
 
84
      Assert.AreEqual(expected, json);
 
85
    }
 
86
 
 
87
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
 
88
    [Test]
 
89
    public void SerializeInheritedType()
 
90
    {
 
91
      InheritedType serializableType = new InheritedType("protected")
 
92
      {
 
93
        publicField = "public",
 
94
        protectedInternalField = "protected internal",
 
95
        internalField = "internal",
 
96
        PublicProperty = "private",
 
97
        nonSerializedField = "Error",
 
98
        inheritedTypeField = "inherited"
 
99
      };
 
100
 
 
101
      string json = JsonConvert.SerializeObject(serializableType);
 
102
 
 
103
      Assert.AreEqual(@"{""inheritedTypeField"":""inherited"",""publicField"":""public"",""PublicProperty"":""private""}", json);
 
104
    }
 
105
#endif
 
106
  }
 
107
 
 
108
  public class InheritedType : SerializableType
 
109
  {
 
110
    public string inheritedTypeField;
 
111
 
 
112
    public InheritedType(string protectedFieldValue) : base(protectedFieldValue)
 
113
    {
 
114
    }
 
115
  }
 
116
 
 
117
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
 
118
  [Serializable]
 
119
#else
 
120
  [JsonObject(MemberSerialization.Fields)]
 
121
#endif
 
122
  public class SerializableType : IEquatable<SerializableType>
 
123
  {
 
124
    public SerializableType(string protectedFieldValue)
 
125
    {
 
126
      this.protectedField = protectedFieldValue;
 
127
    }
 
128
 
 
129
    public string publicField;
 
130
    internal string internalField;
 
131
    protected internal string protectedInternalField;
 
132
    protected string protectedField;
 
133
    private string privateField;
 
134
 
 
135
    public string PublicProperty
 
136
    {
 
137
      get
 
138
      {
 
139
        return privateField;
 
140
      }
 
141
      set
 
142
      {
 
143
        this.privateField = value;
 
144
      }
 
145
    }
 
146
 
 
147
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
 
148
    [NonSerialized]
 
149
#else
 
150
    [JsonIgnore]
 
151
#endif
 
152
    public string nonSerializedField;
 
153
 
 
154
    public bool Equals(SerializableType other)
 
155
    {
 
156
      return this.publicField == other.publicField &&
 
157
          this.internalField == other.internalField &&
 
158
          this.protectedInternalField == other.protectedInternalField &&
 
159
          this.protectedField == other.protectedField &&
 
160
          this.privateField == other.privateField;
 
161
    }
 
162
  }
 
163
}
 
 
b'\\ No newline at end of file'