-
Notifications
You must be signed in to change notification settings - Fork 5.9k
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
Document mutable DOM #24963
Conversation
There was a problem hiding this 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: |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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: |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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). |
There was a problem hiding this comment.
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( |
There was a problem hiding this comment.
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; } |
There was a problem hiding this comment.
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.
Fixes #24641
Fixes #24252