-
Notifications
You must be signed in to change notification settings - Fork 4.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Provide a recipe for creating JsonDocument via the serializer #29501
Comments
I'm tagging this as blocking to have a discussion about it. If this is really cheap to do, I think we should consider it - this will help fill gaps due to |
After discussing with @bartonjs, adding a convenience API on the serializer to return a Especially after we have serializer interop with the writer (see https://github.com/dotnet/corefx/issues/36714), adding an api that returns a document should be quite straightforward. |
This workaround might have been required previously because of lack of dictionary support within the serializer. I believe the caller can now write something like this: private JsonDocument CopyPayload(Dictionary<string, StringValues> content)
{
byte[] utf8JsonData = JsonSerializer.ToBytes<Dictionary<string, StringValues>>(content);
return JsonDocument.Parse(utf8JsonData);
} Given that, I am don't see this issue as blocking. The convenience API is nice to have, but deferring for now. Please let me know if the above solution doesn't work and we can re-evaluate. |
I tried out the above with preview 5, but right now it's throwing an exception:
|
Hmm, corefx issue tracking doesn't tell you what preview work items were completed in... Here's the PR: dotnet/corefx#37186, merged 22 days ago so that might be preview6. |
@Tratcher The merge commit from that PR (dotnet/corefx@c5bb6ab) only says it's part of master. Backing up to dotnet/corefx@431f655 we see one that is tagged as part of preview5. Ergo, I agree with your conclusion 😄. |
@martincostello, are you still seeing an issue on latest preview? |
@ahsonkhan I just tried it out with the following tweaked update to the sample above for preview 6 and got this exception: private JsonDocument CopyPayload(Dictionary<string, StringValues> content)
{
byte[] utf8JsonData = JsonSerializer.ToUtf8Bytes(content);
return JsonDocument.Parse(utf8JsonData);
}
|
cc @steveharter, @layomia |
@martincostello are you still seeing the |
@layomia I haven't tried it out yet - I was waiting for the "official" preview 7 to drop. |
@layomia Still no joy with Using this snippet: private JsonDocument CopyPayload(Dictionary<string, StringValues> content)
{
byte[] utf8JsonData = JsonSerializer.SerializeToUtf8Bytes(content);
return JsonDocument.Parse(utf8JsonData);
} I get this exception:
|
@martincostello Enumerable support in Support for derived types was added in dotnet/corefx#39001 (part of preview 8). I thought this PR would enable support for The solution here is to add a custom converter that implements |
@martincostello My comments above are for deserialization. Your serialization scenario above works now in preview 8/latest code in master. |
@layomia Thanks - I'll try swapping over to it in the providers' PR to swap over to preview 8 once it lands officially. |
@layomia The snippet I posted above now works with preview 8. It has now exposed that the OAuth provider was assuming each key-value pair only had one value in the query string (which in practical terms isn't unreasonable, even if technically incorrect) and was treating the whole thing as a string, so the new recipe actually breaks things as it serializes the payload (correctly) as an array now. I'll need to stick with the original code anyway to ensure the OAuth token is built in the right format, but otherwise the general case for |
Closing as duplicate of https://github.com/dotnet/corefx/issues/42056. The semantics of this feature are covered in the API proposal: namespace System.Text.Json
{
public static partial class JsonSerializer
{
public static TValue Deserialize<TValue>(this JsonDocument document);
public static TValue Deserialize<TValue>(this JsonElement element);
public static JsonDocument SerializeToDocument<TValue>(TValue value);
}
} |
See here: aspnet-contrib/AspNet.Security.OAuth.Providers@9a80d10#diff-f9b7fb28139b9e23c519bca3be7c2930R154
This is a community project that @Tratcher was helping with, so this could be considered external customer feedback.
They are trying to use
JsonDocument
as part of their object model to represent some unstructured JSON data, but there's no easy recipe to create aJsonDocument
unless you are starting from a string or bytes.Here's the cool workaround that they used:
This example uses a dictionary, but there are cases where they have the data already is structured like a POCO object.
A proposal to make this much better would be the serializer could be used to create a
JsonDocument
directly. This would work really well for cases where you need to transform strongly-typed data into a "JSON Container". We'll also end up needing this anyway once it becomes possible to mutateJsonDocument
.In JSON.NET this is exposed as: https://www.newtonsoft.com/json/help/html/M_Newtonsoft_Json_Linq_JObject_FromObject.htm
The text was updated successfully, but these errors were encountered: