public void WriteJsonGeneric() { StringWriter sw = new StringWriter(); JsonTextWriter jsonWriter = new JsonTextWriter(sw); TestGenericConverter converter = new TestGenericConverter(); converter.WriteJson(jsonWriter, "String!", null); Assert.AreEqual(@"""String!""", sw.ToString()); }
public void WriteJsonBadType() { StringWriter sw = new StringWriter(); JsonTextWriter jsonWriter = new JsonTextWriter(sw); TestGenericConverter converter = new TestGenericConverter(); ExceptionAssert.Throws <JsonSerializationException>(() => { converter.WriteJson(jsonWriter, 123, null); }, "Converter cannot write specified value to JSON. System.String is required."); }
public void ReadJsonObjectExistingValueNull() { StringReader sr = new StringReader("'String!'"); JsonTextReader jsonReader = new JsonTextReader(sr); jsonReader.Read(); TestGenericConverter converter = new TestGenericConverter(); string s = (string)converter.ReadJson(jsonReader, typeof(string), null, null); Assert.AreEqual(@"String!", s); }
public void ReadJsonGenericExistingValueString() { StringReader sr = new StringReader("'String!'"); JsonTextReader jsonReader = new JsonTextReader(sr); jsonReader.Read(); TestGenericConverter converter = new TestGenericConverter(); string s = converter.ReadJson(jsonReader, typeof(string), "Existing!", true, null); Assert.AreEqual(@"String!Existing!", s); }
public void ReadJsonObjectExistingValueWrongType() { StringReader sr = new StringReader("'String!'"); JsonTextReader jsonReader = new JsonTextReader(sr); jsonReader.Read(); TestGenericConverter converter = new TestGenericConverter(); ExceptionAssert.Throws <JsonSerializationException>(() => { converter.ReadJson(jsonReader, typeof(string), 12345, null); }, "Converter cannot read JSON with the specified existing value. System.String is required."); }