Open
Description
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
andJsonElement
types are not used.
To allow the non-source generator to safely link out JsonNode
:
- Hard reference on
JsonExtensionDataAttribute
toJsonElement
andJsonNode
to root these types when that feature is used. - Add a root dependency to
JsonNode
on the setter toJsonSerializerOptions.UnknownTypeHandling
(default is to useJsonElement
, so the setter must be called to change toJsonNode
) 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
orJsonObject
as a property\field\element instead ofSystem.Object
in order to be explicit and avoid having to setUnknownTypeHandling
. - Note any API changes can be reconciled with the potential new support for true polymorphic deserialization being discussed for 6.0.