Skip to content

We should be able serialize and deserialize from DOM #31274

Closed

Description

@terrajobst

Consider this coding snippet of JSON.NET outlined by @robertmclaws:

public async Task<bool> SomeMethod([FromBody] JObject data)
{
    var model = data.ToObject<SomeObject>();
}

Today, you can't do this with System.Text.Json. I propose we add APIs so that you can write this:

public async Task<bool> SomeMethod([FromBody] JsonElement data)
{
    var model = data.Deserialize<SomeObject>();
}

API Proposal

namespace System.Text.Json
{
    public static partial class JsonSerializer
    {
+    public static TValue? Deserialize<TValue>(this JsonDocument document, JsonSerializerOptions? options = null);
+    public static object? Deserialize(this JsonDocument document, Type returnType, JsonSerializerOptions? options = null);
+    public static TValue? Deserialize<TValue>(this JsonDocument document, JsonTypeInfo<TValue> jsonTypeInfo);
+    public static object? Deserialize(this JsonDocument document, Type returnType, JsonSerializerContext context);

+    public static JsonDocument SerializeToDocument<TValue>(TValue value, JsonSerializerOptions? options = null);
+    public static JsonDocument SerializeToDocument(object? value, Type inputType, JsonSerializerOptions? options = null);
+    public static JsonDocument SerializeToDocument<TValue>(TValue value, JsonTypeInfo<TValue> jsonTypeInfo);
+    public static JsonDocument SerializeToDocument(object? value, Type inputType, JsonSerializerContext context);

+    public static TValue? Deserialize<TValue>(this JsonElement element, JsonSerializerOptions? options = null);
+    public static TValue? Deserialize<TValue>(this JsonElement element, JsonTypeInfo<TValue> jsonTypeInfo);
+    public static object? Deserialize(this JsonElement element, Type returnType, JsonSerializerOptions? options = null);
+    public static object? Deserialize(this JsonElement element, Type returnType, JsonSerializerContext context);

+    public static JsonElement SerializeToElement<TValue>(TValue value, JsonSerializerOptions? options = null);
+    public static JsonElement SerializeToElement(object? value, Type inputType, JsonSerializerOptions? options = null);
+    public static JsonElement SerializeToElement<TValue>(TValue value, JsonTypeInfo<TValue> jsonTypeInfo);
+    public static JsonElement SerializeToElement(object? value, Type inputType, JsonSerializerContext context);

+    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);

+    public static JsonNode? SerializeToNode<TValue>(TValue value, JsonSerializerOptions? options = null);
+    public static JsonNode? SerializeToNode(object? value, Type inputType, JsonSerializerOptions? options = null);
+    public static JsonNode? SerializeToNode<TValue>(TValue value, JsonTypeInfo<TValue> jsonTypeInfo);
+    public static JsonNode? SerializeToNode(object? value, Type inputType, JsonSerializerContext context);
    }
}

Activity

  1. ericstj commented on Oct 23, 2019

    @ericstj
    Member
  2. bjornen77 commented on Jan 31, 2020

    @bjornen77
    Contributor

    Is it possible to add the following to the API proposal?

    public static JsonElement SerializeToElement(TValue value);

  3. transferred this issue fromdotnet/corefxon Feb 1, 2020
  4. added this to the 5.0 milestone on Feb 1, 2020
  5. robertmclaws commented on Jun 7, 2020

    @robertmclaws

    Any progress on this? Just ran into the issue again in one of my projects. Thanks!

  6. NoRelect commented on Jun 28, 2020

    @NoRelect

    I propose to not only have the generics variant, but also one where the Type can be specified as a parameter, so use-cases involving reflection are covered as well. The new API would look like this (Also added the suggestion of @bjornen77):

    API Proposal

    namespace System.Text.Json
    {
        public static partial class JsonSerializer
        {
            public static TValue Deserialize<TValue>(this JsonDocument document);
            public static object Deserialize(this JsonDocument document, Type valueType);
            public static TValue Deserialize<TValue>(this JsonElement element);
            public static object Deserialize(this JsonElement element, Type valueType);
    
            public static JsonDocument SerializeToDocument<TValue>(TValue value);
            public static JsonDocument SerializeToDocument(object value, Type valueType);
            public static JsonElement SerializeToElement<TValue>(TValue value);
            public static JsonElement SerializeToElement(object value, Type valueType);
        }
    }
  7. modified the milestones: 5.0.0, Future on Jul 8, 2020
  8. changed the title [-]We should be able serialize and serialize from DOM[/-] [+]We should be able serialize and deserialize from DOM[/+] on Aug 4, 2020
  9. modified the milestones: Future, 6.0.0 on Aug 4, 2020
  10. cluetjen commented on Sep 29, 2020

    @cluetjen

    There should be an overload that allows us to pass in serializer options, e.g. custom converters.

  11. steveharter commented on Feb 22, 2021

    @steveharter
    Contributor

    The proposal should also pass JsonSerializerOptions as an optional parametes in order to pick up any custom converters added at runtime as well as other options such as null handling.

  12. wizofaus commented on Mar 3, 2021

    @wizofaus

    Would also like this feature, currently using the GetRawText() hack.

  13. thepirat000 commented on Mar 3, 2021

    @thepirat000

    Would also like this feature, currently using the GetRawText() hack.

    what's the GetRawText() hack?

  14. 12 remaining items

  15. terrajobst commented on Jul 19, 2021

    @terrajobst
    ContributorAuthor

    @robertmclaws

    Why did this issue get closed in when the problem is not actually solved yet?

    I blame GitHub's UX

    image

  16. steveharter commented on Jul 20, 2021

    @steveharter
    Contributor

    Updated the API proposal to include JsonDocument, JsonElement and also JsonNode per #54158.

    Also all 4 permutations were included (generic + options, non-generic + options, generic + context, non-generic + context).

    The Deserialize methods are extension methods for document\element\node so they can be used with an instance which may help discoverability:

    using JsonDocument dom = JsonDocument.Parse(Json);
    
    // Using the instance
    MyPoco obj1 = dom.Deserialize<MyPoco>();
    
    // Using JsonSerializer
    MyPoco obj2 = JsonSerializer.Deserialize<MyPoco>(dom);

    For V6 there are two choices:

    1. Add these new APIs (special approval necessary since we are passed the Preview 7 cutoff).
    2. Show samples on how to add extensions to your own helper class.

    Option (2) will not be as performant depending on the permutation:

    • The Serialize extensions will perform unnecessary escaping for the temporary JSON needed to bridge between the CLR object and the DOM type. Also SerializeToDocument() will perform an unnecessary buffer alloc\copy.
    • The Deserialize for JsonNode will perform unnecessary escaping and a buffer alloc\copy.
  17. added
    api-ready-for-review
    and removed
    api-suggestion
    on Jul 20, 2021
  18. steveharter commented on Jul 20, 2021

    @steveharter
    Contributor

    In theory, the JsonDocument APIs could be removed which would force usage of the JsonElement APIs. However, this has some minor usability concerns:

    • For serialize, the document.RootElement would need to be called to obtain the root element.
    • For serialize, JsonDocument has a perf advantage over JsonElement since the same buffer that is used by the writer (which contains the JSON obtained from the object) can be held by JsonDocument, avoiding an extra alloc\copy; the buffer is returned back to the pool when document.Dispose() is called.
    • For deserialize, existing user methods that take JsonDocument would need to be changed to take JsonElement since it is not possible to obtain JsonDocument from a JsonElement.
  19. added
    blocking
    on Jul 20, 2021
  20. bartonjs commented on Jul 20, 2021

    @bartonjs
    Member

    Video

    Looks good as proposed.

    namespace System.Text.Json
    {
        public static partial class JsonSerializer
        {
    	       public static TValue? Deserialize<TValue>(this JsonDocument document, JsonSerializerOptions? options = null);
    	       public static object? Deserialize(this JsonDocument document, Type returnType, JsonSerializerOptions? options = null);
    	       public static TValue? Deserialize<TValue>(this JsonDocument document, JsonTypeInfo<TValue> jsonTypeInfo);
    	       public static object? Deserialize(this JsonDocument document, Type returnType, JsonSerializerContext context);
    	  
    	       public static JsonDocument SerializeToDocument<TValue>(TValue value, JsonSerializerOptions? options = null);
    	       public static JsonDocument SerializeToDocument(object? value, Type inputType, JsonSerializerOptions? options = null);
    	       public static JsonDocument SerializeToDocument<TValue>(TValue value, JsonTypeInfo<TValue> jsonTypeInfo);
    	       public static JsonDocument SerializeToDocument(object? value, Type inputType, JsonSerializerContext context);
    	  
    	       public static TValue? Deserialize<TValue>(this JsonElement element, JsonSerializerOptions? options = null);
    	       public static TValue? Deserialize<TValue>(this JsonElement element, JsonTypeInfo<TValue> jsonTypeInfo);
    	       public static object? Deserialize(this JsonElement element, Type returnType, JsonSerializerOptions? options = null);
    	       public static object? Deserialize(this JsonElement element, Type returnType, JsonSerializerContext context);
    	  
    	       public static JsonElement SerializeToElement<TValue>(TValue value, JsonSerializerOptions? options = null);
    	       public static JsonElement SerializeToElement(object? value, Type inputType, JsonSerializerOptions? options = null);
    	       public static JsonElement SerializeToElement<TValue>(TValue value, JsonTypeInfo<TValue> jsonTypeInfo);
    	       public static JsonElement SerializeToElement(object? value, Type inputType, JsonSerializerContext context);
    	  
    	       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);
    	  
    	       public static JsonNode? SerializeToNode<TValue>(TValue value, JsonSerializerOptions? options = null);
    	       public static JsonNode? SerializeToNode(object? value, Type inputType, JsonSerializerOptions? options = null);
    	       public static JsonNode? SerializeToNode<TValue>(TValue value, JsonTypeInfo<TValue> jsonTypeInfo);
    	       public static JsonNode? SerializeToNode(object? value, Type inputType, JsonSerializerContext context);
        }
    }
  21. added
    api-approved
    and removed
    api-ready-for-review
    blocking
    on Jul 20, 2021
  22. ghost added
    in-pr
    on Jul 21, 2021
  23. ghost removed
    in-pr
    on Jul 28, 2021
  24. dhhoang commented on Aug 11, 2021

    @dhhoang
    Contributor

    Is this API available in Preview 7?

  25. ghost locked as resolved and limited conversation to collaborators on Sep 10, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

api-approvedarea-System.Text.Json

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions