Bodu.Numerics.Serialization.Json
1.0.0
dotnet add package Bodu.Numerics.Serialization.Json --version 1.0.0
NuGet\Install-Package Bodu.Numerics.Serialization.Json -Version 1.0.0
<PackageReference Include="Bodu.Numerics.Serialization.Json" Version="1.0.0" />
<PackageVersion Include="Bodu.Numerics.Serialization.Json" Version="1.0.0" />
<PackageReference Include="Bodu.Numerics.Serialization.Json" />
paket add Bodu.Numerics.Serialization.Json --version 1.0.0
#r "nuget: Bodu.Numerics.Serialization.Json, 1.0.0"
#:package Bodu.Numerics.Serialization.Json@1.0.0
#addin nuget:?package=Bodu.Numerics.Serialization.Json&version=1.0.0
#tool nuget:?package=Bodu.Numerics.Serialization.Json&version=1.0.0
Bodu.Numerics.Serialization.Json
API stability — Stable. The public API surface is committed; breaking changes are reserved for a major-version bump per SemVer.
System.Text.Json integration for Bodu.Numerics. The core library is deliberately serialization-agnostic — its value types carry no [JsonConverter] attribute and take no System.Text.Json dependency — so JSON support is opt-in through this companion package (the NodaTime companion-package pattern):
AddNumericsJsonConverters(options, policy)— one call registers a coherent converter set for every serializableBodu.Numericstype.- Converters (+ factories for the generic types) for
Fraction<T>,Interval<T>,DiscreteInterval<T>,IntervalSet<T>, andBigDecimal. NumericsJsonPolicy—Strict(canonical object shapes, the default),Lenient(Strict plus read tolerance for external feeds),Compact(single strings such as"3/4"and"[1, 5)").- The transient result types
IntervalPair<T>/DiscreteIntervalPair<T>are deliberately not serializable — callToIntervalSet()and persist the set instead.
Installation
dotnet add package Bodu.Numerics.Serialization.Json
Targets net8.0. Depends on Bodu.Numerics.
Usage
Register the converters once, before the options instance is first used:
using System.Text.Json;
using Bodu.Numerics;
using Bodu.Numerics.Serialization.Json;
var options = new JsonSerializerOptions().AddNumericsJsonConverters(); // Strict
string json = JsonSerializer.Serialize(new Fraction<int>(3, 4), options);
// → {"numerator":3,"denominator":4}
var compact = new JsonSerializerOptions()
.AddNumericsJsonConverters(NumericsJsonPolicy.Compact);
// Fraction → "3/4", Interval → "[1, 5)", empty interval → "∅"
One policy value shapes every registered converter:
| Policy | Fraction | Interval family | Intended use |
|---|---|---|---|
Strict (default) |
object { "numerator": 3, "denominator": 4 } |
object with lower / upper / inclusivity flags, { "empty": true }, or unbounded markers |
Canonical persistence and interchange. |
Lenient |
as Strict, plus a top-level string accepted on read |
as Strict, plus "min" / "max" aliases and defaulted inclusivity |
Spreadsheet / external-feed ingest. Writes as Strict. |
Compact |
string "3/4" |
ISO 31-11 bracket string "[1, 5)", "∅" for empty |
Compact payloads where size matters. |
BigDecimal serializes as the canonical object { "unscaledValue": 12340, "scale": 3 } under Strict — the unscaled value is a raw JSON number, so an arbitrary-magnitude mantissa round-trips exactly — and as the plain decimal string "12.340" under Compact (a string rather than a bare number, because many consumers narrow long JSON numbers to double). Lenient reads either shape.
Converters can also be registered individually (for example only fractions, or a single closed backing type) via options.Converters.Add(new FractionJsonConverterFactory(NumericsJsonPolicy.Compact)).
Documentation
See the JSON serialization guide for the full wire-shape reference, error behaviour, and per-converter registration.
License
MIT. © Bodu Pty. Ltd.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- Bodu.Numerics (>= 1.0.0)
-
net8.0
- Bodu.Numerics (>= 1.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.