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