简体   繁体   English

将枚举列表定义为DTO C#类

[英]Defining List of Enum as DTO Class C#

This is my input JSON response 这是我输入的JSON响应

{
   "permissions": {
            "user": [
                "Add",
                "Update",
                "Delete"
            ],
            "Product": [
                "Read",
                "Create"
            ]
        }
}

I'm trying to create the DTO class for the same. 我正在尝试为此创建DTO类。 So far I've created a set of Enum with the available values 到目前为止,我已经创建了一组具有可用值的枚举

PermissionAction Enum PermissionAction枚举

 public enum PermissionAction
    {
        Add,
        Update,
        Read,
        Delete,
        Create
    }

PermissionItem Enum PermissionItem枚举

public enum PermissionItem
{
    User,
    Product
}

How to create a DTO Class for the given JSON Response with the available Enums? 如何使用可用的枚举为给定的JSON响应创建DTO类?

I tired like differently like 我累得像

public class Permissions
{

    public List<PermissionAction> User { get; set; }

    public List<PermissionItem> Product { get; set; }
}

Which is wrong. 哪有错

Since the response may vary dynamically. 由于响应可能会动态变化。 What I mean that is sometimes a User List will not available and sometimes Product is not available. 我的意思是有时User列表不可用,有时Product不可用。

Like, 喜欢,

{
   "permissions": {
            "user": [
                "Add",
                "Update",
                "Delete"
            ]
      }
}

I need to support those kinds of response also with my DTO Class. 我还需要在我的DTO课程中支持这些回应。

UPDATE 1: 更新1:

I think the JSON which I posted above is confusing others. 我认为我上面发布的JSON使其他人感到困惑。 Sorry for that confusion. 抱歉让您感到困惑。 Actually, my permissions JSON Object is contained List of Permission Item(s) in which each Permission Items itself again having a List of Permission Actions. 实际上,我的权限JSON对象包含在权限项目列表中,其中每个权限项目本身又具有一个权限动作列表。

It seems like you need to use binary or (ie | ) customizing the serialization. 似乎您需要使用二进制或(即| )自定义序列化。

Enum can already parse many names: Enum已经可以解析许多名称:

[Flags] // <--- Don't forget to apply this attribute if you want masks!
public enum A { X, Y }

Enum.TryParse<A>("X, Y", out var flags);

if(flags.HasFlag(A.X) && flags.HasFlag(A.Y))
{
    // It'll enter here!
}

Definitively, you need to customize your deserialization so that flags coming as arrays get turned into a comma-separated capitalized flag string. 明确地说,您需要自定义反序列化,以便将数组中出现的标志转换为以逗号分隔的大写标志字符串。

That is, you'll end up with this class: 也就是说,您将结束此类:

public class Permissions
{
    public Permissions(A a, B b) 
    {
        A = a;
        B = b;
    }

    public A A { get; }
    public B B { get; }
}

You can do this with Newtosoft.Json that you get from NuGet: 您可以使用从NuGet获得的Newtosoft.Json进行此操作:

void Main()
{
    string json = @"{
   ""permissions"": {
            ""user"": [
                ""Add"",
                ""Update"",
                ""Delete""
            ],
            ""Product"": [
                ""Read"",
                ""Create""
            ]
        }
}";

    var root = Root.FromJson(json);

    Console.WriteLine("C# Object");
    Console.WriteLine("Permissions:");
    Console.WriteLine("\tUser:[");
    foreach (var v in root.Permissions.User)
    {
        Console.WriteLine($"\t\t{v}");
    }
    Console.WriteLine("\t]");
    Console.WriteLine("\tProduct:[");
    foreach (var v in root.Permissions.Product)
    {
        Console.WriteLine($"\t\t{v}");
    }
    Console.WriteLine("\t]");


    Console.WriteLine("As JSON string");
    Console.WriteLine(root.ToJson());
}

[JsonConverter(typeof(StringEnumConverter))]
public enum UserAction { 
    Add,
    Update,
    Delete
}

[JsonConverter(typeof(StringEnumConverter))]
public enum ProductAction
{
    Read,
    Create
}

public partial class Root
{
    [JsonProperty("permissions")]
    public Permissions Permissions { get; set; }

    public static Root FromJson(string json) => JsonConvert.DeserializeObject<Root>(json);
    public string ToJson() => JsonConvert.SerializeObject(this);
}

public class Permissions
{
    [JsonProperty("user")]
    public List<UserAction> User { get; set ; }

    [JsonProperty("Product")]
    public List<ProductAction> Product { get; set; }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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