Skip to content

Add JsonElement.ToObject<T>() #40393

Closed
Closed

Description

@MikeStall

AB#1166325

Background and Motivation

We want to fluidly convert between a JsonElement and an object.

Json.Net supports this (https://www.newtonsoft.com/json/help/html/M_Newtonsoft_Json_Linq_JToken_ToObject__1.htm)

Proposed API

Add JsonElement.ToObject() for converting a jsonelement to an object.

Current workaround is to double deserialize, like https://stackoverflow.com/a/61047681

    public static T ToObject<T>(this JsonElement element, JsonSerializerOptions options = null)
    {
        var bufferWriter = new ArrayBufferWriter();
        using (var writer = new Utf8JsonWriter(bufferWriter))
        {
            element.WriteTo(writer);
        }

        return JsonSerializer.Deserialize<T>(bufferWriter.WrittenSpan, options);
    }

see also: #31274

Usage Examples

Alternative Designs

Risks

Risk should be low.

  • This is an additive API, so it shouldn't break anything.
  • Json.Net does this, so the pattern is well understood and used with existing audiences.

Activity

  1. added
    api-suggestion
    on Aug 5, 2020
  2. added
    untriaged
    on Aug 5, 2020
  3. layomia commented on Aug 6, 2020

    @layomia
    Contributor
  4. removed
    untriaged
    on Aug 6, 2020
  5. added this to the 6.0.0 milestone on Aug 6, 2020
  6. layomia commented on Jul 8, 2021

    @layomia
    Contributor

    Related to #31274, cc @steveharter.

  7. danmoseley commented on Jul 21, 2021

    @danmoseley
    Contributor

    @layomia is this still committed for .NET 6?

  8. ericstj commented on Jul 21, 2021

    @ericstj
    Member

    I think this will be covered by @steveharter's proposal #31274. It permits serialization/deserialization from the new writable DOM. I believe there are also ways to go from JsonElement to new DOM.

  9. steveharter commented on Jul 21, 2021

    @steveharter
    Contributor

    Yes this is covered by #31274 and is currently under development for 6.0.

    Specifically to go from a JsonNode instance to an arbitrary object:

    namespace System.Text.Json
    {
        public static partial class JsonSerializer
        {
    +    public static TValue? Deserialize<TValue>(this JsonNode node, JsonSerializerOptions? options = null);
    +    public static TValue? Deserialize<TValue>(this JsonNode node, JsonTypeInfo<TValue> jsonTypeInfo);
    +    public static object? Deserialize(this JsonNode node, Type returnType, JsonSerializerOptions? options = null);
    +    public static object? Deserialize(this JsonNode node, Type returnType, JsonSerializerContext context);
        }
    }
  10. ghost locked as resolved and limited conversation to collaborators on Aug 20, 2021
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

    api-suggestionarea-System.Text.Json

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions