2
// Copyright (c) 2007 James Newton-King
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
13
// The above copyright notice and this permission notice shall be
14
// included in all copies or substantial portions of the Software.
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.
27
using System.Collections.Generic;
34
using NUnit.Framework;
36
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
37
using TestFixture = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute;
38
using Test = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAttribute;
40
#if !(SILVERLIGHT || NETFX_CORE || NET20)
41
using System.Runtime.Serialization.Json;
43
using Newtonsoft.Json.Serialization;
45
namespace Newtonsoft.Json.Tests.Serialization
48
public class WebApiIntegrationTests : TestFixtureBase
51
public void SerializeSerializableType()
53
SerializableType serializableType = new SerializableType("protected")
55
publicField = "public",
56
protectedInternalField = "protected internal",
57
internalField = "internal",
58
PublicProperty = "private",
59
nonSerializedField = "Error"
62
#if !(SILVERLIGHT || NETFX_CORE || NET20 || PORTABLE)
63
MemoryStream ms = new MemoryStream();
64
DataContractJsonSerializer dataContractJsonSerializer = new DataContractJsonSerializer(typeof(SerializableType));
65
dataContractJsonSerializer.WriteObject(ms, serializableType);
67
string dtJson = Encoding.UTF8.GetString(ms.ToArray());
68
string dtExpected = @"{""internalField"":""internal"",""privateField"":""private"",""protectedField"":""protected"",""protectedInternalField"":""protected internal"",""publicField"":""public""}";
70
Assert.AreEqual(dtExpected, dtJson);
73
string expected = "{\"publicField\":\"public\",\"internalField\":\"internal\",\"protectedInternalField\":\"protected internal\",\"protectedField\":\"protected\",\"privateField\":\"private\"}";
74
string json = JsonConvert.SerializeObject(serializableType, new JsonSerializerSettings
76
ContractResolver = new DefaultContractResolver
78
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
79
IgnoreSerializableAttribute = false
84
Assert.AreEqual(expected, json);
87
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
89
public void SerializeInheritedType()
91
InheritedType serializableType = new InheritedType("protected")
93
publicField = "public",
94
protectedInternalField = "protected internal",
95
internalField = "internal",
96
PublicProperty = "private",
97
nonSerializedField = "Error",
98
inheritedTypeField = "inherited"
101
string json = JsonConvert.SerializeObject(serializableType);
103
Assert.AreEqual(@"{""inheritedTypeField"":""inherited"",""publicField"":""public"",""PublicProperty"":""private""}", json);
108
public class InheritedType : SerializableType
110
public string inheritedTypeField;
112
public InheritedType(string protectedFieldValue) : base(protectedFieldValue)
117
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
120
[JsonObject(MemberSerialization.Fields)]
122
public class SerializableType : IEquatable<SerializableType>
124
public SerializableType(string protectedFieldValue)
126
this.protectedField = protectedFieldValue;
129
public string publicField;
130
internal string internalField;
131
protected internal string protectedInternalField;
132
protected string protectedField;
133
private string privateField;
135
public string PublicProperty
143
this.privateField = value;
147
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
152
public string nonSerializedField;
154
public bool Equals(SerializableType other)
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;
b'\\ No newline at end of file'