Skip to content

Commit ac4ebb1

Browse files
authored
TimeSpan serialization breaking change (#27655)
1 parent 57346ce commit ac4ebb1

File tree

5 files changed

+73
-4
lines changed

5 files changed

+73
-4
lines changed

docs/core/compatibility/6.0.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ If you're migrating an app to .NET 6, the breaking changes listed here might aff
131131

132132
| Title | Binary compatible | Source compatible | Introduced |
133133
| - | - | - | - |
134+
| [Default serialization format for TimeSpan](serialization/6.0/timespan-serialization-format.md) || ✔️ | Servicing 6.0.2 |
134135
| [IAsyncEnumerable serialization](serialization/6.0/iasyncenumerable-serialization.md) | ✔️ || Preview 4 |
135136
| [JSON source-generation API refactoring](serialization/6.0/json-source-gen-api-refactor.md) || ✔️ | RC 2 |
136137
| [JsonNumberHandlingAttribute on collection properties](serialization/6.0/jsonnumberhandlingattribute-behavior.md) || ✔️ | RC 1 |
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
---
2+
title: "Breaking change: Default serialization format for TimeSpan"
3+
description: Learn about the .NET 6 servicing release breaking change where the default serialization format for TimeSpan in source generators has changed.
4+
ms.date: 01/11/2022
5+
---
6+
# Default serialization format for TimeSpan
7+
8+
<xref:System.Text.Json?displayProperty=fullName> added support for <xref:System.TimeSpan> in .NET 6 GA, however this change did not include support for source generators. In .NET 6 servicing release 6.0.2, <xref:System.Text.Json?displayProperty=fullName> includes support for source-generator serialization of <xref:System.TimeSpan> values. This support changes the default serialization format for <xref:System.TimeSpan> values in source generators.
9+
10+
## Previous behavior
11+
12+
In .NET 6 GA, source generators serialize <xref:System.TimeSpan> values by outputting all public properties of the type, which is the default serialization behavior for objects:
13+
14+
```json
15+
{"days":0,"hours":0,"milliseconds":0,"minutes":0,"seconds":1,"ticks":10000000,"totalDays":1.1574074074074073E-05,"totalHours":0.0002777777777777778,"totalMilliseconds":1000,"totalMinutes":0.016666666666666666,"totalSeconds":1}
16+
```
17+
18+
## New behavior
19+
20+
In servicing release .NET 6.0.2, source generators serialize <xref:System.TimeSpan> values in the following format, which is consistent with the reflection-based serializer format:
21+
22+
```json
23+
"00:00:01"
24+
```
25+
26+
## Version introduced
27+
28+
.NET 6.0.2 (servicing release)
29+
30+
## Type of breaking change
31+
32+
This change may affect [binary compatibility](../../categories.md#binary-compatibility).
33+
34+
## Reason for change
35+
36+
[System.Text.Json source generation](../../../../standard/serialization/system-text-json-source-generation.md) is a new feature, and its serialization behavior should be as consistent as possible with the reflection-based serializer. This change simplifies migration to source generators.
37+
38+
## Recommended action
39+
40+
It's unlikely for users to depend on the current <xref:System.TimeSpan> serialization format, as it redundantly outputs all public properties of the type (which is the default serialization behavior for objects), and it doesn't roundtrip.
41+
42+
If you do depend on the existing behavior, the recommended course of action is to [author a custom converter](../../../../standard/serialization/system-text-json-converters-how-to.md) that outputs the needed properties from <xref:System.TimeSpan>:
43+
44+
```csharp
45+
public class TimeSpanConverter : JsonConverter<TimeSpan>
46+
{
47+
public void WriteValue(Utf8JsonWriter writer, TimeSpan value, JsonSerializerOptions options)
48+
{
49+
writer.WriteStartObject();
50+
writer.WriteNumber("days", value.Days);
51+
writer.WriteNumber("hours", value.Hours);
52+
/* insert any needed properties here */
53+
writer.WriteEndObject();
54+
}
55+
}
56+
```
57+
58+
## Affected APIs
59+
60+
- <xref:System.Text.Json.JsonSerializer.Serialize%2A?displayProperty=fullName>
61+
62+
## See also
63+
64+
- [How to use source generation in System.Text.Json](../../../../standard/serialization/system-text-json-source-generation.md)

docs/core/compatibility/toc.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,8 @@ items:
185185
href: sdk/6.0/runtimeidentifier-self-contained.md
186186
- name: Serialization
187187
items:
188+
- name: Default serialization format for TimeSpan
189+
href: serialization/6.0/timespan-serialization-format.md
188190
- name: IAsyncEnumerable serialization
189191
href: serialization/6.0/iasyncenumerable-serialization.md
190192
- name: JSON source-generation API refactoring
@@ -901,6 +903,8 @@ items:
901903
items:
902904
- name: .NET 6
903905
items:
906+
- name: Default serialization format for TimeSpan
907+
href: serialization/6.0/timespan-serialization-format.md
904908
- name: IAsyncEnumerable serialization
905909
href: serialization/6.0/iasyncenumerable-serialization.md
906910
- name: JSON source-generation API refactoring

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ The following sections provide converter samples that address some common scenar
227227
* [Deserialize inferred types to object properties](#deserialize-inferred-types-to-object-properties).
228228
* [Support polymorphic deserialization](#support-polymorphic-deserialization).
229229
* [Support round-trip for Stack\<T>](#support-round-trip-for-stackt).
230-
* [Support enum string value deserialization](#support-enum-string-value-deserialization)
230+
* [Support enum string value deserialization](#support-enum-string-value-deserialization).
231231
::: zone-end
232232

233233
::: zone pivot="dotnet-core-3-1"

docs/standard/serialization/system-text-json-source-generation.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ Here are the preceding examples in a complete program:
7272

7373
You can specify metadata collection mode or serialization optimization mode for an entire context, which may include multiple types. Or you can specify the mode for an individual type. If you do both, the mode specification for a type wins.
7474

75-
* For an entire context, use the [`GenerationMode`](https://github.com/dotnet/runtime/blob/a85d36fed49b8c56d3365417e047fc4306cd74fc/src/libraries/System.Text.Json/Common/JsonSourceGenerationOptionsAttribute.cs#L53-L56) property of `JsonSourceGenerationOptionsAttribute`.
76-
* For an individual type, use the <xref:System.Text.Json.Serialization.JsonSerializableAttribute.GenerationMode> property of <xref:System.Text.Json.Serialization.JsonSerializableAttribute>.
75+
* For an entire context, use the <xref:System.Text.Json.Serialization.JsonSourceGenerationOptionsAttribute.GenerationMode?displayProperty=nameWithType> property.
76+
* For an individual type, use the <xref:System.Text.Json.Serialization.JsonSerializableAttribute.GenerationMode?displayProperty=nameWithType> property.
7777

7878
### Serialization optimization mode example
7979

@@ -113,7 +113,7 @@ You can specify metadata collection mode or serialization optimization mode for
113113

114114
## Specify options for serialization optimization mode
115115

116-
Use [`JsonSourceGenerationOptionsAttribute`](https://github.com/dotnet/runtime/blob/a85d36fed49b8c56d3365417e047fc4306cd74fc/src/libraries/System.Text.Json/Common/JsonSourceGenerationOptionsAttribute.cs) to specify options that are supported by serialization optimization mode. You can use these options without causing a fallback to `JsonSerializer` code. For example, `WriteIndented` and `CamelCase` are supported:
116+
Use <xref:System.Text.Json.Serialization.JsonSourceGenerationOptionsAttribute> to specify options that are supported by serialization optimization mode. You can use these options without causing a fallback to `JsonSerializer` code. For example, `WriteIndented` and `CamelCase` are supported:
117117

118118
:::code language="csharp" source="snippets/system-text-json-source-generation/csharp/SerializeOnlyWithOptions.cs" id="JsonSourceGenerationOptions":::
119119

0 commit comments

Comments
 (0)