Ejemplo n.º 1
0
        public static void ReadStructWithSimpleClassArrayValueTest()
        {
            string           json         = "{\"MySimpleTestClass\":{\"MyInt32Array\":[1],\"MyStringToStringDict\":null,\"MyStringToStringIDict\":null},\"MyInt32Array\":[2]}";
            SimpleTestStruct parsedObject = JsonSerializer.Deserialize <SimpleTestStruct>(json);

            Assert.Equal(1, parsedObject.MySimpleTestClass.MyInt32Array[0]);
            Assert.Equal(2, parsedObject.MyInt32Array[0]);
        }
Ejemplo n.º 2
0
        public static void ReadSimpleStruct()
        {
            byte[]           utf8       = Encoding.UTF8.GetBytes(SimpleTestStruct.s_json);
            var              reader     = new Utf8JsonReader(utf8, isFinalBlock: true, state: default);
            SimpleTestStruct testStruct = JsonSerializer.Deserialize <SimpleTestStruct>(ref reader);

            testStruct.Verify();

            reader = new Utf8JsonReader(utf8, isFinalBlock: true, state: default);
            object obj = JsonSerializer.Deserialize(ref reader, typeof(SimpleTestStruct));

            ((SimpleTestStruct)obj).Verify();
        }
Ejemplo n.º 3
0
        public static void ReadSimpleTestStructWithSimpleTestClass()
        {
            SimpleTestStruct testObject = new SimpleTestStruct();

            testObject.Initialize();
            testObject.MySimpleTestClass = new SimpleTestClass  {
                MyString = "Hello", MyDouble = 3.14
            };

            string           json         = JsonSerializer.ToString(testObject);
            SimpleTestStruct parsedObject = JsonSerializer.Parse <SimpleTestStruct>(json);

            parsedObject.Verify();
        }
Ejemplo n.º 4
0
        public static void ReadSimpleTestStructWithSimpleTestClass()
        {
            SimpleTestStruct testObject = new SimpleTestStruct();

            testObject.Initialize();
            testObject.MySimpleTestClass = new SimpleTestClass {
                MyString = "Hello", MyDouble = 3.14
            };

            string           json         = JsonSerializer.Serialize(testObject);
            SimpleTestStruct parsedObject = JsonSerializer.Deserialize <SimpleTestStruct>(json);

            parsedObject.Verify();
            Assert.Equal(3.14, parsedObject.MySimpleTestClass.MyDouble);
            Assert.Equal("Hello", parsedObject.MySimpleTestClass.MyString);
        }
