public void DecimalDeserializationNegative() { List<Tuple<string, string>> testCases = new List<Tuple<string, string>>() { new Tuple<string, string>("{\"Decimal\":-7.9228162514264338E+28}","Error converting value -7.92281625142643E+28 to type 'System.Decimal'. Path 'Decimal', line 1, position 34."), new Tuple<string, string>("{\"Decimal\":7.9228162514264338E+28}","Error converting value 7.92281625142643E+28 to type 'System.Decimal'. Path 'Decimal', line 1, position 33."), }; foreach (var testCase in testCases) { var input = testCase.Item1; var expected = testCase.Item2; DecimalType actual = new DecimalType(); 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(DecimalType)); DefaultSerializer.Deserialize(input, actual); } catch (Exception e) { actualError = e; } Assert.AreEqual(actualError.Message, expected); } }
public void DecimalDeserialization() { decimal twoToTheFifthyThird = 0x20000000000000; // 2^53 List<Tuple<DecimalType, string>> testCases = new List<Tuple<DecimalType, string>>() { new Tuple<DecimalType, string>(new DecimalType() { Decimal = 0 }, "{\"Decimal\":0}"), new Tuple<DecimalType, string>(new DecimalType() { Decimal = -1 }, "{\"Decimal\":-1}"), new Tuple<DecimalType, string>(new DecimalType() { Decimal = 1 }, "{\"Decimal\":1}"), new Tuple<DecimalType, string>(new DecimalType() { Decimal = 1 }, "{\"Decimal\":true}"), new Tuple<DecimalType, string>(new DecimalType() { Decimal = 0 }, "{\"Decimal\":false}"), new Tuple<DecimalType, string>(new DecimalType() { Decimal = twoToTheFifthyThird }, "{\"Decimal\":9007199254740992}"), new Tuple<DecimalType, string>(new DecimalType() { Decimal = -twoToTheFifthyThird }, "{\"Decimal\":-9007199254740992}"), new Tuple<DecimalType, string>(new DecimalType() { Decimal = twoToTheFifthyThird - 1 }, "{\"Decimal\":9007199254740991}"), new Tuple<DecimalType, string>(new DecimalType() { Decimal = -twoToTheFifthyThird + 1 }, "{\"Decimal\":-9007199254740991}"), new Tuple<DecimalType, string>(new DecimalType() { Decimal = twoToTheFifthyThird + 1 }, "{\"Decimal\":9007199254740993}"), new Tuple<DecimalType, string>(new DecimalType() { Decimal = -twoToTheFifthyThird - 1 }, "{\"Decimal\":-9007199254740993}"), new Tuple<DecimalType, string>(new DecimalType() { Decimal = 0 }, "{\"Decimal\":null}"), new Tuple<DecimalType, string>(new DecimalType() { Decimal = 5.00M }, "{\"Decimal\":\"5.00\"}"), }; // Need to ensure that the type is registered as a table to force the id property check DefaultSerializer.SerializerSettings.ContractResolver.ResolveTableName(typeof(DecimalType)); foreach (var testCase in testCases) { var input = JToken.Parse(testCase.Item2); var expected = testCase.Item1; DecimalType actual = new DecimalType(); DefaultSerializer.Deserialize(input, actual); Assert.AreEqual(actual.Decimal, expected.Decimal); if (testCase.Item2 != "{}") { actual = new DecimalType(); actual.Decimal = 34.5M; DefaultSerializer.Deserialize(input, actual); } Assert.AreEqual(actual.Decimal, expected.Decimal); JArray json = JToken.Parse("[" + testCase.Item2 + "]") as JArray; actual = DefaultSerializer.Deserialize<DecimalType>(json).FirstOrDefault(); Assert.AreEqual(actual.Decimal, expected.Decimal); actual = (DecimalType)DefaultSerializer.Deserialize<DecimalType>(input); Assert.AreEqual(actual.Decimal, expected.Decimal); } }