Table of Contents

MoneyJsonConverter Class

Definition

Namespace
Bodu.Financial.Serialization.Json
Assembly
Bodu.Financial.Serialization.Json.dll
Package
Bodu.Financial.Serialization.Json 1.0.0
Source
MoneyJsonConverter.cs

Converts a Money to and from JSON using the policy supplied at construction. Mirrors the shape vocabulary of MoneyOfTCurrencyJsonConverter<TCurrency> so a single FinancialJsonPolicy selection produces a coherent on-the-wire format across the monetary types.

public sealed class MoneyJsonConverter : JsonConverter<Money>
Inheritance
MoneyJsonConverter
Inherited Members
Extension Methods

Remarks

The Strict and Lenient policies share the canonical object shape {"amount":19.99,"currency":"USD"}. A value carrying an explicit minor-unit scale - a unit price whose precision differs from the currency's registered minor units (see FromExplicitScale(decimal, CurrencyCode, int, MidpointRounding)) - additionally emits a scale property, for example {"amount":145.678912,"currency":"USD","scale":6}, and the reader reconstructs the value at that scale so the precision (including trailing zeros) survives the round-trip. A payload without scale deserializes at the registry precision, keeping previously written documents valid.

The Compact policy writes a single string, "19.99 USD", padding the amount to the value's minor units. On read, an amount printed with more fractional digits than the currency's registered minor units is reconstructed at that finer scale (via TryParse(ReadOnlySpan<char>, IFormatProvider?, out Money)), so a unit price round-trips through the compact form without separate scale metadata; a scale coarser than the registered precision is preserved only by the object shapes.

Constructors

MoneyJsonConverter()

Initializes a new instance of the MoneyJsonConverter class configured for the Strict shape.

public MoneyJsonConverter()

MoneyJsonConverter(FinancialJsonPolicy)

Initializes a new instance of the MoneyJsonConverter class configured for the supplied policy.

public MoneyJsonConverter(FinancialJsonPolicy policy)

Parameters

policy FinancialJsonPolicy

The serialization policy.

Exceptions

ArgumentOutOfRangeException

policy is not a defined FinancialJsonPolicy value.

Methods

Read(ref Utf8JsonReader, Type, JsonSerializerOptions)

Reads and converts the JSON to type Money.

public override Money Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)

Parameters

reader Utf8JsonReader

The reader.

typeToConvert Type

The type to convert.

options JsonSerializerOptions

An object that specifies serialization options to use.

Returns

Money

The converted value.

Write(Utf8JsonWriter, Money, JsonSerializerOptions)

Writes a specified value as JSON.

public override void Write(Utf8JsonWriter writer, Money value, JsonSerializerOptions options)

Parameters

writer Utf8JsonWriter

The writer to write to.

value Money

The value to convert to JSON.

options JsonSerializerOptions

An object that specifies serialization options to use.

Applies to

ProductVersions
.NET8, 10