JsonSerializer.Serialize 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
重载
Serialize(Object, Type, JsonSerializerOptions)
将指定类型的值转换为 JSON 字符串。
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);
static member Serialize : obj * Type * System.Text.Json.JsonSerializerOptions -> string
Public Shared Function Serialize (value As Object, inputType As Type, Optional options As JsonSerializerOptions = Nothing) As String
Public Function Serialize (value As Object, inputType As Type, Optional options As JsonSerializerOptions = Nothing) As String
参数
- value
- Object
要转换的值。
- inputType
- Type
要转换的 value
的类型。
- options
- JsonSerializerOptions
用于控制转换行为的选项。
返回
值的 JSON 字符串表示形式。
例外
inputType
与 value
不兼容。
inputType
为 null
。
inputType
或其可序列化成员没有兼容的 JsonConverter。
注解
String由于实现在内部使用 UTF-8,因此使用 UTF-8 编码没有那么高效。 另请参见 SerializeToUtf8Bytes(Object, Type, JsonSerializerOptions) 和 SerializeAsync(Stream, Object, Type, JsonSerializerOptions, CancellationToken)。
有关详细信息,请参阅 如何序列化和反序列化 JSON。
适用于
Serialize(Object, Type, JsonSerializerContext)
将所提供的值转换为 .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值的表示形式。
例外
inputType
或其可序列化成员没有兼容的 JsonConverter。
GetTypeInfo(Type)提供context
的方法返回null
要转换的类型。
inputType
或 context
为 null
。
注解
String使用 UTF-8 编码效率不如使用,因为实现在内部使用 UTF-8。 另请参阅 SerializeToUtf8Bytes(Object, Type, JsonSerializerContext) 和 SerializeAsync(Stream, Object, Type, JsonSerializerContext, CancellationToken)。
适用于
Serialize(Stream, Object, Type, JsonSerializerOptions)
将所提供的值转换为 UTF-8 编码的 JSON 文本并将其写入 Stream。
public static void Serialize (System.IO.Stream utf8Json, object? value, Type inputType, System.Text.Json.JsonSerializerOptions? options = default);
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)
参数
- value
- Object
要转换的值。
- inputType
- Type
要转换的 value
的类型。
- options
- JsonSerializerOptions
用于控制转换行为的选项。
例外
inputType
与 value
不兼容。
utf8Json
或 inputType
为 null
。
无法与其inputType
可序列化的成员兼容JsonConverter。
适用于
Serialize(Stream, Object, Type, JsonSerializerContext)
将所提供的值转换为 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)
参数
- value
- Object
要转换的值。
- inputType
- Type
要转换的 value
的类型。
- context
- JsonSerializerContext
可序列化类型的元数据提供程序。
例外
inputType
与 value
不兼容。
utf8Json
、inputType
或 context
为 null
。
无法与其inputType
可序列化的成员兼容JsonConverter。
适用于
Serialize(Utf8JsonWriter, Object, Type, JsonSerializerOptions)
将指定类型的 JSON 表示形式写入提供的编写器。
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);
static member Serialize : System.Text.Json.Utf8JsonWriter * obj * Type * System.Text.Json.JsonSerializerOptions -> unit
Public Shared Sub Serialize (writer As Utf8JsonWriter, value As Object, inputType As Type, Optional options As JsonSerializerOptions = Nothing)
Public 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
用于控制序列化行为的选项。
例外
inputType
与 value
不兼容
writer
或 inputType
为 null
。
inputType
或其可序列化成员没有兼容的 JsonConverter。
注解
JsonWriterOptions用于创建优先JsonSerializerOptions于冲突时的实例Utf8JsonWriter。 因此,JsonWriterOptions.IndentedJsonWriterOptions.SkipValidationJsonWriterOptions.Encoder在写入时使用。
有关详细信息,请参阅 如何序列化和反序列化 JSON。
适用于
Serialize(Utf8JsonWriter, Object, Type, JsonSerializerContext)
将一个 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
可序列化类型的元数据提供程序。
例外
inputType
与 value
不兼容。
writer
或 inputType
为 null
。
inputType
或其可序列化成员没有兼容的 JsonConverter。
GetTypeInfo(Type)提供context
的方法返回null
要转换的类型。
适用于
Serialize<TValue>(TValue, JsonSerializerOptions)
将泛型类型参数指定的类型的值转换为 JSON 字符串。
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);
static member Serialize : 'Value * System.Text.Json.JsonSerializerOptions -> string
Public Shared Function Serialize(Of TValue) (value As TValue, Optional options As JsonSerializerOptions = Nothing) As String
Public Function Serialize(Of TValue) (value As TValue, Optional options As JsonSerializerOptions = Nothing) As String
类型参数
- TValue
要序列化的值的类型。
参数
- value
- TValue
要转换的值。
- options
- JsonSerializerOptions
用于控制序列化行为的选项。
返回
值的 JSON 字符串表示形式。
例外
TValue
或其可序列化成员没有兼容的 JsonConverter。
注解
使用 A String 不像使用 UTF-8 编码那么高效,因为实现在内部使用 UTF-8。 另请参见 SerializeToUtf8Bytes(Object, Type, JsonSerializerOptions) 和 SerializeAsync(Stream, Object, Type, JsonSerializerOptions, CancellationToken)。
有关详细信息,请参阅 如何序列化和反序列化 JSON。
适用于
Serialize<TValue>(TValue, JsonTypeInfo<TValue>)
将所提供的值转换为 .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值的表示形式。
例外
TValue
或其可序列化成员没有兼容的 JsonConverter。
jsonTypeInfo
上声明的默认值为 null
。
注解
使用 A String 不像使用 UTF-8 编码那么高效,因为实现在内部使用 UTF-8。 另请参见 SerializeToUtf8Bytes<TValue>(TValue, JsonTypeInfo<TValue>) 和 SerializeAsync<TValue>(Stream, TValue, JsonTypeInfo<TValue>, CancellationToken)。
适用于
Serialize<TValue>(Stream, TValue, JsonSerializerOptions)
将所提供的值转换为 UTF-8 编码的 JSON 文本,并将其写入到 .Stream
public static void Serialize<TValue> (System.IO.Stream utf8Json, TValue value, System.Text.Json.JsonSerializerOptions? options = default);
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
要序列化的值的类型。
参数
- value
- TValue
要转换的值。
- options
- JsonSerializerOptions
用于控制转换行为的选项。
例外
utf8Json
为 null
。
TValue
或其可序列化成员没有兼容的 JsonConverter。
适用于
Serialize<TValue>(Stream, TValue, JsonTypeInfo<TValue>)
将所提供的值转换为 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
要序列化的值的类型。
参数
- value
- TValue
要转换的值。
- jsonTypeInfo
- JsonTypeInfo<TValue>
要转换的类型的元数据。
例外
utf8Json
为 null
。
TValue
或其可序列化成员没有兼容的 JsonConverter。
适用于
Serialize<TValue>(Utf8JsonWriter, TValue, JsonSerializerOptions)
将泛型类型参数指定的类型的 JSON 表示形式写入提供的编写器。
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);
static member Serialize : System.Text.Json.Utf8JsonWriter * 'Value * System.Text.Json.JsonSerializerOptions -> unit
Public Shared Sub Serialize(Of TValue) (writer As Utf8JsonWriter, value As TValue, Optional options As JsonSerializerOptions = Nothing)
Public Sub Serialize(Of TValue) (writer As Utf8JsonWriter, value As TValue, Optional options As JsonSerializerOptions = Nothing)
类型参数
- TValue
要序列化的值的类型。
参数
- writer
- Utf8JsonWriter
要写入到的 JSON 编写器。
- value
- TValue
要转换并写入的值。
- options
- JsonSerializerOptions
用于控制序列化行为的选项。
例外
writer
为 null
。
TValue
或其可序列化成员没有兼容的 JsonConverter。
注解
JsonWriterOptions用于创建实例的Utf8JsonWriter实例优先于JsonSerializerOptions冲突时。 因此, JsonWriterOptions.Indented在 JsonWriterOptions.SkipValidation编写时使用。因此, JsonWriterOptions.Encoder 在编写时使用。
有关详细信息,请参阅 如何序列化和反序列化 JSON。
适用于
Serialize<TValue>(Utf8JsonWriter, TValue, JsonTypeInfo<TValue>)
将一个 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>
要转换的类型的元数据。
例外
writer
或 jsonTypeInfo
为 null
。
TValue
或其可序列化成员没有兼容的 JsonConverter。