using System.Collections.Generic;
using Newtonsoft.Json.Linq;
public static void Main()
""PM00000001"": { ""description"": ""Manufacturing"",""cost"": -1,""group"":""Manufacturing"",""WeldAngleDegrees"": 60},
""PM00000010"": {""description"": ""Plate Roll"",""cost"": 90,""unit"": ""hr"",""group"": ""Roll"",""setup"": 0.5,""speed"": 0.4},
""PM00000011"": {""description"": ""Weld SAW"",""cost"": 90,""unit"": ""hr"",""group"": ""Weld"",""width"": 0.5,""thickness"": 50}
var dict = JsonConvert.DeserializeObject<Dictionary<string, Item>>(json, new ItemConverter());
foreach (var kvp in dict)
Console.WriteLine(kvp.Key);
Console.WriteLine(kvp.Value + "\n");
public string description { get; set; }
public double cost { get; set; }
public string group { get; set; }
public override string ToString()
return string.Format("description: {0}\ncost: {1}\ngroup: {2}", description, cost, group);
public class ManufacturingItem : Item
public string WeldAngleDegrees { get; set; }
public override string ToString()
return string.Format("{0}\nweld angle: {1}", base.ToString(), WeldAngleDegrees);
public class RollItem : Item
public double setup { get; set; }
public double speed { get; set; }
public override string ToString()
return string.Format("{0}\nsetup: {1}\nspeed: {2}", base.ToString(), setup, speed);
public class WeldItem : Item
public double width { get; set; }
public double thickness { get; set; }
public override string ToString()
return string.Format("{0}\nwidth: {1}\nthickness: {2}", base.ToString(), width, thickness);
public class ItemConverter : JsonConverter
public override bool CanConvert(Type objectType)
return objectType == typeof(Item);
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
JObject jo = JObject.Load(reader);
string group = (string)jo["group"];
if (group == "Manufacturing")
return jo.ToObject<ManufacturingItem>();
else if (group == "Roll")
return jo.ToObject<RollItem>();
else if (group == "Weld")
return jo.ToObject<WeldItem>();
throw new JsonSerializationException("Unexpected item (group) type");
public override bool CanWrite
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
throw new NotImplementedException();