Skip to content

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

Open

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.

Activity

  1. added
    linkable-framework
    on Apr 23, 2021
  2. ghost added
    untriaged
    on Apr 23, 2021
  3. added
    size-reduction
    and removed
    linkable-framework
    on Apr 23, 2021
  4. added this to the 6.0.0 milestone on Apr 23, 2021
  5. removed
    untriaged
    on Apr 23, 2021
  6. self-assigned this
    on Aug 2, 2021
  7. steveharter commented on Aug 3, 2021

    @steveharter
    ContributorAuthor

    Since the main scenario for trimming is Blazor client, this suggestion will not help much since the JsonElement is used by Blazor. The bulk of JsonNode should already be trimmed for Blazor since it is not used.

    Moving to 7.0 if we want to refactor Blazor to use Utf8JsonReader instead of JsonElement, so that JsonElement can be trimmed.

  8. modified the milestones: 6.0.0, 7.0.0 on Aug 3, 2021
  9. removed their assignment
    on Sep 23, 2021
  10. modified the milestones: 7.0.0, Future on Jul 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.Text.Jsonsize-reduction

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions