FractionJsonConverter<T> Class
Definition
- Assembly
- Bodu.Numerics.Serialization.Json.dll
- Package
- Bodu.Numerics.Serialization.Json 1.0.0
Converts a Fraction<T> to and from JSON using the policy supplied at construction.
public sealed class FractionJsonConverter<T> : JsonConverter<Fraction<T>> where T : IBinaryInteger<T>
Type Parameters
TThe backing integer type of the fraction.
- Inheritance
-
FractionJsonConverter<T>
- Inherited Members
- Extension Methods
Remarks
The converter shape is selected by NumericsJsonPolicy:
-
Strict - canonical object form
{ "numerator": 3, "denominator": 4 }; rejects duplicate properties and non-object tokens. Property values may be either JSON numbers or numeric strings, so BigInteger-backed values survive without precision loss. - Lenient - same object shape as Strict, but a top-level JSON string token is accepted and routed through Parse(ReadOnlySpan<char>, IFormatProvider?) as a tolerant fallback.
-
Compact - string form
"3/4"; reads delegate to TryParse(ReadOnlySpan<char>, IFormatProvider?, out Fraction<T>) and writes call ToString(string?, IFormatProvider?) with the invariant culture.
Constructors
FractionJsonConverter()
Initializes a new instance of the FractionJsonConverter<T> class configured for the Strict shape.
public FractionJsonConverter()
FractionJsonConverter(NumericsJsonPolicy)
Initializes a new instance of the FractionJsonConverter<T> class configured for the supplied
policy.
public FractionJsonConverter(NumericsJsonPolicy policy)
Parameters
policyNumericsJsonPolicyThe serialization policy.
Exceptions
- ArgumentOutOfRangeException
policyis not a defined NumericsJsonPolicy value.
Methods
Read(ref Utf8JsonReader, Type, JsonSerializerOptions)
Reads and converts the JSON to type Bodu.Numerics.Fraction{`0}.
public override Fraction<T> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
Parameters
readerUtf8JsonReaderThe reader.
typeToConvertTypeThe type to convert.
optionsJsonSerializerOptionsAn object that specifies serialization options to use.
Returns
- Fraction<T>
The converted value.
Write(Utf8JsonWriter, Fraction<T>, JsonSerializerOptions)
Writes a specified value as JSON.
public override void Write(Utf8JsonWriter writer, Fraction<T> value, JsonSerializerOptions options)
Parameters
writerUtf8JsonWriterThe writer to write to.
valueFraction<T>The value to convert to JSON.
optionsJsonSerializerOptionsAn object that specifies serialization options to use.
Applies to
| Product | Versions |
|---|---|
| .NET | 8, 10 |