I have this simple JsonFormatter:
public sealed class Int64StringConversionFormatter : IJsonFormatter<long> {
public void Serialize(ref JsonWriter writer, long value, IJsonFormatterResolver formatterResolver) {
writer.WriteString(value.ToString(NumberFormatInfo.InvariantInfo));
}
public long Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver) {
var token = reader.GetCurrentJsonToken();
if (token == JsonToken.String) {
var s = reader.ReadString();
return
long.TryParse(s, NumberStyles.Any, NumberFormatInfo.InvariantInfo, out var l)
? l
: 0;
}
if (token != JsonToken.Number)
throw new ValueProviderException("The provided value is not String or Int64.");
var value = reader.ReadInt64();
return value;
}
}
which is an implementation of Utf8Json.IJsonFormatter<>
for working with long
values. I've added this formatter to an AspNetCore WebApi application like this:
public static MvcOptions SetupCustomJsonFormatter(
this MvcOptions options) {
CompositeResolver.RegisterAndSetAsDefault(_formatters, _resolvers);
options.InputFormatters.Insert(0, new Utf8JsonInputFormatter());
options.OutputFormatters.Insert(0, new Utf8JsonOutputFormatter());
return options;
}
And here is my _formatters
and _resolvers
:
static readonly IJsonFormatterResolver[] resolvers = {
StandardResolver.ExcludeNullCamelCase,
ImmutableCollectionResolver.Instance,
EnumResolver.Default,
DynamicGenericResolver.Instance,
};
static readonly IJsonFormatter[] _formatters
= new [] {new Int64StringConversionFormatter()};
Also, here is my Utf8JsonInputFormatter
:
internal sealed class Utf8JsonInputFormatter : IInputFormatter {
private readonly IJsonFormatterResolver _resolver;
public Utf8JsonInputFormatter() : this(null) {
}
public Utf8JsonInputFormatter(IJsonFormatterResolver resolver) {
_resolver = resolver ?? JsonSerializer.DefaultResolver;
}
public bool CanRead(InputFormatterContext context)
=> context.HttpContext.Request.ContentType?.StartsWith("application/json") == true;
public async Task<InputFormatterResult> ReadAsync(InputFormatterContext context) {
var request = context.HttpContext.Request;
if (request.Body.CanSeek && request.Body.Length == 0) return await InputFormatterResult.NoValueAsync();
var result = await JsonSerializer.NonGeneric.DeserializeAsync(context.ModelType, request.Body, _resolver);
return await InputFormatterResult.SuccessAsync(result);
}
}
Everything seems to should be OK. But Int64StringConversionFormatter.Serialize
and Int64StringConversionFormatter.Deserializ
methods never get called. I tested the configuration with another simple formatter (say UnixDateTimeFormatter
) and it works just fine. But I cannot figure it out why this one isn't getting called. Do you have any idea what am I missing here?