JsonSerializer.Serialize Method

Definition

Overloads

Serialize(Object, Type, JsonSerializerOptions)

Converts the value of a specified type into a JSON string.

Serialize(Utf8JsonWriter, Object, Type, JsonSerializerOptions)

Writes the JSON representation of the specified type 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>(Utf8JsonWriter, TValue, JsonSerializerOptions)

Writes the JSON representation of a type specified by a generic type parameter to the provided writer.

Serialize(Object, Type, JsonSerializerOptions)

Converts the value of a specified type into a JSON string.

public static string Serialize (object value, Type inputType, System.Text.Json.JsonSerializerOptions options = null);
static member Serialize : obj * Type * System.Text.Json.JsonSerializerOptions -> string
Public Shared Function Serialize (value As Object, inputType As Type, Optional options As JsonSerializerOptions = null) 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.

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.

Serialize(Utf8JsonWriter, Object, Type, JsonSerializerOptions)

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 = null);
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 = null)

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.

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.

Serialize<TValue>(TValue, JsonSerializerOptions)

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 = null);
static member Serialize : 'Value * System.Text.Json.JsonSerializerOptions -> string
Public Shared Function Serialize(Of TValue) (value As TValue, Optional options As JsonSerializerOptions = null) 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.

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.

Serialize<TValue>(Utf8JsonWriter, TValue, JsonSerializerOptions)

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 = null);
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 = null)

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.

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