1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518
|
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
using Microsoft.TestCommon;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Xunit;
using Xunit.Extensions;
namespace System.Net.Http.Formatting
{
public class JsonNetSerializationTest
{
public static IEnumerable<object[]> SerializedJson
{
get
{
return new TheoryDataSet<object, string>()
{
// Primitives
{ 'f', "\"f\"" },
{ "abc", "\"abc\"" },
{ "\"\\", @"""\""\\""" },
{ 256, "256" },
{ (ulong)long.MaxValue, long.MaxValue.ToString() },
{ 45.78m, "45.78" },
{ .00000457823432, "4.57823432E-06" },
{ (byte)24, "24" },
{ false, "false" },
{ AttributeTargets.Assembly | AttributeTargets.Constructor, "33" },
{ ConsoleColor.DarkCyan, "3" },
{ new DateTimeOffset(1999, 5, 27, 4, 34, 45, TimeSpan.Zero), "\"1999-05-27T04:34:45+00:00\"" },
{ new TimeSpan(5, 30, 0), "\"05:30:00\"" },
{ new Uri("http://www.bing.com"), @"""http://www.bing.com/""" },
{ new Guid("4ed1cd44-11d7-4b27-b623-0b8b553c8906"), "\"4ed1cd44-11d7-4b27-b623-0b8b553c8906\"" },
// Structs
{ new Point() { x = 45, Y = -5}, "{\"x\":45,\"Y\":-5}" },
// Arrays
{ new object[] {}, "[]" },
{ new int[] { 1, 2, 3}, "[1,2,3]" },
{ new string[] { "a", "b"}, "[\"a\",\"b\"]" },
{ new Point[] { new Point() { x = 10, Y = 10}, new Point() { x = 20, Y = 20}}, "[{\"x\":10,\"Y\":10},{\"x\":20,\"Y\":20}]" },
// Collections
{ new List<int> { 1, 2, 3}, "[1,2,3]" },
{ new List<string> { "a", "b"}, "[\"a\",\"b\"]" },
{ new List<Point> { new Point() { x = 10, Y = 10}, new Point() { x = 20, Y = 20}}, "[{\"x\":10,\"Y\":10},{\"x\":20,\"Y\":20}]" },
{ new MyList<int> { 1, 2, 3}, "[1,2,3]" },
{ new MyList<string> { "a", "b"}, "[\"a\",\"b\"]" },
{ new MyList<Point> { new Point() { x = 10, Y = 10}, new Point() { x = 20, Y = 20}}, "[{\"x\":10,\"Y\":10},{\"x\":20,\"Y\":20}]" },
// Dictionaries
{ new Dictionary<string, string> { { "k1", "v1" }, { "k2", "v2" } }, "{\"k1\":\"v1\",\"k2\":\"v2\"}" },
{ new Dictionary<int, string> { { 1, "v1" }, { 2, "v2" } }, "{\"1\":\"v1\",\"2\":\"v2\"}" },
// Anonymous types
{ new { Anon1 = 56, Anon2 = "foo"}, "{\"Anon1\":56,\"Anon2\":\"foo\"}" },
// Classes
{ new DataContractType() { s = "foo", i = 49, NotAMember = "Error" }, "{\"s\":\"foo\",\"i\":49}" },
{ new POCOType() { s = "foo", t = "Error"}, "{\"s\":\"foo\"}" },
{ new SerializableType("protected") { publicField = "public", protectedInternalField = "protected internal", internalField = "internal", PublicProperty = "private", nonSerializedField = "Error" }, "{\"publicField\":\"public\",\"internalField\":\"internal\",\"protectedInternalField\":\"protected internal\",\"protectedField\":\"protected\",\"privateField\":\"private\"}" },
// Generics
{ new KeyValuePair<string, bool>("foo", false), "{\"Key\":\"foo\",\"Value\":false}" },
// ISerializable types
{ new ArgumentNullException("param"), "{\"ClassName\":\"System.ArgumentNullException\",\"Message\":\"Value cannot be null.\",\"Data\":null,\"InnerException\":null,\"HelpURL\":null,\"StackTraceString\":null,\"RemoteStackTraceString\":null,\"RemoteStackIndex\":0,\"ExceptionMethod\":null,\"HResult\":-2147467261,\"Source\":null,\"WatsonBuckets\":null,\"ParamName\":\"param\"}" },
// JSON Values
{ new JValue(false), "false" },
{ new JValue(54), "54" },
{ new JValue("s"), "\"s\"" },
{ new JArray() { new JValue(1), new JValue(2) }, "[1,2]" },
{ new JObject() { { "k1", new JValue("v1") }, { "k2", new JValue("v2") } }, "{\"k1\":\"v1\",\"k2\":\"v2\"}" },
{ new KeyValuePair<JToken, JToken>(new JValue("k"), new JArray() { new JValue("v1"), new JValue("v2") }), "{\"Key\":\"k\",\"Value\":[\"v1\",\"v2\"]}" },
};
}
}
public static IEnumerable<object[]> TypedSerializedJson
{
get
{
return new TheoryDataSet<object, string, Type>()
{
// Null
{ null, "null", typeof(POCOType) },
{ null, "null", typeof(JToken) },
// Nullables
{ new int?(), "null", typeof(int?) },
{ new Point?(), "null", typeof(Point?) },
{ new ConsoleColor?(), "null", typeof(ConsoleColor?) },
{ new int?(45), "45", typeof(int?) },
{ new Point?(new Point() { x = 45, Y = -5 }), "{\"x\":45,\"Y\":-5}", typeof(Point?) },
{ new ConsoleColor?(ConsoleColor.DarkMagenta), "5", typeof(ConsoleColor?)},
};
}
}
[Theory]
[PropertyData("SerializedJson")]
public void ObjectsSerializeToExpectedJson(object o, string expectedJson)
{
ObjectsSerializeToExpectedJsonWithProvidedType(o, expectedJson, o.GetType());
}
[Theory]
[PropertyData("SerializedJson")]
public void JsonDeserializesToExpectedObject(object expectedObject, string json)
{
JsonDeserializesToExpectedObjectWithProvidedType(expectedObject, json, expectedObject.GetType());
}
[Theory]
[PropertyData("TypedSerializedJson")]
public void ObjectsSerializeToExpectedJsonWithProvidedType(object o, string expectedJson, Type type)
{
Assert.Equal(expectedJson, Serialize(o, type));
}
[Theory]
[PropertyData("TypedSerializedJson")]
public void JsonDeserializesToExpectedObjectWithProvidedType(object expectedObject, string json, Type type)
{
if (expectedObject == null)
{
Assert.Null(Deserialize(json, type));
}
else
{
Assert.Equal(expectedObject, Deserialize(json, type), new ObjectComparer());
}
}
[Fact]
public void CallbacksGetCalled()
{
TypeWithCallbacks o = new TypeWithCallbacks();
string json = Serialize(o, typeof(TypeWithCallbacks));
Assert.Equal("12", o.callbackOrder);
TypeWithCallbacks deserializedObject = Deserialize(json, typeof(TypeWithCallbacks)) as TypeWithCallbacks;
Assert.Equal("34", deserializedObject.callbackOrder);
}
[Fact]
public void DerivedTypesArePreserved()
{
JsonMediaTypeFormatter formatter = new JsonMediaTypeFormatter();
formatter.SerializerSettings.TypeNameHandling = TypeNameHandling.Objects;
string json = Serialize(new Derived(), typeof(Base), formatter);
object deserializedObject = Deserialize(json, typeof(Base), formatter);
Assert.IsType(typeof(Derived), deserializedObject);
}
[Fact]
public void ArbitraryTypesArentDeserializedByDefault()
{
string json = "{\"$type\":\"" + typeof(DangerousType).AssemblyQualifiedName + "\"}";
object deserializedObject = Deserialize(json, typeof(object));
Assert.IsNotType(typeof(DangerousType), deserializedObject);
}
[Fact]
public void ReferencesArePreserved()
{
Ref ref1 = new Ref();
Ref ref2 = new Ref();
ref1.Reference = ref2;
ref2.Reference = ref1;
string json = Serialize(ref1, typeof(Ref));
Ref deserializedObject = Deserialize(json, typeof(Ref)) as Ref;
Assert.ReferenceEquals(deserializedObject, deserializedObject.Reference.Reference);
}
[Fact]
public void DeserializingDeepArraysThrows()
{
StringBuilder sb = new StringBuilder();
int depth = 500;
for (int i = 0; i < depth; i++)
{
sb.Append("[");
}
sb.Append("null");
for (int i = 0; i < depth; i++)
{
sb.Append("]");
}
string json = sb.ToString();
Assert.Throws(typeof(JsonReaderQuotaException), () => Deserialize(json, typeof(object)));
}
[Theory]
// existing good surrogate pair
[InlineData("ABC \\ud800\\udc00 DEF", "ABC \ud800\udc00 DEF")]
// invalid surrogates (two high back-to-back)
[InlineData("ABC \\ud800\\ud800 DEF", "ABC \ufffd\ufffd DEF")]
// invalid high surrogate at end of string
[InlineData("ABC \\ud800", "ABC \ufffd")]
// high surrogate not followed by low surrogate
[InlineData("ABC \\ud800 DEF", "ABC \ufffd DEF")]
// low surrogate not preceded by high surrogate
[InlineData("ABC \\udc00\\ud800 DEF", "ABC \ufffd\ufffd DEF")]
// make sure unencoded invalid surrogate characters don't make it through
[InlineData("\udc00\ud800\ud800", "??????")]
public void InvalidUnicodeStringsAreFixedUp(string input, string expectedString)
{
string json = "\"" + input + "\"";
string deserializedString = Deserialize(json, typeof(string)) as string;
Assert.Equal(expectedString, deserializedString);
}
private static string Serialize(object o, Type type, MediaTypeFormatter formatter = null)
{
formatter = formatter ?? new JsonMediaTypeFormatter();
MemoryStream ms = new MemoryStream();
formatter.WriteToStreamAsync(type, o, ms, null, null).Wait();
ms.Flush();
ms.Position = 0;
return new StreamReader(ms).ReadToEnd();
}
internal static object Deserialize(string json, Type type, MediaTypeFormatter formatter = null, IFormatterLogger formatterLogger = null)
{
formatter = formatter ?? new JsonMediaTypeFormatter();
MemoryStream ms = new MemoryStream();
byte[] bytes = Encoding.Default.GetBytes(json);
ms.Write(bytes, 0, bytes.Length);
ms.Flush();
ms.Position = 0;
Task<object> readTask = formatter.ReadFromStreamAsync(type, ms, contentHeaders: null, formatterLogger: formatterLogger);
readTask.WaitUntilCompleted();
if (readTask.IsFaulted)
{
throw readTask.Exception.GetBaseException();
}
return readTask.Result;
}
}
public class ObjectComparer : IEqualityComparer<object>
{
bool IEqualityComparer<object>.Equals(object x, object y)
{
Type xType = x.GetType();
if (xType == y.GetType())
{
if (typeof(JToken).IsAssignableFrom(xType) || xType == typeof(ArgumentNullException) || xType == typeof(KeyValuePair<JToken, JToken>))
{
return x.ToString() == y.ToString();
}
if (xType == typeof(DataContractType))
{
return Equals<DataContractType>(x, y);
}
if (xType == typeof(POCOType))
{
return Equals<POCOType>(x, y);
}
if (xType == typeof(SerializableType))
{
return Equals<SerializableType>(x, y);
}
if (xType == typeof(Point))
{
return Equals<Point>(x, y);
}
if (typeof(IEnumerable).IsAssignableFrom(xType))
{
IEnumerator xEnumerator = ((IEnumerable)x).GetEnumerator();
IEnumerator yEnumerator = ((IEnumerable)y).GetEnumerator();
while (xEnumerator.MoveNext())
{
// if x is longer than y
if (!yEnumerator.MoveNext())
{
return false;
}
else
{
if (!xEnumerator.Current.Equals(yEnumerator.Current))
{
return false;
}
}
}
// if y is longer than x
if (yEnumerator.MoveNext())
{
return false;
}
return true;
}
}
return x.Equals(y);
}
int IEqualityComparer<object>.GetHashCode(object obj)
{
throw new NotImplementedException();
}
bool Equals<T>(object x, object y) where T : IEquatable<T>
{
IEquatable<T> yEquatable = (IEquatable<T>)y;
return yEquatable.Equals((T)x);
}
}
// Marked as [Serializable] to check that [DataContract] takes precedence over [Serializable]
[DataContract]
[Serializable]
public class DataContractType : IEquatable<DataContractType>
{
[DataMember]
public string s;
[DataMember]
internal int i;
public string NotAMember;
public bool Equals(DataContractType other)
{
return this.s == other.s && this.i == other.i;
}
}
public class POCOType : IEquatable<POCOType>
{
public string s;
internal string t;
public bool Equals(POCOType other)
{
return this.s == other.s;
}
}
public class MyList<T> : ICollection<T>
{
List<T> innerList = new List<T>();
public IEnumerator<T> GetEnumerator()
{
return innerList.GetEnumerator();
}
public void Add(T item)
{
innerList.Add(item);
}
public void Clear()
{
innerList.Clear();
}
public bool Contains(T item)
{
return innerList.Contains(item);
}
public void CopyTo(T[] array, int arrayIndex)
{
innerList.CopyTo(array, arrayIndex);
}
public int Count
{
get { return innerList.Count; }
}
public bool IsReadOnly
{
get { return ((ICollection<T>)innerList).IsReadOnly; }
}
public bool Remove(T item)
{
return innerList.Remove(item);
}
IEnumerator IEnumerable.GetEnumerator()
{
return innerList.GetEnumerator();
}
IEnumerator<T> IEnumerable<T>.GetEnumerator()
{
return innerList.GetEnumerator();
}
}
[Serializable]
class SerializableType : IEquatable<SerializableType>
{
public SerializableType(string protectedFieldValue)
{
this.protectedField = protectedFieldValue;
}
public string publicField;
internal string internalField;
protected internal string protectedInternalField;
protected string protectedField;
private string privateField;
public string PublicProperty
{
get
{
return privateField;
}
set
{
this.privateField = value;
}
}
[NonSerialized]
public string nonSerializedField;
public bool Equals(SerializableType other)
{
return this.publicField == other.publicField &&
this.internalField == other.internalField &&
this.protectedInternalField == other.protectedInternalField &&
this.protectedField == other.protectedField &&
this.privateField == other.privateField;
}
}
public struct Point : IEquatable<Point>
{
public int x;
public int Y { get; set; }
public bool Equals(Point other)
{
return this.x == other.x && this.Y == other.Y;
}
}
[DataContract(IsReference = true)]
public class Ref
{
[DataMember]
public Ref Reference;
}
public class Base
{
}
public class Derived : Base
{
}
[DataContract]
public class TypeWithCallbacks
{
public string callbackOrder = "";
[OnSerializing]
public void OnSerializing(StreamingContext c)
{
callbackOrder += "1";
}
[OnSerialized]
public void OnSerialized(StreamingContext c)
{
callbackOrder += "2";
}
[OnDeserializing]
public void OnDeserializing(StreamingContext c)
{
callbackOrder += "3";
}
[OnDeserialized]
public void OnDeserialized(StreamingContext c)
{
callbackOrder += "4";
}
}
public class DangerousType
{
}
}
|