I am working with an API that provides currency conversion rates in JSON like this:
{
"fx_rate": 1.01
}
When reading from that API, I get a map %{"fx_rate" => 1.01} and as far as I understand I immediately lose precision because I convert the literal representation of “1.01” to float 1.01 which might not be the same internally.
I’d like to make sure that Jason does not parse this 1.01 to float but instead returns it as string %{"fx_rate" => "1.01"}. Then I have exact representation and can use Decimal.new to make further computations.
Is there a way to advise Jason to treat that field as string?
I was thinking about making a plug that changes the request body still represented as a string. It would find the "fx_rate": 1.01, and change it to "fx_rate": "1.01" with a regular expression, so that Jason can later do its job.
But I figured I will ask here first, if you have less hacky ideas