Mapping outgoing enums to a specific shape in Asp.net Core [Answered]
Hello!
I'm trying to find a way to take any outgoing enums in DTOs from an ASP.NET Core API, and map them into a specific shape before they head down the wire. I have an
Enumeration
class that's used by an existing mapper to map enum values to.
I'm using
I've tried looking at implementing a JsonConverter
and it almost works, but falls down when it encounters a nullable enum. There's a built in JsonEnumStringConverter
but that only maps the enum to it's corresponding string.
Current output example from an API endpoint:
Expected output example:
I hope that makes sense? I think I need to find a way to handle the nullable enum situation (or there's an easier way and i'm just going about this all wrong). It's an annoying requirement of this API migration that i'm doing5 Replies
This is as far as I got with the JsonConverter implementation that almost works:
Why not just use the enumeration class, and ignore the enum entirely?
it's a huge legacy project with thousands of things depending on enums being the correct type in the DTOs, would take forever to change everything
i think i've resolved it by convincing it to use
AddNewtonsoftJson()
and using the converter we originally hadUnknown User•3y ago
Message Not Public
Sign In & Join Server To View
✅ This post has been marked as answered!