Skip to content

Leave System.Text.Json's own types to its built-in converters [patch] - #101

Merged
matt-edmondson merged 2 commits into
mainfrom
fix/98-exclude-stj-types
Sep 27, 2026
Merged

matt-edmondson merged 2 commits into
mainfrom
fix/98-exclude-stj-types

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

Fixes #98

What changed

JsonNode.Parse(string, …) and JsonDocument.Parse(string, …) return their own types and have only optional extra parameters, so CanConvert accepted them. Converters in options.Converters take priority over STJ's built-in converters, so registering the factory took these types over: they were written as a quoted string, and object JSON failed with "Expected string token, got StartObject".

IsBuiltInType now also excludes every type whose namespace is System.Text.Json or starts with System.Text.Json.. That covers JsonNode, JsonObject, JsonArray, JsonValue, JsonDocument and JsonElement.

Tests

New SystemTextJsonTypeTests:

  • CanConvert is false for JsonNode, JsonObject, JsonArray, JsonValue, JsonDocument and JsonElement
  • a JsonNode property serializes as {"Node":{"a":1}} and deserializes from object JSON
  • a JsonDocument property round-trips as an object

Without the fix, 7 of the new cases fail and 103 other tests pass. With it, all 110 tests pass. The library builds cleanly for all its target frameworks.

This PR is independent of #100 (for #99). Both start from main and touch different members.

🤖 Generated with Claude Code

https://claude.ai/code/session_01B9RFdjRB3ff72GWre48NPs


Generated by Claude Code

JsonNode and JsonDocument have a Parse(string) the factory accepts, so
registering it took them over: they were written as a quoted string and
object JSON no longer deserialized. IsBuiltInType now excludes every type
in System.Text.Json and its sub-namespaces.

Fixes #98

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01B9RFdjRB3ff72GWre48NPs
Comment thread RoundTripStringJsonConverter.Test/SystemTextJsonTypeTests.cs Fixed
Comment thread RoundTripStringJsonConverter.Test/SystemTextJsonTypeTests.cs Fixed
@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

Development

Successfully merging this pull request may close these issues.

Registering the factory hijacks JsonNode and JsonDocument: they serialize as a quoted string, and ordinary object JSON no longer deserializes

2 participants