public void BoolDeserializationNegative() { List<string> testCases = new List<string>() { "{\"Bool\":\"I can't be parsed\"}" }; foreach (var testCase in testCases) { var input = JToken.Parse(testCase); BoolType actual = new BoolType(); 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(BoolType)); DefaultSerializer.Deserialize(input, actual); } catch (Exception e) { actualError = e; } Assert.AreEqual(actualError.Message, "Error converting value \"I can't be parsed\" to type 'System.Boolean'. Path 'Bool', line 2, position 30."); } }
public void BoolDeserialization() { List<Tuple<BoolType, string>> testCases = new List<Tuple<BoolType, string>>() { new Tuple<BoolType, string>(new BoolType() { Bool = true }, "{\"Bool\":true}"), new Tuple<BoolType, string>(new BoolType() { Bool = true }, "{\"Bool\":\"true\"}"), new Tuple<BoolType, string>(new BoolType() { Bool = true }, "{\"Bool\":1}"), new Tuple<BoolType, string>(new BoolType() { Bool = false }, "{}"), new Tuple<BoolType, string>(new BoolType() { Bool = false }, "{\"Bool\":false}"), new Tuple<BoolType, string>(new BoolType() { Bool = false }, "{\"Bool\":\"false\"}"), new Tuple<BoolType, string>(new BoolType() { Bool = false }, "{\"Bool\":0}"), }; // Need to ensure that the type is registered as a table to force the id property check DefaultSerializer.SerializerSettings.ContractResolver.ResolveTableName(typeof(BoolType)); foreach (var testCase in testCases) { var input = JToken.Parse(testCase.Item2); var expected = testCase.Item1; BoolType actual = new BoolType(); DefaultSerializer.Deserialize(input, actual); Assert.AreEqual(actual.Bool, expected.Bool); actual = new BoolType(); actual.Bool = false; DefaultSerializer.Deserialize(input, actual); Assert.AreEqual(actual.Bool, expected.Bool); JArray json = JToken.Parse("[" + input + "]") as JArray; actual = DefaultSerializer.Deserialize<BoolType>(json).FirstOrDefault(); Assert.AreEqual(actual.Bool, expected.Bool); actual = (BoolType)DefaultSerializer.Deserialize<BoolType>(input); Assert.AreEqual(actual.Bool, expected.Bool); } }