Lire en anglais

Partager via


JsonSerializer.Serialize Méthode

Définition

Surcharges

Serialize(Stream, Object, Type, JsonSerializerContext)

Convertit la valeur fournie en texte JSON encodé en UTF-8 et l’écrit dans le Stream.

Serialize(Utf8JsonWriter, Object, Type, JsonSerializerContext)

Écrit une valeur JSON (y compris des objets ou des tableaux) dans l’enregistreur fourni.

Serialize(Utf8JsonWriter, Object, Type, JsonSerializerOptions)

Écrit la représentation JSON du type spécifié dans l’enregistreur fourni.

Serialize(Utf8JsonWriter, Object, JsonTypeInfo)

Écrit une valeur JSON (y compris des objets ou des tableaux) dans l’enregistreur fourni.

Serialize(Stream, Object, Type, JsonSerializerOptions)

Convertit la valeur fournie en texte JSON encodé en UTF-8 et l’écrit dans le Stream.

Serialize(Object, Type, JsonSerializerOptions)

Convertit la valeur d’un type spécifié en chaîne JSON.

Serialize(Stream, Object, JsonTypeInfo)

Convertit la valeur fournie en texte JSON encodé en UTF-8 et l’écrit dans le Stream.

Serialize(Object, JsonTypeInfo)

Convertit la valeur fournie en String.

Serialize(Object, Type, JsonSerializerContext)

Convertit la valeur fournie en String.

Serialize<TValue>(Utf8JsonWriter, TValue, JsonSerializerOptions)

Écrit la représentation JSON d’un type spécifié par un paramètre de type générique dans l’enregistreur fourni.

Serialize<TValue>(TValue, JsonSerializerOptions)

Convertit la valeur d’un type spécifié par un paramètre de type générique en chaîne JSON.

Serialize<TValue>(TValue, JsonTypeInfo<TValue>)

Convertit la valeur fournie en String.

Serialize<TValue>(Stream, TValue, JsonSerializerOptions)

Convertit la valeur fournie en texte JSON encodé en UTF-8 et l’écrit dans le Stream.

Serialize<TValue>(Stream, TValue, JsonTypeInfo<TValue>)

Convertit la valeur fournie en texte JSON encodé en UTF-8 et l’écrit dans le Stream.

Serialize<TValue>(Utf8JsonWriter, TValue, JsonTypeInfo<TValue>)

Écrit une valeur JSON (y compris des objets ou des tableaux) dans l’enregistreur fourni.

Serialize(Stream, Object, Type, JsonSerializerContext)

Source:
JsonSerializer.Write.Stream.cs
Source:
JsonSerializer.Write.Stream.cs
Source:
JsonSerializer.Write.Stream.cs
Source:
JsonSerializer.Write.Stream.cs
Source:
JsonSerializer.Write.Stream.cs
Source:
JsonSerializer.Write.Stream.cs

Convertit la valeur fournie en texte JSON encodé en UTF-8 et l’écrit dans le Stream.

C#
public static void Serialize(System.IO.Stream utf8Json, object? value, Type inputType, System.Text.Json.Serialization.JsonSerializerContext context);

Paramètres

utf8Json
Stream

L’UTF-8 Stream à écrire.

value
Object

Valeur à convertir.

inputType
Type

Type du value à convertir.

context
JsonSerializerContext

Fournisseur de métadonnées pour les types sérialisables.

Exceptions

inputType n’est pas compatible avec value.

utf8Json, inputTypeou context est null.

Il n’existe aucune JsonConverter compatible pour inputType ou ses membres sérialisables.

S’applique à

.NET 10 et autres versions
Produit Versions
.NET 6, 7, 8 (package-provided), 8, 9 (package-provided), 9, 10 (package-provided), 10
.NET Standard 2.0 (package-provided)

Serialize(Utf8JsonWriter, Object, Type, JsonSerializerContext)

Source:
JsonSerializer.Write.Utf8JsonWriter.cs
Source:
JsonSerializer.Write.Utf8JsonWriter.cs
Source:
JsonSerializer.Write.Utf8JsonWriter.cs
Source:
JsonSerializer.Write.Utf8JsonWriter.cs
Source:
JsonSerializer.Write.Utf8JsonWriter.cs
Source:
JsonSerializer.Write.Utf8JsonWriter.cs

Écrit une valeur JSON (y compris des objets ou des tableaux) dans l’enregistreur fourni.

C#
public static void Serialize(System.Text.Json.Utf8JsonWriter writer, object? value, Type inputType, System.Text.Json.Serialization.JsonSerializerContext context);

Paramètres

writer
Utf8JsonWriter

Enregistreur JSON dans lequel écrire.

value
Object

Valeur à convertir et à écrire.

inputType
Type

Type du value à convertir.

context
JsonSerializerContext

Fournisseur de métadonnées pour les types sérialisables.

Exceptions

inputType n’est pas compatible avec value.

writer ou inputType est null.

Il n’existe aucune JsonConverter compatible pour inputType ou ses membres sérialisables.

La méthode GetTypeInfo(Type) du context fourni retourne null pour le type à convertir.

S’applique à

.NET 10 et autres versions
Produit Versions
.NET 6, 7, 8 (package-provided), 8, 9 (package-provided), 9, 10 (package-provided), 10
.NET Standard 2.0 (package-provided)

Serialize(Utf8JsonWriter, Object, Type, JsonSerializerOptions)

Source:
JsonSerializer.Write.Utf8JsonWriter.cs
Source:
JsonSerializer.Write.Utf8JsonWriter.cs
Source:
JsonSerializer.Write.Utf8JsonWriter.cs
Source:
JsonSerializer.Write.Utf8JsonWriter.cs
Source:
JsonSerializer.Write.Utf8JsonWriter.cs
Source:
JsonSerializer.Write.Utf8JsonWriter.cs

Écrit la représentation JSON du type spécifié dans l’enregistreur fourni.

C#
public static void Serialize(System.Text.Json.Utf8JsonWriter writer, object? value, Type inputType, System.Text.Json.JsonSerializerOptions? options = default);
C#
public static void Serialize(System.Text.Json.Utf8JsonWriter writer, object value, Type inputType, System.Text.Json.JsonSerializerOptions options = default);

Paramètres

writer
Utf8JsonWriter

Enregistreur JSON dans lequel écrire.

value
Object

Valeur à convertir et à écrire.

inputType
Type

Type du value à convertir.

options
JsonSerializerOptions

Options pour contrôler le comportement de sérialisation.

Exceptions

inputType n’est pas compatible avec value

writer ou inputType est null.

Il n’existe aucune JsonConverter compatible pour inputType ou ses membres sérialisables.

Remarques

Les JsonWriterOptions utilisées pour créer l’instance du Utf8JsonWriter sont prioritaires sur les JsonSerializerOptions lorsqu’elles sont en conflit. Par conséquent, JsonWriterOptions.Indented, JsonWriterOptions.SkipValidationet JsonWriterOptions.Encoder sont utilisés lors de l’écriture.

Pour plus d’informations, consultez Comment sérialiser et désérialiser json.

S’applique à

.NET 10 et autres versions
Produit Versions
.NET Core 3.0, Core 3.1, 5, 6, 7, 8 (package-provided), 8, 9 (package-provided), 9, 10 (package-provided), 10
.NET Standard 2.0 (package-provided)

Serialize(Utf8JsonWriter, Object, JsonTypeInfo)

Source:
JsonSerializer.Write.Utf8JsonWriter.cs
Source:
JsonSerializer.Write.Utf8JsonWriter.cs
Source:
JsonSerializer.Write.Utf8JsonWriter.cs
Source:
JsonSerializer.Write.Utf8JsonWriter.cs
Source:
JsonSerializer.Write.Utf8JsonWriter.cs

Écrit une valeur JSON (y compris des objets ou des tableaux) dans l’enregistreur fourni.

C#
public static void Serialize(System.Text.Json.Utf8JsonWriter writer, object? value, System.Text.Json.Serialization.Metadata.JsonTypeInfo jsonTypeInfo);

Paramètres

writer
Utf8JsonWriter

Enregistreur à écrire.

value
Object

Valeur à convertir et à écrire.

jsonTypeInfo
JsonTypeInfo

Métadonnées sur le type à convertir.

Exceptions

writer ou jsonTypeInfo est null.

