Table of Contents

IntervalJsonConverter<T> Class

Definition

Namespace
Bodu.Numerics.Serialization.Json
Assembly
Bodu.Numerics.Serialization.Json.dll
Package
Bodu.Numerics.Serialization.Json 1.0.0
Source
IntervalJsonConverter{T}.cs

Converts an Interval<T> to and from JSON using the policy supplied at construction.

public sealed class IntervalJsonConverter<T> : JsonConverter<Interval<T>> where T : INumber<T>

Type Parameters

T

The numeric endpoint type of the interval.

Inheritance
IntervalJsonConverter<T>
Inherited Members
Extension Methods

Remarks

The converter shape is selected by NumericsJsonPolicy:

  • Strict - canonical object form { "lower": 1, "upper": 5, "lowerInclusive": true, "upperInclusive": true }; the empty interval emits as the single-property object { "empty": true }. A bounded side requires its endpoint value and inclusion flag; an unbounded side emits the marker "lowerUnbounded": true / "upperUnbounded": true in place of both (for example { "lower": 1, "upperUnbounded": true, "lowerInclusive": true } for [1, +∞), or { "lowerUnbounded": true, "upperUnbounded": true } for the whole line). Duplicate properties are rejected, and an "empty": true marker rejects any sibling endpoint or unbounded property.
  • Lenient - same object shape as Strict, with additional tolerance: "min" and "max" are accepted as aliases for "lower" and "upper", missing endpoint-inclusion flags default to closed (true), and a top-level JSON string is accepted and routed through TryParse(string?, IFormatProvider?, out Interval<T>) as a fallback.
  • Compact - string form in ISO 31-11 bracket notation (for example "[1, 5)") or the empty-set glyph "∅". Reads delegate to TryParse(string?, IFormatProvider?, out Interval<T>) and writes call ToString(string?, IFormatProvider?) with the invariant culture.

Constructors

IntervalJsonConverter()

Initializes a new instance of the IntervalJsonConverter<T> class configured for the Strict shape.

public IntervalJsonConverter()

IntervalJsonConverter(NumericsJsonPolicy)

Initializes a new instance of the IntervalJsonConverter<T> class configured for the supplied policy.

public IntervalJsonConverter(NumericsJsonPolicy policy)

Parameters

policy NumericsJsonPolicy

The serialization policy.

Exceptions

ArgumentOutOfRangeException

policy is not a defined NumericsJsonPolicy value.

Methods

Read(ref Utf8JsonReader, Type, JsonSerializerOptions)

Reads and converts the JSON to type Interval.

public override Interval<T> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)

Parameters

reader Utf8JsonReader

The reader.

typeToConvert Type

The type to convert.

options JsonSerializerOptions

An object that specifies serialization options to use.

Returns

Interval<T>

The converted value.

Write(Utf8JsonWriter, Interval<T>, JsonSerializerOptions)

Writes a specified value as JSON.

public override void Write(Utf8JsonWriter writer, Interval<T> value, JsonSerializerOptions options)

Parameters

writer Utf8JsonWriter

The writer to write to.

value Interval<T>

The value to convert to JSON.

options JsonSerializerOptions

An object that specifies serialization options to use.

Applies to

ProductVersions
.NET8, 10