tags:

views:

45

answers:

1

When I try to decode a JSON file with a floating point number, the Text.JSON package gives me the number as a JSRational. So, I can do a readJSON on a JSRational. However, I can't write rational numbers! Is this on purpose?

+4  A: 

The background to the problem is that JSON conflates floating point and integer types -- they're not distinguished via a type tag in the JSON format. So we represent all numeric types in JSON via Rationals, under the hood.

Instances to convert into the JSON type are provided for Double, Int etc, but not for Rational -- though there is actually no good reason for this, as the instance is trivial:

instance JSON Rational where
    showJSON r = JSRational True r 
    readJSON (JSRational _ r) = return r
Don Stewart