Skip to content
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

Document mutable DOM #24963

Merged
merged 22 commits into from
Jul 28, 2021
Merged

Document mutable DOM #24963

merged 22 commits into from
Jul 28, 2021

Conversation

tdykstra
Copy link
Contributor

@tdykstra tdykstra commented Jul 1, 2021

Fixes #24641
Fixes #24252

File Type File Name Published Url
Content docs/standard/serialization/system-text-json-use-dom-utf8jsonreader-utf8jsonwriter.md https://review.docs.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-use-dom-utf8jsonreader-utf8jsonwriter?branch=main
Content docs/standard/serialization/system-text-json-migrate-from-newtonsoft-how-to.md https://review.docs.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-migrate-from-newtonsoft-how-to?branch=main

@dotnet-bot dotnet-bot added this to the July 2021 milestone Jul 1, 2021
@tdykstra tdykstra closed this Jul 2, 2021
@tdykstra tdykstra reopened this Jul 2, 2021
@tdykstra tdykstra changed the title WIP: Document mutable DOM Document mutable DOM Jul 2, 2021
@tdykstra tdykstra marked this pull request as ready for review July 2, 2021 19:19
@tdykstra tdykstra requested review from layomia, CamSoper and a team July 2, 2021 19:19
@tdykstra tdykstra closed this Jul 21, 2021
@tdykstra tdykstra reopened this Jul 21, 2021
@tdykstra tdykstra merged commit 269adc3 into dotnet:main Jul 28, 2021
@tdykstra tdykstra added the okr-freshness OKR: Freshness of content label Sep 14, 2021
Copy link
Contributor

@layomia layomia left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few questions but looks great!

The following sections show how to use these APIs for reading and writing JSON.
* A [JSON Document Object Model (DOM)](#json-dom-choices) for random access to data in a JSON payload.
* The [`Utf8JsonWriter`](#use-utf8jsonwriter) type for building custom serializers.
* The [`Utf8JsonReader`](#use-utf8jsonreader) type for building custom parsers and deserializers.

> [!NOTE]
> The article about migrating from Newtonsoft has more information about these APIs. See the following sections in that article:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I read this to mean the referenced article has more detailed info about these APIs. Seems weird given that the current one is dedicated to these APIs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm moving the content that isn't specific to migration from Newtonsoft from the Newtonsoft article to this article, which eliminates the need for this note.

## Use JsonDocument for access to data
## JSON DOM choices

Working with a DOM is an alternative to deserialization:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: to deserialization *with JsonSerializer

:::zone pivot="dotnet-6-0,dotnet-5-0,dotnet-core-3-1"
<xref:System.Text.Json.JsonDocument> provides the ability to build a read-only Document Object Model (DOM) by using `Utf8JsonReader`. The DOM provides random access to data in a JSON payload. The JSON elements that compose the payload can be accessed via the <xref:System.Text.Json.JsonElement> type. The `JsonElement` type provides array and object enumerators along with APIs to convert JSON text to common .NET types. `JsonDocument` exposes a <xref:System.Text.Json.JsonDocument.RootElement> property.
:::zone-end
# How to use a DOM, Utf8JsonReader, and Utf8JsonWriter in System.Text.Json
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit, since this is the first occurrence: How to use a JSON document (DOM)

@@ -738,7 +738,7 @@ Searches for JSON tokens using `JObject` or `JArray` from `Newtonsoft.Json` tend
* Use the built-in enumerators (<xref:System.Text.Json.JsonElement.EnumerateArray%2A> and <xref:System.Text.Json.JsonElement.EnumerateObject%2A>) rather than doing your own indexing or loops.
* Don't do a sequential search on the whole `JsonDocument` through every property by using `RootElement`. Instead, search on nested JSON objects based on the known structure of the JSON data. For example, if you're looking for a `Grade` property in `Student` objects, loop through the `Student` objects and get the value of `Grade` for each, rather than searching through all `JsonElement` objects looking for `Grade` properties. Doing the latter will result in unnecessary passes over the same data.

For a code example, see [Use JsonDocument for access to data](system-text-json-use-dom-utf8jsonreader-utf8jsonwriter.md#use-jsondocument-for-access-to-data).
For a code example, see [Use JsonDocument](system-text-json-use-dom-utf8jsonreader-utf8jsonwriter.md#use-jsondocument).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this section also talk about mutable DOM types? Seems like an omission

};

// Add an object.
forecastObject["TemperatureRanges"].AsObject().Add(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be

forecastObject["TemperatureRanges"]["Hot"] = ...;

{
public class Program
{
public static DateTime[] DatesAvailable { get; set; }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This property is not used.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
4 participants