2021-03-12 06:43:57 -07:00
|
|
|
|
using System;
|
2021-03-13 08:41:16 -07:00
|
|
|
|
using System.Buffers;
|
2021-03-12 06:43:57 -07:00
|
|
|
|
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
|
2021-03-13 08:41:16 -07:00
|
|
|
|
? reader.ValueSequence.ToArray()
|
2021-03-12 06:43:57 -07:00
|
|
|
|
: reader.ValueSpan;
|
|
|
|
|
return Encoding.UTF8.GetString(utf8Bytes);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|