Esempio n. 1
0
        public void DateTimeOffsetDeserializationNegative()
        {
            List <Tuple <string, string> > testCases = new List <Tuple <string, string> >()
            {
                new Tuple <string, string>("{\"DateTimeOffset\":\"I'm not a Date\"}", "String was not recognized as a valid DateTime."),
                new Tuple <string, string>("{\"DateTimeOffset\":true}", "Unexpected token parsing date. Expected String, got Boolean. Path 'DateTimeOffset', line 1, position 22."),
                new Tuple <string, string>("{\"DateTimeOffset\":5}", "Unexpected token parsing date. Expected String, got Integer. Path 'DateTimeOffset', line 1, position 19."),
                new Tuple <string, string>("{\"DateTimeOffset\":\"\t\"}", "String was not recognized as a valid DateTime."),
            };

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

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

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

                Assert.AreEqual(actualError.Message, expectedError);
            }
        }
        public void DateTimeOffsetDeserializationNegative()
        {
            List<Tuple<string, string>> testCases = new List<Tuple<string, string>>() {
                new Tuple<string, string>("{\"DateTimeOffset\":\"I'm not a Date\"}", "String was not recognized as a valid DateTime."),
                new Tuple<string, string>("{\"DateTimeOffset\":true}", "Unexpected token parsing date. Expected String, got Boolean. Path 'DateTimeOffset', line 1, position 22."),
                new Tuple<string, string>("{\"DateTimeOffset\":5}", "Unexpected token parsing date. Expected String, got Integer. Path 'DateTimeOffset', line 1, position 19."),
                new Tuple<string, string>("{\"DateTimeOffset\":\"\t\"}", "String was not recognized as a valid DateTime."),
            };

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

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

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

                Assert.AreEqual(actualError.Message, expectedError);
            }
        }
        public void DateTimeOffsetDeserialization()
        {
            List<Tuple<DateTimeOffsetType, string>> testCases = new List<Tuple<DateTimeOffsetType, string>>()
            {
                new Tuple<DateTimeOffsetType, string>(new DateTimeOffsetType() { DateTimeOffset = new DateTimeOffset() }, "{}"),
                new Tuple<DateTimeOffsetType, string>(new DateTimeOffsetType() { DateTimeOffset = new DateTimeOffset() }, "{\"DateTimeOffset\":null}"),
            };

            List<DateTimeOffset> dates = new List<DateTimeOffset>
            {
                new DateTimeOffset(new DateTime(1999, 12, 31, 23, 59, 59)).ToLocalTime(),
                new DateTimeOffset(new DateTime(2005, 3, 14, 12, 34, 16, DateTimeKind.Local)).ToLocalTime(),
                new DateTimeOffset(new DateTime(2005, 4, 14, 12, 34, 16, DateTimeKind.Unspecified)).ToLocalTime(),
                new DateTimeOffset(new DateTime(2005, 3, 14, 12, 34, 16, DateTimeKind.Local)).ToLocalTime(),
                new DateTimeOffset(new DateTime(2005, 5, 14, 12, 34, 16, DateTimeKind.Utc)).ToLocalTime(),
                new DateTimeOffset(new DateTime(2012, 2, 29, 12, 0, 0, DateTimeKind.Utc)).ToLocalTime(), // leap day
            };

            foreach (var date in dates)
            {
                testCases.Add(new Tuple<DateTimeOffsetType,string>(
                    new DateTimeOffsetType { DateTimeOffset = date },
                    "{\"DateTimeOffset\":" + ExpectedDateTimeOffsetSerialization(date) + "}"));
            }

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

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

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

                Assert.AreEqual(actual.DateTimeOffset.ToLocalTime(), expected.DateTimeOffset.ToLocalTime());
                Assert.AreEqual(actual.DateTimeOffset.ToUniversalTime(), expected.DateTimeOffset.ToUniversalTime());

                if (testCase.Item2 != "{}")
                {
                    actual = new DateTimeOffsetType();
                    actual.DateTimeOffset = DateTimeOffset.Now;
                    DefaultSerializer.Deserialize(input, actual);
                }

                Assert.AreEqual(actual.DateTimeOffset.ToUniversalTime(), expected.DateTimeOffset.ToUniversalTime());

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

                Assert.AreEqual(actual.DateTimeOffset.ToUniversalTime(), expected.DateTimeOffset.ToUniversalTime());

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

                Assert.AreEqual(actual.DateTimeOffset.ToUniversalTime(), expected.DateTimeOffset.ToUniversalTime());
            }
        }
        public void DateTimeOffsetDeserialization()
        {
            List<Tuple<DateTimeOffsetType, string>> testCases = new List<Tuple<DateTimeOffsetType, string>>() {
                new Tuple<DateTimeOffsetType, string>(new DateTimeOffsetType() { DateTimeOffset = new DateTimeOffset() }, "{}"),
                new Tuple<DateTimeOffsetType, string>(new DateTimeOffsetType() { DateTimeOffset = new DateTimeOffset() }, "{\"DateTimeOffset\":null}"),
                new Tuple<DateTimeOffsetType, string>(new DateTimeOffsetType() { DateTimeOffset = new DateTimeOffset(new DateTime(1999, 12, 31, 23, 59, 59)).ToLocalTime() }, "{\"DateTimeOffset\":\"2000-01-01T07:59:59.000Z\"}"),
                new Tuple<DateTimeOffsetType, string>(new DateTimeOffsetType() { DateTimeOffset = new DateTimeOffset(new DateTime(2005, 3, 14, 12, 34, 16, DateTimeKind.Local)).ToLocalTime() }, "{\"DateTimeOffset\":\"2005-03-14T20:34:16.000Z\"}"),
                new Tuple<DateTimeOffsetType, string>(new DateTimeOffsetType() { DateTimeOffset = new DateTimeOffset(new DateTime(2005, 4, 14, 12, 34, 16, DateTimeKind.Unspecified)).ToLocalTime() }, "{\"DateTimeOffset\":\"2005-04-14T19:34:16.000Z\"}"),
                new Tuple<DateTimeOffsetType, string>(new DateTimeOffsetType() { DateTimeOffset = new DateTimeOffset(new DateTime(2005, 3, 14, 12, 34, 16, DateTimeKind.Local)).ToLocalTime() }, "{\"DateTimeOffset\":\"2005-03-14T20:34:16.000Z\"}"),
                new Tuple<DateTimeOffsetType, string>(new DateTimeOffsetType() { DateTimeOffset = new DateTimeOffset(new DateTime(2005, 5, 14, 12, 34, 16, DateTimeKind.Utc)).ToLocalTime() }, "{\"DateTimeOffset\":\"2005-05-14T12:34:16.000Z\"}"),
                new Tuple<DateTimeOffsetType, string>(new DateTimeOffsetType() { DateTimeOffset = new DateTimeOffset(new DateTime(2012, 2, 29, 12, 0, 0, DateTimeKind.Utc)).ToLocalTime() }, "{\"DateTimeOffset\":\"2012-02-29T12:00:00.000Z\"}"), // Leap Day
            };

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

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

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

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

                if (testCase.Item2 != "{}")
                {
                    actual = new DateTimeOffsetType();
                    actual.DateTimeOffset = DateTimeOffset.Now;
                    DefaultSerializer.Deserialize(input, actual);
                }

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

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

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

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

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