public void UIntDeserializationNegative() { List<Tuple<string, string>> testCases = new List<Tuple<string, string>>() { new Tuple<string, string>("{\"UInt\":4294967296}", "Error converting value 4294967296 to type 'System.UInt32'. Path 'UInt', line 1, position 18."), new Tuple<string, string>("{\"UInt\":\"4294967296\"}", "Error converting value \"4294967296\" to type 'System.UInt32'. Path 'UInt', line 1, position 20."), new Tuple<string, string>("{\"UInt\":-1}", "Error converting value -1 to type 'System.UInt32'. Path 'UInt', line 1, position 10."), }; foreach (var testCase in testCases) { var input = testCase.Item1; var expected = testCase.Item2; UIntType actual = new UIntType(); 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(UIntType)); DefaultSerializer.Deserialize(input, actual); } catch (Exception e) { actualError = e; } Assert.AreEqual(actualError.Message, expected); } }
public void UIntDeserialization() { List<Tuple<UIntType, string>> testCases = new List<Tuple<UIntType, string>>() { new Tuple<UIntType, string>(new UIntType() { UInt = 0 }, "{}"), new Tuple<UIntType, string>(new UIntType() { UInt = 0 }, "{\"UInt\":null}"), new Tuple<UIntType, string>(new UIntType() { UInt = 0 }, "{\"UInt\":0}"), new Tuple<UIntType, string>(new UIntType() { UInt = 1 }, "{\"UInt\":1}"), new Tuple<UIntType, string>(new UIntType() { UInt = 0 }, "{\"UInt\":false}"), new Tuple<UIntType, string>(new UIntType() { UInt = 1 }, "{\"UInt\":true}"), new Tuple<UIntType, string>(new UIntType() { UInt = 150 }, "{\"UInt\":\"150\"}"), new Tuple<UIntType, string>(new UIntType() { UInt = UInt32.MaxValue }, "{\"UInt\":4294967295}"), new Tuple<UIntType, string>(new UIntType() { UInt = UInt32.MaxValue }, "{\"UInt\":\"4294967295\"}"), }; // Need to ensure that the type is registered as a table to force the id property check DefaultSerializer.SerializerSettings.ContractResolver.ResolveTableName(typeof(UIntType)); foreach (var testCase in testCases) { var input = JToken.Parse(testCase.Item2); var expected = testCase.Item1; UIntType actual = new UIntType(); DefaultSerializer.Deserialize(input, actual); Assert.AreEqual(actual.UInt, expected.UInt); if (testCase.Item2 != "{}") { actual = new UIntType(); actual.UInt = 12; DefaultSerializer.Deserialize(input, actual); } Assert.AreEqual(actual.UInt, expected.UInt); JArray json = JToken.Parse("[" + testCase.Item2 + "]") as JArray; actual = DefaultSerializer.Deserialize<UIntType>(json).FirstOrDefault(); Assert.AreEqual(actual.UInt, expected.UInt); actual = (UIntType)DefaultSerializer.Deserialize<UIntType>(input); Assert.AreEqual(actual.UInt, expected.UInt); } }