✅ JSON Contract : modifying the value of a property, using System.Text.Json 7
Hello! I'm trying to encrypt only certain fields in a DTO before serializing them, using an attribute in the class definition. The attribute part I got the hang of, however I'm uncertain about how to go about modifying the value of a given property. Here's what I've been doing, trying to follow the tips from this dev blog : https://devblogs.microsoft.com/dotnet/system-text-json-in-dotnet-7/
- extend the
DefaultJsonTypeInfoResolver
class
- override the GetTypeInfo(Type, JsonSerializerOptions)
method
- in that method, 'reject' anything that isn't of Kind JsonTypeInfoKind.Object
- iterate on the JsonPropertyInfo
list returned by JsonTypeInfo.Properties
- check each of the JsonPropertyInfo.AttributeProvider
to see if they are ICustomAttributeProvider
and if my custom attribute is defined in it.
Once I've confirmed that, that is where I'm kinda lost. It's probably dumb, but I haven't managed yet to find what to do from here. I'm thinking either :
- I need to change the delegate method of JsonPropertyInfo.Get
and JsonPropertyInfo.Set
or
- I need to tell it here and there to take the property value and to pass it through my encryption method.
Anyone here could point me to the correct way to proceed?
Current code :
Any insight appreciated.1 Reply
Solved it like a big boy.
Note that the Encryption class is a custom class, not one from the Framework.