-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enum Converters - Unknown Value Handling #1361
Comments
I ended up hitting a very similar scenario recently. I have a web service that returns a json containing an array of strings, and each of those strings corresponds to an enum value. However, the client program that consumes that API endpoint only needs to know a small subset of those strings, which are conveniently represented as enums in my code (using the My current solution is to use a class called The code for the https://gist.github.com/gubenkoved/999eb73e227b7063a67a50401578c3a7 I agree with you that using this is only a stopgap, it's pretty overkill since it mostly just uses code from In order for this functionality to be built into Json.Net, I think If the enum is nullable, the then If any of the enums are named "Unknown", they are used as the default. Otherwise, the first enum is used as the default. This should be modified to also take into account and prioritise the Another thing: How would the |
If I understand what you're trying to do correctly, this is already possible by putting the
The |
Any updates on this issue? Is it even considered? Still have to inherit from StringEnumConverter to support DefaultValue. |
Any updates? It's quite useful for me. If being able, I will implement this while solving my problem here at #1597 |
Hi Newtonsoft,
I'm not sure if this is a bug or a feature request, but here goes:
I am building an app that connects to a third-party service. In lots of places, the service returns string constants that can better be represented as Enums. New values aren't added often, but they are added on occasion, and when that happens, I don't want my code to start crashing. I want to just be able to deserialize the unknown enum as some other value.
I've tried using DefaultValue and other methods to no avail. StackOverflow seems to indicate that you have to roll your own converter to make this happen but that seems like overkill.
I've noticed that JsonSerializerSettings has a lot of properties that control how different types are deserialized but none have anything to do with Enums.
Perhaps I'm doing something wrong, but if not, I propose adding the following member:
JsonSerializerSettings.UnknownEnumValueHandling with the options of:
ThrowException - The current default behavior - an exception is thrown.
Ignore - the enum value is ignored.
UseDefault - The enum's default value is used. This should pick up the value from a DefaultValue attribute or alternately, perhaps a new UnknownValue attribute that can be applied to members or enums.
It would also be great if it could convert Enums that have underscores in the values. For example,
in_progress (string) -> Status.InProgress (enum)
Below is the code that gives a small example of the problem.
Source/destination types
The text was updated successfully, but these errors were encountered: