Skip to content

Commit e3421a4

Browse files
Merge pull request #43113 from dotnet/main
Merge main into live
2 parents 0df9ac5 + 6d63bec commit e3421a4

26 files changed

+307
-266
lines changed

.openpublishing.redirection.standard.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -739,6 +739,11 @@
739739
"source_path_from_root": "/docs/standard/serialization/system-text-json-use-dom-utf8jsonreader-utf8jsonwriter.md",
740740
"redirect_url": "/dotnet/standard/serialization/system-text-json/use-dom"
741741
},
742+
{
743+
"source_path_from_root": "/docs/standard/serialization/system-text-json/json-schema-exporter.md",
744+
"redirect_url": "/dotnet/standard/serialization/system-text-json/extract-schema",
745+
"redirect_document_id": true
746+
},
742747
{
743748
"source_path_from_root": "/docs/standard/serialization/system-text-json/use-dom-utf8jsonreader-utf8jsonwriter.md",
744749
"redirect_url": "/dotnet/standard/serialization/system-text-json/use-dom",

docs/core/whats-new/dotnet-9/libraries.md

Lines changed: 3 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -493,33 +493,7 @@ If you want to serialize with the [default options that ASP.NET Core uses](../..
493493

494494
JSON is frequently used to represent types in method signatures as part of remote procedure&ndash;calling schemes. It's used, for example, as part of OpenAPI specifications, or as part of tool calling with AI services like those from OpenAI. Developers can serialize and deserialize .NET types as JSON using <xref:System.Text.Json>. But they also need to be able to get a JSON schema that describes the shape of the .NET type (that is, describes the shape of what would be serialized and what can be deserialized). <xref:System.Text.Json> now provides the <xref:System.Text.Json.Schema.JsonSchemaExporter> type, which supports generating a JSON schema that represents a .NET type.
495495

496-
The following code generates a JSON schema from a type.
497-
498-
:::code language="csharp" source="../snippets/dotnet-9/csharp/Serialization.cs" id="Schema":::
499-
500-
The type is defined as follows:
501-
502-
:::code language="csharp" source="../snippets/dotnet-9/csharp/Serialization.cs" id="Book":::
503-
504-
The generated schema is:
505-
506-
```json
507-
{
508-
"type": ["object", "null"],
509-
"properties": {
510-
"Title": {
511-
"type": "string"
512-
},
513-
"Author": {
514-
"type": ["string", "null"]
515-
},
516-
"PublishYear": {
517-
"type": "integer"
518-
}
519-
},
520-
"required": ["Title"]
521-
}
522-
```
496+
For more information, see [JSON schema exporter](../../../standard/serialization/system-text-json/extract-schema.md).
523497

524498
### Respect nullable annotations
525499

@@ -588,50 +562,9 @@ enum MyEnum
588562

589563
### Stream multiple JSON documents
590564

591-
<xref:System.Text.Json.Utf8JsonReader?displayProperty=nameWithType> now supports reading multiple, whitespace-separated JSON documents from a single buffer or stream. By default, the reader throws an exception if it detects any non-whitespace characters that are trailing the first top-level document. You can change this behavior using the <xref:System.Text.Json.JsonReaderOptions.AllowMultipleValues> flag:
592-
593-
```csharp
594-
JsonReaderOptions options = new() { AllowMultipleValues = true };
595-
Utf8JsonReader reader = new("null {} 1 \r\n [1,2,3]"u8, options);
596-
597-
reader.Read();
598-
Console.WriteLine(reader.TokenType); // Null
599-
600-
reader.Read();
601-
Console.WriteLine(reader.TokenType); // StartObject
602-
reader.Skip();
603-
604-
reader.Read();
605-
Console.WriteLine(reader.TokenType); // Number
565+
<xref:System.Text.Json.Utf8JsonReader?displayProperty=nameWithType> now supports reading multiple, whitespace-separated JSON documents from a single buffer or stream. By default, the reader throws an exception if it detects any non-whitespace characters that are trailing the first top-level document. You can change this behavior using the <xref:System.Text.Json.JsonReaderOptions.AllowMultipleValues> flag.
606566

607-
reader.Read();
608-
Console.WriteLine(reader.TokenType); // StartArray
609-
reader.Skip();
610-
611-
Console.WriteLine(reader.Read()); // False
612-
```
613-
614-
This flag also makes it possible to read JSON from payloads that might contain trailing data that's invalid JSON:
615-
616-
```csharp
617-
Utf8JsonReader reader = new("[1,2,3] <NotJson/>"u8, new() { AllowMultipleValues = true });
618-
619-
reader.Read();
620-
reader.Skip(); // Success
621-
reader.Read(); // throws JsonReaderException
622-
```
623-
624-
When it comes to streaming deserialization, a new <xref:System.Text.Json.JsonSerializer.DeserializeAsyncEnumerable%60%601(System.IO.Stream,System.Boolean,System.Text.Json.JsonSerializerOptions,System.Threading.CancellationToken)?displayProperty=nameWithType> overload makes streaming multiple top-level values possible. By default, the method attempts to stream elements that are contained in a top-level JSON array. You can toggle this behavior using the new `topLevelValues` flag:
625-
626-
```csharp
627-
ReadOnlySpan<byte> utf8Json = """[0] [0,1] [0,1,1] [0,1,1,2] [0,1,1,2,3]"""u8;
628-
using var stream = new MemoryStream(utf8Json.ToArray());
629-
630-
await foreach (int[] item in JsonSerializer.DeserializeAsyncEnumerable<int[]>(stream, topLevelValues: true))
631-
{
632-
Console.WriteLine(item.Length);
633-
}
634-
```
567+
For more information, see [Read multiple JSON documents](../../../standard/serialization/system-text-json/use-utf8jsonreader.md#read-multiple-json-documents).
635568

636569
## Spans
637570

docs/csharp/whats-new/csharp-13.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ In the same fashion, C# 13 allows `unsafe` contexts in iterator methods. However
110110

111111
Before C# 13, `ref struct` types couldn't be declared as the type argument for a generic type or method. Now, generic type declarations can add an anti-constraint, `allows ref struct`. This anti-constraint declares that the type argument supplied for that type parameter can be a `ref struct` type. The compiler enforces ref safety rules on all instances of that type parameter.
112112

113-
For example, you may declare an interface like the following code:
113+
For example, you may declare a generic type like the following code:
114114

115115
```csharp
116116
public class C<T> where T : allows ref struct

docs/fundamentals/toc.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -786,7 +786,7 @@ items:
786786
- name: Customize contracts
787787
href: ../standard/serialization/system-text-json/custom-contracts.md
788788
- name: Extract JSON schema
789-
href: ../standard/serialization/system-text-json/json-schema-exporter.md
789+
href: ../standard/serialization/system-text-json/extract-schema.md
790790
- name: XML and SOAP serialization
791791
items:
792792
- name: Overview

docs/standard/frameworks.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ Use these guidelines to determine which TFM to use in your app:
104104

105105
You can also specify an optional OS version at the end of an OS-specific TFM, for example, `net6.0-ios15.0`. The version indicates which APIs are available to your app or library. It doesn't control the OS version that your app or library supports at run time. It's used to select the reference assemblies that your project compiles against, and to select assets from NuGet packages. Think of this version as the "platform version" or "OS API version" to disambiguate it from the run-time OS version.
106106

107-
When an OS-specific TFM doesn't specify the platform version explicitly, it has an implied value that can be inferred from the base TFM and platform name. For example, the default platform value for iOS in .NET 6 is `15.0`, which means that `net6.0-ios` is shorthand for the canonical `net6.0-ios15.0` TFM. The implied platform version for a newer base TFM may be higher, for example, a future `net8.0-ios` TFM could map to `net8.0-ios16.0`. The shorthand form is intended for use in project files only, and is expanded to the canonical form by the .NET SDK's MSBuild targets before being passed to other tools, such as NuGet.
107+
When an OS-specific TFM doesn't specify the platform version explicitly, it has an implied value that can be inferred from the base TFM and platform name. For example, the default platform value for Android in .NET 6 is `31.0`, which means that `net6.0-android` is shorthand for the canonical `net6.0-android31.0` TFM. The implied platform version for a newer base TFM may be higher, for example, a future `net8.0-android` TFM could map to `net8.0-android34.0`. The shorthand form is intended for use in project files only, and is expanded to the canonical form by the .NET SDK's MSBuild targets before being passed to other tools, such as NuGet.
108108

109109
The following table shows the default target platform values (TPV) for each .NET release.
110110

@@ -114,7 +114,18 @@ The following table shows the default target platform values (TPV) for each .NET
114114
| .NET 7 | 33.0 | 16.1 | 16.1 | 13.0 | 16.1 | 7.0 | 7.0 |
115115
| .NET 8 | 34.0 | 17.2 | 17.2 | 14.2 | 17.1 | 8.0 | 7.0 |
116116

117-
The .NET SDK is designed to be able to support newly released APIs for an individual platform without a new version of the base TFM. This enables you to access platform-specific functionality without waiting for a major release of .NET. You can gain access to these newly released APIs by incrementing the platform version in the TFM. For example, if the iOS platform added iOS 15.1 APIs in a .NET 6.0.x SDK update, you could access them by using the TFM `net6.0-ios15.1`.
117+
> [!NOTE]
118+
> On Apple platforms (iOS, macOS, tvOS, and Mac Catalyst) in .NET 8 and earlier,
119+
> the default TPV is the latest supported version in the currently installed workload.
120+
> That means that updating the iOS workload in .NET 8, for example, might result in a higher default
121+
> TPV, if support for a new version of iOS has been added in that workload. In the preceding table,
122+
> the default TPV is the one in the initial release for the stated .NET version.
123+
>
124+
> Starting in .NET 9, this special behavior only applies to executable projects.
125+
> The default TPV for library projects now stays the same for the entirety of
126+
> a major .NET release, like all other platforms.
127+
128+
The .NET SDK is designed to be able to support newly released APIs for an individual platform without a new version of the base TFM. This enables you to access platform-specific functionality without waiting for a major release of .NET. You can gain access to these newly released APIs by incrementing the platform version in the TFM. For example, if the Android platform added API level 32 APIs in a .NET 6.0.x SDK update, you could access them by using the TFM `net6.0-android32.0`.
118129

119130
#### Precedence
120131

docs/standard/serialization/system-text-json/json-schema-exporter.md renamed to docs/standard/serialization/system-text-json/extract-schema.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ dev_langs:
88

99
# JSON schema exporter
1010

11-
The new <xref:System.Text.Json.Schema.JsonSchemaExporter> class lets you extract [JSON schema](https://json-schema.org/) documents from .NET types using either a <xref:System.Text.Json.JsonSerializerOptions> or <xref:System.Text.Json.Serialization.Metadata.JsonTypeInfo> instance. The resultant schema provides a specification of the JSON serialization contract for the type.
11+
The <xref:System.Text.Json.Schema.JsonSchemaExporter> class, introduced in .NET 9, lets you extract [JSON schema](https://json-schema.org/) documents from .NET types using either a <xref:System.Text.Json.JsonSerializerOptions> or <xref:System.Text.Json.Serialization.Metadata.JsonTypeInfo> instance. The resultant schema provides a specification of the JSON serialization contract for the .NET type. The schema describes the shape of what would be serialized and what can be deserialized.
1212

1313
The following code snippet shows an example.
1414

docs/standard/serialization/system-text-json/handle-overflow.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ helpviewer_keywords:
1414
ms.topic: how-to
1515
---
1616

17-
# How to handle overflow JSON or use JsonElement or JsonNode in System.Text.Json
17+
# How to handle overflow JSON or use JsonElement or JsonNode
1818

1919
This article shows how to handle overflow JSON with the <xref:System.Text.Json> namespace. It also shows how to deserialize into <xref:System.Text.Json.JsonElement> or <xref:System.Text.Json.Nodes.JsonNode>, as an alternative for other scenarios where the target type might not perfectly match all of the JSON being deserialized.
2020

@@ -86,7 +86,7 @@ The following example shows a round trip from JSON to a deserialized object and
8686

8787
## Deserialize into JsonElement or JsonNode
8888

89-
If you just want to be flexible about what JSON is acceptable for a particular property, an alternative is to deserialize into <xref:System.Text.Json.JsonElement> or <xref:System.Text.Json.Nodes.JsonNode>. Any valid JSON property can be deserialized into `JsonElement` or `JsonNode`. Choose `JsonElement` to create an immutable object or `JsonNode` to create a mutable object.
89+
If you just want to be flexible about what JSON is acceptable for a particular property, an alternative is to deserialize into <xref:System.Text.Json.JsonElement> or <xref:System.Text.Json.Nodes.JsonNode>. Any valid JSON property can be deserialized into `JsonElement` or `JsonNode`. Choose `JsonElement` to create an *immutable* object or `JsonNode` to create a *mutable* object.
9090

9191
The following example shows a round trip from JSON and back to JSON for a class that includes properties of type `JsonElement` and `JsonNode`.
9292

docs/standard/serialization/system-text-json/how-to.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ The following example creates JSON as a string:
3333
:::code language="csharp" source="snippets/how-to/csharp/SerializeBasic.cs" id="all" highlight="23":::
3434
:::code language="vb" source="snippets/how-to/vb/RoundtripToString.vb" id="Serialize":::
3535

36-
The JSON output is minified (whitespace, indentation, and new-line characters are removed) by default.
36+
The JSON output is *minified* (whitespace, indentation, and new-line characters are removed) by default.
3737

3838
The following example uses synchronous code to create a JSON file:
3939

@@ -104,6 +104,8 @@ To pretty-print the JSON output, set <xref:System.Text.Json.JsonSerializerOption
104104
:::code language="csharp" source="snippets/how-to/csharp/SerializeWriteIndented.cs" highlight="24":::
105105
:::code language="vb" source="snippets/how-to/vb/RoundtripToString.vb" id="SerializePrettyPrint":::
106106

107+
Starting in .NET 9, you can also customize the indent character and size using <xref:System.Text.Json.JsonSerializerOptions.IndentCharacter> and <xref:System.Text.Json.JsonSerializerOptions.IndentSize>.
108+
107109
> [!TIP]
108110
> If you use `JsonSerializerOptions` repeatedly with the same options, don't create a new `JsonSerializerOptions` instance each time you use it. Reuse the same instance for every call. For more information, see [Reuse JsonSerializerOptions instances](configure-options.md#reuse-jsonserializeroptions-instances).
109111

docs/standard/serialization/system-text-json/migrate-from-newtonsoft.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -782,8 +782,8 @@ If you need to continue to use `Newtonsoft.Json` for certain target frameworks,
782782

783783
Starting in .NET 9, you can customize the indentation character and size for <xref:System.Text.Json.Utf8JsonWriter> using options exposed by the <xref:System.Text.Json.JsonWriterOptions> struct:
784784

785-
* `JsonWriterOptions.IndentCharacter` <!-- <xref:System.Text.Json.JsonWriterOptions.IndentCharacter> -->
786-
* `JsonWriterOptions.IndentSize` <!-- <xref:System.Text.Json.JsonWriterOptions.IndentSize> -->
785+
* <xref:System.Text.Json.JsonWriterOptions.IndentCharacter?displayProperty=nameWithType>
786+
* <xref:System.Text.Json.JsonWriterOptions.IndentSize?displayProperty=nameWithType>
787787

788788
::: zone-end
789789

docs/standard/serialization/system-text-json/snippets/how-to-contd/csharp/ImmutableTypes.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public struct Forecast
88
public DateTime Date { get; }
99
public int TemperatureC { get; }
1010
public string Summary { get; }
11-
11+
1212
[JsonConstructor]
1313
public Forecast(DateTime date, int temperatureC, string summary) =>
1414
(Date, TemperatureC, Summary) = (date, temperatureC, summary);
@@ -27,7 +27,7 @@ public static void Main()
2727
""";
2828
Console.WriteLine($"Input JSON: {json}");
2929

30-
var options = new JsonSerializerOptions(JsonSerializerDefaults.Web);
30+
var options = JsonSerializerOptions.Web;
3131

3232
Forecast forecast = JsonSerializer.Deserialize<Forecast>(json, options);
3333

0 commit comments

Comments
 (0)