public void Write_Object() { TestType value = new TestType { StringProperty = "string value", IntProperty = 72, AdditionalProperty = "Not part of the interface" }; // This is horrible, but we need a JSON comparer. string expected = "{\"IntProperty\":72,\"StringProperty\":\"string value\",\"AdditionalProperty\":\"Not part of the interface\"}"; JsonSerializerOptions serializerOptions = new JsonSerializerOptions(); using var stream = new MemoryStream(); using (var writer = new Utf8JsonWriter(stream, new JsonWriterOptions())) { InterfaceJsonConverter <ITestInterface> converter = new InterfaceJsonConverter <ITestInterface>(); converter.Write(writer, value, serializerOptions); } stream.Seek(0, SeekOrigin.Begin); // Reset stream to beginning. using StreamReader sr = new StreamReader(stream); string actual = sr.ReadToEnd(); Assert.Equal(expected, actual); }
public void Write_NullObject() { TestType value = null; JsonSerializerOptions serializerOptions = new JsonSerializerOptions(); using var stream = new MemoryStream(); using (var writer = new Utf8JsonWriter(stream, new JsonWriterOptions())) { InterfaceJsonConverter <ITestInterface> converter = new InterfaceJsonConverter <ITestInterface>(); converter.Write(writer, value, serializerOptions); } stream.Seek(0, SeekOrigin.Begin); // Reset stream to beginning. using StreamReader sr = new StreamReader(stream); string actual = sr.ReadToEnd(); Assert.Equal("null", actual); }
public void Read_ExpectFailure() { InterfaceJsonConverter <object> converter = new InterfaceJsonConverter <object>(); Utf8JsonReader reader = new Utf8JsonReader(); Type type = typeof(ITestInterface); JsonSerializerOptions options = new JsonSerializerOptions(); Exception actualEx = null; // This can't use Assert.Throws because refs can't be passed from within a lambda. try { converter.Read(ref reader, type, options); } catch (Exception ex) { actualEx = ex; } Assert.NotNull(actualEx); Assert.IsType <NotImplementedException>(actualEx); }