mirror of
				https://github.com/jellyfin/jellyfin.git
				synced 2025-11-04 03:27:21 -05:00 
			
		
		
		
	Merge pull request #5504 from crobibero/json-string-converter
This commit is contained in:
		
						commit
						1a0ce16f4d
					
				
							
								
								
									
										39
									
								
								MediaBrowser.Common/Json/Converters/JsonStringConverter.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										39
									
								
								MediaBrowser.Common/Json/Converters/JsonStringConverter.cs
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,39 @@
 | 
			
		||||
using System;
 | 
			
		||||
using System.Buffers;
 | 
			
		||||
using System.Text;
 | 
			
		||||
using System.Text.Json;
 | 
			
		||||
using System.Text.Json.Serialization;
 | 
			
		||||
 | 
			
		||||
namespace MediaBrowser.Common.Json.Converters
 | 
			
		||||
{
 | 
			
		||||
    /// <summary>
 | 
			
		||||
    /// Converter to allow the serializer to read strings.
 | 
			
		||||
    /// </summary>
 | 
			
		||||
    public class JsonStringConverter : JsonConverter<string>
 | 
			
		||||
    {
 | 
			
		||||
        /// <inheritdoc />
 | 
			
		||||
        public override string Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
 | 
			
		||||
        {
 | 
			
		||||
            return reader.TokenType switch
 | 
			
		||||
            {
 | 
			
		||||
                JsonTokenType.Null => null,
 | 
			
		||||
                JsonTokenType.String => reader.GetString(),
 | 
			
		||||
                _ => GetRawValue(reader)
 | 
			
		||||
            };
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        /// <inheritdoc />
 | 
			
		||||
        public override void Write(Utf8JsonWriter writer, string value, JsonSerializerOptions options)
 | 
			
		||||
        {
 | 
			
		||||
            writer.WriteStringValue(value);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private static string GetRawValue(Utf8JsonReader reader)
 | 
			
		||||
        {
 | 
			
		||||
            var utf8Bytes = reader.HasValueSequence
 | 
			
		||||
                ? reader.ValueSequence.ToArray()
 | 
			
		||||
                : reader.ValueSpan;
 | 
			
		||||
            return Encoding.UTF8.GetString(utf8Bytes);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@ -39,7 +39,8 @@ namespace MediaBrowser.Common.Json
 | 
			
		||||
                new JsonStringEnumConverter(),
 | 
			
		||||
                new JsonNullableStructConverterFactory(),
 | 
			
		||||
                new JsonBoolNumberConverter(),
 | 
			
		||||
                new JsonDateTimeConverter()
 | 
			
		||||
                new JsonDateTimeConverter(),
 | 
			
		||||
                new JsonStringConverter()
 | 
			
		||||
            }
 | 
			
		||||
        };
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										39
									
								
								tests/Jellyfin.Common.Tests/Json/JsonStringConverterTests.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										39
									
								
								tests/Jellyfin.Common.Tests/Json/JsonStringConverterTests.cs
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,39 @@
 | 
			
		||||
using System.Text.Json;
 | 
			
		||||
using MediaBrowser.Common.Json.Converters;
 | 
			
		||||
using Xunit;
 | 
			
		||||
 | 
			
		||||
namespace Jellyfin.Common.Tests.Json
 | 
			
		||||
{
 | 
			
		||||
    public class JsonStringConverterTests
 | 
			
		||||
    {
 | 
			
		||||
        private readonly JsonSerializerOptions _jsonSerializerOptions
 | 
			
		||||
            = new ()
 | 
			
		||||
            {
 | 
			
		||||
                Converters =
 | 
			
		||||
                {
 | 
			
		||||
                    new JsonStringConverter()
 | 
			
		||||
                }
 | 
			
		||||
            };
 | 
			
		||||
 | 
			
		||||
        [Theory]
 | 
			
		||||
        [InlineData("\"test\"", "test")]
 | 
			
		||||
        [InlineData("123", "123")]
 | 
			
		||||
        [InlineData("123.45", "123.45")]
 | 
			
		||||
        [InlineData("true", "true")]
 | 
			
		||||
        [InlineData("false", "false")]
 | 
			
		||||
        public void Deserialize_String_Valid_Success(string input, string output)
 | 
			
		||||
        {
 | 
			
		||||
            var deserialized = JsonSerializer.Deserialize<string>(input, _jsonSerializerOptions);
 | 
			
		||||
            Assert.Equal(deserialized, output);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void Deserialize_Int32asInt32_Valid_Success()
 | 
			
		||||
        {
 | 
			
		||||
            const string? input = "123";
 | 
			
		||||
            const int output = 123;
 | 
			
		||||
            var deserialized = JsonSerializer.Deserialize<int>(input, _jsonSerializerOptions);
 | 
			
		||||
            Assert.Equal(deserialized, output);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user