How can I set the attributes and its values for a complex Json payload and serialize?

落花浮王杯 提交于 2020-03-25 16:05:39

问题


I have the following structure in my framework. I have a helper where I have the following Attribute code, Resolver code and the Serialization method. Link to this in the other part of the question How to create one model class to use with multiple API methods that require different JSON payloads?

I now have any API that takes a Payload that is large with root object. How can I use the SerializeForApiMethod method for this, since it has the root object. I am trying to set the values in the same model object reference for this Jsonpayload.

//Here is the Json schema

{
  "tagNode": {
    "query": null,
    "type": 0,
    "filter": null,
    "ldapPaths": null,
    "editPermissions": 0,
    "id": 0,
    "disallowed": false,
    "name": "NewTag"
  },
  "parentId": 0
}

//Model class
public class TagModel
{
   public static TagModel mode = new TagModel
   {
            endpointIds = new List<int> { -2147483612, -2147483611 },
            tagIds = new List<int> { 35, 37 }, 
            id = -2147483639,
            parentId = 37,
            nodeId = 1,
            oldParentId = null,
            isEndpoint = false,
            state = 2,
            destinationTag = 2,



   };

    //This Object I am Confused on how to Serialize as above schema it in the 
   //same class whichI w ant to set the values inside the TagModel instance. 
    //Also has same attributes like id and parentId used already in the 
    //other methods and also this complex payload has a class TagNode which is 
    //the root object. 
     [UseWithApiMethods("UpdateTag")]
     public TagNode tagNode { get; set; }
     public object query { get; set; }
     public int type { get; set; }
     public object filter { get; set; }
     public object ldapPaths { get; set; }
     public int editPermissions { get; set; }
     public int id { get; set; }
     public bool disallowed { get; set; }
     public string name { get; set; }
     public int parentId { get; set; }



             //For this Object I am able to serialize it
           [UseWithApiMethods("UpdateTagToRoot")]
            public int nodeId { get; set; }
            [UseWithApiMethods("UpdateTagToRoot")]
            public object oldParentId { get; set; }
            [UseWithApiMethods("UpdateTagToRoot")]
            public bool isEndpoint { get; set; }
            [UseWithApiMethods("UpdateTagToRoot")]
            public int state { get; set; }
            [UseWithApiMethods("UpdateTagToRoot")]
            public int destinationTag { get; set; }



              //For this Object I am able to serialize it
            [UseWithApiMethods("UpdateEndpointsToTags")]
            public List<int> endpointIds { get; set; }
            [UseWithApiMethods("UpdateEndpointsToTags")]
            public List<int> tagIds { get; set; }


          //For this Object I am able to serialize it
           [UseWithApiMethods("UpdateEndpointsFromTags")]
           public int id { get; set; }
           [UseWithApiMethods("UpdateEndpointsFromTags")] 
           public int parentId { get; set; }


}

}


回答1:


To get the JSON payload you want using the custom resolver from the other question, you would need your class structure to look like this:

public class TagModel
{
    [UseWithApiMethods("UpdateTag")]
    public TagNode tagNode { get; set; }

    public class TagNode
    {
        [UseWithApiMethods("UpdateTag")]
        public object query { get; set; }

        [UseWithApiMethods("UpdateTag")]
        public int type { get; set; }

        [UseWithApiMethods("UpdateTag")]
        public object filter { get; set; }

        [UseWithApiMethods("UpdateTag")]
        public object ldapPaths { get; set; }

        [UseWithApiMethods("UpdateTag")]
        public int editPermissions { get; set; }

        [UseWithApiMethods("UpdateTag")]
        public int id { get; set; }

        [UseWithApiMethods("UpdateTag")]
        public bool disallowed { get; set; }

        [UseWithApiMethods("UpdateTag")]
        public string name { get; set; }
    }

    [UseWithApiMethods("UpdateEndpointsFromTags", "UpdateTag")]
    public int parentId { get; set; }

    ... (other properties for your other API methods) ...
}

Note that you must put [UseWithApiMethods("UpdateTag")] on the tagNode property in the root class and also on all the properties within the TagNode child class that you want to include in the JSON. (This is necessary because the resolver will only include properties that you specifically mark with a [UseWithApiMethods] attribute. So you need to mark them all if you want to include them all).

For the parentId property, you were already using it for the UpdateEndpointsFromTags method, but that is no problem; you can also reuse it for other methods like UpdateTag. You just need to add the method names to the [UseWithApiMethods] attribute like this:

    [UseWithApiMethods("UpdateEndpointsFromTags", "UpdateTag")]
    public int parentId { get; set; }

Here is a fiddle which demonstrates: https://dotnetfiddle.net/6TqbFz

Hopefully this is what you are looking for and resolves your confusion.



来源:https://stackoverflow.com/questions/60782852/how-can-i-set-the-attributes-and-its-values-for-a-complex-json-payload-and-seria

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!