Skip to content

We should be able serialize and deserialize from DOM #31274

@terrajobst

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

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions