JsonSerializer.Serialize Method
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Serialize(Stream, Object, Type, JsonSerializerContext) |
Converts the provided value to UTF-8 encoded JSON text and write it to the Stream. |
Serialize(Utf8JsonWriter, Object, Type, JsonSerializerContext) |
Writes one JSON value (including objects or arrays) to the provided writer. |
Serialize(Utf8JsonWriter, Object, Type, JsonSerializerOptions) |
Writes the JSON representation of the specified type to the provided writer. |
Serialize(Utf8JsonWriter, Object, JsonTypeInfo) |
Writes one JSON value (including objects or arrays) to the provided writer. |
Serialize(Stream, Object, Type, JsonSerializerOptions) |
Converts the provided value to UTF-8 encoded JSON text and write it to the Stream. |
Serialize(Object, Type, JsonSerializerOptions) |
Converts the value of a specified type into a JSON string. |
Serialize(Stream, Object, JsonTypeInfo) |
Converts the provided value to UTF-8 encoded JSON text and write it to the Stream. |
Serialize(Object, JsonTypeInfo) |
Converts the provided value into a String. |
Serialize(Object, Type, JsonSerializerContext) |
Converts the provided value into a String. |
Serialize<TValue>(Utf8JsonWriter, TValue, JsonSerializerOptions) |
Writes the JSON representation of a type specified by a generic type parameter to the provided writer. |
Serialize<TValue>(TValue, JsonSerializerOptions) |
Converts the value of a type specified by a generic type parameter into a JSON string. |
Serialize<TValue>(TValue, JsonTypeInfo<TValue>) |
Converts the provided value into a String. |
Serialize<TValue>(Stream, TValue, JsonSerializerOptions) |
Converts the provided value to UTF-8 encoded JSON text and write it to the Stream. |
Serialize<TValue>(Stream, TValue, JsonTypeInfo<TValue>) |
Converts the provided value to UTF-8 encoded JSON text and write it to the Stream. |
Serialize<TValue>(Utf8JsonWriter, TValue, JsonTypeInfo<TValue>) |
Writes one JSON value (including objects or arrays) to the provided writer. |
Converts the provided value to UTF-8 encoded JSON text and write it to the 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)
Parameters
- value
- Object
The value to convert.
- inputType
- Type
The type of the value
to convert.
- context
- JsonSerializerContext
A metadata provider for serializable types.
Exceptions
inputType
is not compatible with value
.
utf8Json
, inputType
, or context
is null
.
There is no compatible JsonConverter for inputType
or its serializable members.
Applies to
.NET 9 and other versions
Product | Versions |
---|---|
.NET | 6, 7, 8 (package-provided), 8, 9 (package-provided), 9 |
.NET Framework | 4.7 (package-provided), 4.7.1 (package-provided), 4.7.2 (package-provided), 4.8 (package-provided) |
.NET Standard | 2.0 (package-provided) |
Writes one JSON value (including objects or arrays) to the provided writer.
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)
Parameters
- writer
- Utf8JsonWriter
A JSON writer to write to.
- value
- Object
The value to convert and write.
- inputType
- Type
The type of the value
to convert.
- context
- JsonSerializerContext
A metadata provider for serializable types.
Exceptions
inputType
is not compatible with value
.
writer
or inputType
is null
.
There is no compatible JsonConverter for inputType
or its serializable members.
The GetTypeInfo(Type) method of the provided context
returns null
for the type to convert.
Applies to
.NET 9 and other versions
Product | Versions |
---|---|
.NET | 6, 7, 8 (package-provided), 8, 9 (package-provided), 9 |
.NET Framework | 4.7 (package-provided), 4.7.1 (package-provided), 4.7.2 (package-provided), 4.8 (package-provided) |
.NET Standard | 2.0 (package-provided) |
Writes the JSON representation of the specified type to the provided writer.
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)
Parameters
- writer
- Utf8JsonWriter
The JSON writer to write to.
- value
- Object
The value to convert and write.
- inputType
- Type
The type of the value
to convert.
- options
- JsonSerializerOptions
Options to control serialization behavior.
Exceptions
inputType
is not compatible with value
writer
or inputType
is null
.
There is no compatible JsonConverter for inputType
or its serializable members.
Remarks
The JsonWriterOptions used to create the instance of the Utf8JsonWriter take precedence over the JsonSerializerOptions when they conflict. Hence, JsonWriterOptions.Indented, JsonWriterOptions.SkipValidation, and JsonWriterOptions.Encoder are used while writing.
For more information, see How to serialize and deserialize JSON.
Applies to
.NET 9 and other versions
Product | Versions |
---|---|
.NET | Core 3.0, Core 3.1, 5, 6, 7, 8 (package-provided), 8, 9 (package-provided), 9 |
.NET Framework | 4.7 (package-provided), 4.7.1 (package-provided), 4.7.2 (package-provided), 4.8 (package-provided) |
.NET Standard | 2.0 (package-provided) |
Writes one JSON value (including objects or arrays) to the provided writer.
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)
Parameters
- writer
- Utf8JsonWriter
The writer to write.
- value
- Object
The value to convert and write.
- jsonTypeInfo
- JsonTypeInfo
Metadata about the type to convert.
Exceptions
writer
or jsonTypeInfo
is null
.
value
does not match the type of jsonTypeInfo
.
Applies to
.NET 9 and other versions
Product | Versions |
---|---|
.NET | 8 (package-provided), 8, 9 (package-provided), 9 |
.NET Framework | 4.7 (package-provided), 4.7.1 (package-provided), 4.7.2 (package-provided), 4.8 (package-provided) |
.NET Standard | 2.0 (package-provided) |
Converts the provided value to UTF-8 encoded JSON text and write it to the 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)
Parameters
- value
- Object
The value to convert.
- inputType
- Type
The type of the value
to convert.
- options
- JsonSerializerOptions
Options to control the conversion behavior.
Exceptions
inputType
is not compatible with value
.
utf8Json
or inputType
is null
.
There is no compatible JsonConverter for inputType
or its serializable members.
Applies to
.NET 9 and other versions
Product | Versions |
---|---|
.NET | 6, 7, 8 (package-provided), 8, 9 (package-provided), 9 |
.NET Framework | 4.7 (package-provided), 4.7.1 (package-provided), 4.7.2 (package-provided), 4.8 (package-provided) |
.NET Standard | 2.0 (package-provided) |
Converts the value of a specified type into a JSON string.
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
Parameters
- value
- Object
The value to convert.
- inputType
- Type
The type of the value
to convert.
- options
- JsonSerializerOptions
Options to control the conversion behavior.
Returns
The JSON string representation of the value.
Exceptions
inputType
is not compatible with value
.
inputType
is null
.
There is no compatible JsonConverter for inputType
or its serializable members.
Remarks
Using a String is not as efficient as using UTF-8 encoding since the implementation internally uses UTF-8. See also SerializeToUtf8Bytes(Object, Type, JsonSerializerOptions) and SerializeAsync(Stream, Object, Type, JsonSerializerOptions, CancellationToken).
For more information, see How to serialize and deserialize JSON.
Applies to
.NET 9 and other versions
Product | Versions |
---|---|
.NET | Core 3.0, Core 3.1, 5, 6, 7, 8 (package-provided), 8, 9 (package-provided), 9 |
.NET Framework | 4.7 (package-provided), 4.7.1 (package-provided), 4.7.2 (package-provided), 4.8 (package-provided) |
.NET Standard | 2.0 (package-provided) |
Converts the provided value to UTF-8 encoded JSON text and write it to the 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)
Parameters
- value
- Object
The value to convert.
- jsonTypeInfo
- JsonTypeInfo
Metadata about the type to convert.
Exceptions
utf8Json
is null
.
value
does not match the type of jsonTypeInfo
.
Applies to
.NET 9 and other versions
Product | Versions |
---|---|
.NET | 8 (package-provided), 8, 9 (package-provided), 9 |
.NET Framework | 4.7 (package-provided), 4.7.1 (package-provided), 4.7.2 (package-provided), 4.8 (package-provided) |
.NET Standard | 2.0 (package-provided) |
Converts the provided value into a 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
Parameters
- value
- Object
The value to convert.
- jsonTypeInfo
- JsonTypeInfo
Metadata about the type to convert.
Returns
A String representation of the value.
Exceptions
jsonTypeInfo
is null
.
value
does not match the type of jsonTypeInfo
.
Remarks
Using a String is not as efficient as using UTF-8 encoding since the implementation internally uses UTF-8. See also SerializeToUtf8Bytes(Object, JsonTypeInfo) and SerializeAsync(Stream, Object, JsonTypeInfo, CancellationToken).
Applies to
.NET 9 and other versions
Product | Versions |
---|---|
.NET | 8 (package-provided), 8, 9 (package-provided), 9 |
.NET Framework | 4.7 (package-provided), 4.7.1 (package-provided), 4.7.2 (package-provided), 4.8 (package-provided) |
.NET Standard | 2.0 (package-provided) |
Converts the provided value into a 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
Parameters
- value
- Object
The value to convert.
- inputType
- Type
The type of the value
to convert.
- context
- JsonSerializerContext
A metadata provider for serializable types.
Returns
A String representation of the value.
Exceptions
There is no compatible JsonConverter for inputType
or its serializable members.
The GetTypeInfo(Type) method of the provided context
returns null
for the type to convert.
inputType
or context
is null
.
Remarks
Using a String is not as efficient as using UTF-8 encoding, since the implementation internally uses UTF-8. Also see SerializeToUtf8Bytes(Object, Type, JsonSerializerContext) and SerializeAsync(Stream, Object, Type, JsonSerializerContext, CancellationToken).
Applies to
.NET 9 and other versions
Product | Versions |
---|---|
.NET | 6, 7, 8 (package-provided), 8, 9 (package-provided), 9 |
.NET Framework | 4.7 (package-provided), 4.7.1 (package-provided), 4.7.2 (package-provided), 4.8 (package-provided) |
.NET Standard | 2.0 (package-provided) |
Writes the JSON representation of a type specified by a generic type parameter to the provided writer.
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)
Type Parameters
- TValue
The type of the value to serialize.
Parameters
- writer
- Utf8JsonWriter
A JSON writer to write to.
- value
- TValue
The value to convert and write.
- options
- JsonSerializerOptions
Options to control serialization behavior.
Exceptions
writer
is null
.
There is no compatible JsonConverter for TValue
or its serializable members.
Remarks
The JsonWriterOptions used to create the instance of the Utf8JsonWriter take precedence over the JsonSerializerOptions when they conflict. Hence, JsonWriterOptions.Indented, JsonWriterOptions.SkipValidation, and JsonWriterOptions.Encoder are used while writing.
For more information, see How to serialize and deserialize JSON.
Applies to
.NET 9 and other versions
Product | Versions |
---|---|
.NET | Core 3.0, Core 3.1, 5, 6, 7, 8 (package-provided), 8, 9 (package-provided), 9 |
.NET Framework | 4.7 (package-provided), 4.7.1 (package-provided), 4.7.2 (package-provided), 4.8 (package-provided) |
.NET Standard | 2.0 (package-provided) |
Converts the value of a type specified by a generic type parameter into a JSON string.
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
Type Parameters
- TValue
The type of the value to serialize.
Parameters
- value
- TValue
The value to convert.
- options
- JsonSerializerOptions
Options to control serialization behavior.
Returns
A JSON string representation of the value.
Exceptions
There is no compatible JsonConverter for TValue
or its serializable members.
Remarks
Using a String is not as efficient as using UTF-8 encoding since the implementation internally uses UTF-8. See also SerializeToUtf8Bytes(Object, Type, JsonSerializerOptions) and SerializeAsync(Stream, Object, Type, JsonSerializerOptions, CancellationToken).
For more information, see How to serialize and deserialize JSON.
Applies to
.NET 9 and other versions
Product | Versions |
---|---|
.NET | Core 3.0, Core 3.1, 5, 6, 7, 8 (package-provided), 8, 9 (package-provided), 9 |
.NET Framework | 4.7 (package-provided), 4.7.1 (package-provided), 4.7.2 (package-provided), 4.8 (package-provided) |
.NET Standard | 2.0 (package-provided) |
Converts the provided value into a 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
Type Parameters
- TValue
The type of the value to serialize.
Parameters
- value
- TValue
The value to convert.
- jsonTypeInfo
- JsonTypeInfo<TValue>
Metadata about the type to convert.
Returns
A String representation of the value.
Exceptions
There is no compatible JsonConverter for TValue
or its serializable members.
jsonTypeInfo
is null
.
Remarks
Using a String is not as efficient as using UTF-8 encoding since the implementation internally uses UTF-8. See also SerializeToUtf8Bytes<TValue>(TValue, JsonTypeInfo<TValue>) and SerializeAsync<TValue>(Stream, TValue, JsonTypeInfo<TValue>, CancellationToken).
Applies to
.NET 9 and other versions
Product | Versions |
---|---|
.NET | 6, 7, 8 (package-provided), 8, 9 (package-provided), 9 |
.NET Framework | 4.7 (package-provided), 4.7.1 (package-provided), 4.7.2 (package-provided), 4.8 (package-provided) |
.NET Standard | 2.0 (package-provided) |
Converts the provided value to UTF-8 encoded JSON text and write it to the 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)
Type Parameters
- TValue
The type of the value to serialize.
Parameters
- value
- TValue
The value to convert.
- options
- JsonSerializerOptions
Options to control the conversion behavior.
Exceptions
utf8Json
is null
.
There is no compatible JsonConverter for TValue
or its serializable members.
Applies to
.NET 9 and other versions
Product | Versions |
---|---|
.NET | 6, 7, 8 (package-provided), 8, 9 (package-provided), 9 |
.NET Framework | 4.7 (package-provided), 4.7.1 (package-provided), 4.7.2 (package-provided), 4.8 (package-provided) |
.NET Standard | 2.0 (package-provided) |
Converts the provided value to UTF-8 encoded JSON text and write it to the 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))
Type Parameters
- TValue
The type of the value to serialize.
Parameters
- value
- TValue
The value to convert.
- jsonTypeInfo
- JsonTypeInfo<TValue>
Metadata about the type to convert.
Exceptions
utf8Json
is null
.
There is no compatible JsonConverter for TValue
or its serializable members.
Applies to
.NET 9 and other versions
Product | Versions |
---|---|
.NET | 6, 7, 8 (package-provided), 8, 9 (package-provided), 9 |
.NET Framework | 4.7 (package-provided), 4.7.1 (package-provided), 4.7.2 (package-provided), 4.8 (package-provided) |
.NET Standard | 2.0 (package-provided) |
Writes one JSON value (including objects or arrays) to the provided writer.
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))
Type Parameters
- TValue
The type of the value to serialize.
Parameters
- writer
- Utf8JsonWriter
The writer to write.
- value
- TValue
The value to convert and write.
- jsonTypeInfo
- JsonTypeInfo<TValue>
Metadata about the type to convert.
Exceptions
writer
or jsonTypeInfo
is null
.
There is no compatible JsonConverter for TValue
or its serializable members.
Applies to
.NET 9 and other versions
Product | Versions |
---|---|
.NET | 6, 7, 8 (package-provided), 8, 9 (package-provided), 9 |
.NET Framework | 4.7 (package-provided), 4.7.1 (package-provided), 4.7.2 (package-provided), 4.8 (package-provided) |
.NET Standard | 2.0 (package-provided) |
.NET feedback
.NET is an open source project. Select a link to provide feedback: