Use System.Text.Json number handling for long values
This commit is contained in:
@@ -1,7 +1,6 @@
|
|||||||
using System.Text.Json.Serialization;
|
using System.Text.Json.Serialization;
|
||||||
using Musify.Api.Endpoints;
|
using Musify.Api.Endpoints;
|
||||||
using Musify.Api.Extensions;
|
using Musify.Api.Extensions;
|
||||||
using Musify.Api.Serialization;
|
|
||||||
using Musify.Infrastructure.Observability;
|
using Musify.Infrastructure.Observability;
|
||||||
using Scalar.AspNetCore;
|
using Scalar.AspNetCore;
|
||||||
|
|
||||||
@@ -12,9 +11,10 @@ var builder = WebApplication.CreateBuilder(args);
|
|||||||
builder.Services.ConfigureHttpJsonOptions(options =>
|
builder.Services.ConfigureHttpJsonOptions(options =>
|
||||||
{
|
{
|
||||||
options.SerializerOptions.Converters.Add(new JsonStringEnumConverter());
|
options.SerializerOptions.Converters.Add(new JsonStringEnumConverter());
|
||||||
options.SerializerOptions.Converters.Add(new LongAsStringConverter());
|
options.SerializerOptions.NumberHandling =
|
||||||
options.SerializerOptions.Converters.Add(new NullableLongAsStringConverter());
|
JsonNumberHandling.AllowReadingFromString |
|
||||||
});
|
JsonNumberHandling.WriteAsString;
|
||||||
|
} );
|
||||||
|
|
||||||
builder.Services
|
builder.Services
|
||||||
.AddApplicationServices(builder.Configuration)
|
.AddApplicationServices(builder.Configuration)
|
||||||
|
|||||||
@@ -1,37 +0,0 @@
|
|||||||
using System.Globalization;
|
|
||||||
using System.Text.Json;
|
|
||||||
using System.Text.Json.Serialization;
|
|
||||||
|
|
||||||
namespace Musify.Api.Serialization;
|
|
||||||
|
|
||||||
public sealed class LongAsStringConverter : JsonConverter<long>
|
|
||||||
{
|
|
||||||
public override long Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) =>
|
|
||||||
reader.TokenType == JsonTokenType.String
|
|
||||||
? long.Parse(reader.GetString()!, CultureInfo.InvariantCulture)
|
|
||||||
: reader.GetInt64();
|
|
||||||
|
|
||||||
public override void Write(Utf8JsonWriter writer, long value, JsonSerializerOptions options) =>
|
|
||||||
writer.WriteStringValue(value.ToString(CultureInfo.InvariantCulture));
|
|
||||||
}
|
|
||||||
|
|
||||||
public sealed class NullableLongAsStringConverter : JsonConverter<long?>
|
|
||||||
{
|
|
||||||
public override long? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
|
||||||
{
|
|
||||||
if (reader.TokenType == JsonTokenType.Null)
|
|
||||||
return null;
|
|
||||||
|
|
||||||
return reader.TokenType == JsonTokenType.String
|
|
||||||
? string.IsNullOrEmpty(reader.GetString()) ? null : long.Parse(reader.GetString()!, CultureInfo.InvariantCulture)
|
|
||||||
: reader.GetInt64();
|
|
||||||
}
|
|
||||||
|
|
||||||
public override void Write(Utf8JsonWriter writer, long? value, JsonSerializerOptions options)
|
|
||||||
{
|
|
||||||
if (value.HasValue)
|
|
||||||
writer.WriteStringValue(value.Value.ToString(CultureInfo.InvariantCulture));
|
|
||||||
else
|
|
||||||
writer.WriteNullValue();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user