using System; using System.Text.Json; using System.Text.Json.Serialization; namespace MediaBrowser.Common.Json.Converters { /// /// Converts a nullable int32 object or value to/from JSON. /// Required - some clients send an empty string. /// public class JsonNullableInt32Converter : JsonConverter { private readonly JsonConverter _baseJsonConverter; /// /// Initializes a new instance of the class. /// /// The base json converter. public JsonNullableInt32Converter(JsonConverter baseJsonConverter) { _baseJsonConverter = baseJsonConverter; } /// public override int? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { if (reader.TokenType == JsonTokenType.String && ((reader.HasValueSequence && reader.ValueSequence.IsEmpty) || reader.ValueSpan.IsEmpty)) { return null; } return _baseJsonConverter.Read(ref reader, typeToConvert, options); } /// public override void Write(Utf8JsonWriter writer, int? value, JsonSerializerOptions options) { _baseJsonConverter.Write(writer, value, options); } } }