I don't have power to impact the incoming JSON schema so i need find solution.
Say i have some simple DTO classes:
public class ClassA { prop1, prop2, ... }
public class ClassB { prop3, prop4, ... }
And the incoming JSON is like:
{
"type": "determinant",
"data": { ... }
}
Of course i created root DTO class:
public class CallbackEvent
{
public string Type { get; set; }
[JsonConverter(typeof(DataConverter))]
public object Data { get; set; }
}
And in my DataConverter i haven't found any ways to access the root context of deserialization process. I do hope I was not searching very well
Warning: the unreal code snippet below
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
// i'd like to code smth like this
var context = reader.Root;
var typeValue = context["value"].GetValue<string>();
switch (typeValue)
{
case "event_a": return serializer.Deserialize<ClassA>(reader);
case "event_b": return serializer.Deserialize<ClassB>(reader);
default: return null;
}
}
I would really appreciate your help, folks!