/
aprogrammer
/
dotnet-docs
Обзор
Документация
Войти
/
aprogrammer
/
dotnet-docs
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
docs/standard/serialization/system-text-json/snippets/nullable-annotations/Nullable.cs
59 строк
1 KB
Genevieve Warren
More STJ updates for .NET 9 (#43168)
23 окт 2024, 20:42
Не верифицирован
23 окт 2024, 20:42
46d3873
Код
Авторство
О чём код?
using System.Text.Json; public static class Nullable1 { // <Unspecified> public static void RunIt() { JsonSerializerOptions options = new() { RespectNullableAnnotations = true }; var result = JsonSerializer.Deserialize<MyPoco>("{}", options); Console.WriteLine(result.Name is null); // True. } class MyPoco { public string Name { get; set; } } // </Unspecified> } public static class Nullable2 { // <Serialization> public static void RunIt() { #nullable enable JsonSerializerOptions options = new() { RespectNullableAnnotations = true }; Person invalidValue = new(Name: null!); JsonSerializer.Serialize(invalidValue, options); } record Person(string Name); // </Serialization> } public static class Nullable3 { // <Deserialization> public static void RunIt() { #nullable enable JsonSerializerOptions options = new() { RespectNullableAnnotations = true }; string json = """{"Name":null}"""; JsonSerializer.Deserialize<Person>(json, options); } record Person(string Name); // </Deserialization> }