Baza wiedzyCentrum egzaminacyjne
PLPolski
Wybierz język13

CKTIS / C#

System.Text.Json.JsonSerializer: odniesienie praktyczne

System.Text.Json.JsonSerializer: odniesienie praktyczne: JsonSerializer mapuje wartości .NET na JSON i z powrotem według kontraktów typów.

Wersja: .NET API; verified against .NET 10 reference surface

Składnia

System.Text.Json.JsonSerializer /* select a documented member or constructor */

JsonSerializer mapuje wartości .NET na JSON i z powrotem według kontraktów typów.

Stosuj do modeli DTO z jawnymi opcjami i kontekstem source-generated przy AOT.

Domyślne nazwy, null i pola nie zawsze odpowiadają kontraktowi zewnętrznemu.

Błędny JSON zgłasza JsonException; cykle wymagają jawnej konfiguracji.

Kompletny program rzeczywiście używa System.Text.Json.JsonSerializer; oczekiwany stdout to dokładnie [1,2].

Przykłady

using System;
using System.Buffers;
using System.Collections.Concurrent;
using System.Collections.Frozen;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Channels;
using System.Threading.Tasks;
using Number = System.Int32;

Console.WriteLine(JsonSerializer.Serialize(new[] { 1, 2 }));

Wynik

[1,2]

Kompletny program rzeczywiście używa `System.Text.Json.JsonSerializer`; oczekiwany stdout to dokładnie `[1,2]`.

Źródła