I have a webservice returning json data. I have no control over the server side generated json.
I'm deserializing json like this:
JsonConvert.DeserializeObject<OuterObject>(jsonString);
The problem is there are inner objects embedded (with a whole lot of more nested inner objects). I have no interest in modeling them in my application.
The json-data goes like this:
{
id : "xyz",
name : "Some Object",
properties : {
prop_1 : "foo",
prop_2 : "bar"
},
inner_object : {
id : "abc$1",
name : "Inner Object Name",
....
// a whole lot of stuff here
// with more embedded objects
....
}
}
I would like to model the outer object as a simple POCO, where the inner-object is referenced by just a (String) id, not an object reference.
public class Outer
{
public String Id { get; internal set; }
public String Name { get; internal set; }
public Dictionary<String,String> Properties { get; internal set; }
// Just keep the InnerObject Id, no real reference to an instance
public String InnerObjectId { get; set; }
}
I guess I could write an JsonOuterObject version with a real object reference to JsonInnerObject and construct my real OuterObject from there, throwing away the other objects afterwards ... but that's way too lame. (I don't want to see my name next in a commit like that)
So I'm playing around with Json.NET's IContractResolver (overriding the DefaultContractResolver) now, but it looks like I'm in for a long ride here.
Am I missing some obvious Json.NET feature that takes care of this for me here ?
Or maybe some pointers what methods of IContractResolver are interesting here ?
EDIT: A POJO in .NET is a POCO aparently.