Ejemplo n.º 1
0
        public void CanConvert()
        {
            // Arrange
            var converter = new Int32ToStringJsonConverter();

            // Act
            var result = converter.CanConvert(typeof(string));

            // Assert
            Assert.True(result);
        }
Ejemplo n.º 2
0
        public void Write(string value, string expectedResult)
        {
            // Arrange
            var options = new JsonSerializerOptions();

            using var stream = new MemoryStream();
            using var writer = new Utf8JsonWriter(stream);
            var converter = new Int32ToStringJsonConverter();

            writer.WriteStartObject();
            writer.WritePropertyName("test");

            // Act
            converter.Write(writer, value, options);

            // Assert
            writer.WriteEndObject();
            writer.Flush();
            var result = Encoding.UTF8.GetString(stream.ToArray());

            Assert.Equal(expectedResult, result);
        }
Ejemplo n.º 3
0
        public void Read(JsonTokenType type, object value, string expectedResult)
        {
            // Arrange
            var options = new JsonSerializerOptions();

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

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

            // Act
            var result = converter.Read(ref reader, value?.GetType(), options);

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