镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

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

Closed

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

Activity

  1. added
    api-suggestionEarly API idea and discussion, it is NOT ready for implementation
    on Jun 24, 2023
  2. ghost added
    untriagedNew issue has not been triaged by the area owner
    on Jun 24, 2023
  3. added
    api-ready-for-reviewAPI is ready for review, it is NOT ready for implementation
    and removed
    api-suggestionEarly API idea and discussion, it is NOT ready for implementation
    untriagedNew issue has not been triaged by the area owner
    on Jun 24, 2023
  4. added this to the 8.0.0 milestone on Jun 24, 2023
  5. Sergio0694 commented on Jun 24, 2023

    @Sergio0694
    Contributor

    More of a side question, but out of curiosity:

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

    Why is that the case? I get that they're more advanced and specific APIs (in fact they're in a different namespace too), but why make them entirely not visible? I've found myself using them in a few cases, namely when writing custom converters. It's pretty handy to be able to get some specific built-in converters in those cases and then build your own custom converter on top of them, using composition. Would it make sense to consider making that type not hidden? 🤔

  6. eiriktsarpalis commented on Jun 26, 2023

    @eiriktsarpalis
    MemberAuthor

    The short answer is that these served as proxy APIs back when contract customization wasn't available. The plan is to eventually have the source generator retarget contract customization APIs only and obsolete JsonMetadataServices (although it's a missing a few pieces before that becomes possible).

  7. eiriktsarpalis commented on Jun 27, 2023

    @eiriktsarpalis
    MemberAuthor

    Punting BigInteger from the proposal since we'd probably need to implement a max digit cutoff in the API surface as well.

  8. bartonjs commented on Jul 6, 2023

    @bartonjs
    Member

    Video

    Looks good as proposed. There was discussion to inspire a future change of accepting things based on the new numeric interfaces, but that's not for this issue. There was also a question of whether nint/nuint should be included, and the answer was 'no'.

    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; }
    }
  9. added
    api-approvedAPI was approved in API review, it can be implemented
    and removed
    blockingMarks issues that we want to fast track in order to unblock other important work
    api-ready-for-reviewAPI is ready for review, it is NOT ready for implementation
    on Jul 6, 2023
  10. ghost added
    in-prThere is an active PR which will close this issue when it is merged
    on Jul 16, 2023
  11. ghost removed
    in-prThere is an active PR which will close this issue when it is merged
    on Jul 18, 2023
  12. ghost locked as resolved and limited conversation to collaborators on Aug 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions