Skip to content

NewtonsoftJsonSerializationProvider writes object graphs deeper than 64 levels that it then can't read back: Serialize succeeds, Deserialize returns null #155

Closed

Description

@matt-edmondson

What's wrong

NewtonsoftJsonSerializationProvider (Essentials.SerializationProviders.NewtonsoftJson/NewtonsoftJsonSerializationProvider.cs:21) uses a default JsonSerializerSettings. In Newtonsoft.Json 13, MaxDepth defaults to 64 and is enforced only when reading.

  • TrySerialize writes a graph of any depth and returns true.
  • Deserialize<T> on that same output throws JsonReaderException, which the provider catches and turns into default.

The provider can therefore produce data that it cannot read.

Reproduction (on a9a00b9)

A linked chain class Node { string Name; Node? Next; }, round-tripped with Serialize and then Deserialize<Node>:

Depth Serialize Deserialize
64 ok ok
65 ok null
200 ok null

With a file-backed persistence provider using Newtonsoft, StoreAsync reports success but RetrieveAsync returns null. RetrieveOrCreateAsync would then overwrite the stored data with a fresh instance (see #82).

JsonSerializationProvider (System.Text.Json) handles this consistently: for the 200-deep graph, TrySerialize returns false.

Suggested fix

Make writing and reading enforce the same limit. Either:

  • have TrySerialize fail when the output would go deeper than settings.MaxDepth, for example with a JsonTextWriter subclass that checks depth in WriteStartObject/WriteStartArray; or
  • set one explicit MaxDepth (including null for unlimited) that applies to both directions.

Combine this with the buffering fix in the companion issue about TrySerialize leaving truncated output, so a depth failure also leaves the writer empty.

Acceptance criteria

  • Any graph TrySerialize accepts always deserializes back.
  • A test round-trips a 65-deep chain: either both directions succeed, or TrySerialize returns false.

Activity

  1. matt-edmondson commented on Oct 19, 2026

    @matt-edmondson
    ContributorAuthor

    Triage


    Generated by Claude Code

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

Metadata

Metadata

Labels

bugSomething isn't working

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions