镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Report a member's own exception as a failed JSON serialize or deserialize [patch] - #203

Open
matt-edmondson wants to merge 1 commit into
mainfrom
fix/201-json-member-exceptions
Open

matt-edmondson wants to merge 1 commit into
mainfrom
fix/201-json-member-exceptions

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

Fixes #201

What was wrong

JsonSerializationProvider.TrySerialize and Deserialize caught only the exception types System.Text.Json throws itself (JsonException, NotSupportedException, IOException, ArgumentException, InvalidOperationException). System.Text.Json runs a type's getters, setters and constructors through compiled delegates, so an exception they throw escapes unwrapped. As a result:

  • TrySerialize threw where it should have returned false.
  • Serialize threw that exception rather than the InvalidOperationException it documents.
  • Deserialize threw where it should have returned default.

Newtonsoft, YAML and TOML already report these cases as failures.

Change

  • Both catches now take every exception except OutOfMemoryException and AccessViolationException, through a single IsFailure filter. Serialize goes through TrySerialize, so it now throws its documented InvalidOperationException.

Tests

With the provider change reverted, the JSON case of each test fails (System.FormatException: unreadable, System.ArithmeticException: 3 is unwritable). With the change, both pass.

Full suite on Linux, net10.0: 1418 tests, 0 failed. The JSON provider builds in Release for every target framework with no warnings.


Generated by Claude Code

…lize [patch]

JsonSerializationProvider caught only the exception types System.Text.Json
throws itself. A getter, setter or constructor runs through a compiled
delegate, so whatever it throws (FormatException, NullReferenceException,
NotImplementedException) escaped TrySerialize, Serialize and Deserialize,
where the other three providers report false or default. Both catches now
take every exception but OutOfMemoryException and AccessViolationException.

The shared throwing-getter test threw InvalidOperationException, which the
JSON provider happened to catch; it now throws FormatException, and a
throwing-setter case holds all four providers to Deserialize's contract.

Fixes #201

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

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

2 participants