1

Is is possible to dynamically deserialize my enum string into enums.

In my API and my application that consumes my API i have this class.

 public static class ErrorCodes
    {
        public enum General
        {
            INCORRECT_PASSWORD,
            INCORRECT_USERNAME,
            INVALID_USERNAME
        }

        public enum ERRORTYPE_1
        {
            DATESPAN_NOT_PERMITTED
        }

        public enum ERRORTYPE_2
        {
            PERIOD_NOT_ALLOWED
        }
    }

In my API i have this. This is what is send back to my application in JSON form.

public class Error
{

    public string Message { get; set; }
    public Enum Code { get; set; }

    public Error() { }

    public Error(Enum code)
    {
        this.Code = code;
    }

    public Error(string message, Enum code)
    {
        this.Message = message;
        this.Code = code;
    }
}

I can then return my error like this.

new Error(ErrorCodes.ERRORTYPE_2.PERIOD_NOT_ALLOWED)

Is there a way to keep my enums seperate? I feel like a super long list of enum error codes is not very clean or maintainable.

This blog post is pretty much what i want to do but with more abstraction of enums. https://bytefish.de/blog/enums_json_net/

Zapcrack
  • 25
  • 8
  • It's not clear what you're expecting to happen here. What are you expecting the JSON to look like? – Heretic Monkey May 12 '20 at 14:25
  • I want to deserialize the string json into the appropriate enum type on the client side. I am passing the string value of the enum and the string message in the Error object as JSON. – Zapcrack May 12 '20 at 14:29
  • Can the set of possible enums have duplicate value names `INCORRECT_PASSWORD`, `DATESPAN_NOT_PERMITTED`, ..., , or are you sure they are unique? – dbc May 12 '20 at 14:39
  • Does it matter? For now they are unique. – Zapcrack May 12 '20 at 14:57
  • If they are not unique you will have no way of deserializing the JSON just from the enum value string. Or if you are willing to encapsulate the enum type information, see e.g. [Deserialize specific enum into system.enum in Json.Net](https://stackoverflow.com/q/31351262/3744182). – dbc May 12 '20 at 15:15

2 Answers2

0

Is is possible to dynamically deserialize my enum string into enums?

If you are building a Web API project on dotnet-core, the solution in the blog you linked to works great, although I would use the standard System.Text.Json library (since core 3.0):

using System.Text.Json.Serialization;

namespace Foobar
{
    [JsonConverter(typeof(JsonStringEnumConverter))]
    public enum Foo
    {
        Bar = 0,
        Baz = 1
    }
}

This will convert the enum Foo.Bar into "Bar" on the client, and "Bar" -> Foo.Bar on the server.

Is there a way to keep my enums separate? I feel like a super long list of enum error codes is not very clean or maintainable.

I don't know if I can answer this part yet, because it looks like you've already done that with your class structure. I am confused about whether the code you gave is your goal or your current code.


For more on serialization and deserialization of enums, check out this post.

Connor Low
  • 5,900
  • 3
  • 31
  • 52
0

When your Enum values are deserialized by the JSON Converter and sent back to the application, the enum values are converted to the Int32 type. To be able to convert an int or string value to the Enum type, you should know the type of enum you want to convert/deserialize the value to. So you either need to declare all the ErrorCodes as part of a single Enum type (that way you know the target enum type), or include the typeof enum value in the response, so that can do Enum.Parse(typeof(TargetEnumType),enumValue). If you are using C# 8.0, the latter approach can be achieved by updating your Error class as below

public class Error<TEnum> where TEnum:Enum
{

    public string Message { get; set; }
    public TEnum Code { get; set; }


    public Type GetEnumType()
    {
        return Code?.GetType();
    }

    public Error() { }

    public Error(TEnum code)
    {
        this.Code = code;
    }

    public Error(string message, TEnum code)
    {
        this.Message = message;
        this.Code = code;
    }
}
zafar
  • 1,965
  • 1
  • 15
  • 14