-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
[API Proposal]: Add ReadCommentHandling to JsonSourceGenerationOptions #81131
Comments
Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis Issue DetailsBackground and motivationTypical API Proposal sealed class JsonSourceGenerationOptionsAttribute : JsonAttribute
{
// other properties
/// <summary>
/// Gets or sets a value that defines how comments are handled during deserialization.
/// </summary>
public JsonCommentHandling ReadCommentHandling { get; set; }
} API Usage[JsonSourceGenerationOptions(ReadCommentHandling = JsonCommentHandling.Skip)]
[JsonSerializable(typeof(MyAppSettings))]
internal partial class SourceGenerationContext : JsonSerializerContext
{
} Alternative DesignsNo response RisksNo response
|
|
@layomia what is the behavior today when encountering comments? I am trying to understand what will be broken if didn't ignore the comments. |
|
The current behavior is throw an exception by default, but we can configure the right option to ignore at runtime: @Shane32 does the following work for you? JsonSerializerOptions options = new() { ReadCommentHandling = ... } ;
SourceGenerationContext context = new(options);
// Deserialize as needed
... |
Sorry that was the exception trace from Yes, that does work actually. I thought I tried using the options class but must have tried it differently. Is this the intended design then? Thanks in advance! |
@Shane32 yes the |
Background and motivation
Typical
appsettings.json
files often contain comments, and in order for console apps to read such files in a NativeAOT environment, it is required that the deserialization process ignore comments. It appears this behavior is not yet supported. Since NativeAOT is targeted towards console applications, it seems that this would be a feature worthwhile to support. I would certainly make use of it.API Proposal
I suggest the same name and description as is found within
JsonSerializerOptions
.API Usage
Alternative Designs
Risks
If
Allow
is not supported but onlySkip
andDisallow
, using a boolean property likeSkipComments
may reduce the ability to gracefully expand the feature in the future.References
The text was updated successfully, but these errors were encountered: