Replies: 3 comments 1 reply
-
Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis |
Beta Was this translation helpful? Give feedback.
-
Your JSON converter is only called after the serializer reads the property name and moves on to read the string value. Typically converters only read the value, not also the property name. If you want to capture the property name, you'll need a converter for your object type, not |
Beta Was this translation helpful? Give feedback.
-
Hello @dnxit you can achieve required behavior without converters. var o = new JsonSerializerOptions()
{
TypeInfoResolver = new DefaultJsonTypeInfoResolver().WithAddedModifier(p =>
{
foreach (var prop in p.Properties)
if (prop is { Name: "Prop", Set: { } setter })
{
prop.Set = (o, value) =>
{
value = "Modified: " + value;
setter(o, value);
};
}
})
};
var a = JsonSerializer.Deserialize<A>("""{"Prop":"str"}""", o);
Console.WriteLine(a.Prop); /// print Modified: str
class A
{
public string Prop { get; set; }
} |
Beta Was this translation helpful? Give feedback.
-
I don't want to decorate my properties with this converter like
I need to use it like this
}
reader.TokenType is always equals to JsonTokenType.String so it never goes in that if statement
any help would be much appreciated
Beta Was this translation helpful? Give feedback.
All reactions