value ne correspond pas au type de jsonTypeInfo.

S’applique à

.NET 10 et autres versions
Produit Versions
.NET 8 (package-provided), 8, 9 (package-provided), 9, 10 (package-provided), 10
.NET Standard 2.0 (package-provided)

Serialize(Stream, Object, Type, JsonSerializerOptions)

Source:
JsonSerializer.Write.Stream.cs
Source:
JsonSerializer.Write.Stream.cs
Source:
JsonSerializer.Write.Stream.cs
Source:
JsonSerializer.Write.Stream.cs
Source:
JsonSerializer.Write.Stream.cs
Source:
JsonSerializer.Write.Stream.cs

Convertit la valeur fournie en texte JSON encodé en UTF-8 et l’écrit dans le Stream.

C#
public static void Serialize(System.IO.Stream utf8Json, object? value, Type inputType, System.Text.Json.JsonSerializerOptions? options = default);

Paramètres

utf8Json
Stream

L’UTF-8 Stream à écrire.

value
Object

Valeur à convertir.

inputType
Type

Type du value à convertir.

options
JsonSerializerOptions

Options permettant de contrôler le comportement de conversion.

Exceptions

inputType n’est pas compatible avec value.

utf8Json ou inputType est null.

Il n’existe aucune JsonConverter compatible pour inputType ou ses membres sérialisables.

S’applique à

.NET 10 et autres versions
Produit Versions
.NET 6, 7, 8 (package-provided), 8, 9 (package-provided), 9, 10 (package-provided), 10
.NET Standard 2.0 (package-provided)

Serialize(Object, Type, JsonSerializerOptions)

Source:
JsonSerializer.Write.String.cs
Source:
JsonSerializer.Write.String.cs
Source:
JsonSerializer.Write.String.cs
Source:
JsonSerializer.Write.String.cs
Source:
JsonSerializer.Write.String.cs
Source:
JsonSerializer.Write.String.cs

Convertit la valeur d’un type spécifié en chaîne JSON.

C#
public static string Serialize(object? value, Type inputType, System.Text.Json.JsonSerializerOptions? options = default);
C#
public static string Serialize(object value, Type inputType, System.Text.Json.JsonSerializerOptions options = default);

Paramètres

value
Object

Valeur à convertir.

inputType
Type

Type du value à convertir.

options
JsonSerializerOptions

Options permettant de contrôler le comportement de conversion.

Retours

Représentation sous forme de chaîne JSON de la valeur.

Exceptions

inputType n’est pas compatible avec value.

inputType est null.

Il n’existe aucune JsonConverter compatible pour inputType ou ses membres sérialisables.

Remarques

L’utilisation d’un String n’est pas aussi efficace que l’encodage UTF-8, car l’implémentation utilise en interne UTF-8. Voir aussi SerializeToUtf8Bytes(Object, Type, JsonSerializerOptions) et SerializeAsync(Stream, Object, Type, JsonSerializerOptions, CancellationToken).

Pour plus d’informations, consultez Comment sérialiser et désérialiser json.

S’applique à

.NET 10 et autres versions
Produit Versions
.NET Core 3.0, Core 3.1, 5, 6, 7, 8 (package-provided), 8, 9 (package-provided), 9, 10 (package-provided), 10
.NET Standard 2.0 (package-provided)

Serialize(Stream, Object, JsonTypeInfo)

Source:
JsonSerializer.Write.Stream.cs
Source:
JsonSerializer.Write.Stream.cs
Source:
JsonSerializer.Write.Stream.cs
Source:
JsonSerializer.Write.Stream.cs
Source:
JsonSerializer.Write.Stream.cs

Convertit la valeur fournie en texte JSON encodé en UTF-8 et l’écrit dans le Stream.

C#
public static void Serialize(System.IO.Stream utf8Json, object? value, System.Text.Json.Serialization.Metadata.JsonTypeInfo jsonTypeInfo);

Paramètres

utf8Json
Stream

L’UTF-8 Stream à écrire.

value
Object

Valeur à convertir.

jsonTypeInfo
JsonTypeInfo

Métadonnées sur le type à convertir.

Exceptions

utf8Json est null.

value ne correspond pas au type de jsonTypeInfo.

