Пример #1
0
        private T AssertRoundtrip <T>(string json) where T : Geometry
        {
            var element  = JsonDocument.Parse(json).RootElement;
            var geometry = GeometryJsonConverter.Read(element);

            var memoryStreamOutput = new MemoryStream();

            using (Utf8JsonWriter writer = new Utf8JsonWriter(memoryStreamOutput))
            {
                GeometryJsonConverter.Write(writer, geometry);
            }

            var element2  = JsonDocument.Parse(memoryStreamOutput.ToArray()).RootElement;
            var geometry2 = GeometryJsonConverter.Read(element2);

            var options = new JsonSerializerOptions()
            {
                Converters = { new GeometryJsonConverter() }
            };

            // Serialize and deserialize as a base class
            var bytes     = JsonSerializer.SerializeToUtf8Bytes(geometry2, typeof(Geometry), options);
            var geometry3 = JsonSerializer.Deserialize <Geometry>(bytes, options);

            // Serialize and deserialize as a concrete class
            var bytes2    = JsonSerializer.SerializeToUtf8Bytes(geometry3, options);
            var geometry4 = JsonSerializer.Deserialize <T>(bytes2, options);

            return(geometry4);
        }
        private T AssertRoundtrip <T>(string json) where T : Geometry
        {
            var element  = JsonDocument.Parse(json).RootElement;
            var geometry = GeometryJsonConverter.Read(element);

            var memoryStreamOutput = new MemoryStream();

            using (Utf8JsonWriter writer = new Utf8JsonWriter(memoryStreamOutput))
            {
                GeometryJsonConverter.Write(writer, geometry);
            }

            var element2  = JsonDocument.Parse(memoryStreamOutput.ToArray()).RootElement;
            var geometry2 = GeometryJsonConverter.Read(element2);

            return((T)geometry2);
        }
Пример #3
0
        public void Read(string value, Geometry expectedResult, Type type)
        {
            // Arrange
            var options = new JsonSerializerOptions();

            var jsonUtf8Bytes = System.Text.Json.JsonSerializer.SerializeToUtf8Bytes(value, options);
            var reader        = new Utf8JsonReader(jsonUtf8Bytes);
            var converter     = new GeometryJsonConverter();

            while (reader.Read())
            {
                if (reader.TokenType == JsonTokenType.String)
                {
                    break;
                }
            }

            // Act
            var result = converter.Read(ref reader, type, options);

            // Assert
            Assert.Equal(expectedResult, result);
        }