Skip to content

Commit b596166

Browse files
Merge pull request #42778 from eiriktsarpalis/improve-stj-whatsnew
Make a few cleanups and improvements to the STJ section of the 'What's New' article.
2 parents 0335694 + 95cee64 commit b596166

File tree

2 files changed

+63
-32
lines changed

2 files changed

+63
-32
lines changed

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

Lines changed: 62 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,8 @@ The following example demonstrates `Regex.EnumerateSplits`, taking a `ReadOnlySp
474474
- [Respect nullable annotations](#respect-nullable-annotations)
475475
- [Require non-optional constructor parameters](#require-non-optional-constructor-parameters)
476476
- [Order JsonObject properties](#order-jsonobject-properties)
477-
- [Additional contract metadata APIs](#additional-contract-metadata-apis)
477+
- [Customize enum member names](#customize-enum-member-names)
478+
- [Stream multiple JSON documents](#stream-multiple-json-documents)
478479

479480
### Indentation options
480481

@@ -515,7 +516,8 @@ The generated schema is:
515516
"PublishYear": {
516517
"type": "integer"
517518
}
518-
}
519+
},
520+
"required": ["Title"]
519521
}
520522
```
521523

@@ -527,11 +529,14 @@ The following code shows how to set the option (the `Book` type definition is sh
527529

528530
:::code language="csharp" source="../snippets/dotnet-9/csharp/Serialization.cs" id="RespectNullable":::
529531

530-
You can also enable this setting globally using the `System.Text.Json.JsonSerializerOptions.RespectNullableAnnotations` feature switch in your project file (for example, _.csproj_ file):
532+
> [!NOTE]
533+
> Due to how nullability annotations are represented in IL, the feature is restricted to annotations of non-generic properties.
534+
535+
You can also enable this setting globally using the `System.Text.Json.Serialization.RespectNullableAnnotationsDefault` feature switch in your project file (for example, _.csproj_ file):
531536

532537
```xml
533538
<ItemGroup>
534-
<RuntimeHostConfigurationOption Include="System.Text.Json.JsonSerializerOptions.RespectNullableAnnotations" Value="true" />
539+
<RuntimeHostConfigurationOption Include="System.Text.Json.Serialization.RespectNullableAnnotationsDefault" Value="true" />
535540
</ItemGroup>
536541
```
537542

@@ -549,11 +554,11 @@ The `MyPoco` type is defined as follows:
549554

550555
:::code language="csharp" source="../snippets/dotnet-9/csharp/Serialization.cs" id="Poco":::
551556

552-
You can also enable this setting globally using the `System.Text.Json.JsonSerializerOptions.RespectRequiredConstructorParameters` feature switch in your project file (for example, _.csproj_ file):
557+
You can also enable this setting globally using the `System.Text.Json.Serialization.RespectRequiredConstructorParametersDefault` feature switch in your project file (for example, _.csproj_ file):
553558

554559
```xml
555560
<ItemGroup>
556-
<RuntimeHostConfigurationOption Include="System.Text.Json.JsonSerializerOptions.RespectRequiredConstructorParameters" Value="true" />
561+
<RuntimeHostConfigurationOption Include="System.Text.Json.Serialization.RespectRequiredConstructorParametersDefault" Value="true" />
557562
</ItemGroup>
558563
```
559564

@@ -565,40 +570,66 @@ The <xref:System.Json.JsonObject> type now exposes ordered dictionary&ndash;like
565570

566571
:::code language="csharp" source="../snippets/dotnet-9/csharp/Serialization.cs" id="PropertyOrder":::
567572

568-
### Additional contract metadata APIs
573+
### Customize enum member names
569574

570-
The JSON contract API now exposes additional metadata including constructor metadata information and improved attribute provider support for the case of the source generator.
571-
572-
The new APIs have the following shape:
575+
The new <xref:System.Text.Json.Serialization.JsonStringEnumMemberNameAttribute?displayProperty=nameWithType> attribute can be used to customize the names of individual enum members for types that are serialized as strings:
573576

574577
```csharp
575-
namespace System.Text.Json.Serialization.Metadata;
578+
JsonSerializer.Serialize(MyEnum.Value1 | MyEnum.Value2); // "Value1, Custom enum value"
576579
577-
public partial class JsonTypeInfo
580+
[Flags, JsonConverter(typeof(JsonStringEnumConverter))]
581+
enum MyEnum
578582
{
579-
// Typically the ConstructorInfo of the active deserialization constructor.
580-
public ICustomAttributeProvider? ConstructorAttributeProvider { get; }
583+
Value1 = 1,
584+
[JsonStringEnumMemberName("Custom enum value")]
585+
Value2 = 2,
581586
}
587+
```
582588

583-
public partial class JsonPropertyInfo
584-
{
585-
public Type DeclaringType { get; }
586-
// Typically the FieldInfo or PropertyInfo of the property.
587-
public ICustomAttributeProvider? AttributeProvider { get; set; }
588-
// The constructor parameter that has been associated with the current property.
589-
public JsonParameterInfo? AssociatedParameter { get; }
590-
}
589+
### Stream multiple JSON documents
590+
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
606+
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());
591629

592-
public sealed class JsonParameterInfo
630+
await foreach (int[] item in JsonSerializer.DeserializeAsyncEnumerable<int[]>(stream, topLevelValues: true))
593631
{
594-
public Type DeclaringType { get; }
595-
public int Position { get; }
596-
public Type ParameterType { get; }
597-
public bool HasDefaultValue { get; }
598-
public object? DefaultValue { get; }
599-
public bool IsNullable { get; }
600-
// Typically the ParameterInfo of the parameter.
601-
public ICustomAttributeProvider? AttributeProvider { get; }
632+
Console.WriteLine(item.Length);
602633
}
603634
```
604635

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ For more information, see [What's new in the .NET 9 runtime](runtime.md).
2626

2727
## .NET libraries
2828

29-
<xref:System.Text.Json> has new options that let you customize the indentation character and size of written JSON. It also includes a new <xref:System.Text.Json.JsonSerializerOptions.Web?displayProperty=nameWithType> singleton that makes it easier to serialize using web defaults.
29+
<xref:System.Text.Json> adds support for nullable reference type annotations and exporting JSON schemas from types. It adds new options that let you customize the indentation of written JSON and read multiple root-level JSON values from a single stream.
3030

3131
In LINQ, the new methods <xref:System.Linq.Enumerable.CountBy%2A> and <xref:System.Linq.Enumerable.AggregateBy%2A> make it possible to aggregate state by key without needing to allocate intermediate groupings via <xref:System.Linq.Enumerable.GroupBy%2A>.
3232

0 commit comments

Comments
 (0)