I'm dealing with a 3rd Party API that returns data like this:
{
"dynamicFields": {
"prop1": "val1",
"prop2": "val2",
"prop3": "val3"
},
// otherFields ...
}
Since these "dynamic fields" are not strongly typed. I want to represent my POCO as:
class ApiResponse {
public DynamicFields DynamicFields { get; get; }
}
class DynamicFields {
public Dictionary<string, string> Values { get; set; }
}
The rub is, I want to be able to prefix the keys in the dictionary, such that they have the parent property name like so:
{
"DynamicFields": {
"dynamicFields.prop1": "val1",
"dynamicFields.prop2": "val2",
"dynamicFields.prop3": "val2",
}
}
However, it doesn't look like System.Text.Json.Serialization.JsonConverter<T>
gives me access to the parent property name. Is there another way to accomplish this such that I can get the desired output? I'm really trying to not use Newtonsoft/Json.NET as I believe System.Text.Json is "the future".
class DynamicFieldConverter : JsonConverter<DynamicField>
{
public override DynamicField Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
// How do I get the name of the parent property here?
...
}
public override void Write(Utf8JsonWriter writer, DynamicField value, JsonSerializerOptions options)
{
...
}
}