Skip to content

Possible linker removal of JsonDocument and JsonNode (+ ancillary classes) to save 35% of STJ.dll size #51756

Open
@steveharter

Description

@steveharter

A prototype showed a 35% reduction of a trimmed System.Text.Json.dll with JsonDocument and JsonNode classes linked out. This also includes ancillary classes like JsonElement, JsonObject, JsonArray and JsonValue.

There are two scenarios for trimming: one with the JSON source generator used, and one without.

  • JsonValue is already trimmed (or should be) if the source generator is used, but not for the non-source generator case.
  • JsonDocument is never currently trimmed.

These can be safely trimmed out if:

  • [JsonExtensionData] is not used
  • Polymorphic deserialization to System.Object members is not used
  • The new JsonNode classes are not used
  • The JsonDocument and JsonElement types are not used.

To allow the non-source generator to safely link out JsonNode:

  • Hard reference on JsonExtensionDataAttribute to JsonElement and JsonNode to root these types when that feature is used.
  • Add a root dependency to JsonNode on the setter to JsonSerializerOptions.UnknownTypeHandling (default is to use JsonElement, so the setter must be called to change to JsonNode) or add alternative API that enables polymorphic (de)serialization in general (see below for potential breaking change).

To allow the source generator to link out JsonDocument the serializer dependencies can be addressed by:

  • A loosely typed converter model for these types, perhaps using reflection to instantiate the converter.
  • Hard references to these types replaced with string-based type name comparisons (similar to how this is done with JsonNode today).
  • The extension property code reworked to support late binding (similar to how this is done with JsonNode today).
  • The reference handling code reworked to support late binding

To allow the non-source generator cases we could introduce a breaking change for polymorphic deserialization of System.Object-declared properties\fields\elements:

  • Change the JsonSerializerOptions.UnknownTypeHandling to add a new default value of "none" (or similar new API).
  • Note that today a user can also specify JsonElement or JsonObject as a property\field\element instead of System.Object in order to be explicit and avoid having to set UnknownTypeHandling.
  • Note any API changes can be reconciled with the potential new support for true polymorphic deserialization being discussed for 6.0.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions