Skip to content

[API Proposal]: Add System.Text.Json built-in support for more numeric types #87994

Closed
@eiriktsarpalis

Description

@eiriktsarpalis

Background and motivation

System.Text.Json does not currently have built-in support for Int128/UInt128 and Half.

API Proposal

namespace System.Text.Json.Serialization.Metadata;

public partial class JsonMetadataServices
{
    public static JsonConverter<Int128> Int128Converter { get; }
    public static JsonConverter<UInt128> UInt128Converter { get; }
    public static JsonConverter<Half> HalfConverter { get; }
}

API Usage

The JsonMetadataServices APIs are marked EditorBrowsable.Never and are only intended for use by the source generator.

Alternative Designs

Note that this proposes adding numeric type support on the converter level only and not on the Utf8JsonWriter/Utf8JsonReader types or the DOM types. Since the underlying types do not have support for large numbers representable in Int128 or BigInteger, we need to make use of other primitives. Examples of this can be found in this PR. Deserialization without incurring allocation also necessitates making this change.

Risks

Adding built-in support for new types risks regressing code size for apps that use both reflection and trimming such as Blazor. If folks think this is unacceptable, we might consider making these opt-in specifically for reflection, requiring users to manually touch the relevant converters:

var options = new JsonSerializerOptions {  Converters = { JsonMetadataServices.BigIntegerConverter } };

Although it goes without saying this would introduce inconsistent behavior between the two serializers.

Related to #86442

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions