public void NullableDeserialization() { List<Tuple<NullableType, string>> testCases = new List<Tuple<NullableType, string>>() { new Tuple<NullableType, string>(new NullableType() { Nullable = null }, "{\"Nullable\":null}"), new Tuple<NullableType, string>(new NullableType() { Nullable = null }, "{}"), new Tuple<NullableType, string>(new NullableType() { Nullable = 0 }, "{\"Nullable\":0}"), new Tuple<NullableType, string>(new NullableType() { Nullable = 0 }, "{\"Nullable\":4.94065645841247E-325}"), new Tuple<NullableType, string>(new NullableType() { Nullable = 0 }, "{\"Nullable\":4.94065645841247E-325}"), new Tuple<NullableType, string>(new NullableType() { Nullable = -1 }, "{\"Nullable\":-1}"), new Tuple<NullableType, string>(new NullableType() { Nullable = 1 }, "{\"Nullable\":1}"), new Tuple<NullableType, string>(new NullableType() { Nullable = double.Epsilon }, "{\"Nullable\":4.94065645841247E-324}"), new Tuple<NullableType, string>(new NullableType() { Nullable = -double.Epsilon }, "{\"Nullable\":-4.94065645841247E-324}"), new Tuple<NullableType, string>(new NullableType() { Nullable = double.MaxValue }, "{\"Nullable\":1.7976931348623157E+308}"), new Tuple<NullableType, string>(new NullableType() { Nullable = double.MinValue }, "{\"Nullable\":-1.7976931348623157E+308}"), new Tuple<NullableType, string>(new NullableType() { Nullable = double.MaxValue }, "{\"Nullable\":1.79769313486231575E+308}"), // We're ok with lossing precision here; note the extra 5 digit at the end new Tuple<NullableType, string>(new NullableType() { Nullable = double.MinValue }, "{\"Nullable\":-1.79769313486231575E+308}"), // We're ok with lossing precision here; note the extra 5 digit at the end }; // Need to ensure that the type is registered as a table to force the id property check DefaultSerializer.SerializerSettings.ContractResolver.ResolveTableName(typeof(NullableType)); foreach (var testCase in testCases) { var input = JToken.Parse(testCase.Item2); var expected = testCase.Item1; NullableType actual = new NullableType(); DefaultSerializer.Deserialize(input, actual); if (actual.Nullable.HasValue) { if (double.IsPositiveInfinity(expected.Nullable.Value)) { Assert.IsTrue(double.IsPositiveInfinity(actual.Nullable.Value)); } else if (double.IsNegativeInfinity(expected.Nullable.Value)) { Assert.IsTrue(double.IsNegativeInfinity(actual.Nullable.Value)); } else { Assert.AreEqual(actual.Nullable.Value, expected.Nullable.Value); } } if (testCase.Item2 != "{}") { actual = new NullableType(); actual.Nullable = 34.6; DefaultSerializer.Deserialize(input, actual); if (actual.Nullable.HasValue) { if (double.IsPositiveInfinity(expected.Nullable.Value)) { Assert.IsTrue(double.IsPositiveInfinity(actual.Nullable.Value)); } else if (double.IsNegativeInfinity(expected.Nullable.Value)) { Assert.IsTrue(double.IsNegativeInfinity(actual.Nullable.Value)); } else { Assert.AreEqual(actual.Nullable.Value, expected.Nullable.Value); } } } JArray json = JToken.Parse("[" + testCase.Item2 + "]") as JArray; actual = DefaultSerializer.Deserialize<NullableType>(json).FirstOrDefault(); if (actual.Nullable.HasValue) { if (double.IsPositiveInfinity(expected.Nullable.Value)) { Assert.IsTrue(double.IsPositiveInfinity(actual.Nullable.Value)); } else if (double.IsNegativeInfinity(expected.Nullable.Value)) { Assert.IsTrue(double.IsNegativeInfinity(actual.Nullable.Value)); } else { Assert.AreEqual(actual.Nullable.Value, expected.Nullable.Value); } } actual = DefaultSerializer.Deserialize<NullableType>(input); if (actual.Nullable.HasValue) { if (double.IsPositiveInfinity(expected.Nullable.Value)) { Assert.IsTrue(double.IsPositiveInfinity(actual.Nullable.Value)); } else if (double.IsNegativeInfinity(expected.Nullable.Value)) { Assert.IsTrue(double.IsNegativeInfinity(actual.Nullable.Value)); } else { Assert.AreEqual(actual.Nullable.Value, expected.Nullable.Value); } } } }
public void NullableDeserializationNegative() { List<string> testCases = new List<string>() { "{\"Nullable\":1.7976931348623157E+309}", "{\"Nullable\":-1.7976931348623157E+309}", }; foreach (var testCase in testCases) { var input = testCase; NullableType actual = new NullableType(); Exception actualError = null; try { // Need to ensure that the type is registered as a table to force the id property check DefaultSerializer.SerializerSettings.ContractResolver.ResolveTableName(typeof(NullableType)); DefaultSerializer.Deserialize(input, actual); } catch (Exception e) { actualError = e; } Assert.Contains(actualError.Message, "is not a valid number. Path 'Nullable'"); } }
public void NullableDeserializationNegative() { List<string> testCases = new List<string>() { "{\"Nullable\":1.7976931348623157E+309}", "{\"Nullable\":-1.7976931348623157E+309}", }; // Need to ensure that the type is registered as a table to force the id property check DefaultSerializer.SerializerSettings.ContractResolver.ResolveTableName(typeof(NullableType)); foreach (var testCase in testCases) { var input = testCase; NullableType actual = new NullableType(); Exception actualError = null; try { DefaultSerializer.Deserialize(input, actual); } catch (Exception e) { actualError = e; } Assert.AreEqual(actualError.Message, "Value was either too large or too small for a Double."); } }