|
| 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) |
0 commit comments