Skip to content

Feature Request: Ease a way of constructing JsonObject #54158

Closed

Description

@JohnyL

Using new writeable JSON DOM feature I can easily do the following for simple values:

JsonObject json = new()
{
  ["values"] = JsonValue.Create(new [1, 2, 3])
};

However, when I want to use objects, things get messy. Say, I have a simple Person record:

record Person(int Age, string Name);
Person person = new(Age: 22, Name: "Linda");

I want to create JSON from this record and query its Age property. Alas, JSON API doesn't have similar JsonObject.Create() which would accept an object. Also this doesn't work either:

JsonObject json = new()
{
  ["customer"] = new JsonObject { person }
};

So, I have found several ways in order to create an object:

First way

var bytesPerson = JsonSerializer.SerializeToUtf8Bytes(person);
var reader = new Utf8JsonReader(bytesPerson);
JsonObject json = new()
{
  ["customer"] = JsonObject.Create(JsonElement.ParseValue(ref reader))
};

LOTS of ceremony for simple action.

Second way

We can eliminate using JsonElement by using JsonObject.Parse() instead of JsonObject.Create():

var bytesPerson = JsonSerializer.SerializeToUtf8Bytes(person);
var reader = new Utf8JsonReader(bytesPerson);
JsonObject json = new()
{
    ["customer"] = JsonObject.Parse(ref reader)
};

Better, but again lots of ceremony.

Third way

Use JsonSerializer.Serialize():

JsonObject json = new()
{
  ["customer"] = JsonObject.Parse(JsonSerializer.Serialize(person))
};

Much better, but serialization could be hidden.


So, here's the request: allow JsonObject.Create() accept object alike JsonValue.Create().

Activity

  1. ghost added
    untriaged
    on Jun 14, 2021
  2. eiriktsarpalis commented on Jun 14, 2021

    @eiriktsarpalis
    Member
  3. huoyaoyuan commented on Jun 14, 2021

    @huoyaoyuan
    Member

    This would require reflection

  4. added
    needs-further-triage
    and removed
    untriaged
    on Jul 16, 2021
  5. added this to the 6.0.0 milestone on Jul 16, 2021
  6. steveharter commented on Jul 26, 2021

    @steveharter
    Contributor

    This is covered by #31274. Specifically:

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

    Example of deserialize node to object:

        string json = ...
        JsonNode node = JsonNode.Parse(json);
        Person person = node.Deserialize<Person>(node);

    Example of serialize object to node:

        Person person = ...
        JsonNode node = JsonSerializer.SerializeToNode(person);
  7. ghost locked as resolved and limited conversation to collaborators on Aug 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-System.Text.Jsonneeds-further-triage

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions