Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serialize amount as string #1

Open
totemcaf opened this issue Jul 16, 2022 · 1 comment
Open

Serialize amount as string #1

totemcaf opened this issue Jul 16, 2022 · 1 comment
Labels
enhancement New feature or request

Comments

@totemcaf
Copy link

The fpmoney.Amount serializes values to JSON as a floating point amount, and a currency code like this:

{
  "amount": 128.45, 
  "currency": "ARS"
}

That is used in frontends and persisted in databases as a floating point number.

Other currency packages serialize the amount as a string instead, to avoid conversions and lost of precision. For example:

{
  "amount": "128.45", 
  "currency": "ARS"
}

What do you think of having a global option or be customized by the format string stored under the "json" key (or a money key) in the struct field's tag:

type Invoice struct {
    TotalAmount fpmoney.Amount `json:"totalAmount,asString"`
}

I can provide a PR.

@nikolaydubina nikolaydubina added the enhancement New feature or request label Jul 16, 2022
@nikolaydubina
Copy link
Owner

nikolaydubina commented Jul 16, 2022

nice. that's a good one. 👍🏻

could be easily added to parser too.

Few notes:

  • better not to use json tags. to avoid reflection and gencode.
  • global option (package level var) - good option. please proceed draft PR if you want!
  • please add fuzz/unit test for this too if you make PR

I also need to think how this package can nicely provide both versions (JSON string vs JSON number) at same time. Should we have new type? Same type name but sub-package? 🤔

One thing that may be odd here though — people use string for serialising numbers (money) to avoid using parsing, conversions (float/int/bigint) and arithmetics codebases. Here in this package we exposing parsing and arithmetics. People who use string as number in their JSON may not want to use fpmoney. So use-case may not be a strong.

However, this is interesting extension to this package!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants