Home > Enterprise >  deserialize then serialize json with dynamic field name
deserialize then serialize json with dynamic field name

Time:12-31

I have a JSON file that looks something like this.

{
  "id": "top1",
  "components": [
    {
      "type": "resistor",
      "id": "res1",
      "resistance": {
        "default": 100,
        "min": 10,
        "max": 1000
      },
      "netlist": {
        "t1": "vdd",
        "t2": "n1"
      }
    },
    {
      "type": "nmos",
      "id": "m1",
      "m(l)": {
        "default": 1.5,
        "min": 1,
        "max": 2
      },
      "netlist": {
        "drain": "n1",
        "gate": "vin",
        "source": "vss"
      }
    }
  ]
}

I want to make an API using oop to work with that JSON file, I made the following classes.

public class Topology
    {
        [Required]
        public string id { get; set; }
        [Required]
        public List<TopologyComponents> components { get; set; }
    }
public class TopologyComponents
    {
        [Required]
        public string type { get; set; }
        [Required]
        public string id { get; set; }
        [Required]
        public Values ???????? {get; set; }
        [Required]
        public Dictionary<string, string> netlist { get; set; }
    }
public class Values
    {
        [Required]
        public double @default { get; set; }
        [Required]
        public double min { get; set; }
        [Required]
        public double max { get; set; }
    }

my question is those question marks???????? the field name is dynamic resistance, m(l), .....
how can I handle those cases?
I tried Jackson annotations, expandobject, and dictionaries. but none of them worked as I want.

CodePudding user response:

From the looks of it, your Topology class will need to have Dictionary<string, dynamic> data type since the keys of components will be arbitrary. Even though type and id will be the same across all components, netlist and one other property will be dynamic.

Change your list of components to Dictionary<string, dynamic> and then get the data you need by first checking which property actually exists in the component.

public class Topology
{
    [Required]
    public string id { get; set; }
    [Required]
    public List<Dictionary<string, dynamic>> components { get; set; }
}

this will give you a list of dictionary with string as the keys and dynamic objects as the values. You can iterate through the keys using foreach loop on components.Keys and perhaps a switch statement to see if the key you expect exists when iterating through each component.

Example on how you can create your own list of components... not sure how you are going to use the data since that will drive the way you deserialize this,

var obj = JsonConvert.DeserializeObject<Topology>(jsonText);
List<dynamic> allComps = new List<dynamic>();
foreach(var component in obj.components)
{
    var comp = new ExpandoObject() as IDictionary<string, object>;
    foreach(var key in component.Keys)
    {
        switch (key)
        {
            case "id":
            case "type":
                comp.Add(key, component[key].ToString());
                break;

            case "netlist":
                comp.Add(key, component[key].ToObject<Dictionary<string, string>>());
                break;

            default:
                comp.Add(key, component[key].ToObject<Values>());
                break;
        }
    }
    allComps.Add(comp);
}
  • Related