Bodu.Financial.Serialization.Json
1.3.0
dotnet add package Bodu.Financial.Serialization.Json --version 1.3.0
NuGet\Install-Package Bodu.Financial.Serialization.Json -Version 1.3.0
<PackageReference Include="Bodu.Financial.Serialization.Json" Version="1.3.0" />
<PackageVersion Include="Bodu.Financial.Serialization.Json" Version="1.3.0" />
<PackageReference Include="Bodu.Financial.Serialization.Json" />
paket add Bodu.Financial.Serialization.Json --version 1.3.0
#r "nuget: Bodu.Financial.Serialization.Json, 1.3.0"
#:package Bodu.Financial.Serialization.Json@1.3.0
#addin nuget:?package=Bodu.Financial.Serialization.Json&version=1.3.0
#tool nuget:?package=Bodu.Financial.Serialization.Json&version=1.3.0
Bodu.Financial.Serialization.Json
API stability - Stable. The public API surface is committed: breaking changes are reserved for a major-version bump per SemVer, except a deliberate break that is recorded in the release notes and in the package's
CompatibilitySuppressions.xml. See versioning and support.
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 serializableBodu.Financialtype.- Converters (+ a factory for the generic type) for
Money,Money<TCurrency>,CalculatedMoney,MoneyBag,ExchangeRate, andCurrencyPair. 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 configuredJsonSerializerOptionsas a keyed singleton (key"Financial").
Migration note. Earlier
Bodu.Financialbuilds 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 does not throw -JsonSerializersilently falls back to reflection-shaped output, and reads money back as zero or currency-less values - so audit anyJsonSerializercall sites for these types when upgrading.
Installation
dotnet add package Bodu.Financial.Serialization.Json
Targets net8.0 and net10.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.Currencies;
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; a repeated balances property or ISO key rejected |
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, with the same read tolerance for the ISO keys |
as Strict, with the same read tolerance |
External-feed ingest. Writes as Strict. |
Compact |
string "19.99 USD" |
flat ISO→amount object; a repeated ISO key rejected | 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 JSON serialization guide for the full wire-shape reference and error behaviour.
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.Financial (>= 1.3.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.12)
-
net8.0
- Bodu.Financial (>= 1.3.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.2)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.