You could use a custom JsonConverter for this similar to this:
public class EmbeddedJsonConverter : JsonConverter
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
return serializer.Deserialize(new StringReader((string)reader.Value), objectType);
}
public override bool CanConvert(Type objectType)
{
return true;
}
}
Mark the property with [JsonConverter(typeof(EmbeddedJsonConverter))]
like:
public class Response
{
public DateTime time { get; set; }
public string event_name { get; set; }
public string event_type { get; set; }
public string method { get; set; }
[JsonConverter(typeof(EmbeddedJsonConverter))]
public MessageDetails message_details { get; set; }
}
Then you will be able to deserialize with JsonConvert.DeserializeObject()
.
The EmbeddedJsonConverter class extracts the json string from the object and then deserializes it. CanConvert
should probably be made smarter for a truly generic use.