Home JsonSerializerOptionsExtensions Class
JsonSerializerOptionsExtensions Class
Cancel

JsonSerializerOptionsExtensions Class

Namespace: Json.More

Inheritance: JsonSerializerOptionsExtensions 🡒 object

Provides extension functionality for System.Text.Json.JsonSerializerOptions.

Remarks

Most (if not all) of these extension methods are workarounds for https://github.com/dotnet/runtime/issues/50205.

Methods

GetConverter(this JsonSerializerOptions options, JsonTypeInfo typeInfo)

Returns the converter for the specified type.

Declaration

1
public static JsonConverter<T> GetConverter(this JsonSerializerOptions options, JsonTypeInfo typeInfo)
ParameterTypeDescription
optionsJsonSerializerOptionsThe System.Text.Json.JsonSerializerOptions being used.
typeInfoJsonTypeInfoAn explicit typeInfo to use for looking up the Converter. If not provided, options.GetTypeInfo will be used.

Returns

An implementation of System.Text.Json.Serialization.JsonConverter`1 as determined by the provided options

Read(this JsonSerializerOptions options, ref Utf8JsonReader reader, JsonTypeInfo<T> typeInfo)

Read and convert the JSON to T.

Declaration

1
public static T Read(this JsonSerializerOptions options, ref Utf8JsonReader reader, JsonTypeInfo<T> typeInfo)
ParameterTypeDescription
optionsJsonSerializerOptionsThe System.Text.Json.JsonSerializerOptions being used.
readerref Utf8JsonReaderThe System.Text.Json.Utf8JsonReader to read from.
typeInfoJsonTypeInfo<T>An explicit typeInfo to use for looking up the Converter. If not provided, options.GetTypeInfo will be used.

Returns

The value that was converted.

Remarks

A converter may throw any Exception, but should throw JsonException when the JSON is invalid.

ReadArray(this JsonSerializerOptions options, ref Utf8JsonReader reader, JsonTypeInfo<T> typeInfo)

Read and convert the JSON to multiple T.

Declaration

1
public static T[] ReadArray(this JsonSerializerOptions options, ref Utf8JsonReader reader, JsonTypeInfo<T> typeInfo)
ParameterTypeDescription
optionsJsonSerializerOptionsThe System.Text.Json.JsonSerializerOptions being used.
readerref Utf8JsonReaderThe System.Text.Json.Utf8JsonReader to read from.
typeInfoJsonTypeInfo<T>An explicit typeInfo to use for looking up the Converter. If not provided, options.GetTypeInfo will be used.

Returns

The value that was converted.

Remarks

A converter may throw any Exception, but should throw JsonException when the JSON is invalid.

ReadDictionary(this JsonSerializerOptions options, ref Utf8JsonReader reader, JsonTypeInfo<T> valueTypeInfo)

Read and convert string/T dictionary to JSON

Declaration

1
public static Dictionary<string, T> ReadDictionary(this JsonSerializerOptions options, ref Utf8JsonReader reader, JsonTypeInfo<T> valueTypeInfo)
ParameterTypeDescription
optionsJsonSerializerOptionsThe System.Text.Json.JsonSerializerOptions being used.
readerref Utf8JsonReaderThe System.Text.Json.Utf8JsonReader to read from.
valueTypeInfoJsonTypeInfo<T>An explicit typeInfo to use for looking up the Converter. If not provided, options.GetTypeInfo will be used.

Returns

The value that was converted.

ReadDictionaryList(this JsonSerializerOptions options, ref Utf8JsonReader reader, JsonTypeInfo<T> valueTypeInfo)

Read and convert string/list-T dictionary to JSON

Declaration

1
public static Dictionary<string, List<T>> ReadDictionaryList(this JsonSerializerOptions options, ref Utf8JsonReader reader, JsonTypeInfo<T> valueTypeInfo)
ParameterTypeDescription
optionsJsonSerializerOptionsThe System.Text.Json.JsonSerializerOptions being used.
readerref Utf8JsonReaderThe System.Text.Json.Utf8JsonReader to read from.
valueTypeInfoJsonTypeInfo<T>An explicit typeInfo to use for looking up the Converter. If not provided, options.GetTypeInfo will be used.

Returns

The value that was converted.

ReadList(this JsonSerializerOptions options, ref Utf8JsonReader reader, JsonTypeInfo<T> typeInfo)

Read and convert the JSON to multiple T.

Declaration

1
public static List<T> ReadList(this JsonSerializerOptions options, ref Utf8JsonReader reader, JsonTypeInfo<T> typeInfo)
ParameterTypeDescription
optionsJsonSerializerOptionsThe System.Text.Json.JsonSerializerOptions being used.
readerref Utf8JsonReaderThe System.Text.Json.Utf8JsonReader to read from.
typeInfoJsonTypeInfo<T>An explicit typeInfo to use for looking up the Converter. If not provided, options.GetTypeInfo will be used.

Returns

The value that was converted.

Remarks

A converter may throw any Exception, but should throw JsonException when the JSON is invalid.

Write(this JsonSerializerOptions options, Utf8JsonWriter writer, T value, JsonTypeInfo<T> typeInfo)

Write a T to JSON.

Declaration

1
public static void Write(this JsonSerializerOptions options, Utf8JsonWriter writer, T value, JsonTypeInfo<T> typeInfo)
ParameterTypeDescription
optionsJsonSerializerOptionsThe System.Text.Json.JsonSerializerOptions being used.
writerUtf8JsonWriterThe System.Text.Json.Utf8JsonReader to read from.
valueTThe value to serialize.
typeInfoJsonTypeInfo<T>An explicit typeInfo to use for looking up the Converter. If not provided, options.GetTypeInfo will be used.

Remarks

A converter may throw any Exception, but should throw JsonException when the JSON is invalid.

Write(this JsonSerializerOptions options, Utf8JsonWriter writer, object value, Type inputType)

Declaration

1
public static void Write(this JsonSerializerOptions options, Utf8JsonWriter writer, object value, Type inputType)
ParameterTypeDescription
optionsJsonSerializerOptionsThe System.Text.Json.JsonSerializerOptions being used.
writerUtf8JsonWriterThe System.Text.Json.Utf8JsonReader to read from.
valueobjectThe value to serialize.
inputTypeTypeThe type to serialize.

Remarks

A converter may throw any Exception, but should throw JsonException when the JSON is invalid.

WriteDictionary(this JsonSerializerOptions options, Utf8JsonWriter writer, IEnumerable<KeyValuePair<string, T>> values, JsonTypeInfo<T> valueTypeInfo)

Convert and write string/T dictionary to JSON

Declaration

1
public static void WriteDictionary(this JsonSerializerOptions options, Utf8JsonWriter writer, IEnumerable<KeyValuePair<string, T>> values, JsonTypeInfo<T> valueTypeInfo)
ParameterTypeDescription
optionsJsonSerializerOptionsThe System.Text.Json.JsonSerializerOptions being used.
writerUtf8JsonWriterThe System.Text.Json.Utf8JsonWriter to write to.
valuesIEnumerable<KeyValuePair<string, T>>The collection of values to convert.
valueTypeInfoJsonTypeInfo<T>An explicit typeInfo to use for looking up the Converter. If not provided, options.GetTypeInfo will be used.

WriteDictionaryList(this JsonSerializerOptions options, Utf8JsonWriter writer, IEnumerable<KeyValuePair<string, IReadOnlyList<T>>> values, JsonTypeInfo<T> valueTypeInfo)

Convert and write string/list-T dictionary to JSON

Declaration

1
public static void WriteDictionaryList(this JsonSerializerOptions options, Utf8JsonWriter writer, IEnumerable<KeyValuePair<string, IReadOnlyList<T>>> values, JsonTypeInfo<T> valueTypeInfo)
ParameterTypeDescription
optionsJsonSerializerOptionsThe System.Text.Json.JsonSerializerOptions being used.
writerUtf8JsonWriterThe System.Text.Json.Utf8JsonWriter to write to.
valuesIEnumerable<KeyValuePair<string, IReadOnlyList<T>>>The collection of values to convert.
valueTypeInfoJsonTypeInfo<T>An explicit typeInfo to use for looking up the Converter. If not provided, options.GetTypeInfo will be used.

WriteList(this JsonSerializerOptions options, Utf8JsonWriter writer, IEnumerable<T> values, JsonTypeInfo<T> typeInfo)

Convert and write multiple T to JSON

Declaration

1
public static void WriteList(this JsonSerializerOptions options, Utf8JsonWriter writer, IEnumerable<T> values, JsonTypeInfo<T> typeInfo)
ParameterTypeDescription
optionsJsonSerializerOptionsThe System.Text.Json.JsonSerializerOptions being used.
writerUtf8JsonWriterThe System.Text.Json.Utf8JsonWriter to write to.
valuesIEnumerable<T>The collection of values to convert.
typeInfoJsonTypeInfo<T>An explicit typeInfo to use for looking up the Converter. If not provided, options.GetTypeInfo will be used.
Contents