clojurewerkz / money

A Clojure library that deals with monetary values and currencies. Built on top of Joda Money.
155 stars 20 forks source link

JSON serializer uses locale-specific formatting #9

Closed brendanyounger closed 8 years ago

brendanyounger commented 9 years ago

I would like to be able to serialize an amount and read it back with the proper currency. Unfortunately, the json serializer uses locale-specific formatting, so I cannot use clojurewerkz.money.amounts/parse on the result. I would like to suggest that the amount be serialized in a locale-independent format so that it can be read again.

As an example, the following code throws org.joda.money.IllegalCurrencyException: Unknown currency '$10' for me (with default locale of "US").

(require '[clojurewerkz.money.amounts :as ma])
(require '[clojurewerkz.money.currencies :as mc])
(require 'clojurewerkz.money.json)
(require '[cheshire.core :as json])

(->>
  100.1
  (ma/amount-of mc/USD)
  (json/generate-string) ;; yields "$100.10" for me
  (json/parse-string)
  (ma/parse))
michaelklishin commented 9 years ago

@brendanyounger I'd make existing serialisation functions locale-independent and introduce new ones that are locale-dependent. Feel free to submit a PR.