Question

I want to deserialize this schema using JSON.Net.

{ 
  "color" : { 
    "type" : "String", 
    "description" : "What color do you want your taco", 
    "required" : false, 
    "default" : "Green", 
    "options" : [ "Green", "Blue", "Red"]
  },
  "include_beans" : { 
    "type"  : "Boolean", 
    "description" : "Do you want beans on your taco", 
    "required" : false, 
    "default" : false 
  }, 
  "pounds"  : { 
    "type" : "Double", 
    "description" : "How  many pounds of meat do you want?", 
    "required"  : false, 
    "default" : 0.1 
  },  
  "count" : { 
    "type"  : "Integer", 
    "description" : "How  many tacos would you like?", 
    "required"  : false, 
    "default" : 0.0 
  }
}

Notice that each property has the same structure. What I want to end up with is a Dictionary<string, TacoProperty>, where TacoProperty is defined as:

public class TacoProperty
{
    public string type { get; set; }
    public string description { get; set; }
    public bool required { get; set; }
    [JsonProperty(PropertyName = "default")]
    public string defaultValue { get; set; }
    public List<string> options { get; set; }
}

The keys into the dictionary should be "color", "include_beans" etc., and all the TacoPropertys should be the values.

Was it helpful?

Solution

Json.NET can deserialize the data directly:

var tacoProperties =
       JsonConvert.DeserializeObject<IDictionary<string, TacoProperty>>(json);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top