Skip to content

Read metadata comments and trailing commas, and let a generator set its JSON options [minor] - #69

Open
matt-edmondson wants to merge 1 commit into
mainfrom
fix/metadata-json-options-49
Open

matt-edmondson wants to merge 1 commit into
mainfrom
fix/metadata-json-options-49

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

Fixes #49

Hand-maintained metadata files that contained a // or /* */ comment or a trailing comma failed with MetadataParseFailed. Consumers also had no way to change the fixed, private JsonSerializerOptions that MetadataFile used. This PR implements both options from the issue.

Changes

  • Option 1, friendlier default. The shared options now set ReadCommentHandling = Skip and AllowTrailingCommas = true. This only accepts input that was rejected before. The options are exposed as MetadataFile.DefaultSerializerOptions and made read-only (MakeReadOnly), so a consumer can copy them but cannot change the shared instance.
  • Option 2, override hook. There is a new overload, MetadataFile.Deserialize<T>(context, parseFailed, JsonSerializerOptions). GeneratorBase<T> gets a protected virtual JsonSerializerOptions MetadataSerializerOptions, which defaults to the shared instance and is passed through to that overload. The XML docs and README tell consumers to return a cached static readonly instance (CA1869).
  • README API tables are updated. [minor] because this adds public API.

Tests

  • AHandWrittenMetadataFileWithCommentsAndTrailingCommasParses: a fixture with // and /* */ comments and trailing commas in both objects and arrays generates with no diagnostic.
  • AGeneratorCanOverrideTheMetadataSerializerOptions: a fixture generator adds JsonStringEnumConverter and reads an enum from its string name.
  • TheDefaultMetadataSerializerOptionsAreReadOnly.
  • Both behaviour tests fail with the fix reverted: the defaults restored, and GeneratorBase<T> ignoring the override. They pass with it. The full suite passes (83 tests), including the existing parse-failure tests, and dotnet build -warnaserror is clean.

Generated by Claude Code

…ts JSON options [minor]

Hand-maintained metadata with a // or /* */ comment or a trailing comma
failed with MetadataParseFailed, and a consumer had no way to change the
fixed JsonSerializerOptions MetadataFile used. The shared default now skips
comments and allows trailing commas, and is exposed read-only as
MetadataFile.DefaultSerializerOptions. Deserialize<T> gains an overload that
takes options, and GeneratorBase<T> passes a virtual MetadataSerializerOptions
through it.

Fixes #49

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017vfJW3R5YfaNjqL9oXpRg6
@sonarqubecloud

sonarqubecloud Bot commented Nov 7, 2026

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Hand-written metadata with a // comment or a trailing comma fails to parse, and consumers have no way to change the JSON options

2 participants