Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion docs/standard/datetime/how-to-use-dateonly-timeonly.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,26 @@ Because `TimeOnly` only represents a 24-hour period, it rolls over forwards or b

### Serialize DateOnly and TimeOnly types

[!INCLUDE [dateonly-and-timeonly-serialization](includes/dateonly-and-timeonly-serialization.md)]
<!-- This section is somewhat duplicated in system-text-json-support.md section 'Serialize DateOnly and TimeOnly properties' -->

Starting in .NET 7, `System.Text.Json` supports serializing and deserializing <xref:System.DateOnly> and <xref:System.TimeOnly> types. Consider the following object:

:::code source="./snippets/how-to-use-dateonly-timeonly/csharp/Program.cs" id="appointment":::
:::code source="./snippets/how-to-use-dateonly-timeonly/vb/Program.vb" id="appointment":::

The following example serializes an `Appointment` object, displays the resulting JSON, and then deserializes it back into a new instance of the `Appointment` type. Finally, the original and newly deserialized instances are compared for equality and the results are written to the console:

:::code source="./snippets/how-to-use-dateonly-timeonly/csharp/Program.cs" id="serialization":::
:::code source="./snippets/how-to-use-dateonly-timeonly/vb/Program.vb" id="serialization":::

In the preceding code:

- An `Appointment` object is instantiated and assigned to the `appointment` variable.
- The `appointment` instance is serialized to JSON using <xref:System.Text.Json.JsonSerializer.Serialize%2A?displayProperty=nameWithType>.
- The resulting JSON is written to the console.
- The JSON is deserialized back into a new instance of the `Appointment` type using <xref:System.Text.Json.JsonSerializer.Deserialize%2A?displayProperty=nameWithType>.
- The original and newly deserialized instances are compared for equality.
- The result of the comparison is written to the console.

For more information, see [How to serialize and deserialize JSON in .NET](../serialization/system-text-json/how-to.md).

Expand Down

This file was deleted.

This file was deleted.

19 changes: 18 additions & 1 deletion docs/standard/datetime/system-text-json-support.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,24 @@ If you attempt to read non-compliant formats with <xref:System.Text.Json.Utf8Jso

## Serialize DateOnly and TimeOnly properties

[!INCLUDE [dateonly-and-timeonly-serialization-cs](includes/dateonly-and-timeonly-serialization-cs.md)]
Starting in .NET 7, `System.Text.Json` supports serializing and deserializing <xref:System.DateOnly> and <xref:System.TimeOnly> types. Consider the following object:

<!-- This section is somewhat duplicated in how-to-use-dateonly-timeonly.md section 'Serialize DateOnly and TimeOnly types' -->

:::code source="snippets/how-to-use-dateonly-timeonly/csharp/Program.cs" id="appointment":::

The following example serializes an `Appointment` object, displays the resulting JSON, and then deserializes it back into a new instance of the `Appointment` type. Finally, the original and newly deserialized instances are compared for equality and the results are written to the console:

:::code source="snippets/how-to-use-dateonly-timeonly/csharp/Program.cs" id="serialization":::

In the preceding code:

- An `Appointment` object is instantiated and assigned to the `appointment` variable.
- The `appointment` instance is serialized to JSON using <xref:System.Text.Json.JsonSerializer.Serialize%2A?displayProperty=nameWithType>.
- The resulting JSON is written to the console.
- The JSON is deserialized back into a new instance of the `Appointment` type using <xref:System.Text.Json.JsonSerializer.Deserialize%2A?displayProperty=nameWithType>.
- The original and newly deserialized instances are compared for equality.
- The result of the comparison is written to the console.

## Custom support for <xref:System.DateTime> and <xref:System.DateTimeOffset>

Expand Down