public void EveryType_String() { // Arrange EveryType obj = ArrangeEveryType(); // Act string json = JsonSerializer.SerializeObject(obj); EveryType newEveryType = JsonSerializer.ReadObject <EveryType>(json); // Assert AssertEveryType(obj, newEveryType); }
public void EveryType_Stream() { // Arrange EveryType obj = ArrangeEveryType(); // Act string json = JsonSerializer.SerializeObject(obj); EveryType newEveryType; using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(json))) { newEveryType = JsonSerializer.ReadObject <EveryType>(stream); } // Assert AssertEveryType(obj, newEveryType); }
private static void AssertEveryType(EveryType oldEveryType, EveryType newEveryType) { // Assert Assert.IsNotNull(newEveryType); Assert.IsNotNull(newEveryType.StringArray); Assert.IsNotNull(newEveryType.Int16Array); Assert.IsNotNull(newEveryType.UInt16Array); Assert.IsNotNull(newEveryType.Int32Array); Assert.IsNotNull(newEveryType.UInt32Array); Assert.IsNotNull(newEveryType.Int64Array); Assert.IsNotNull(newEveryType.UInt64Array); Assert.IsNotNull(newEveryType.SingleArray); Assert.IsNotNull(newEveryType.DoubleArray); Assert.IsNotNull(newEveryType.DecimalArray); Assert.IsNotNull(newEveryType.StringDictionary); Assert.IsNotNull(newEveryType.Int16Dictionary); Assert.IsNotNull(newEveryType.UInt16Dictionary); Assert.IsNotNull(newEveryType.Int32Dictionary); Assert.IsNotNull(newEveryType.UInt32Dictionary); Assert.IsNotNull(newEveryType.Int64Dictionary); Assert.IsNotNull(newEveryType.UInt64Dictionary); Assert.IsNotNull(newEveryType.SingleDictionary); Assert.IsNotNull(newEveryType.DoubleDictionary); Assert.IsNotNull(newEveryType.DecimalDictionary); Assert.IsNotNull(newEveryType.EnumArray); Assert.IsNotNull(newEveryType.EnumDictionary); Assert.IsNotNull(newEveryType.Class); Assert.IsNotNull(newEveryType.ClassArray); Assert.IsNotNull(newEveryType.ClassDictionary); Assert.AreEqual(oldEveryType.String, newEveryType.String); Assert.IsNull(newEveryType.NullString); Assert.AreEqual(oldEveryType.Boolean, newEveryType.Boolean); Assert.AreEqual(oldEveryType.DateTime, newEveryType.DateTime); Assert.AreEqual(oldEveryType.Int16, newEveryType.Int16); Assert.AreEqual(oldEveryType.UInt16, newEveryType.UInt16); Assert.AreEqual(oldEveryType.Int32, newEveryType.Int32); Assert.AreEqual(oldEveryType.UInt32, newEveryType.UInt32); Assert.AreEqual(oldEveryType.Int64, newEveryType.Int64); Assert.AreEqual(oldEveryType.UInt64, newEveryType.UInt64); Assert.AreEqual(oldEveryType.Single, newEveryType.Single); Assert.AreEqual(oldEveryType.Double, newEveryType.Double); Assert.AreEqual(oldEveryType.Decimal, newEveryType.Decimal); Assert.AreEqual(oldEveryType.Enum, newEveryType.Enum); Assert.AreEqual(oldEveryType.Class.Name, newEveryType.Class.Name); Assert.AreEqual(oldEveryType.BooleanDictionary.Count, newEveryType.BooleanDictionary.Count); foreach (var kvp in oldEveryType.BooleanDictionary) { Assert.AreEqual(kvp.Value, newEveryType.BooleanDictionary[kvp.Key]); } Assert.AreEqual(oldEveryType.DateTimeDictionary.Count, newEveryType.DateTimeDictionary.Count); foreach (var kvp in oldEveryType.DateTimeDictionary) { Assert.AreEqual(kvp.Value, newEveryType.DateTimeDictionary[kvp.Key]); } Assert.AreEqual(oldEveryType.StringDictionary.Count, newEveryType.StringDictionary.Count); foreach (var kvp in oldEveryType.StringDictionary) { Assert.AreEqual(kvp.Value, newEveryType.StringDictionary[kvp.Key]); } Assert.AreEqual(oldEveryType.Int16Dictionary.Count, newEveryType.Int16Dictionary.Count); foreach (var kvp in oldEveryType.Int16Dictionary) { Assert.AreEqual(kvp.Value, newEveryType.Int16Dictionary[kvp.Key]); } Assert.AreEqual(oldEveryType.UInt16Dictionary.Count, newEveryType.UInt16Dictionary.Count); foreach (var kvp in oldEveryType.UInt16Dictionary) { Assert.AreEqual(kvp.Value, newEveryType.UInt16Dictionary[kvp.Key]); } Assert.AreEqual(oldEveryType.Int32Dictionary.Count, newEveryType.Int32Dictionary.Count); foreach (var kvp in oldEveryType.Int32Dictionary) { Assert.AreEqual(kvp.Value, newEveryType.Int32Dictionary[kvp.Key]); } Assert.AreEqual(oldEveryType.UInt32Dictionary.Count, newEveryType.UInt32Dictionary.Count); foreach (var kvp in oldEveryType.UInt32Dictionary) { Assert.AreEqual(kvp.Value, newEveryType.UInt32Dictionary[kvp.Key]); } Assert.AreEqual(oldEveryType.Int64Dictionary.Count, newEveryType.Int64Dictionary.Count); foreach (var kvp in oldEveryType.Int64Dictionary) { Assert.AreEqual(kvp.Value, newEveryType.Int64Dictionary[kvp.Key]); } Assert.AreEqual(oldEveryType.UInt64Dictionary.Count, newEveryType.UInt64Dictionary.Count); foreach (var kvp in oldEveryType.UInt64Dictionary) { Assert.AreEqual(kvp.Value, newEveryType.UInt64Dictionary[kvp.Key]); } Assert.AreEqual(oldEveryType.SingleDictionary.Count, newEveryType.SingleDictionary.Count); foreach (var kvp in oldEveryType.SingleDictionary) { Assert.AreEqual(kvp.Value, newEveryType.SingleDictionary[kvp.Key]); } Assert.AreEqual(oldEveryType.DoubleDictionary.Count, newEveryType.DoubleDictionary.Count); foreach (var kvp in oldEveryType.DoubleDictionary) { Assert.AreEqual(kvp.Value, newEveryType.DoubleDictionary[kvp.Key]); } Assert.AreEqual(oldEveryType.DecimalDictionary.Count, newEveryType.DecimalDictionary.Count); foreach (var kvp in oldEveryType.DecimalDictionary) { Assert.AreEqual(kvp.Value, newEveryType.DecimalDictionary[kvp.Key]); } Assert.AreEqual(oldEveryType.EnumDictionary.Count, newEveryType.EnumDictionary.Count); foreach (var kvp in oldEveryType.EnumDictionary) { Assert.AreEqual(kvp.Value, newEveryType.EnumDictionary[kvp.Key]); } Assert.AreEqual(oldEveryType.ClassDictionary.Count, newEveryType.ClassDictionary.Count); foreach (var kvp in oldEveryType.ClassDictionary) { Assert.AreEqual(kvp.Value.Name, newEveryType.ClassDictionary[kvp.Key].Name); } Assert.AreEqual(oldEveryType.BooleanArray.Length, newEveryType.BooleanArray.Length); for (int i = 0; i < oldEveryType.BooleanArray.Length; i++) { Assert.AreEqual(oldEveryType.BooleanArray[i], newEveryType.BooleanArray[i]); } Assert.AreEqual(oldEveryType.DateTimeArray.Length, newEveryType.DateTimeArray.Length); for (int i = 0; i < oldEveryType.DateTimeArray.Length; i++) { Assert.AreEqual(oldEveryType.DateTimeArray[i], newEveryType.DateTimeArray[i]); } Assert.AreEqual(oldEveryType.StringArray.Length, newEveryType.StringArray.Length); for (int i = 0; i < oldEveryType.StringArray.Length; i++) { Assert.AreEqual(oldEveryType.StringArray[i], newEveryType.StringArray[i]); } Assert.AreEqual(oldEveryType.Int16Array.Length, newEveryType.Int16Array.Length); for (int i = 0; i < oldEveryType.Int16Array.Length; i++) { Assert.AreEqual(oldEveryType.Int16Array[i], newEveryType.Int16Array[i]); } Assert.AreEqual(oldEveryType.UInt16Array.Length, newEveryType.UInt16Array.Length); for (int i = 0; i < oldEveryType.UInt16Array.Length; i++) { Assert.AreEqual(oldEveryType.UInt16Array[i], newEveryType.UInt16Array[i]); } Assert.AreEqual(oldEveryType.Int32Array.Length, newEveryType.Int32Array.Length); for (int i = 0; i < oldEveryType.Int32Array.Length; i++) { Assert.AreEqual(oldEveryType.Int32Array[i], newEveryType.Int32Array[i]); } Assert.AreEqual(oldEveryType.UInt32Array.Length, newEveryType.UInt32Array.Length); for (int i = 0; i < oldEveryType.UInt32Array.Length; i++) { Assert.AreEqual(oldEveryType.UInt32Array[i], newEveryType.UInt32Array[i]); } Assert.AreEqual(oldEveryType.Int64Array.Length, newEveryType.Int64Array.Length); for (int i = 0; i < oldEveryType.Int64Array.Length; i++) { Assert.AreEqual(oldEveryType.Int64Array[i], newEveryType.Int64Array[i]); } Assert.AreEqual(oldEveryType.UInt64Array.Length, newEveryType.UInt64Array.Length); for (int i = 0; i < oldEveryType.UInt64Array.Length; i++) { Assert.AreEqual(oldEveryType.UInt64Array[i], newEveryType.UInt64Array[i]); } Assert.AreEqual(oldEveryType.SingleArray.Length, newEveryType.SingleArray.Length); for (int i = 0; i < oldEveryType.SingleArray.Length; i++) { Assert.AreEqual(oldEveryType.SingleArray[i], newEveryType.SingleArray[i]); } Assert.AreEqual(oldEveryType.DoubleArray.Length, newEveryType.DoubleArray.Length); for (int i = 0; i < oldEveryType.DoubleArray.Length; i++) { Assert.AreEqual(oldEveryType.DoubleArray[i], newEveryType.DoubleArray[i]); } Assert.AreEqual(oldEveryType.DecimalArray.Length, newEveryType.DecimalArray.Length); for (int i = 0; i < oldEveryType.DecimalArray.Length; i++) { Assert.AreEqual(oldEveryType.DecimalArray[i], newEveryType.DecimalArray[i]); } Assert.AreEqual(oldEveryType.EnumArray.Length, newEveryType.EnumArray.Length); for (int i = 0; i < oldEveryType.EnumArray.Length; i++) { Assert.AreEqual(oldEveryType.EnumArray[i], newEveryType.EnumArray[i]); } Assert.AreEqual(oldEveryType.ClassArray.Length, newEveryType.ClassArray.Length); for (int i = 0; i < oldEveryType.ClassArray.Length; i++) { Assert.AreEqual(oldEveryType.ClassArray[i].Name, newEveryType.ClassArray[i].Name); } }
private static EveryType ArrangeEveryType() { // Arrange EveryType everyType = new EveryType { String = "Hello \"World\"!", NullString = null, Boolean = true, DateTime = new DateTime(2004, 3, 2), Int16 = 2, UInt16 = UInt16.MaxValue, Int32 = -5, UInt32 = 42, Int64 = 99, UInt64 = 9234032984, Single = 0.5f, Double = 4.5d, Decimal = 9.9m, BooleanArray = new[] { true, true, false, true, false }, DateTimeArray = new[] { new DateTime(2011, 6, 29), new DateTime(1776, 7, 4) }, StringArray = new[] { "Hello", "\"World\"", ",testing,", null }, Int16Array = new short[] { 3, 4, 5 }, UInt16Array = new ushort[] { 1, 2, 9 }, Int32Array = new[] { -1, -8, -7, 0, 1 }, UInt32Array = new uint[] { 7, 8, 9 }, Int64Array = new long[] { -1232384, 120989123, -1293813 }, UInt64Array = new ulong[] { 239874, 982347, 9237489 }, SingleArray = new[] { 1.1f, -2.2f, 3.3f }, DoubleArray = new[] { 4.4d, -5.5d, 6.6d }, DecimalArray = new[] { 0.12345678m, 123123.123123m, -129380.12313m }, BooleanDictionary = new Dictionary <string, bool>(), DateTimeDictionary = new Dictionary <string, DateTime>(), StringDictionary = new Dictionary <string, string>(), Int16Dictionary = new Dictionary <string, short>(), UInt16Dictionary = new Dictionary <string, ushort>(), Int32Dictionary = new Dictionary <string, int>(), UInt32Dictionary = new Dictionary <string, uint>(), Int64Dictionary = new Dictionary <string, long>(), UInt64Dictionary = new Dictionary <string, ulong>(), SingleDictionary = new Dictionary <string, float>(), DoubleDictionary = new Dictionary <string, double>(), DecimalDictionary = new Dictionary <string, decimal>(), Enum = TestEnum.Two, EnumArray = new[] { TestEnum.One, TestEnum.Three }, EnumDictionary = new Dictionary <string, TestEnum>(), Class = new TestClass { Name = "foo" }, ClassArray = new[] { new TestClass { Name = "bar" }, new TestClass { Name = "2000" } }, ClassDictionary = new Dictionary <string, TestClass>() }; everyType.BooleanDictionary.Add("isPassing", true); everyType.BooleanDictionary.Add("isFailing", false); everyType.DateTimeDictionary.Add("anniversary", new DateTime(2008, 11, 1)); everyType.DateTimeDictionary.Add("herBirthday", new DateTime(1980, 2, 6)); everyType.StringDictionary.Add("one", "hello"); everyType.StringDictionary.Add("two", "wo\\\"rld]"); everyType.Int16Dictionary.Add("three", -3); everyType.Int16Dictionary.Add("four", 4); everyType.UInt16Dictionary.Add("five", 5123); everyType.UInt16Dictionary.Add("six", 13213); everyType.Int32Dictionary.Add("one", -123); everyType.Int32Dictionary.Add("\"", 1234); everyType.UInt32Dictionary.Add("asdf", 4444445); everyType.UInt32Dictionary.Add("\\", 555); everyType.Int64Dictionary.Add("'", 109823424); everyType.Int64Dictionary.Add("\"\"", 8972234); everyType.UInt64Dictionary.Add("one", 982374987234); everyType.UInt64Dictionary.Add("two", 823982748234); everyType.SingleDictionary.Add("uno", 0.543f); everyType.SingleDictionary.Add("dos", -0.599f); everyType.DoubleDictionary.Add("three", 9.999d); everyType.DoubleDictionary.Add("four", -99.999d); everyType.DecimalDictionary.Add("five", 1231313.12313123m); everyType.DecimalDictionary.Add("six", -2908234.23847924m); everyType.EnumDictionary.Add("[]", TestEnum.Three); everyType.EnumDictionary.Add("][", TestEnum.One); everyType.EnumDictionary.Add("abc", TestEnum.Two); everyType.EnumDictionary.Add("xyz", TestEnum.One); everyType.ClassDictionary.Add("class1", new TestClass { Name = "name 1" }); everyType.ClassDictionary.Add("class2", new TestClass { Name = "{}[]\"\\" }); return(everyType); }