示例#1
0
        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);
        }
示例#2
0
        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);
        }
示例#3
0
        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);
        }