JsonSerializer.Serialize Method

Definition

Overloads

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.

Serialize(Stream, Object, Type, JsonSerializerContext)

Source:
JsonSerializer.Write.Stream.cs
Source:
JsonSerializer.Write.Stream.cs
Source:
JsonSerializer.Write.Stream.cs
Source:
JsonSerializer.Write.Stream.cs
Source:
JsonSerializer.Write.Stream.cs

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.Serialization.JsonSerializerContext context);

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

.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)

Serialize(Utf8JsonWriter, Object, Type, JsonSerializerContext)

Source:
JsonSerializer.Write.Utf8JsonWriter.cs
Source:
JsonSerializer.Write.Utf8JsonWriter.cs
Source:
JsonSerializer.Write.Utf8JsonWriter.cs
Source:
JsonSerializer.Write.Utf8JsonWriter.cs
Source:
JsonSerializer.Write.Utf8JsonWriter.cs

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

public static void Serialize (System.Text.Json.Utf8JsonWriter writer, object? value, Type inputType, System.Text.Json.Serialization.JsonSerializerContext context);

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)

Serialize(Utf8JsonWriter, Object, Type, JsonSerializerOptions)

Source:
JsonSerializer.Write.Utf8JsonWriter.cs
Source:
JsonSerializer.Write.Utf8JsonWriter.cs
Source:
JsonSerializer.Write.Utf8JsonWriter.cs
Source:
JsonSerializer.Write.Utf8JsonWriter.cs
Source:
JsonSerializer.Write.Utf8JsonWriter.cs

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

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)

Serialize(Utf8JsonWriter, Object, JsonTypeInfo)

Source:
JsonSerializer.Write.Utf8JsonWriter.cs
Source:
JsonSerializer.Write.Utf8JsonWriter.cs
Source:
JsonSerializer.Write.Utf8JsonWriter.cs
Source:
JsonSerializer.Write.Utf8JsonWriter.cs

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

public static void Serialize (System.Text.Json.Utf8JsonWriter writer, object? value, System.Text.Json.Serialization.Metadata.JsonTypeInfo 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)

Serialize(Stream, Object, Type, JsonSerializerOptions)

Source:
JsonSerializer.Write.Stream.cs
Source:
JsonSerializer.Write.Stream.cs
Source:
JsonSerializer.Write.Stream.cs
Source:
JsonSerializer.Write.Stream.cs
Source:
JsonSerializer.Write.Stream.cs

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

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

.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)

Serialize(Object, Type, JsonSerializerOptions)

Source:
JsonSerializer.Write.String.cs
Source:
JsonSerializer.Write.String.cs
Source:
JsonSerializer.Write.String.cs
Source:
JsonSerializer.Write.String.cs
Source:
JsonSerializer.Write.String.cs

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

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)

Serialize(Stream, Object, JsonTypeInfo)

Source:
JsonSerializer.Write.Stream.cs
Source:
JsonSerializer.Write.Stream.cs
Source:
JsonSerializer.Write.Stream.cs
Source:
JsonSerializer.Write.Stream.cs

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, System.Text.Json.Serialization.Metadata.JsonTypeInfo 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

.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)

Serialize(Object, JsonTypeInfo)

Source:
JsonSerializer.Write.String.cs
Source:
JsonSerializer.Write.String.cs
Source:
JsonSerializer.Write.String.cs
Source:
JsonSerializer.Write.String.cs

Converts the provided value into a String.

public static string Serialize (object? value, System.Text.Json.Serialization.Metadata.JsonTypeInfo jsonTypeInfo);

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)

Serialize(Object, Type, JsonSerializerContext)

Source:
JsonSerializer.Write.String.cs
Source:
JsonSerializer.Write.String.cs
Source:
JsonSerializer.Write.String.cs
Source:
JsonSerializer.Write.String.cs
Source:
JsonSerializer.Write.String.cs

Converts the provided value into a String.

public static string Serialize (object? value, Type inputType, System.Text.Json.Serialization.JsonSerializerContext context);

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)

Serialize<TValue>(Utf8JsonWriter, TValue, JsonSerializerOptions)

Source:
JsonSerializer.Write.Utf8JsonWriter.cs
Source:
JsonSerializer.Write.Utf8JsonWriter.cs
Source:
JsonSerializer.Write.Utf8JsonWriter.cs
Source:
JsonSerializer.Write.Utf8JsonWriter.cs
Source:
JsonSerializer.Write.Utf8JsonWriter.cs

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

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)

Serialize<TValue>(TValue, JsonSerializerOptions)

Source:
JsonSerializer.Write.String.cs
Source:
JsonSerializer.Write.String.cs
Source:
JsonSerializer.Write.String.cs
Source:
JsonSerializer.Write.String.cs
Source:
JsonSerializer.Write.String.cs

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

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)

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

Source:
JsonSerializer.Write.String.cs
Source:
JsonSerializer.Write.String.cs
Source:
JsonSerializer.Write.String.cs
Source:
JsonSerializer.Write.String.cs
Source:
JsonSerializer.Write.String.cs

Converts the provided value into a String.

public static string Serialize<TValue> (TValue value, System.Text.Json.Serialization.Metadata.JsonTypeInfo<TValue> jsonTypeInfo);

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)

Serialize<TValue>(Stream, TValue, JsonSerializerOptions)

Source:
JsonSerializer.Write.Stream.cs
Source:
JsonSerializer.Write.Stream.cs
Source:
JsonSerializer.Write.Stream.cs
Source:
JsonSerializer.Write.Stream.cs
Source:
JsonSerializer.Write.Stream.cs

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

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

.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)

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

Source:
JsonSerializer.Write.Stream.cs
Source:
JsonSerializer.Write.Stream.cs
Source:
JsonSerializer.Write.Stream.cs
Source:
JsonSerializer.Write.Stream.cs
Source:
JsonSerializer.Write.Stream.cs

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.Serialization.Metadata.JsonTypeInfo<TValue> jsonTypeInfo);

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

.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)

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

Source:
JsonSerializer.Write.Utf8JsonWriter.cs
Source:
JsonSerializer.Write.Utf8JsonWriter.cs
Source:
JsonSerializer.Write.Utf8JsonWriter.cs
Source:
JsonSerializer.Write.Utf8JsonWriter.cs
Source:
JsonSerializer.Write.Utf8JsonWriter.cs

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

public static void Serialize<TValue> (System.Text.Json.Utf8JsonWriter writer, TValue value, System.Text.Json.Serialization.Metadata.JsonTypeInfo<TValue> jsonTypeInfo);

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)