You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add JsonIncludeAttribute/JsonConstructorAttribute support for private/inaccessible members in the source generator #88519
The change in #88452 added internal/private member support for JsonIncludeAttribute/JsonConstructorAttribute in the reflection-based serializer. We should consider extending this to the source generator via the recent feature introduced in #81741. Note that this would only work for the latest TFMs (with some reflection-based fallback being necessary in older targets).
with some reflection-based fallback being necessary in older targets
We should simply not support this feature with source generators for older targets.
Reflection-based callback in source generators for older targets would violate promise of System.Text.Json source generator producing fast code that is trimming and AOT friendly.
Reacted by Jeremy Koritzinsky, Pavel Voronin and Jure
I'm happy to see this enhancement is officially endorsed by the team, I hope to see it for .NET9/10. While studying this limitation, I made a strange discovery and found out that putting a serializer context partial class within the class to be serialized (see the snippet below) allows the source generator to produce code that is indeed accessing the private fields, but still the compilation produces warnings and the serialization throws at runtime. I don't know if this can cause some concern today, but I am sure it will be properly replaced with the visibility suppression mechanism in the future.
publicpartialclassVehicle{[JsonInclude,JsonPropertyName(nameof(Brand))]privatestring?_Brand;[JsonIgnore]publicstring?Brand=>_Brand;[JsonSerializable(typeof(Vehicle))]internalpartialclassVehicleSerializerContext:JsonSerializerContext;}/* The following code is produced by the generator, and obviously compiles correctly * Still it has the warning "warning SYSLIB1038: The member 'Vehicle._Model' has been * annotated with the JsonIncludeAttribute but is not visible to the source generator" * and throws at runtime * * Getter = static obj => ((global::ConsoleApp1.Vehicle)obj)._Model, * Setter = static (obj, value) => ((global::ConsoleApp1.Vehicle)obj)._Model = value!, */
While studying this limitation, I made a strange discovery and found out that putting a serializer context partial class within the class to be serialized (see the snippet below) allows the source generator to produce code that is indeed accessing the private fields.
This is expected behavior. It's not that (opted in) private members aren't supported in general, the more accurate statement is that members inaccessible to the generated class are not supported. Per C# semantics private members are accessible to nested types, so in this (relatively rare) case the source generator happens to work.
but still the compilation produces warnings and the serialization throws at runtime.
I can't reproduce in .NET 8. Is the project using multi-targeting perhaps?
I can't reproduce in .NET 8. Is the project using multi-targeting perhaps?
No. I can reproduce in .NET 8 (I'm using latest stable VS 17.9.6) with the following steps:
Create an empty console application;
Paste the code below and run (produces warnings and throws the exception reported below).
(Workaround step): As per the documentation of the warning (and the System.Text.Json documentation in general about source generators), making all the private fields in the code internals, compile and run will have everything run just fine. But you just told me private members in some contexts can be supported and that you can't reproduce...
usingSystem.Text.Json;usingSystem.Text.Json.Serialization;namespaceConsoleApp1{[JsonDerivedType(typeof(MotorVehicle),"MotorVehicle")][JsonDerivedType(typeof(Car),"Car")]publicpartialclassVehicle{[JsonInclude,JsonPropertyName(nameof(Model))]privatestring?_Model;[JsonIgnore]publicstring?Model=>_Model;[JsonSerializable(typeof(Vehicle))]internalpartialclassVehicleSerializerContext:JsonSerializerContext;}publicpartialclassMotorVehicle:Vehicle{[JsonInclude][JsonPropertyName(nameof(TransmissionType))][JsonConverter(typeof(JsonStringEnumConverter<TransmissionType>))]privateTransmissionType_TransmissionType;[JsonIgnore]publicTransmissionTypeTransmissionType=>_TransmissionType;[JsonSerializable(typeof(MotorVehicle))]internalpartialclassMotorVehicleSerializerContext:JsonSerializerContext;}publicpartialclassCar:MotorVehicle{[JsonInclude][JsonPropertyName(nameof(CarClassification))][JsonConverter(typeof(JsonStringEnumConverter<CarClassification>))]privateCarClassification_CarClassification;[JsonIgnore]publicCarClassificationCarClassification=>_CarClassification;[JsonSerializable(typeof(Car))]internalpartialclassCarSerializerContext:JsonSerializerContext;}publicenumCarClassification{Unknown=0,Small,Medium,Large,}publicenumTransmissionType{Unknown=0,Manual,Automatic}classProgram{publicstaticintMain(string[]_){varjson=JsonSerializer.Serialize(newCar(),Vehicle.VehicleSerializerContext.Default.Vehicle);Console.WriteLine(json);varresult=JsonSerializer.Deserialize(json,Vehicle.VehicleSerializerContext.Default.Vehicle);Console.WriteLine($"result is {result!.GetType()}");return0;}}}
The compilation produces these warnings:
warning SYSLIB1038: The member 'MotorVehicle._TransmissionType' has been annotated with the JsonIncludeAttribute but is not visible to the source generator. (https://learn.microsoft.com/dotnet/fundamentals/syslib-diagnostics/syslib1038)
warning SYSLIB1038: The member 'Car._CarClassification' has been annotated with the JsonIncludeAttribute but is not visible to the source generator. (https://learn.microsoft.com/dotnet/fundamentals/syslib-diagnostics/syslib1038)
warning SYSLIB1038: The member 'Vehicle._Model' has been annotated with the JsonIncludeAttribute but is not visible to the source generator. (https://learn.microsoft.com/dotnet/fundamentals/syslib-diagnostics/syslib1038)
And the exception is:
Unhandled exception. System.InvalidOperationException: The property '_CarClassification' on type 'ConsoleApp1.Car' which is annotated with 'JsonIncludeAttribute' is not accesible by the source generator.
at System.Text.Json.ThrowHelper.ThrowInvalidOperationException_JsonIncludeOnInaccessibleProperty(String memberName, Type declaringType)
at System.Text.Json.Serialization.Metadata.JsonMetadataServices.PopulateProperties(JsonTypeInfo typeInfo, JsonPropertyInfoList propertyList, Func`2 propInitFunc)
at System.Text.Json.Serialization.Metadata.JsonTypeInfo.<get_PropertyList>g__CreatePropertyList|65_0()
at System.Text.Json.Serialization.Metadata.JsonTypeInfo.ConfigureProperties()
at System.Text.Json.Serialization.Metadata.JsonTypeInfo.Configure()
at System.Text.Json.Serialization.Metadata.JsonTypeInfo.<EnsureConfigured>g__ConfigureSynchronized|172_0()
at System.Text.Json.JsonSerializerOptions.GetTypeInfoInternal(Type type, Boolean ensureConfigured, Nullable`1 ensureNotNull, Boolean resolveIfMutable, Boolean fallBackToNearestAncestorType)
at System.Text.Json.Serialization.Metadata.PolymorphicTypeResolver.DerivedJsonTypeInfo.GetJsonTypeInfo(JsonSerializerOptions options)
at System.Text.Json.Serialization.Metadata.PolymorphicTypeResolver.TryGetDerivedJsonTypeInfo(Type runtimeType, JsonTypeInfo& jsonTypeInfo, Object& typeDiscriminator)
at System.Text.Json.Serialization.JsonConverter.ResolvePolymorphicConverter(Object value, JsonTypeInfo jsonTypeInfo, JsonSerializerOptions options, WriteStack& state)
at System.Text.Json.Serialization.JsonConverter`1.TryWrite(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state)
at System.Text.Json.Serialization.JsonConverter`1.WriteCore(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state)
at System.Text.Json.Serialization.Metadata.JsonTypeInfo`1.Serialize(Utf8JsonWriter writer, T& rootValue, Object rootValueBoxed)
at System.Text.Json.JsonSerializer.WriteString[TValue](TValue& value, JsonTypeInfo`1 jsonTypeInfo)
at System.Text.Json.JsonSerializer.Serialize[TValue](TValue value, JsonTypeInfo`1 jsonTypeInfo)
at ConsoleApp1.Program.Main(String[] _) in C:\Users\ceztko\Source\Repos\ConsoleApp1\ConsoleApp2\Program.cs:line 68
The relevant code in the source generator uses the Compilation.IsSymbolAccessibleWithin method to determine if a member is accessible from a given symbol:
That being said, it seems like a super low-priority issue with a clear workaround.
As said, we were trying to exploit the limits of the system, and I came up with this idea of the inner class context. The workaround (making the fields internal) is acceptable for now but as you understand this lowers the quality of the encapsulation in my code and that's why I'm happy serialization of private fields will come anyway in a more general form, at some point.
Perhaps that method is hitting a corner case? cc @dotnet/roslyn
Will there be a knob to disable runtime reflection in case UnsafeAccessor cant support given property/field? Important use case for source gen is being able to work in AoT scenarios so we need knob then we can fix this ourselves by supplying custom contract resolver
Also UnsafeAccessor does support most cases of generics in newer runtimes (9 or 10 .net dont remember which) is this planned as a next phase?
i found this was added in later if im not mistaken but AoT scenario is still relevant i think since not all kinds of generics and private members can be accessed by unsafeaccessor
Activity
jkotas commentedon Jul 7, 2023
We should simply not support this feature with source generators for older targets.
Reflection-based callback in source generators for older targets would violate promise of System.Text.Json source generator producing fast code that is trimming and AOT friendly.
ceztko commentedon May 10, 2024
I'm happy to see this enhancement is officially endorsed by the team, I hope to see it for .NET9/10. While studying this limitation, I made a strange discovery and found out that putting a serializer context partial class within the class to be serialized (see the snippet below) allows the source generator to produce code that is indeed accessing the private fields, but still the compilation produces warnings and the serialization throws at runtime. I don't know if this can cause some concern today, but I am sure it will be properly replaced with the visibility suppression mechanism in the future.
eiriktsarpalis commentedon May 10, 2024
This is expected behavior. It's not that (opted in) private members aren't supported in general, the more accurate statement is that members inaccessible to the generated class are not supported. Per C# semantics private members are accessible to nested types, so in this (relatively rare) case the source generator happens to work.
I can't reproduce in .NET 8. Is the project using multi-targeting perhaps?
ceztko commentedon May 10, 2024
No. I can reproduce in .NET 8 (I'm using latest stable VS 17.9.6) with the following steps:
System.Text.Jsondocumentation in general about source generators), making all the private fields in the code internals, compile and run will have everything run just fine. But you just told me private members in some contexts can be supported and that you can't reproduce...The compilation produces these warnings:
And the exception is:
eiriktsarpalis commentedon May 10, 2024
I've been able to trim down to the following repro:
The relevant code in the source generator uses the
Compilation.IsSymbolAccessibleWithinmethod to determine if a member is accessible from a given symbol:runtime/src/libraries/System.Text.Json/gen/JsonSourceGenerator.Parser.cs
Lines 1373 to 1382 in 56d7e5d
Perhaps that method is hitting a corner case? cc @dotnet/roslyn
That being said, it seems like a super low-priority issue with a clear workaround.
ceztko commentedon May 10, 2024
As said, we were trying to exploit the limits of the system, and I came up with this idea of the inner class context. The workaround (making the fields internal) is acceptable for now but as you understand this lowers the quality of the encapsulation in my code and that's why I'm happy serialization of private fields will come anyway in a more general form, at some point.
FYI, it looks like the cc failed.
Joe4evr commentedon May 10, 2024
Nah, github simply doesn't render the tag if you don't have permission to see inside the group being tagged.
Temporarily disable [JsonInclude] for inaccessible members in source gen
Temporarily disable [JsonInclude] for inaccessible members in source gen
Temporarily disable [JsonInclude] for inaccessible members in source gen
Temporarily disable [JsonInclude] for inaccessible members in source gen
eiriktsarpalis commentedon Apr 2, 2026
Reopening until
JsonIncludeAttributesupport is fully implemented.[STJ Source gen] Use UnsafeAccessor/reflection for inaccessible membe…
BreyerW commentedon Apr 3, 2026
Will there be a knob to disable runtime reflection in case
UnsafeAccessorcant support given property/field? Important use case for source gen is being able to work in AoT scenarios so we need knob then we can fix this ourselves by supplying custom contract resolverAlsoUnsafeAccessordoes support most cases of generics in newer runtimes (9 or 10 .net dont remember which) is this planned as a next phase?i found this was added in later if im not mistaken but AoT scenario is still relevant i think since not all kinds of generics and private members can be accessed by unsafeaccessor
eiriktsarpalis commentedon Apr 3, 2026
Even if a reflection-based fallback is used, it follows patterns that guarantee Native AOT support.
[STJ Source gen] Use UnsafeAccessor/reflection for inaccessible membe…
Re-enable source generator support for inaccessible [JsonInclude] mem…
[STJ Source gen] Use UnsafeAccessor/reflection for inaccessible membe…
Re-enable source generator support for inaccessible [JsonInclude] mem…