简体   繁体   中英

Deserializing Enums C#

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/

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 .

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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM