Table of Contents

IntervalJsonConverter<T> Class

Definition

Namespace
Bodu.Numerics.Serialization.Json
Assembly
Bodu.Numerics.Serialization.Json.dll
Package
Bodu.Numerics.Serialization.Json 1.3.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). An endpoint value is a JSON number, except that an infinite endpoint of a bounded side is written as the string "Infinity" or "-Infinity"; an endpoint may be read from either a number or a numeric string. Duplicate properties are rejected, an "empty": true marker rejects any sibling endpoint or unbounded property, and a NaN endpoint on a bounded side, such as "lower": "NaN", is rejected because NaN has no place in the order of the endpoints.
  • 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, so an infinite endpoint on an open side reads back as an unbounded side, as the text form does.

Under every policy, a payload that cannot form an interval, a NaN endpoint included, is reported as JsonException. A half-bounded interval whose bounded endpoint is the infinity on its unbounded side, such as [Infinity, +∞), holds no value and is the empty interval, so it is written, and read, as Empty is.

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