public void CharDeserializationNegative() { List<Tuple<string, string>> testCases = new List<Tuple<string, string>>() { new Tuple<string, string>("{\"Char\":\"I'm not a char\"}", "Error converting value \"I'm not a char\" to type 'System.Char'. Path 'Char', line 1, position 24."), new Tuple<string, string>("{\"Char\":true}","Error converting value True to type 'System.Char'. Path 'Char', line 1, position 12.") }; foreach (var testCase in testCases) { var input = testCase.Item1; var expectedError = testCase.Item2; CharType actual = new CharType(); 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(CharType)); DefaultSerializer.Deserialize(input, actual); } catch (Exception e) { actualError = e; } Assert.AreEqual(actualError.Message, expectedError); } }
public void CharDeserialization() { List<Tuple<CharType, string>> testCases = new List<Tuple<CharType, string>>() { new Tuple<CharType, string>(new CharType() { Char = (char)0 }, "{}"), new Tuple<CharType, string>(new CharType() { Char = (char)0 }, "{\"Char\":null}"), new Tuple<CharType, string>(new CharType() { Char = (char)0 }, "{\"Char\":\"\\u0000\"}"), new Tuple<CharType, string>(new CharType() { Char = (char)1 }, "{\"Char\":\"\\u0001\"}"), new Tuple<CharType, string>(new CharType() { Char = (char)5 }, "{\"Char\":5}"), new Tuple<CharType, string>(new CharType() { Char = ' ' }, "{\"Char\":\" \"}"), new Tuple<CharType, string>(new CharType() { Char = '\n' }, "{\"Char\":\"\\n\"}"), new Tuple<CharType, string>(new CharType() { Char = '\t' }, "{\"Char\":\"\\t\"}"), new Tuple<CharType, string>(new CharType() { Char = '\n' }, "{\"Char\":\"\n\"}"), new Tuple<CharType, string>(new CharType() { Char = '\t' }, "{\"Char\":\"\t\"}"), new Tuple<CharType, string>(new CharType() { Char = '?' }, "{\"Char\":\"?\"}"), new Tuple<CharType, string>(new CharType() { Char = '\u1000' }, "{\"Char\":\"က\"}"), new Tuple<CharType, string>(new CharType() { Char = 'Ã' }, "{\"Char\":\"Ã\"}"), }; // Need to ensure that the type is registered as a table to force the id property check DefaultSerializer.SerializerSettings.ContractResolver.ResolveTableName(typeof(CharType)); foreach (var testCase in testCases) { var input = JToken.Parse(testCase.Item2); var expected = testCase.Item1; CharType actual = new CharType(); DefaultSerializer.Deserialize(input, actual); Assert.AreEqual(actual.Char, expected.Char); if (testCase.Item2 != "{}") { actual = new CharType(); actual.Char = 'a'; DefaultSerializer.Deserialize(input, actual); } Assert.AreEqual(actual.Char, expected.Char); JArray json = JToken.Parse("[" + testCase.Item2 + "]") as JArray; actual = DefaultSerializer.Deserialize<CharType>(json).FirstOrDefault(); Assert.AreEqual(actual.Char, expected.Char); actual = (CharType)DefaultSerializer.Deserialize<CharType>(input); Assert.AreEqual(actual.Char, expected.Char); } }