JsonSerializer.Serialize 方法

定义

重载

名称 说明
Serialize(Object, JsonTypeInfo)

将提供的值转换为一个 String

Serialize(Stream, Object, JsonTypeInfo)

将所提供的值转换为 UTF-8 编码的 JSON 文本并将其写入 Stream

Serialize(Object, Type, JsonSerializerOptions)

将指定类型的值转换为 JSON 字符串。

Serialize(Object, Type, JsonSerializerContext)

将提供的值转换为一个 String

Serialize(Utf8JsonWriter, Object, JsonTypeInfo)

将一个 JSON 值(包括对象或数组)写入提供的编写器。

Serialize(Stream, Object, Type, JsonSerializerOptions)

将所提供的值转换为 UTF-8 编码的 JSON 文本并将其写入 Stream

Serialize(Stream, Object, Type, JsonSerializerContext)

将所提供的值转换为 UTF-8 编码的 JSON 文本并将其写入 Stream

Serialize(Utf8JsonWriter, Object, Type, JsonSerializerOptions)

将指定类型的 JSON 表示形式写入提供的编写器。

Serialize(Utf8JsonWriter, Object, Type, JsonSerializerContext)

将一个 JSON 值(包括对象或数组)写入提供的编写器。

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

将一个 JSON 值(包括对象或数组)写入提供的编写器。

Serialize<TValue>(Utf8JsonWriter, TValue, JsonSerializerOptions)

将泛型类型参数指定的类型的 JSON 表示形式写入提供的编写器。

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

将所提供的值转换为 UTF-8 编码的 JSON 文本并将其写入 Stream

Serialize<TValue>(TValue, JsonSerializerOptions)

将泛型类型参数指定的类型的值转换为 JSON 字符串。

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

将提供的值转换为一个 String

Serialize<TValue>(Stream, TValue, JsonSerializerOptions)

将所提供的值转换为 UTF-8 编码的 JSON 文本并将其写入 Stream

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
Source:
JsonSerializer.Write.String.cs

将提供的值转换为一个 String

public:
 static System::String ^ Serialize(System::Object ^ value, System::Text::Json::Serialization::Metadata::JsonTypeInfo ^ jsonTypeInfo);
public static string Serialize(object? value, System.Text.Json.Serialization.Metadata.JsonTypeInfo jsonTypeInfo);
static member Serialize : obj * System.Text.Json.Serialization.Metadata.JsonTypeInfo -> string
Public Function Serialize (value As Object, jsonTypeInfo As JsonTypeInfo) As String

参数

value
Object

要转换的值。

jsonTypeInfo
JsonTypeInfo

要转换的类型的元数据。

返回

String值的表示形式。

例外

jsonTypeInfonull

value 与类型的 jsonTypeInfo类型不匹配。

注解

String使用 UTF-8 编码效率不如使用 UTF-8 编码,因为实现在内部使用 UTF-8。 另请参阅 SerializeToUtf8Bytes(Object, JsonTypeInfo)SerializeAsync(Stream, Object, JsonTypeInfo, CancellationToken)

适用于

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
Source:
JsonSerializer.Write.Stream.cs

将所提供的值转换为 UTF-8 编码的 JSON 文本并将其写入 Stream

public:
 static void Serialize(System::IO::Stream ^ utf8Json, System::Object ^ value, System::Text::Json::Serialization::Metadata::JsonTypeInfo ^ jsonTypeInfo);
public static void Serialize(System.IO.Stream utf8Json, object? value, System.Text.Json.Serialization.Metadata.JsonTypeInfo jsonTypeInfo);
static member Serialize : System.IO.Stream * obj * System.Text.Json.Serialization.Metadata.JsonTypeInfo -> unit
Public Sub Serialize (utf8Json As Stream, value As Object, jsonTypeInfo As JsonTypeInfo)

参数

utf8Json
Stream

要写入到的 UTF-8 Stream

value
Object

要转换的值。

jsonTypeInfo
JsonTypeInfo

要转换的类型的元数据。

例外

utf8Jsonnull

