Skip to content

TimeSpanSecondsConverter Class

A custom JSON converter for TimeSpan objects that serializes and deserializes them as a number of seconds.

C#
public class TimeSpanSecondsConverter : System.Text.Json.Serialization.JsonConverter<System.TimeSpan>

Inheritance System.ObjectSystem.Text.Json.Serialization.JsonConverterSystem.Text.Json.Serialization.JsonConverter<System.TimeSpan> → TimeSpanSecondsConverter

Methods

TimeSpanSecondsConverter.Read(Utf8JsonReader, Type, JsonSerializerOptions) Method

Reads a JSON number representing the total seconds and converts it to a TimeSpan object.

C#
public override System.TimeSpan Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options);

Parameters

reader System.Text.Json.Utf8JsonReader

The reader to read JSON data from.

typeToConvert System.Type

The type to convert; expected to be TimeSpan.

options System.Text.Json.JsonSerializerOptions

Options for the serializer.

Returns

System.TimeSpan
A TimeSpan object representing the value read from the JSON.

TimeSpanSecondsConverter.Write(Utf8JsonWriter, TimeSpan, JsonSerializerOptions) Method

Writes a TimeSpan object to JSON as a number representing its total seconds.

C#
public override void Write(System.Text.Json.Utf8JsonWriter writer, System.TimeSpan value, System.Text.Json.JsonSerializerOptions options);

Parameters

writer System.Text.Json.Utf8JsonWriter

The writer to write JSON data to.

value System.TimeSpan

The TimeSpan value to write.

options System.Text.Json.JsonSerializerOptions

Options for the serializer.