开发者

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

开发者 https://www.devze.com 2023-01-10 09:33 出处:网络
When I try to d开发者_运维知识库ecode 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 wr

When I try to d开发者_运维知识库ecode 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?


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
0

精彩评论

暂无评论...
验证码 换一张
取 消