Haskell Text.Json package can read but not write Rationals? Haskell Text.Json package can read but not write Rationals? json json

Haskell Text.Json package can read but not write Rationals?


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