Open amiry-jd opened 4 years ago
I also encountered a similar problem (I wanted to make it so int64s are serialized as strings and could be deserialized from either a number or a string). However, the formatter was ignored. Checking into the source of the library, I found the problem in https://github.com/neuecc/Utf8Json/blob/master/src/Utf8Json/Resolvers/DynamicObjectResolver.cs, line 507. It looks like there is a list of primitive types that won't call formatters and instead just write directly. I was able to work around this by creating a resolver that, as part of its constructor, uses reflection to locate the static Hashset jsonPrimitives in the type Utf8Json.Resolvers.Internal.DynamicObjectTypeBuilder and remove the entries that I needed to provide custom formatters for. This proved to do the trick and got utf8json to start using my formatter for int64.
I have this simple JsonFormatter:
which is an implementation of
Utf8Json.IJsonFormatter<>
for working withlong
values. I've added this formatter to an AspNetCore WebApi application like this:And here is my
_formatters
and_resolvers
:Also, here is my
Utf8JsonInputFormatter
:Everything seems to should be OK. But
Int64StringConversionFormatter.Serialize
andInt64StringConversionFormatter.Deserializ
methods never get called. I tested the configuration with another simple formatter (sayUnixDateTimeFormatter
) 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?P.S. I also asked the question on stack overflow, but so far nothing.