Ejemplo n.º 5
0
        public static void ReadSimpleStruct()
        {
            SimpleTestStruct obj = JsonSerializer.Deserialize <SimpleTestStruct>(SimpleTestStruct.s_json);

            obj.Verify();
        }
        public void Initialize()
        {
            MyInt16               = 1;
            MyInt32               = 2;
            MyInt64               = 3;
            MyUInt16              = 4;
            MyUInt32              = 5;
            MyUInt64              = 6;
            MyByte                = 7;
            MySByte               = 8;
            MyChar                = 'a';
            MyString              = "Hello";
            MyBooleanTrue         = true;
            MyBooleanFalse        = false;
            MySingle              = 1.1f;
            MyDouble              = 2.2d;
            MyDecimal             = 3.3m;
            MyDateTime            = new DateTime(2019, 1, 30, 12, 1, 2, DateTimeKind.Utc);
            MyDateTimeOffset      = new DateTimeOffset(2019, 1, 30, 12, 1, 2, new TimeSpan(1, 0, 0));
            MyGuid                = new Guid("1B33498A-7B7D-4DDA-9C13-F6AA4AB449A6");
            MyUri                 = new Uri("https://github.com/dotnet/runtime");
            MyEnum                = SampleEnum.Two;
            MyInt64Enum           = SampleEnumInt64.MinNegative;
            MyUInt64Enum          = SampleEnumUInt64.Max;
            MyInt16Array          = new short[] { 1 };
            MyInt32Array          = new int[] { 2 };
            MyInt64Array          = new long[] { 3 };
            MyUInt16Array         = new ushort[] { 4 };
            MyUInt32Array         = new uint[] { 5 };
            MyUInt64Array         = new ulong[] { 6 };
            MyByteArray           = new byte[] { 7 };
            MySByteArray          = new sbyte[] { 8 };
            MyCharArray           = new char[] { 'a' };
            MyStringArray         = new string[] { "Hello" };
            MyBooleanTrueArray    = new bool[] { true };
            MyBooleanFalseArray   = new bool[] { false };
            MySingleArray         = new float[] { 1.1f };
            MyDoubleArray         = new double[] { 2.2d };
            MyDecimalArray        = new decimal[] { 3.3m };
            MyDateTimeArray       = new DateTime[] { new DateTime(2019, 1, 30, 12, 1, 2, DateTimeKind.Utc) };
            MyDateTimeOffsetArray = new DateTimeOffset[] { new DateTimeOffset(2019, 1, 30, 12, 1, 2, new TimeSpan(1, 0, 0)) };
            MyGuidArray           = new Guid[] { new Guid("1B33498A-7B7D-4DDA-9C13-F6AA4AB449A6") };
            MyUriArray            = new Uri[] { new Uri("https://github.com/dotnet/runtime") };
            MyEnumArray           = new SampleEnum[] { SampleEnum.Two };
            MySimpleStruct        = new SimpleStruct {
                One = 11, Two = 1.9999
            };
            MySimpleTestStruct = new SimpleTestStruct {
                MyInt64 = 64, MyString = "Hello", MyInt32Array = new int[] { 32 }
            };

            MyInt16TwoDimensionArray    = new int[2][];
            MyInt16TwoDimensionArray[0] = new int[] { 10, 11 };
            MyInt16TwoDimensionArray[1] = new int[] { 20, 21 };

            MyInt16TwoDimensionList = new List <List <int> >();
            MyInt16TwoDimensionList.Add(new List <int> {
                10, 11
            });
            MyInt16TwoDimensionList.Add(new List <int> {
                20, 21
            });

            MyInt16ThreeDimensionArray       = new int[2][][];
            MyInt16ThreeDimensionArray[0]    = new int[2][];
            MyInt16ThreeDimensionArray[1]    = new int[2][];
            MyInt16ThreeDimensionArray[0][0] = new int[] { 11, 12 };
            MyInt16ThreeDimensionArray[0][1] = new int[] { 13, 14 };
            MyInt16ThreeDimensionArray[1][0] = new int[] { 21, 22 };
            MyInt16ThreeDimensionArray[1][1] = new int[] { 23, 24 };

            MyInt16ThreeDimensionList = new List <List <List <int> > >();
            var list1 = new List <List <int> >();

            MyInt16ThreeDimensionList.Add(list1);
            list1.Add(new List <int> {
                11, 12
            });
            list1.Add(new List <int> {
                13, 14
            });
            var list2 = new List <List <int> >();

            MyInt16ThreeDimensionList.Add(list2);
            list2.Add(new List <int> {
                21, 22
            });
            list2.Add(new List <int> {
                23, 24
            });

            MyStringList = new List <string>()
            {
                "Hello"
            };

            MyStringIEnumerable = new string[] { "Hello" };
            MyStringIList       = new string[] { "Hello" };
            MyStringICollection = new string[] { "Hello" };

            MyStringIEnumerableT         = new string[] { "Hello" };
            MyStringIListT               = new string[] { "Hello" };
            MyStringICollectionT         = new string[] { "Hello" };
            MyStringIReadOnlyCollectionT = new string[] { "Hello" };
            MyStringIReadOnlyListT       = new string[] { "Hello" };
            MyStringISetT = new HashSet <string> {
                "Hello"
            };

            MyStringToStringKeyValuePair = new KeyValuePair <string, string>("myKey", "myValue");
            MyStringToStringIDict        = new Dictionary <string, string> {
                { "key", "value" }
            };

            MyStringToStringGenericDict = new Dictionary <string, string> {
                { "key", "value" }
            };
            MyStringToStringGenericIDict = new Dictionary <string, string> {
                { "key", "value" }
            };
            MyStringToStringGenericIReadOnlyDict = new Dictionary <string, string> {
                { "key", "value" }
            };

            MyStringToStringImmutableDict       = ImmutableDictionary.CreateRange(MyStringToStringGenericDict);
            MyStringToStringIImmutableDict      = ImmutableDictionary.CreateRange(MyStringToStringGenericDict);
            MyStringToStringImmutableSortedDict = ImmutableSortedDictionary.CreateRange(MyStringToStringGenericDict);

            MyStringStackT = new Stack <string>(new List <string>()
            {
                "Hello", "World"
            });
            MyStringQueueT = new Queue <string>(new List <string>()
            {
                "Hello", "World"
            });
            MyStringHashSetT = new HashSet <string>(new List <string>()
            {
                "Hello"
            });
            MyStringLinkedListT = new LinkedList <string>(new List <string>()
            {
                "Hello"
            });
            MyStringSortedSetT = new SortedSet <string>(new List <string>()
            {
                "Hello"
            });

            MyStringIImmutableListT = ImmutableList.CreateRange(new List <string> {
                "Hello"
            });
            MyStringIImmutableStackT = ImmutableStack.CreateRange(new List <string> {
                "Hello"
            });
            MyStringIImmutableQueueT = ImmutableQueue.CreateRange(new List <string> {
                "Hello"
            });
            MyStringIImmutableSetT = ImmutableHashSet.CreateRange(new List <string> {
                "Hello"
            });
            MyStringImmutableHashSetT = ImmutableHashSet.CreateRange(new List <string> {
                "Hello"
            });
            MyStringImmutableListT = ImmutableList.CreateRange(new List <string> {
                "Hello"
            });
            MyStringImmutableStackT = ImmutableStack.CreateRange(new List <string> {
                "Hello"
            });
            MyStringImmutablQueueT = ImmutableQueue.CreateRange(new List <string> {
                "Hello"
            });
            MyStringImmutableSortedSetT = ImmutableSortedSet.CreateRange(new List <string> {
                "Hello"
            });

            MyListOfNullString = new List <string> {
                null
            };
        }
Ejemplo n.º 7
0
 public static void ReadStructWithSimpleClassValueTest()
 {
     string           json = "{\"MySimpleTestClass\":{\"MyInt32Array\":[1],\"MyStringToStringDict\":null,\"MyStringToStringIDict\":null},\"MyInt32Array\":[2]}";
     SimpleTestStruct obj3 = JsonSerializer.Parse <SimpleTestStruct>(json);
 }