I keep running into the situation where I want to deserialize some JSON into a type that doesn't exactly match the structure of the JSON. For example, if I have JSON like this:
"foo": {
"bar": {
"1": 10,
"4": 20,
"3": 30,
"2": 40,
"5": 50
},
}
Then I would need something like this:
class Bar
{
[JsonProperty]
int One;
[JsonProperty]
int Two;
[JsonProperty]
int Three;
[JsonProperty]
int Four;
[JsonProperty]
int Five;
}
class Foo
{
[JsonProperty]
Bar bar;
}
But what if I want to turn it into this?
class Foo
{
int[] values;
}
where it converts the keys to 0-based indices by subtracting 1 from each one and making an array out of it? How can I do this?
I have many other examples of this kind of custon deserialization, but I'll post one other just for the sake of illustration, as I don't want a solution that is tailored to this exact example, but rather something I can generalize.
"foo": {
"bar": [
{
// Bunch of unimportant stuff
},
{
// Something about the structure of this object alerts me to the fact that this is an interesting record.
"baz": 12
},
{
// Bunch of unimportant stuff
}
]
}
And I want to turn this into:
class Foo
{
int baz;
}
I've looked at a few examples of CustomCreationConverters, such as this, but I was unable to adapt the solution presented there to either of the above situations.