© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•4y ago•
3 replies
pticrix

✅ 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
DefaultJsonTypeInfoResolver
class
- override the
GetTypeInfo(Type, JsonSerializerOptions)
GetTypeInfo(Type, JsonSerializerOptions)
method
- in that method, 'reject' anything that isn't of Kind
JsonTypeInfoKind.Object
JsonTypeInfoKind.Object

- iterate on the
JsonPropertyInfo
JsonPropertyInfo
list returned by
JsonTypeInfo.Properties
JsonTypeInfo.Properties
 
- check each of the
JsonPropertyInfo.AttributeProvider
JsonPropertyInfo.AttributeProvider
to see if they are
ICustomAttributeProvider
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
JsonPropertyInfo.Get
and
JsonPropertyInfo.Set
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 :
        public override JsonTypeInfo GetTypeInfo(Type type, JsonSerializerOptions options)
        {
            JsonTypeInfo typeInfo = base.GetTypeInfo(type, options);
            if (typeInfo.Kind != JsonTypeInfoKind.Object) return typeInfo;
            
            foreach (JsonPropertyInfo propertyInfo in typeInfo.Properties)
            {
                if (propertyInfo.AttributeProvider is ICustomAttributeProvider provider &&
                    provider.IsDefined(typeof(JsonEncryptAttribute), inherit: true))
                {
                    # todo : Unsure how to proceed here.
                }
            }
            return typeInfo;
        }
        public override JsonTypeInfo GetTypeInfo(Type type, JsonSerializerOptions options)
        {
            JsonTypeInfo typeInfo = base.GetTypeInfo(type, options);
            if (typeInfo.Kind != JsonTypeInfoKind.Object) return typeInfo;
            
            foreach (JsonPropertyInfo propertyInfo in typeInfo.Properties)
            {
                if (propertyInfo.AttributeProvider is ICustomAttributeProvider provider &&
                    provider.IsDefined(typeof(JsonEncryptAttribute), inherit: true))
                {
                    # todo : Unsure how to proceed here.
                }
            }
            return typeInfo;
        }


Any insight appreciated.
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

System.Text.Json: Modifying DerivedTypes list after initial resolution
C#CC# / help
2y ago
❔ ✅ System.Text.Json paths
C#CC# / help
3y ago
System.Text.Json parsing property with dot in it
C#CC# / help
3y ago
MediatR - System.Text.Json.JsonException: 'S' is an invalid start of a value.
C#CC# / help
2y ago