JsonSerializer.Serialize 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
多載
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(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(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(Utf8JsonWriter, Object, JsonTypeInfo)
將一個 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
要轉換之型別的相關中繼資料。
例外狀況
writer
或 jsonTypeInfo
為 null
。
value
不符合 的類型 jsonTypeInfo
。
適用於
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 建立 實例的 Utf8JsonWriter ,在衝突時優先于 JsonSerializerOptions 。 因此,寫入 JsonWriterOptions.Indented 時會使用 、 JsonWriterOptions.SkipValidation 和 JsonWriterOptions.Encoder 。
如需詳細資訊,請參閱 如何序列化和還原序列化 JSON。
適用於
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(Stream, Object, JsonTypeInfo)
將所提供的值轉換為 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)
參數
- value
- Object
要進行轉換的值。
- jsonTypeInfo
- JsonTypeInfo
要轉換之型別的相關中繼資料。
例外狀況
utf8Json
為 null
。
value
不符合 的類型 jsonTypeInfo
。
適用於
Serialize(Object, JsonTypeInfo)
將所提供的值轉換成 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值的表示。
例外狀況
jsonTypeInfo
為 null
。
value
不符合 的類型 jsonTypeInfo
。
備註
String因為實作在內部使用 UTF-8,所以使用 時不如使用 UTF-8 編碼。 請參閱 SerializeToUtf8Bytes(Object, JsonTypeInfo) 和 SerializeAsync(Stream, Object, JsonTypeInfo, CancellationToken)。
適用於
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<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。
備註
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
。
備註
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。
適用於
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應