Skip to content

[STJ Source Gen] Allow stacked JsonSerializerContexts #124889

Open

Description

@eiriktsarpalis

Note

This proposal was drafted with the help of an AI agent. Please review for accuracy and remove this notice once you're satisfied with the content.

Background and motivation

The System.Text.Json source generator performs a full transitive traversal of the type graph for every [JsonSerializable] type. When a type graph includes types from external assemblies that already have their own source-generated JsonSerializerContext, the generator still re-generates metadata for those external types. This causes:

  • Duplicated codegen: The same type metadata is generated in every consuming assembly.
  • Binary size bloat: Real-world applications report 17MB+ / 29% package size increases when 260 models expand to 742 generated types (see System.Text.Json's generator causes considerable package size increase #77897).
  • Slower compilation: The generator traverses and emits code for the entire transitive closure.

Today, users can manually chain contexts using TypeInfoResolverChain or JsonTypeInfoResolver.Combine, but these are runtime-only mechanisms — the source generator still generates all the code at compile time. There is no mechanism to tell the source generator "this assembly already has a canonical context — delegate to it instead of re-generating."

API Proposal

namespace System.Text.Json.Serialization;

[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = false)]
public sealed class DefaultJsonSerializerContextAttribute : JsonAttribute
{
    public DefaultJsonSerializerContextAttribute(Type contextType);
}

public partial class JsonSourceGenerationOptionsAttribute
{
    /// <summary>
    /// When set to true, instructs the source generator to ignore any
    /// DefaultJsonSerializerContextAttribute annotations on referenced assemblies
    /// and traverse the full type graph as it would normally.
    /// </summary>
    public bool ForceFullTypeTraversal { get; set; }
}

API Usage

Library author: designate a canonical context

// In MyLibrary.csproj
using System.Text.Json.Serialization;

[assembly: DefaultJsonSerializerContext(typeof(MyLibrary.LibContext))]

namespace MyLibrary;

public class LibModel
{
    public int Id { get; set; }
    public string Name { get; set; }
    public LibChild Child { get; set; }
}

public class LibChild
{
    public string Value { get; set; }
}

[JsonSerializable(typeof(LibModel))]
public partial class LibContext : JsonSerializerContext { }

App author: automatic delegation

// In MyApp — references MyLibrary
using System.Text.Json.Serialization;
using MyLibrary;

namespace MyApp;

public class AppModel
{
    public string Title { get; set; }
    public LibModel Lib { get; set; }  // From MyLibrary
}

[JsonSerializable(typeof(AppModel))]
internal partial class AppContext : JsonSerializerContext { }

The source generator for AppContext detects [assembly: DefaultJsonSerializerContext] on MyLibrary and:

  1. Skips BFS traversal for LibModel and its transitive dependencies (LibChild, etc.) — these types are not parsed and their subtrees are not explored.
  2. Auto-chains LibContext as a catch-all resolver at the end of the generated IJsonTypeInfoResolver.GetTypeInfo:
// Generated code (simplified)
JsonTypeInfo? IJsonTypeInfoResolver.GetTypeInfo(Type type, JsonSerializerOptions options)
{
    // Local types generated as usual
    if (type == typeof(AppModel))
        return Create_AppModel(options);

    // Catch-all: chain to canonical contexts for types not generated locally.
    // Handles LibModel, LibChild, and any other types the canonical context knows about.
    {
        JsonTypeInfo? typeInfo = ((IJsonTypeInfoResolver)LibContext.Default).GetTypeInfo(type, options);
        if (typeInfo is not null)
        {
            return typeInfo;
        }
    }

    return null;
}

No manual TypeInfoResolverChain configuration is needed — the delegation is automatic.

Opt-out: ForceFullTypeTraversal

// Force full traversal, ignoring any canonical context attributes
[JsonSourceGenerationOptions(ForceFullTypeTraversal = true)]
[JsonSerializable(typeof(AppModel))]
internal partial class AppContext : JsonSerializerContext { }

