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

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 read JSON in JSRational. However, I cannot write rational numbers! Is it on purpose?

+3
source share
1 answer

At the heart of the problem is that JSON combines floating point types and integer types - they are not distinguished using a type tag in JSON format. Therefore, we present all numeric types in JSON through Rationals, under the hood.

JSON Double, Int .., Rational - , :

instance JSON Rational where
    showJSON r = JSRational True r 
    readJSON (JSRational _ r) = return r
+6

Source: https://habr.com/ru/post/1758480/


All Articles