S’applique à

.NET 10 et autres versions
Produit Versions
.NET 8 (package-provided), 8, 9 (package-provided), 9, 10 (package-provided), 10
.NET Standard 2.0 (package-provided)

Serialize(Object, JsonTypeInfo)

Source:
JsonSerializer.Write.String.cs
Source:
JsonSerializer.Write.String.cs
Source:
JsonSerializer.Write.String.cs
Source:
JsonSerializer.Write.String.cs
Source:
JsonSerializer.Write.String.cs

Convertit la valeur fournie en String.

C#
public static string Serialize(object? value, System.Text.Json.Serialization.Metadata.JsonTypeInfo jsonTypeInfo);

Paramètres

value
Object

Valeur à convertir.

jsonTypeInfo
JsonTypeInfo

Métadonnées sur le type à convertir.

Retours

Représentation String de la valeur.

Exceptions

jsonTypeInfo est null.

value ne correspond pas au type de jsonTypeInfo.

Remarques

L’utilisation d’un String n’est pas aussi efficace que l’encodage UTF-8, car l’implémentation utilise en interne UTF-8. Voir aussi SerializeToUtf8Bytes(Object, JsonTypeInfo) et SerializeAsync(Stream, Object, JsonTypeInfo, CancellationToken).

S’applique à

.NET 10 et autres versions
Produit Versions
.NET 8 (package-provided), 8, 9 (package-provided), 9, 10 (package-provided), 10
.NET Standard 2.0 (package-provided)

Serialize(Object, Type, JsonSerializerContext)

Source:
JsonSerializer.Write.String.cs
Source:
JsonSerializer.Write.String.cs
Source:
JsonSerializer.Write.String.cs
Source:
JsonSerializer.Write.String.cs
Source:
JsonSerializer.Write.String.cs
Source:
JsonSerializer.Write.String.cs

Convertit la valeur fournie en String.

C#
public static string Serialize(object? value, Type inputType, System.Text.Json.Serialization.JsonSerializerContext context);

Paramètres

value
Object

Valeur à convertir.

inputType
Type

Type du value à convertir.

context
JsonSerializerContext

Fournisseur de métadonnées pour les types sérialisables.

Retours

Représentation String de la valeur.

Exceptions

Il n’existe aucune JsonConverter compatible pour inputType ou ses membres sérialisables.

La méthode GetTypeInfo(Type) du context fourni retourne null pour le type à convertir.

inputType ou context est null.

Remarques

L’utilisation d’un String n’est pas aussi efficace que l’utilisation de l’encodage UTF-8, car l’implémentation utilise en interne UTF-8. Voir également SerializeToUtf8Bytes(Object, Type, JsonSerializerContext) et SerializeAsync(Stream, Object, Type, JsonSerializerContext, CancellationToken).

S’applique à

.NET 10 et autres versions
Produit Versions
.NET 6, 7, 8 (package-provided), 8, 9 (package-provided), 9, 10 (package-provided), 10
.NET Standard 2.0 (package-provided)

Serialize<TValue>(Utf8JsonWriter, TValue, JsonSerializerOptions)

Source:
JsonSerializer.Write.Utf8JsonWriter.cs
Source:
JsonSerializer.Write.Utf8JsonWriter.cs
Source:
JsonSerializer.Write.Utf8JsonWriter.cs
Source:
JsonSerializer.Write.Utf8JsonWriter.cs
Source:
JsonSerializer.Write.Utf8JsonWriter.cs
Source:
JsonSerializer.Write.Utf8JsonWriter.cs

Écrit la représentation JSON d’un type spécifié par un paramètre de type générique dans l’enregistreur fourni.

C#
public static void Serialize<TValue>(System.Text.Json.Utf8JsonWriter writer, TValue value, System.Text.Json.JsonSerializerOptions? options = default);
C#
public static void Serialize<TValue>(System.Text.Json.Utf8JsonWriter writer, TValue value, System.Text.Json.JsonSerializerOptions options = default);

Paramètres de type

TValue

Type de la valeur à sérialiser.

Paramètres

writer
Utf8JsonWriter

Enregistreur JSON dans lequel écrire.

value
TValue

Valeur à convertir et à écrire.

