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