Design Decisions

  • Assembly-level, AllowMultiple = false: One canonical context per assembly. This is the simplest model and avoids ambiguity about which context "owns" which types. Libraries that need multiple contexts can still define them, but only one is the canonical delegate target.

  • Inherits JsonAttribute: Follows the existing STJ attribute hierarchy pattern. The attribute lives in Common/ so it's shared between the source generator and the runtime library.

  • Property-based coverage check: The source generator determines which types are "covered" by a canonical context by scanning its JsonTypeInfo<T> properties. A type is delegated only if the canonical context has a property returning JsonTypeInfo<T> for that type. This is more precise than checking by assembly alone — the canonical context generates properties for its entire transitive closure, so all types it knows about are covered.

  • BFS traversal skip: When a type is covered by a canonical context, the generator creates a minimal stub instead of parsing properties/constructors. This cuts off the BFS traversal for that type's subtree entirely, avoiding generation of metadata for transitive dependencies.

  • Catch-all resolver chain: After all local typeof checks, the generated resolver chains to each canonical context as a catch-all via ((IJsonTypeInfoResolver)Context.Default).GetTypeInfo(type, options). This correctly forwards the consumer's JsonSerializerOptions, satisfying the IJsonTypeInfoResolver contract. The catch-all pattern handles both directly delegated types and their transitive dependencies (which were never discovered by BFS).

  • Fast-path disabled for types with delegated properties: When an object type has properties whose types are delegated to an external context, the fast-path serializer is not generated for that object. This avoids referencing handlers that don't exist locally. The metadata-based path handles serialization correctly by resolving through the chained context. This is a conservative choice that can be refined in future work.

  • Default context must be public: Because the generated code in the consuming assembly needs to reference LibContext.Default directly, the canonical context type must be public. If an internal context is specified, the source generator emits SYSLIB1226 ("Default JSON serializer context is not accessible") and falls back to generating metadata locally as if the attribute weren't present. This is a deliberate constraint — a library that wants to enable cross-assembly delegation must expose its canonical context publicly.

  • Ambiguity for transitive types: When multiple referenced assemblies define canonical contexts, a type defined in Assembly A is always delegated to Assembly A's canonical context (unambiguous). Ambiguity can only arise for transitive types that multiple canonical contexts happen to generate metadata for. In this case, the first canonical context that returns a non-null JsonTypeInfo wins. This is acceptable undefined behavior.

  • ForceFullTypeTraversal opt-out: When set to true, the generator ignores all [assembly: DefaultJsonSerializerContext] attributes and traverses the full type graph as it does today. This provides a safety valve for consumers who encounter unexpected behavior or need full control.

Alternative Designs

  • Per-type opt-in attribute: An attribute like [JsonDelegateToContext(typeof(LibContext))] on each type would give finer control but adds boilerplate and doesn't solve the core problem — library authors want to say "this assembly has a canonical context" once, not annotate every type.

  • Convention-based discovery: The generator could automatically discover contexts in referenced assemblies without an attribute. This was rejected because it creates ambiguity when an assembly has multiple contexts and provides no explicit opt-in signal.

  • AllowMultiple = true: Supporting multiple canonical contexts per assembly was considered but rejected for simplicity. If a library has multiple contexts, the library author should consolidate into one canonical context or use resolver chains manually.

  • Per-type typeof delegation: An earlier design generated individual typeof checks for each delegated type. This was replaced with catch-all chaining because: (1) it's simpler, (2) it naturally handles transitive dependencies not discovered by BFS, and (3) it avoids generating potentially large blocks of typeof checks for many delegated types.

  • Direct property access: An earlier design delegated by calling LibContext.Default.LibModel directly. This violated the IJsonTypeInfoResolver contract because it returns a JsonTypeInfo bound to the canonical context's JsonSerializerOptions, not the caller's options. The ((IJsonTypeInfoResolver)Context.Default).GetTypeInfo(type, options) pattern correctly forwards options.

Risks

  • Options compatibility: The generated delegation passes the consumer's JsonSerializerOptions to the canonical context's resolver. If the canonical context was designed for different options (e.g., different naming policy), the behavior may differ from what the library author expected. This is consistent with how manual TypeInfoResolverChain works today.

  • No source breaking changes: The attribute is purely additive. Existing code that doesn't use the attribute is unaffected. The source generator's behavior only changes when it encounters the attribute on a referenced assembly.

  • Binary compat: The attribute is a new public type. ForceFullTypeTraversal is a new property on an existing attribute. No existing types or members are modified.

Open Questions

  • Should the source generator emit a diagnostic warning when it encounters [assembly: DefaultJsonSerializerContext] with a contextType that doesn't derive from JsonSerializerContext?
  • Should there be an opt-out mechanism on the consumer side for specific types? E.g., [JsonSerializable(typeof(LibModel), ForceGenerate = true)] to override delegation for a specific type.
  • Should the attribute support AllowMultiple = true for assemblies with multiple domain-specific contexts?

Related Issues

Prototype

eiriktsarpalis@42d52708f50