value 与类型的 jsonTypeInfo类型不匹配。

适用于

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
Source:
JsonSerializer.Write.String.cs

将指定类型的值转换为 JSON 字符串。

[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")]
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")]
public static string Serialize(object? value, Type inputType, System.Text.Json.JsonSerializerOptions? options = default);
public static string Serialize(object? value, Type inputType, System.Text.Json.JsonSerializerOptions? options = default);
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")]
public static string Serialize(object? value, Type inputType, System.Text.Json.JsonSerializerOptions? options = default);
public static string Serialize(object value, Type inputType, System.Text.Json.JsonSerializerOptions options = default);
[<System.Diagnostics.CodeAnalysis.RequiresDynamicCode("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")>]
[<System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")>]
static member Serialize : obj * Type * System.Text.Json.JsonSerializerOptions -> string
static member Serialize : obj * Type * System.Text.Json.JsonSerializerOptions -> string
[<System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")>]
static member Serialize : obj * Type * System.Text.Json.JsonSerializerOptions -> string
Public Function Serialize (value As Object, inputType As Type, Optional options As JsonSerializerOptions = Nothing) As String
Public Shared Function Serialize (value As Object, inputType As Type, Optional options As JsonSerializerOptions = Nothing) As String

参数

value
Object

要转换的值。

inputType
Type

要转换的类型 value

options
JsonSerializerOptions

用于控制转换行为的选项。

返回

值的 JSON 字符串表示形式。

属性

例外

inputTypevalue 不兼容。

inputTypenull

无法与JsonConverter序列化成员或其可序列化成员兼容inputType

注解

String使用 UTF-8 编码效率不如使用 UTF-8 编码,因为实现在内部使用 UTF-8。 另请参阅 SerializeToUtf8Bytes(Object, Type, JsonSerializerOptions)SerializeAsync(Stream, Object, Type, JsonSerializerOptions, CancellationToken)

有关详细信息,请参阅 如何序列化和反序列化 JSON

适用于

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
Source:
JsonSerializer.Write.String.cs

将提供的值转换为一个 String

public:
 static System::String ^ Serialize(System::Object ^ value, Type ^ inputType, System::Text::Json::Serialization::JsonSerializerContext ^ context);
public static string Serialize(object? value, Type inputType, System.Text.Json.Serialization.JsonSerializerContext context);
static member Serialize : obj * Type * System.Text.Json.Serialization.JsonSerializerContext -> string
Public Function Serialize (value As Object, inputType As Type, context As JsonSerializerContext) As String

参数

value
Object

要转换的值。

inputType
Type

要转换的类型 value

context
JsonSerializerContext

可序列化类型的元数据提供程序。

返回

String值的表示形式。

例外

无法与JsonConverter序列化成员或其可序列化成员兼容inputType

GetTypeInfo(Type)为要转换的类型提供的context返回null方法。

inputTypecontextnull.

注解

使用 a String 不像使用 UTF-8 编码那么高效,因为实现在内部使用 UTF-8。 另请参阅 SerializeToUtf8Bytes(Object, Type, JsonSerializerContext)SerializeAsync(Stream, Object, Type, JsonSerializerContext, CancellationToken)

适用于

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
Source:
JsonSerializer.Write.Utf8JsonWriter.cs

将一个 JSON 值(包括对象或数组)写入提供的编写器。

public:
 static void Serialize(System::Text::Json::Utf8JsonWriter ^ writer, System::Object ^ value, System::Text::Json::Serialization::Metadata::JsonTypeInfo ^ jsonTypeInfo);
public static void Serialize(System.Text.Json.Utf8JsonWriter writer, object? value, System.Text.Json.Serialization.Metadata.JsonTypeInfo jsonTypeInfo);
static member Serialize : System.Text.Json.Utf8JsonWriter * obj * System.Text.Json.Serialization.Metadata.JsonTypeInfo -> unit
Public Sub Serialize (writer As Utf8JsonWriter, value As Object, jsonTypeInfo As JsonTypeInfo)

参数

writer
Utf8JsonWriter

要写入的编写器。

value
Object

要转换和写入的值。

jsonTypeInfo
JsonTypeInfo

要转换的类型的元数据。

例外

writerjsonTypeInfonull.

value 与类型的 jsonTypeInfo类型不匹配。

适用于

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
Source:
JsonSerializer.Write.Stream.cs

将所提供的值转换为 UTF-8 编码的 JSON 文本并将其写入 Stream

[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")]
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")]
public static void Serialize(System.IO.Stream utf8Json, object? value, Type inputType, System.Text.Json.JsonSerializerOptions? options = default);
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")]
public static void Serialize(System.IO.Stream utf8Json, object? value, Type inputType, System.Text.Json.JsonSerializerOptions? options = default);
public static void Serialize(System.IO.Stream utf8Json, object? value, Type inputType, System.Text.Json.JsonSerializerOptions? options = default);
[<System.Diagnostics.CodeAnalysis.RequiresDynamicCode("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")>]
[<System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")>]
static member Serialize : System.IO.Stream * obj * Type * System.Text.Json.JsonSerializerOptions -> unit
[<System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")>]
static member Serialize : System.IO.Stream * obj * Type * System.Text.Json.JsonSerializerOptions -> unit
static member Serialize : System.IO.Stream * obj * Type * System.Text.Json.JsonSerializerOptions -> unit
Public Sub Serialize (utf8Json As Stream, value As Object, inputType As Type, Optional options As JsonSerializerOptions = Nothing)

参数

utf8Json
Stream

要写入到的 UTF-8 Stream

value
Object

要转换的值。

inputType
Type

要转换的类型 value

options
JsonSerializerOptions

用于控制转换行为的选项。

属性

例外

inputTypevalue 不兼容。

utf8JsoninputTypenull.

无法与JsonConverter序列化成员或其可序列化成员兼容inputType

适用于

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
Source:
JsonSerializer.Write.Stream.cs

将所提供的值转换为 UTF-8 编码的 JSON 文本并将其写入 Stream

public:
 static void Serialize(System::IO::Stream ^ utf8Json, System::Object ^ value, Type ^ inputType, System::Text::Json::Serialization::JsonSerializerContext ^ context);
public static void Serialize(System.IO.Stream utf8Json, object? value, Type inputType, System.Text.Json.Serialization.JsonSerializerContext context);
static member Serialize : System.IO.Stream * obj * Type * System.Text.Json.Serialization.JsonSerializerContext -> unit
Public Sub Serialize (utf8Json As Stream, value As Object, inputType As Type, context As JsonSerializerContext)

参数

utf8Json
Stream

要写入到的 UTF-8 Stream

value
Object

要转换的值。

inputType
Type

要转换的类型 value

context
JsonSerializerContext

可序列化类型的元数据提供程序。

例外

inputTypevalue 不兼容。

utf8JsoninputTypecontextnull

无法与JsonConverter序列化成员或其可序列化成员兼容inputType

适用于

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
Source:
JsonSerializer.Write.Utf8JsonWriter.cs

将指定类型的 JSON 表示形式写入提供的编写器。

[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")]
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")]
public static void Serialize(System.Text.Json.Utf8JsonWriter writer, object? value, Type inputType, System.Text.Json.JsonSerializerOptions? options = default);
public static void Serialize(System.Text.Json.Utf8JsonWriter writer, object? value, Type inputType, System.Text.Json.JsonSerializerOptions? options = default);
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")]
public static void Serialize(System.Text.Json.Utf8JsonWriter writer, object? value, Type inputType, System.Text.Json.JsonSerializerOptions? options = default);
public static void Serialize(System.Text.Json.Utf8JsonWriter writer, object value, Type inputType, System.Text.Json.JsonSerializerOptions options = default);
[<System.Diagnostics.CodeAnalysis.RequiresDynamicCode("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")>]
[<System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")>]
static member Serialize : System.Text.Json.Utf8JsonWriter * obj * Type * System.Text.Json.JsonSerializerOptions -> unit
static member Serialize : System.Text.Json.Utf8JsonWriter * obj * Type * System.Text.Json.JsonSerializerOptions -> unit
[<System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")>]
static member Serialize : System.Text.Json.Utf8JsonWriter * obj * Type * System.Text.Json.JsonSerializerOptions -> unit
Public Sub Serialize (writer As Utf8JsonWriter, value As Object, inputType As Type, Optional options As JsonSerializerOptions = Nothing)
Public Shared Sub Serialize (writer As Utf8JsonWriter, value As Object, inputType As Type, Optional options As JsonSerializerOptions = Nothing)

参数

writer
Utf8JsonWriter

要写入的 JSON 编写器。

value
Object

要转换和写入的值。

inputType
Type

要转换的类型 value

options
JsonSerializerOptions

用于控制序列化行为的选项。

属性

例外

inputTypevalue

writerinputTypenull.

无法与JsonConverter序列化成员或其可序列化成员兼容inputType

注解

JsonWriterOptions用于创建实例的Utf8JsonWriter优先级高于JsonSerializerOptions冲突的时间。 因此,JsonWriterOptions.IndentedJsonWriterOptions.SkipValidationJsonWriterOptions.Encoder在写入时使用。

有关详细信息,请参阅 如何序列化和反序列化 JSON

适用于

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
Source:
JsonSerializer.Write.Utf8JsonWriter.cs

将一个 JSON 值(包括对象或数组)写入提供的编写器。

public:
 static void Serialize(System::Text::Json::Utf8JsonWriter ^ writer, System::Object ^ value, Type ^ inputType, System::Text::Json::Serialization::JsonSerializerContext ^ context);
public static void Serialize(System.Text.Json.Utf8JsonWriter writer, object? value, Type inputType, System.Text.Json.Serialization.JsonSerializerContext context);
static member Serialize : System.Text.Json.Utf8JsonWriter * obj * Type * System.Text.Json.Serialization.JsonSerializerContext -> unit
Public Sub Serialize (writer As Utf8JsonWriter, value As Object, inputType As Type, context As JsonSerializerContext)

参数

writer
Utf8JsonWriter

要写入的 JSON 编写器。

value
Object

要转换和写入的值。

inputType
Type

要转换的类型 value

context
JsonSerializerContext

可序列化类型的元数据提供程序。

例外

inputTypevalue 不兼容。

writerinputTypenull.

无法与JsonConverter序列化成员或其可序列化成员兼容inputType

GetTypeInfo(Type)为要转换的类型提供的context返回null方法。

适用于

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
Source:
JsonSerializer.Write.Utf8JsonWriter.cs

将一个 JSON 值(包括对象或数组)写入提供的编写器。

public:
generic <typename TValue>
 static void Serialize(System::Text::Json::Utf8JsonWriter ^ writer, TValue value, System::Text::Json::Serialization::Metadata::JsonTypeInfo<TValue> ^ jsonTypeInfo);
public static void Serialize<TValue>(System.Text.Json.Utf8JsonWriter writer, TValue value, System.Text.Json.Serialization.Metadata.JsonTypeInfo<TValue> jsonTypeInfo);
static member Serialize : System.Text.Json.Utf8JsonWriter * 'Value * System.Text.Json.Serialization.Metadata.JsonTypeInfo<'Value> -> unit
Public Sub Serialize(Of TValue) (writer As Utf8JsonWriter, value As TValue, jsonTypeInfo As JsonTypeInfo(Of TValue))

类型参数

TValue

要序列化的值的类型。

参数

writer
Utf8JsonWriter

要写入的编写器。

value
TValue

要转换和写入的值。

jsonTypeInfo
JsonTypeInfo<TValue>

要转换的类型的元数据。

例外

writerjsonTypeInfonull.

无法与JsonConverter序列化成员或其可序列化成员兼容TValue

适用于

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
Source:
JsonSerializer.Write.Utf8JsonWriter.cs

将泛型类型参数指定的类型的 JSON 表示形式写入提供的编写器。

[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")]
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")]
public static void Serialize<TValue>(System.Text.Json.Utf8JsonWriter writer, TValue value, System.Text.Json.JsonSerializerOptions? options = default);
public static void Serialize<TValue>(System.Text.Json.Utf8JsonWriter writer, TValue value, System.Text.Json.JsonSerializerOptions? options = default);
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")]
public static void Serialize<TValue>(System.Text.Json.Utf8JsonWriter writer, TValue value, System.Text.Json.JsonSerializerOptions? options = default);
public static void Serialize<TValue>(System.Text.Json.Utf8JsonWriter writer, TValue value, System.Text.Json.JsonSerializerOptions options = default);
[<System.Diagnostics.CodeAnalysis.RequiresDynamicCode("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")>]
[<System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")>]
static member Serialize : System.Text.Json.Utf8JsonWriter * 'Value * System.Text.Json.JsonSerializerOptions -> unit
static member Serialize : System.Text.Json.Utf8JsonWriter * 'Value * System.Text.Json.JsonSerializerOptions -> unit
[<System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")>]
static member Serialize : System.Text.Json.Utf8JsonWriter * 'Value * System.Text.Json.JsonSerializerOptions -> unit
Public Sub Serialize(Of TValue) (writer As Utf8JsonWriter, value As TValue, Optional options As JsonSerializerOptions = Nothing)
Public Shared Sub Serialize(Of TValue) (writer As Utf8JsonWriter, value As TValue, Optional options As JsonSerializerOptions = Nothing)

类型参数

TValue

要序列化的值的类型。

参数

writer
Utf8JsonWriter

要写入的 JSON 编写器。

value
TValue

要转换和写入的值。

options
JsonSerializerOptions

用于控制序列化行为的选项。

属性

例外

writernull

无法与JsonConverter序列化成员或其可序列化成员兼容TValue

注解

JsonWriterOptions用于创建实例的Utf8JsonWriter优先级高于JsonSerializerOptions冲突的时间。 因此,JsonWriterOptions.IndentedJsonWriterOptions.SkipValidationJsonWriterOptions.Encoder在写入时使用。

有关详细信息,请参阅 如何序列化和反序列化 JSON

适用于

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
Source:
JsonSerializer.Write.Stream.cs

将所提供的值转换为 UTF-8 编码的 JSON 文本并将其写入 Stream

public:
generic <typename TValue>
 static void Serialize(System::IO::Stream ^ utf8Json, TValue value, System::Text::Json::Serialization::Metadata::JsonTypeInfo<TValue> ^ jsonTypeInfo);
public static void Serialize<TValue>(System.IO.Stream utf8Json, TValue value, System.Text.Json.Serialization.Metadata.JsonTypeInfo<TValue> jsonTypeInfo);
static member Serialize : System.IO.Stream * 'Value * System.Text.Json.Serialization.Metadata.JsonTypeInfo<'Value> -> unit
Public Sub Serialize(Of TValue) (utf8Json As Stream, value As TValue, jsonTypeInfo As JsonTypeInfo(Of TValue))

类型参数

TValue

要序列化的值的类型。

参数

utf8Json
Stream

要写入到的 UTF-8 Stream

value
TValue

要转换的值。

jsonTypeInfo
JsonTypeInfo<TValue>

要转换的类型的元数据。

例外

utf8Jsonnull

无法与JsonConverter序列化成员或其可序列化成员兼容TValue

适用于

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
Source:
JsonSerializer.Write.String.cs

将泛型类型参数指定的类型的值转换为 JSON 字符串。

[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")]
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")]
public static string Serialize<TValue>(TValue value, System.Text.Json.JsonSerializerOptions? options = default);
public static string Serialize<TValue>(TValue value, System.Text.Json.JsonSerializerOptions? options = default);
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")]
public static string Serialize<TValue>(TValue value, System.Text.Json.JsonSerializerOptions? options = default);
public static string Serialize<TValue>(TValue value, System.Text.Json.JsonSerializerOptions options = default);
[<System.Diagnostics.CodeAnalysis.RequiresDynamicCode("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")>]
[<System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")>]
static member Serialize : 'Value * System.Text.Json.JsonSerializerOptions -> string
static member Serialize : 'Value * System.Text.Json.JsonSerializerOptions -> string
[<System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")>]
static member Serialize : 'Value * System.Text.Json.JsonSerializerOptions -> string
Public Function Serialize(Of TValue) (value As TValue, Optional options As JsonSerializerOptions = Nothing) As String
Public Shared Function Serialize(Of TValue) (value As TValue, Optional options As JsonSerializerOptions = Nothing) As String

类型参数

TValue

要序列化的值的类型。

参数

value
TValue

要转换的值。

options
JsonSerializerOptions

用于控制序列化行为的选项。

返回

值的 JSON 字符串表示形式。

属性

例外

无法与JsonConverter序列化成员或其可序列化成员兼容TValue

注解

String使用 UTF-8 编码效率不如使用 UTF-8 编码,因为实现在内部使用 UTF-8。 另请参阅 SerializeToUtf8Bytes(Object, Type, JsonSerializerOptions)SerializeAsync(Stream, Object, Type, JsonSerializerOptions, CancellationToken)

有关详细信息,请参阅 如何序列化和反序列化 JSON

适用于

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
Source:
JsonSerializer.Write.String.cs

将提供的值转换为一个 String

public:
generic <typename TValue>
 static System::String ^ Serialize(TValue value, System::Text::Json::Serialization::Metadata::JsonTypeInfo<TValue> ^ jsonTypeInfo);
public static string Serialize<TValue>(TValue value, System.Text.Json.Serialization.Metadata.JsonTypeInfo<TValue> jsonTypeInfo);
static member Serialize : 'Value * System.Text.Json.Serialization.Metadata.JsonTypeInfo<'Value> -> string
Public Function Serialize(Of TValue) (value As TValue, jsonTypeInfo As JsonTypeInfo(Of TValue)) As String

类型参数

TValue

要序列化的值的类型。

参数

value
TValue

要转换的值。

jsonTypeInfo
JsonTypeInfo<TValue>

要转换的类型的元数据。

返回

String值的表示形式。

例外

无法与JsonConverter序列化成员或其可序列化成员兼容TValue

jsonTypeInfonull

注解

String使用 UTF-8 编码效率不如使用 UTF-8 编码,因为实现在内部使用 UTF-8。 另请参阅 SerializeToUtf8Bytes<TValue>(TValue, JsonTypeInfo<TValue>)SerializeAsync<TValue>(Stream, TValue, JsonTypeInfo<TValue>, CancellationToken)

适用于

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
Source:
JsonSerializer.Write.Stream.cs

将所提供的值转换为 UTF-8 编码的 JSON 文本并将其写入 Stream

[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")]
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")]
public static void Serialize<TValue>(System.IO.Stream utf8Json, TValue value, System.Text.Json.JsonSerializerOptions? options = default);
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")]
public static void Serialize<TValue>(System.IO.Stream utf8Json, TValue value, System.Text.Json.JsonSerializerOptions? options = default);
public static void Serialize<TValue>(System.IO.Stream utf8Json, TValue value, System.Text.Json.JsonSerializerOptions? options = default);
[<System.Diagnostics.CodeAnalysis.RequiresDynamicCode("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")>]
[<System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")>]
static member Serialize : System.IO.Stream * 'Value * System.Text.Json.JsonSerializerOptions -> unit
[<System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")>]
static member Serialize : System.IO.Stream * 'Value * System.Text.Json.JsonSerializerOptions -> unit
static member Serialize : System.IO.Stream * 'Value * System.Text.Json.JsonSerializerOptions -> unit
Public Sub Serialize(Of TValue) (utf8Json As Stream, value As TValue, Optional options As JsonSerializerOptions = Nothing)

类型参数

TValue

要序列化的值的类型。

参数

utf8Json
Stream

要写入到的 UTF-8 Stream

value
TValue

要转换的值。

options
JsonSerializerOptions

用于控制转换行为的选项。

属性

例外

utf8Jsonnull

无法与JsonConverter序列化成员或其可序列化成员兼容TValue

适用于