Bodu.Financial.Serialization.Json 0.7.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Bodu.Financial.Serialization.Json --version 0.7.0
                    
NuGet\Install-Package Bodu.Financial.Serialization.Json -Version 0.7.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.Financial.Serialization.Json" Version="0.7.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Bodu.Financial.Serialization.Json" Version="0.7.0" />
                    
Directory.Packages.props
<PackageReference Include="Bodu.Financial.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.Financial.Serialization.Json --version 0.7.0
                    
#r "nuget: Bodu.Financial.Serialization.Json, 0.7.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.Financial.Serialization.Json@0.7.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.Financial.Serialization.Json&version=0.7.0
                    
Install as a Cake Addin
#tool nuget:?package=Bodu.Financial.Serialization.Json&version=0.7.0
                    
Install as a Cake Tool

Bodu.Financial.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.Financial. The core library is deliberately serialization-agnostic — its monetary types carry no [JsonConverter] attribute and take no System.Text.Json integration of their own — so JSON support is opt-in through this companion package (the NodaTime companion-package pattern):

  • AddFinancialJsonConverters(options, policy) — one call registers a coherent converter set for every serializable Bodu.Financial type.
  • Converters (+ a factory for the generic type) for Money, Money<TCurrency>, MoneyBag, ExchangeRate, and CurrencyPair.
  • FinancialJsonPolicy — Strict (canonical object shapes, the default), Lenient (Strict plus read tolerance for external feeds), Compact (single strings such as "19.99 USD" and flat ISO→amount maps).
  • AddFinancialJson(services, policy) — dependency-injection registration of a configured JsonSerializerOptions as a keyed singleton (key "Financial").

Migration note. Earlier Bodu.Financial builds shipped these converters in the core package and serialized through type-level [JsonConverter] attributes with zero configuration. The attributes have been removed: registration is now mandatory. Serializing a monetary type on an options instance without the converters no longer throws — JsonSerializer silently falls back to reflection-shaped output — so audit any JsonSerializer.Serialize(money) call sites when upgrading.

Installation

dotnet add package Bodu.Financial.Serialization.Json

Targets net8.0. Depends on Bodu.Financial.

Usage

Register the converters once, before the options instance is first used:

using System.Text.Json;
using Bodu.Financial;
using Bodu.Financial.Serialization.Json;

var options = new JsonSerializerOptions().AddFinancialJsonConverters();   // Strict

string json = JsonSerializer.Serialize(new Money(19.99m, CurrencyCode.USD), options);
// → {"amount":19.99,"currency":"USD"}

var compact = new JsonSerializerOptions()
    .AddFinancialJsonConverters(FinancialJsonPolicy.Compact);
// Money → "19.99 USD", CurrencyPair → "USD/JPY", MoneyBag → { "EUR": 12.34, "USD": 19.99 }

One policy value shapes every registered converter:

Policy Money / Money<TCurrency> MoneyBag ExchangeRate / CurrencyPair Intended use
Strict (default) object { "amount": 19.99, "currency": "USD" }; duplicate properties, mismatched or lowercase ISO codes rejected object with a "balances" map canonical object shapes (from / to / date / rate / provider; from / to) Ledger, persistence, and audit data.
Lenient as Strict, plus lowercase ISO codes and surrounding whitespace accepted on read as Strict as Strict, with the same read tolerance External-feed ingest. Writes as Strict.
Compact string "19.99 USD" flat ISO→amount object ExchangeRate object with a combined "pair"; CurrencyPair string "USD/JPY" Compact payloads for APIs and logs.

Converters can also be registered individually (for example only the typed money form) via options.Converters.Add(new MoneyOfTCurrencyJsonConverterFactory(FinancialJsonPolicy.Compact)).

Dependency injection

For containers, AddFinancialJson registers a configured JsonSerializerOptions as a keyed singleton under FinancialJsonServiceCollectionExtensions.JsonOptionsKey ("Financial"):

using Bodu.Financial.Serialization.Json;

services.AddFinancialJson(FinancialJsonPolicy.Compact);

var json = provider.GetRequiredKeyedService<JsonSerializerOptions>(
    FinancialJsonServiceCollectionExtensions.JsonOptionsKey);

Bodu.Financial.DependencyInjection's AddFinancialService does not register JSON options — pair it with the call above when financial JSON is needed.

Documentation

See the money guide for the full wire-shape reference and error behaviour.

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 was computed.  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 53 9/24/2026
0.7.0 127 9/24/2026
0.6.0 70 9/24/2026