Activity

  1. added
    api-suggestion
    on Feb 26, 2026
  2. added
    untriaged
    on Feb 26, 2026
  3. added
    source-generator
    on Feb 26, 2026
  4. changed the title [-][API Proposal]: DefaultJsonSerializerContextAttribute for canonical context designation[/-] [+][STJ Source Gen] Allow stacked source JsonSerializerContexts[/+] on Feb 26, 2026
  5. changed the title [-][STJ Source Gen] Allow stacked source JsonSerializerContexts[/-] [+][STJ Source Gen] Allow stacked JsonSerializerContexts[/+] on Feb 26, 2026
  6. dotnet-policy-service commented on Feb 26, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @dotnet/area-system-text-json
    See info in area-owners.md if you want to be subscribed.

  7. MihuBot commented on Feb 26, 2026

    @MihuBot

    I'm a bot. Here is a possible related and/or duplicate issue (I may be wrong):

  8. kevincathcart-cas commented on Mar 4, 2026

    @kevincathcart-cas

    Some thoughts:

    1. It seems like it would probably be a bad idea to ever mark a context as a default context if it used JsonSourceGenerationMode.Serialization for itself, or even for any individual type, since a context that wants to support slow path could end up using the fast-path-only JsonTypeInfo from it.

    2. The proposal talks about skipping BFS beyond directly delegated types. Conceptually this sounds good (and certainly the goal is to not generate metadata for them in the current assembly). However, in order to not be breaking for consumers if a library adds this attribute, wouldn't a full search still be needed when generating a consumer's contexts simply to get a full list of needed JsonTypeInfo<> properties? Right now, I can rely on my context having those for even indirectly referenced types defined in another assembly. If they went away it could break code using them. It would seem to be rather undesirable if adding this attribute to an existing assembly could cause source breaking changes to consumers of that assembly.

    3. The proposal says:

      Ambiguity for transitive types: When multiple referenced assemblies define canonical contexts, a type defined in Assembly A is always delegated to Assembly A's canonical context (unambiguous)....

      Unfortunately, with the fallback-based approach, it doesn't seem like using the metadata from the default context of the type's defining assembly is guaranteed, even when available.

      Assume package Salad references package Lettuce. In a new release Salad added a default context, which included metadata for types from Lettuce, because Lettuce doesn't have a default context. Then Lettuce updates to add a default context, but Salad has not updated again, so is still compiled against the older version of Lettuce. User code references the latest versions of Salad and Lettuce and has its own context. Whether the JsonTypeInfo<Salad.Leaf> will come from Lettuce's default context, or from Salad's default context would depend on the relative order the two are searched in fallback in the user's context. In the prototype the order seems to depend on the order of the assemblies in module.ReferencedAssemblySymbols which I can only guess is either sorted alphabetically for reproducibility or is based the raw order of references in the MsBuild files.

      This could be addressed: By ordering default contexts used for fallback, such that if one assembly with a default context references another, then the default context of the referenced assembly is tried before the default context of referencing assembly (just a simple topological sort). Doing that will guarantee the JsonTypeInfo from the assembly where the type is defined would get preferred, as long as there were no circular references between assemblies with default contexts (if there were circular references between assemblies with default contexts, the generated contexts would likely be broken in the first place).

    4. With the current prototype, it looks like in the worst case trying to lookup a type for which there is no metadata defined anywhere could be exponential in the number of contexts. Consider a long chain of contexts depending on all previous ones. Let's call the innermost C1 the next C2 ... and the outermost CN. When asked to find a JsonTypeInfo CN will check itself and fallback to calling C1 C2 C3 ... C{N-1} (in some order). Each of those will also fall back to calling all smaller numbered ones, and that is a recipe for getting O(2^N) calls.

      This is a pathological case, sure, but one I think could realistically occur. Consider for example, a versioned API using tooling to generate code for each version into a new assembly but with each new assembly only defining stuff that is new or changed, referencing unchanged stuff from previous versions, and each assembly a JsonSerializerContext for all json types used in that version (regardless of where they were defined). Adding default context attribute each to help deduplicate the metadata would make a ton of sense, and would also lead to this exact situation.

    5. The proposal currently says: Fast-path disabled for types with delegated properties

      That would be rather unfortunate (especially since even something as simple as List<string> would likely get pulled from some other default context if any are available), which not only could slow things down, but would make adding a default context in a library breaking for consumers with customized fast-path only contexts. (Beyond the possible break I mentioned in #2).

      But, unless I am missing something, it also seems downright silly, since the generator code already knows how to handle cases where we don't have ready access to a fast path handler: by calling JsonSerializer.Serialize(writer, value.SomeProperty, PropertyType). This would quickly drop back into fast-path if the foreign context has it enabled, and the options are compatible. There even seems to be some potential to even further reduce overhead at a later date. Even if the decision for #2 is to not generate public properties for delegated types, nothing prevents the generator from adding private properties for the directly delegated types so they can use this pattern.

  9. added
    partner-impact
    and removed
    untriaged
    on Mar 27, 2026
  10. added this to the 11.0.0 milestone on Mar 27, 2026
  11. modified the milestones: 11.0.0, 12.0.0 on Aug 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

api-suggestionarea-System.Text.Jsonpartner-impactsource-generator

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions