Skip to main content

Module decimal

Module decimal 

Source
Expand description

Exact decimal parsing and JSON serialization contracts.

Exact decimal ingestion, separate from instrument and currency rounding.

deserialize_json preserves quoted and unquoted JSON tokens with the default features. It uses RawValue, which Serde’s internally tagged and untagged enum buffers do not support.

Use deserialize for those buffers: strings and 64-bit integers work with all feature sets. Larger integers, fractional tokens, and scientific numeric tokens require serde_json/arbitrary_precision from the first JSON parse. Floats are rejected because their original decimal digits are unavailable. Even with arbitrary precision, Number’s Serde deserializer delivers some decimals as floats. For buffered Value use deserialize_json; for Number use parse(&number.to_string()). These routes preserve the buffered decimal text when arbitrary precision was enabled at ingest. Serde enum buffering of a Value may still deliver floats, which are rejected.

The visitor also accepts serde_json’s private number-map encoding with arbitrary precision enabled. A literal JSON object using that reserved key is indistinguishable from the numeric encoding. Use the raw-token route before buffering when every JSON object must be rejected. With arbitrary precision, Value parsing itself interprets the reserved object as a number.

A Value or Number created through floating-point conversion has already lost its source spelling. No API here recovers those digits. In particular, passing a default-feature Value to deserialize_json reparses its rendered value, not the original JSON token.

Required fields reject missing, null, empty, invalid, overflowing, and underflowing values. Optional fields accept null as None; add #[serde(default)] to accept missing fields. Empty strings remain errors. Representability concerns numeric value, so redundant trailing zeros may be removed. Instrument precision and currency rounding belong to model constructors.

serialize emits exact JSON numbers, including through serde_json::to_value. Enable serde_json/arbitrary_precision for the full Decimal range. Without it, values whose numeric value would change are errors. String output uses serialize_decimal_as_str and serialize_optional_decimal_as_str. These JSON number contracts do not extend to MessagePack or other Serde formats. The parent module’s permissive deserializers accept null and empty strings, round excess fractional digits, and preserve exactly representable scientific strings. Oversized integer inputs return errors.

Functions§

deserialize
Deserializes an exact decimal from strings, integers, or arbitrary-precision number maps.
deserialize_json
Deserializes an exact decimal directly from a quoted or unquoted JSON token.
deserialize_optional
Deserializes an optional exact decimal, accepting null as None.
deserialize_optional_json
Deserializes an optional exact JSON decimal, accepting null as None.
parse
Parses a decimal or scientific string without rounding its numeric value.
serialize
Serializes a decimal as a JSON number without changing its numeric value.
serialize_optional
Serializes an optional decimal as an exact JSON number or null.