コード例 #1
0
        public void ULongDeserializationNegative()
        {
            List <Tuple <string, string> > testCases = new List <Tuple <string, string> >()
            {
                new Tuple <string, string>("{\"ULong\":18446744073709551616}", "JSON integer 18446744073709551616 is too large or small for an Int64. Path 'ULong', line 1, position 29."), // ulong.MaxValue + 1
                new Tuple <string, string>("{\"ULong\":-1}", "Error converting value -1 to type 'System.UInt64'. Path 'ULong', line 1, position 11."),
            };

            // Need to ensure that the type is registered as a table to force the id property check
            DefaultSerializer.SerializerSettings.ContractResolver.ResolveTableName(typeof(ULongType));

            foreach (var testCase in testCases)
            {
                var input    = testCase.Item1;
                var expected = testCase.Item2;

                ULongType actual      = new ULongType();
                Exception actualError = null;
                try
                {
                    DefaultSerializer.Deserialize(input, actual);
                }
                catch (Exception e)
                {
                    actualError = e;
                }

                Assert.AreEqual(actualError.Message, expected);
            }
        }
        public void ULongDeserializationNegative()
        {
            List<Tuple<string, string>> testCases = new List<Tuple<string, string>>() {
                new Tuple<string, string>("{\"ULong\":18446744073709551616}","JSON integer 18446744073709551616 is too large or small for an Int64. Path 'ULong', line 1, position 29."), // ulong.MaxValue + 1
                new Tuple<string, string>("{\"ULong\":-1}","Error converting value -1 to type 'System.UInt64'. Path 'ULong', line 1, position 11."),
            };

            // Need to ensure that the type is registered as a table to force the id property check
            DefaultSerializer.SerializerSettings.ContractResolver.ResolveTableName(typeof(ULongType));

            foreach (var testCase in testCases)
            {
                var input = testCase.Item1;
                var expected = testCase.Item2;

                ULongType actual = new ULongType();
                Exception actualError = null;
                try
                {
                    DefaultSerializer.Deserialize(input, actual);
                }
                catch (Exception e)
                {
                    actualError = e;
                }

                Assert.AreEqual(actualError.Message, expected);
            }
        }
        public void ULongDeserialization()
        {
            ulong twoToTheFifthyThird = 0x20000000000000; // 2^53
            List<Tuple<ULongType, string>> testCases = new List<Tuple<ULongType, string>>() {
                new Tuple<ULongType, string>(new ULongType() { ULong = 0 }, "{}"),
                new Tuple<ULongType, string>(new ULongType() { ULong = 0 }, "{\"ULong\":null}"),
                new Tuple<ULongType, string>(new ULongType() { ULong = 0 }, "{\"ULong\":0}"),
                new Tuple<ULongType, string>(new ULongType() { ULong = 1 }, "{\"ULong\":1}"),
                new Tuple<ULongType, string>(new ULongType() { ULong = 0 }, "{\"ULong\":false}"),
                new Tuple<ULongType, string>(new ULongType() { ULong = 1 }, "{\"ULong\":true}"),
                new Tuple<ULongType, string>(new ULongType() { ULong = 15 }, "{\"ULong\":\"15\"}"),
                new Tuple<ULongType, string>(new ULongType() { ULong = ulong.MaxValue }, "{\"ULong\":\"18446744073709551615\"}"),
                new Tuple<ULongType, string>(new ULongType() { ULong = twoToTheFifthyThird - 1 },  "{\"ULong\":9007199254740991}"),
                new Tuple<ULongType, string>(new ULongType() { ULong = twoToTheFifthyThird + 1 },  "{\"ULong\":9007199254740993}"),
                new Tuple<ULongType, string>(new ULongType() { ULong = twoToTheFifthyThird },  "{\"ULong\":9007199254740992}"), // All integers <= 2^53 will fit in a double; this should be our upper limit
            };

            // Need to ensure that the type is registered as a table to force the id property check
            DefaultSerializer.SerializerSettings.ContractResolver.ResolveTableName(typeof(ULongType));

            foreach (var testCase in testCases)
            {
                var input = JToken.Parse(testCase.Item2);
                var expected = testCase.Item1;

                ULongType actual = new ULongType();
                DefaultSerializer.Deserialize(input, actual);

                Assert.AreEqual(actual.ULong, expected.ULong);

                if (testCase.Item2 != "{}")
                {
                    actual = new ULongType();
                    actual.ULong = 19;
                    DefaultSerializer.Deserialize(input, actual);
                }

                Assert.AreEqual(actual.ULong, expected.ULong);

                JArray json = JToken.Parse("[" + testCase.Item2 + "]") as JArray;
                actual = DefaultSerializer.Deserialize<ULongType>(json).FirstOrDefault();

                Assert.AreEqual(actual.ULong, expected.ULong);

                actual = (ULongType)DefaultSerializer.Deserialize<ULongType>(input);

                Assert.AreEqual(actual.ULong, expected.ULong);
            }
        }