Bodu.Financial.Serialization.Json
0.6.0
See the version list below for details.
dotnet add package Bodu.Financial.Serialization.Json --version 0.6.0
NuGet\Install-Package Bodu.Financial.Serialization.Json -Version 0.6.0
<PackageReference Include="Bodu.Financial.Serialization.Json" Version="0.6.0" />
<PackageVersion Include="Bodu.Financial.Serialization.Json" Version="0.6.0" />
<PackageReference Include="Bodu.Financial.Serialization.Json" />
paket add Bodu.Financial.Serialization.Json --version 0.6.0
#r "nuget: Bodu.Financial.Serialization.Json, 0.6.0"
#:package Bodu.Financial.Serialization.Json@0.6.0
#addin nuget:?package=Bodu.Financial.Serialization.Json&version=0.6.0
#tool nuget:?package=Bodu.Financial.Serialization.Json&version=0.6.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.
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>,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 no longer throws —JsonSerializersilently falls back to reflection-shaped output — so audit anyJsonSerializer.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 | 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 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. |
-
net8.0
- Bodu.Financial (>= 0.6.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.