Skip to content

Decimal is a custom type #848

Closed
Closed

Description

@NyanFisher

Hey!

I have a problem when I use msgspec and Decimal. One popular solution to using Decimal is to not round the data as part of the algorithm execution and domain layer. The rounding happens during demarshaling at the API endpoints. Decimal is the native type, so it's not possible to create an Encoder that can handle this.

Is it possible to change the behaviour for Decimal and allow interception in enc_hook?

Example:

from decimal import ROUND_HALF_UP, Decimal
from typing import Any

import msgspec


def enc_hook(obj: Any) -> Any:
    if isinstance(obj, Decimal):
        return str(obj.quantize(Decimal("0.0001"), rounding=ROUND_HALF_UP))
    else:
        raise NotImplementedError(f"Objects of type {type(obj)} are not supported")


class Price(msgspec.Struct):
    primecost: Decimal

enc = msgspec.json.Encoder(enc_hook=enc_hook)

price = Price(primecost=Decimal("1.1234444"))

buf = enc.encode(price)
# b'{"primecost":"1.1234"}'

Creating a custom Decimal MyDecimal looks like a workaround.

Activity

  1. changed the title [-]Decimal should be handled as a custom type during demarshaling.[/-] [+]Decimal is a custom type[/+] on Jun 22, 2025
  2. changed the title [-]Decimal is a custom type[/-] [+]`Decimal` is a custom type[/+] on Jun 22, 2025
  3. JimDabell commented on Aug 4, 2025

    @JimDabell

    See also #829 and #849.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions