I'm working on a project with friends and I'm doing a composite pattern. I have a problem deserializing my json to my composite object. I managed to serialize it, so I will try to give as much info as possible.
My composite classes:
public abstract class Component
{
[JsonProperty("name")]
public string name;
public Component(string name)
{
this.name = name;
}
public virtual void Add(Component component)
{
throw new NotImplementedException();
}
public virtual void Remove(Component component)
{
throw new NotImplementedException();
}
public virtual bool IsComposite()
{
return true;
}
}
[JsonConverter(typeof(CompositeConverter))]
public class Composite : Component
{
[JsonProperty("children")]
public List<Component> _children { get; set; }
public Composite(string name) : base(name)
{
this._children = new List<Component>();
}
public override void Add(Component component)
{
this._children.Add(component);
}
public override void Remove(Component component)
{
this._children.Remove(component);
}
}
public class Leaf : Component
{
public int experience;
public bool achieved;
[JsonConstructor]
public Leaf(string name, int experience) : base(name)
{
this.experience = experience;
}
public override bool IsComposite()
{
return false;
}
}
So, with these classes I'm able to create what I want:
Composite levels = new Composite("Levels");
Composite firstLevel = new Composite("First level");
Composite secondLevel = new Composite("Second level");
Composite thirdLevel = new Composite("Third level");
Leaf firstAchievement = new Leaf("Mission 1", 1);
Leaf secondAchviement = new Leaf("Mission 2", 2);
Leaf thirdAchievement = new Leaf("Mission 3", 3);
Leaf fourthAchievement = new Leaf("Mission 4", 4);
Leaf fifthAchievement = new Leaf("Mission 5", 5);
Leaf sixthAchievement = new Leaf("Mission 6", 6);
firstLevel.Add(firstAchievement);
secondLevel.Add(secondAchviement);
secondLevel.Add(thirdAchievement);
thirdLevel.Add(fourthAchievement);
thirdLevel.Add(fifthAchievement);
thirdLevel.Add(sixthAchievement);
levels.Add(firstLevel);
levels.Add(secondLevel);
levels.Add(thirdLevel);
Now the most interesting part begins. So I have composite object, when I serialize it with my own serialiser:
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
Composite composite = (Composite)value;
PropertyInfo prop = typeof(Composite).GetProperty("_children");
List<Component> children = (List<Component>)prop.GetValue(composite);
JArray array = new JArray();
foreach (Component e in children)
{
array.Add(JToken.FromObject(e, serializer));
}
JObject obj = new JObject();
obj.Add(composite.name, array);
obj.WriteTo(writer);
}
I get this json:
{
"Levels": [
{
"First level": [
{
"experience": 1,
"achieved": false,
"name": "Mission 1"
}
]
},
{
"Second level": [
{
"experience": 2,
"achieved": false,
"name": "Mission 2"
},
{
"experience": 3,
"achieved": false,
"name": "Mission 3"
}
]
},
{
"Third level": [
{
"experience": 4,
"achieved": false,
"name": "Mission 4"
},
{
"experience": 5,
"achieved": false,
"name": "Mission 5"
},
{
"experience": 6,
"achieved": false,
"name": "Mission 6"
}
]
}
]
}
Everything looks perfect and now I need to deserialize it and I'm just banging my head to the wall. Maybe someone has some hints or something on how should I approach it? Wasn't able to find any good resources on this