options
JsonSerializerOptions

Options pour contrôler le comportement de sérialisation.

Exceptions

writer est null.

Il n’existe aucune JsonConverter compatible pour TValue ou ses membres sérialisables.

Remarques

Les JsonWriterOptions utilisées pour créer l’instance du Utf8JsonWriter sont prioritaires sur les JsonSerializerOptions lorsqu’elles sont en conflit. Par conséquent, JsonWriterOptions.Indented, JsonWriterOptions.SkipValidationet JsonWriterOptions.Encoder sont utilisés lors de l’écriture.

Pour plus d’informations, consultez Comment sérialiser et désérialiser json.

S’applique à

.NET 10 et autres versions
Produit Versions
.NET Core 3.0, Core 3.1, 5, 6, 7, 8 (package-provided), 8, 9 (package-provided), 9, 10 (package-provided), 10
.NET Standard 2.0 (package-provided)

Serialize<TValue>(TValue, JsonSerializerOptions)

Source:
JsonSerializer.Write.String.cs
Source:
JsonSerializer.Write.String.cs
Source:
JsonSerializer.Write.String.cs
Source:
JsonSerializer.Write.String.cs
Source:
JsonSerializer.Write.String.cs
Source:
JsonSerializer.Write.String.cs

Convertit la valeur d’un type spécifié par un paramètre de type générique en chaîne JSON.

C#
public static string Serialize<TValue>(TValue value, System.Text.Json.JsonSerializerOptions? options = default);
C#
public static string Serialize<TValue>(TValue value, System.Text.Json.JsonSerializerOptions options = default);

Paramètres de type

TValue

Type de la valeur à sérialiser.

Paramètres

value
TValue

Valeur à convertir.

options
JsonSerializerOptions

Options pour contrôler le comportement de sérialisation.

Retours

Représentation sous forme de chaîne JSON de la valeur.

Exceptions

Il n’existe aucune JsonConverter compatible pour TValue ou ses membres sérialisables.

Remarques

L’utilisation d’un String n’est pas aussi efficace que l’encodage UTF-8, car l’implémentation utilise en interne UTF-8. Voir aussi SerializeToUtf8Bytes(Object, Type, JsonSerializerOptions) et SerializeAsync(Stream, Object, Type, JsonSerializerOptions, CancellationToken).

Pour plus d’informations, consultez Comment sérialiser et désérialiser json.

S’applique à

.NET 10 et autres versions
Produit Versions
.NET Core 3.0, Core 3.1, 5, 6, 7, 8 (package-provided), 8, 9 (package-provided), 9, 10 (package-provided), 10
.NET Standard 2.0 (package-provided)

Serialize<TValue>(TValue, JsonTypeInfo<TValue>)

Source:
JsonSerializer.Write.String.cs
Source:
JsonSerializer.Write.String.cs
Source:
JsonSerializer.Write.String.cs
Source:
JsonSerializer.Write.String.cs
Source:
JsonSerializer.Write.String.cs
Source:
JsonSerializer.Write.String.cs

Convertit la valeur fournie en String.

C#
public static string Serialize<TValue>(TValue value, System.Text.Json.Serialization.Metadata.JsonTypeInfo<TValue> jsonTypeInfo);

Paramètres de type

TValue

Type de la valeur à sérialiser.

Paramètres

value
TValue

Valeur à convertir.

jsonTypeInfo
JsonTypeInfo<TValue>

Métadonnées sur le type à convertir.

Retours

Représentation String de la valeur.

Exceptions

Il n’existe aucune JsonConverter compatible pour TValue ou ses membres sérialisables.

jsonTypeInfo est null.

Remarques

L’utilisation d’un String n’est pas aussi efficace que l’encodage UTF-8, car l’implémentation utilise en interne UTF-8. Voir aussi SerializeToUtf8Bytes<TValue>(TValue, JsonTypeInfo<TValue>) et SerializeAsync<TValue>(Stream, TValue, JsonTypeInfo<TValue>, CancellationToken).

S’applique à

.NET 10 et autres versions
Produit Versions
.NET 6, 7, 8 (package-provided), 8, 9 (package-provided), 9, 10 (package-provided), 10
.NET Standard 2.0 (package-provided)

