JsonSerializer.Serialize Method

Definition

Overloads

Serialize(Utf8JsonWriter, Object, Type, JsonSerializerContext)

Writes one JSON value (including objects or arrays) to the provided writer.

Serialize(Stream, Object, Type, JsonSerializerContext)

Converts the provided value to UTF-8 encoded JSON text and write it to the Stream.

Serialize(Stream, Object, Type, JsonSerializerOptions)

Converts the provided value to UTF-8 encoded JSON text and write it to the Stream.

Serialize(Utf8JsonWriter, Object, JsonTypeInfo)

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(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>(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, JsonSerializerOptions)

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

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

Writes one JSON value (including objects or arrays) to the provided writer.

Serialize(Utf8JsonWriter, Object, Type, JsonSerializerContext)

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

Serialize(Stream, Object, Type, JsonSerializerContext)

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

utf8Json
Stream

The UTF-8 Stream to write to.

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

Serialize(Stream, Object, Type, JsonSerializerOptions)

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

utf8Json
Stream

The UTF-8 Stream to write to.

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

Serialize(Utf8JsonWriter, Object, JsonTypeInfo)

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

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 = 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

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 = 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

Serialize(Stream, Object, JsonTypeInfo)

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

utf8Json
Stream

The UTF-8 Stream to write to.

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

Serialize(Object, JsonTypeInfo)

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

Serialize(Object, Type, JsonSerializerContext)

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

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 = 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

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

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

Serialize<TValue>(Stream, TValue, JsonSerializerOptions)

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

utf8Json
Stream

The UTF-8 Stream to write to.

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

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

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

utf8Json
Stream

The UTF-8 Stream to write to.

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

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 = 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

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

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