Add JsonInto32Converter

Add additional swagger type mapping
This commit is contained in:
David 2020-06-19 12:24:39 +02:00
parent 77bea56708
commit 6651cb8d24
3 changed files with 27 additions and 26 deletions

View File

@ -215,6 +215,19 @@ namespace Jellyfin.Server.Extensions
Format = "string"
})
});
options.MapType<Dictionary<ImageType, Dictionary<string, string>>>(() =>
new OpenApiSchema
{
Type = "object",
Properties = typeof(ImageType).GetEnumNames().ToDictionary(
name => name,
name => new OpenApiSchema
{
Type = "string",
Format = "string"
})
});
}
}
}

View File

@ -14,40 +14,27 @@ namespace MediaBrowser.Common.Json.Converters
/// <inheritdoc />
public override int Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
static void ThrowFormatException() => throw new FormatException("Invalid format for an integer.");
ReadOnlySpan<byte> span = stackalloc byte[0];
if (reader.TokenType == JsonTokenType.String)
{
ReadOnlySpan<byte> span = reader.HasValueSequence ? reader.ValueSequence.ToArray() : reader.ValueSpan;
if (Utf8Parser.TryParse(span, out int number, out int bytesConsumed) && span.Length == bytesConsumed)
{
return number;
}
if (reader.HasValueSequence)
{
long sequenceLength = reader.ValueSequence.Length;
Span<byte> stackSpan = stackalloc byte[(int)sequenceLength];
reader.ValueSequence.CopyTo(stackSpan);
span = stackSpan;
}
else
{
span = reader.ValueSpan;
if (int.TryParse(reader.GetString(), out number))
{
return number;
}
}
if (!Utf8Parser.TryParse(span, out int number, out _))
{
ThrowFormatException();
}
return number;
return reader.GetInt32();
}
/// <inheritdoc />
public override void Write(Utf8JsonWriter writer, int value, JsonSerializerOptions options)
{
static void ThrowInvalidOperationException() => throw new InvalidOperationException();
Span<byte> span = stackalloc byte[16];
if (Utf8Formatter.TryFormat(value, span, out int bytesWritten))
{
writer.WriteStringValue(span.Slice(0, bytesWritten));
}
ThrowInvalidOperationException();
writer.WriteNumberValue(value);
}
}
}

View File

@ -28,6 +28,7 @@ namespace MediaBrowser.Common.Json
};
options.Converters.Add(new JsonGuidConverter());
options.Converters.Add(new JsonInt32Converter());
options.Converters.Add(new JsonStringEnumConverter());
options.Converters.Add(new JsonNonStringKeyDictionaryConverterFactory());