Skip to content

Leave the caller's writer open in the Newtonsoft serialization provider [patch] - #109

Merged
matt-edmondson merged 3 commits into
mainfrom
fix/72-newtonsoft-leave-writer-open
Oct 14, 2026
Merged

matt-edmondson merged 3 commits into
mainfrom
fix/72-newtonsoft-leave-writer-open

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

Fixes #72

What changed

  • NewtonsoftJsonSerializationProvider.TrySerialize now creates its JsonTextWriter with CloseOutput = false and flushes it after serializing. Before, disposing it closed the caller's TextWriter and the stream underneath. The next TrySerialize or TrySerializeAsync call into that writer then threw ObjectDisposedException instead of returning false.
  • TrySerialize now also catches IOException and ObjectDisposedException and returns false, as JsonSerializationProvider already does.
  • Deserialize<T> now catches JsonSerializationException, so JSON whose shape doesn't match T (for example [1,2] read into a class) returns default. Previously the exception escaped, while the System.Text.Json provider already returned default. This is the second gap the issue describes.

Tests

I added two contract tests to RoundTripTests, following the issue's acceptance criteria:

  • Serialization_TrySerialize_Twice_Into_One_StreamWriter_Leaves_It_Open runs against every registered ISerializationProvider. It serializes twice into one StreamWriter over a MemoryStream. Both calls must return true, the stream must still be writable, and both documents must be present.
  • Serialization_Json_Deserialize_ShapeMismatch_Returns_Default runs against every JSON provider (FileExtension == ".json").

I reverted the provider change and reran these tests: only the Newtonsoft rows of both tests fail, and every other provider already passes. With the fix they pass. The full suite passes locally (1022 tests).

🤖 Generated with Claude Code

https://claude.ai/code/session_01SWgHqHW7R9hBKkZ6Zzx5Lg


Generated by Claude Code

…er [patch]

TrySerialize disposed a JsonTextWriter with CloseOutput left at true, which
closed the caller's TextWriter and its stream, so the next call threw
ObjectDisposedException instead of returning false. The JSON writer now
leaves the output open and flushes it, and TrySerialize returns false on
IOException and ObjectDisposedException as the System.Text.Json provider
does. Deserialize also returns default for shape-mismatched JSON instead
of letting JsonSerializationException escape.

Fixes #72

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

Copy link
Copy Markdown
Contributor Author

ci / .NET / Analyze & Release on f2d47b6 was cancelled when it reached the job's 30-minute limit ("The job has exceeded the maximum execution time of 30m0s"). No step failed. The same job on #108 finished in 17 minutes, and SonarCloud had already reported a passing quality gate (100% coverage on new code) for this commit before the cancellation. The commit only adds a test file, so I don't think this PR caused the timeout. I'm not porting a fix, because I don't know of one. I've re-run the failed job once.


Generated by Claude Code

Resolve the RoundTripTests conflict by keeping both this branch's
writer-left-open and shape-mismatch tests and main's
Serialization_Ignores_Members_The_Target_Type_Lacks test.

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

Copy link
Copy Markdown

@matt-edmondson
matt-edmondson merged commit 24b9ebd into main Oct 14, 2026
14 checks passed
@matt-edmondson
matt-edmondson deleted the fix/72-newtonsoft-leave-writer-open branch October 14, 2026 04:16
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.

NewtonsoftJsonSerializationProvider.TrySerialize closes the caller's TextWriter and its stream, so a second write throws ObjectDisposedException

2 participants