Disclaimer: There are many topics on this subject, however so far I found only one on the topic of partial deserialisation thisone, which already is a couple of years old
The question: What is the easiest way in json.net, yet without much overhead, to deserialize a partial JSON string to object?
Given a JSON object:
{
"message":"Login successful",
"data":{
"firstname":"John",
"lastname":"Doe",
}
}
and an object:
public class UserData
{
[JsonProperty("firstname")]
public string FirstName;
[JsonProperty("lastname")]
public string LastName;
}
than, there are many methods you can use. e.g. create another object that holds a variable UserData data and deserialize to that object. However, if you want to access FirstName, you'll always have to add the data (data.FirstName) which is.. not nice.
Another method (as in the post linked above) is to deserialize the object to dynamic/var, get the data part of it, serialize it and feed that string to the
dynamic obj = JsonConvert.DeserializeObject(JSONString);
UserData User = JsonConvert.DeserializeObject<Model.UserData>(JsonConvert.SerializeObject(obj["data"]));
Yes, it works well, but perhaps a bit much overhead? Another option is to use a custom JsonConverter, but that seems to be allot of code for such a simple thing.
I was hoping to use something like
[JsonProperty("data.firstname")]
public string FirstName;
or another setting, to let json.net know it should use the path data for the deserialisation of the JSON string to UserData, but I havent found such a thing in the documentation.
Any suggestions?