Repository navigation
Closed
Labels
Milestone
Description
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);
}
}Reacted by Robert McLaws, isijoc, Ryan Nowak, Martin Zima, Giacomo Bellini, Patrick Kranz, Chao Lu, Andrew Colombi, Mark Ridgwell, Vlad Kovalev and 113 more
Reacted by Alexey Rydaev, Mrxx99 and AraHaan
Reacted by Bryan Hitchcock, Joey Lorich, David Fallah, Soheil Alizadeh, Krzysztof Porebski, jroney, Lemon, Mertsch, Shaun Tonstad, Iain Shepherd and 8 more
Reacted by Mrxx99 and AraHaan
Activity
ericstj commentedon Oct 23, 2019
Might be a dup of https://github.com/dotnet/corefx/issues/37564
bjornen77 commentedon Jan 31, 2020
Is it possible to add the following to the API proposal?
public static JsonElement SerializeToElement(TValue value);
robertmclaws commentedon Jun 7, 2020
Any progress on this? Just ran into the issue again in one of my projects. Thanks!
NoRelect commentedon Jun 28, 2020
I propose to not only have the generics variant, but also one where the
Typecan 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
[-]We should be able serialize and serialize from DOM[/-][+]We should be able serialize and deserialize from DOM[/+]cluetjen commentedon Sep 29, 2020
There should be an overload that allows us to pass in serializer options, e.g. custom converters.
steveharter commentedon Feb 22, 2021
The proposal should also pass
JsonSerializerOptionsas an optional parametes in order to pick up any custom converters added at runtime as well as other options such as null handling.wizofaus commentedon Mar 3, 2021
Would also like this feature, currently using the GetRawText() hack.
thepirat000 commentedon Mar 3, 2021
what's the GetRawText() hack?
12 remaining items
terrajobst commentedon Jul 19, 2021
@robertmclaws
I blame GitHub's UX
steveharter commentedon Jul 20, 2021
Updated the API proposal to include
JsonDocument,JsonElementand alsoJsonNodeper #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:
For V6 there are two choices:
Option (2) will not be as performant depending on the permutation:
SerializeToDocument()will perform an unnecessary buffer alloc\copy.JsonNodewill perform unnecessary escaping and a buffer alloc\copy.steveharter commentedon Jul 20, 2021
In theory, the
JsonDocumentAPIs could be removed which would force usage of theJsonElementAPIs. However, this has some minor usability concerns:document.RootElementwould need to be called to obtain the root element.JsonDocumenthas a perf advantage overJsonElementsince the same buffer that is used by the writer (which contains the JSON obtained from the object) can be held byJsonDocument, avoiding an extra alloc\copy; the buffer is returned back to the pool whendocument.Dispose()is called.JsonDocumentwould need to be changed to takeJsonElementsince it is not possible to obtainJsonDocumentfrom aJsonElement.bartonjs commentedon Jul 20, 2021
Video
Looks good as proposed.
dhhoang commentedon Aug 11, 2021
Is this API available in Preview 7?