Ejemplo n.º 1
0
        public void LongDeserializationNegative()
        {
            List <Tuple <string, string> > testCases = new List <Tuple <string, string> >()
            {
                new Tuple <string, string>("{\"Long\":9.2233720368547758E+18}", "Error converting value 9.22337203685478E+18 to type 'System.Int64'. Path 'Long', line 1, position 30."), // Fails because this will be read as a double, which then doesn't convert into a long
                new Tuple <string, string>("{\"Long\":9223372036854775808}", "JSON integer 9223372036854775808 is too large or small for an Int64. Path 'Long', line 1, position 27."),   // long.MaxValue + 1
            };

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

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

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

                Assert.AreEqual(actualError.Message, expectedError);
            }
        }
        public void LongDeserializationNegative()
        {
            List<Tuple<string, string>> testCases = new List<Tuple<string, string>>() {
                new Tuple<string, string>("{\"Long\":9.2233720368547758E+18}","Error converting value 9.22337203685478E+18 to type 'System.Int64'. Path 'Long', line 1, position 30."),  // Fails because this will be read as a double, which then doesn't convert into a long      
                new Tuple<string, string>("{\"Long\":9223372036854775808}","JSON integer 9223372036854775808 is too large or small for an Int64. Path 'Long', line 1, position 27."), // long.MaxValue + 1
            };

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

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

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

                Assert.AreEqual(actualError.Message, expectedError);
            }
        }
        public void LongDeserialization()
        {
            long twoToTheFifthyThird = 0x20000000000000; // 2^53
            List<Tuple<LongType, string>> testCases = new List<Tuple<LongType, string>>() {
                new Tuple<LongType, string>(new LongType() { Long = 0 }, "{}"),
                new Tuple<LongType, string>(new LongType() { Long = 0 }, "{\"Long\":null}"),
                new Tuple<LongType, string>(new LongType() { Long = 0 }, "{\"Long\":false}"),
                new Tuple<LongType, string>(new LongType() { Long = 1 }, "{\"Long\":true}"),
                new Tuple<LongType, string>(new LongType() { Long = -1 }, "{\"Long\":-1}"),
                new Tuple<LongType, string>(new LongType() { Long = 1 }, "{\"Long\":1}"),
                new Tuple<LongType, string>(new LongType() { Long = -55 }, "{\"Long\":\"-55\"}"),
                new Tuple<LongType, string>(new LongType() { Long = twoToTheFifthyThird }, "{\"Long\":9007199254740992}"),
                new Tuple<LongType, string>(new LongType() { Long = -twoToTheFifthyThird }, "{\"Long\":-9007199254740992}"),
                new Tuple<LongType, string>(new LongType() { Long = long.MinValue }, "{\"Long\":-9223372036854775808}"), 
                new Tuple<LongType, string>(new LongType() { Long = long.MaxValue }, "{\"Long\":9223372036854775807}"),
                new Tuple<LongType, string>(new LongType() { Long = long.MinValue }, "{\"Long\":-9.2233720368547758E+18}"), // There is a loss of precision here, but we'll accept it when deserializing.
            };

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

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

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

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

                if (testCase.Item2 != "{}")
                {
                    actual = new LongType();
                    actual.Long = 62;
                    DefaultSerializer.Deserialize(input, actual);
                }

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

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

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

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

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