Repository navigation
Replies: 3 comments 1 reply
|
Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis |
0 replies
|
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 |
0 replies
|
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; }
} |
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
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
All reactions