Skip to content

Config Option For Money Serialization #723

Description

@Numie

Currently, the default serialization of a Money object results in an object with keys of cents and currency_iso:

m = Money.new(1000, 'USD')
m.to_json

=> "{\"cents\":1000, \"currency_iso\":\"USD\"}"

This is implemented by defining to_hash on Money here:

{ cents: cents, currency_iso: currency.iso_code.to_s }

The serialization is awkward for currencies that don't include cents, for example JPY:

m = Money.new(1000, 'JPY')
m.to_json

=> "{\"cents\":1000, \"currency_iso\":\"JPY\"}"

Is there any openness to adding a config option that would allow for customizing this behavior? E.g. config.amount_serialization_key = :amount could result in:

m = Money.new(1000, 'JPY')
m.to_json

=> "{\"amount\":1000, \"currency_iso\":\"JPY\"}"

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