Serialize<TValue>(Stream, TValue, JsonSerializerOptions)

Source:
JsonSerializer.Write.Stream.cs
Source:
JsonSerializer.Write.Stream.cs
Source:
JsonSerializer.Write.Stream.cs
Source:
JsonSerializer.Write.Stream.cs
Source:
JsonSerializer.Write.Stream.cs
Source:
JsonSerializer.Write.Stream.cs

Convertit la valeur fournie en texte JSON encodé en UTF-8 et l’écrit dans le Stream.

C#
public static void Serialize<TValue>(System.IO.Stream utf8Json, TValue value, System.Text.Json.JsonSerializerOptions? options = default);

Paramètres de type

TValue

Type de la valeur à sérialiser.

Paramètres

utf8Json
Stream

L’UTF-8 Stream à écrire.

value
TValue

Valeur à convertir.

options
JsonSerializerOptions

Options permettant de contrôler le comportement de conversion.

Exceptions

utf8Json est null.

Il n’existe aucune JsonConverter compatible pour TValue ou ses membres sérialisables.

S’applique à

.NET 10 et autres versions
Produit Versions
.NET 6, 7, 8 (package-provided), 8, 9 (package-provided), 9, 10 (package-provided), 10
.NET Standard 2.0 (package-provided)

Serialize<TValue>(Stream, TValue, JsonTypeInfo<TValue>)

Source:
JsonSerializer.Write.Stream.cs
Source:
JsonSerializer.Write.Stream.cs
Source:
JsonSerializer.Write.Stream.cs
Source:
JsonSerializer.Write.Stream.cs
Source:
JsonSerializer.Write.Stream.cs
Source:
JsonSerializer.Write.Stream.cs

Convertit la valeur fournie en texte JSON encodé en UTF-8 et l’écrit dans le Stream.

C#
public static void Serialize<TValue>(System.IO.Stream utf8Json, TValue value, System.Text.Json.Serialization.Metadata.JsonTypeInfo<TValue> jsonTypeInfo);

Paramètres de type

TValue

Type de la valeur à sérialiser.

Paramètres

utf8Json
Stream

L’UTF-8 Stream à écrire.

value
TValue

Valeur à convertir.

jsonTypeInfo
JsonTypeInfo<TValue>

Métadonnées sur le type à convertir.

Exceptions

utf8Json est null.

Il n’existe aucune JsonConverter compatible pour TValue ou ses membres sérialisables.

S’applique à

.NET 10 et autres versions
Produit Versions
.NET 6, 7, 8 (package-provided), 8, 9 (package-provided), 9, 10 (package-provided), 10
.NET Standard 2.0 (package-provided)

Serialize<TValue>(Utf8JsonWriter, TValue, JsonTypeInfo<TValue>)

Source:
JsonSerializer.Write.Utf8JsonWriter.cs
Source:
JsonSerializer.Write.Utf8JsonWriter.cs
Source:
JsonSerializer.Write.Utf8JsonWriter.cs
Source:
JsonSerializer.Write.Utf8JsonWriter.cs
Source:
JsonSerializer.Write.Utf8JsonWriter.cs
Source:
JsonSerializer.Write.Utf8JsonWriter.cs

Écrit une valeur JSON (y compris des objets ou des tableaux) dans l’enregistreur fourni.

C#
public static void Serialize<TValue>(System.Text.Json.Utf8JsonWriter writer, TValue value, System.Text.Json.Serialization.Metadata.JsonTypeInfo<TValue> jsonTypeInfo);

Paramètres de type

TValue

Type de la valeur à sérialiser.

Paramètres

writer
Utf8JsonWriter

Enregistreur à écrire.

value
TValue

Valeur à convertir et à écrire.

jsonTypeInfo
JsonTypeInfo<TValue>

Métadonnées sur le type à convertir.

Exceptions

writer ou jsonTypeInfo est null.

Il n’existe aucune JsonConverter compatible pour TValue ou ses membres sérialisables.

S’applique à

.NET 10 et autres versions
Produit Versions
.NET 6, 7, 8 (package-provided), 8, 9 (package-provided), 9, 10 (package-provided), 10
.NET Standard 2.0 (package-provided)