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
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Bodu.Numerics.Serialization.Json" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Bodu.Numerics.Serialization.Json" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Bodu.Numerics.Serialization.Json" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Bodu.Numerics.Serialization.Json --version 1.0.0
                    
#r "nuget: Bodu.Numerics.Serialization.Json, 1.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Bodu.Numerics.Serialization.Json@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Bodu.Numerics.Serialization.Json&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Bodu.Numerics.Serialization.Json&version=1.0.0
                    
Install as a Cake Tool

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 serializable Bodu.Numerics type.
  • Converters (+ factories for the generic types) for Fraction<T>, Interval<T>, DiscreteInterval<T>, IntervalSet<T>, and BigDecimal.
  • 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 — call ToIntervalSet() 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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0 51 9/24/2026
0.7.0 178 9/24/2026
0.6.0 69 9/24/2026
0.5.0 76 9/23/2026