-
Notifications
You must be signed in to change notification settings - Fork 720
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
39 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
module Cardano.Api.Json | ||
( toRationalJSON | ||
) where | ||
|
||
import Data.Aeson | ||
import Data.Either | ||
import Data.Maybe | ||
import Data.Scientific | ||
import GHC.Real | ||
|
||
-- Rationals and JSON are an awkward mix. We cannot convert rationals | ||
-- like @1/3@ to JSON numbers. But _most_ of the numbers we want to use | ||
-- in practice have simple decimal representations. Our solution here is | ||
-- to use simple decimal representations where we can and representation | ||
-- in a @{"numerator": 1, "denominator": 3}@ style otherwise. | ||
-- | ||
toRationalJSON :: Rational -> Value | ||
toRationalJSON r = | ||
case fromRationalRepetendLimited 20 r of | ||
Right (s, Nothing) -> toJSON s | ||
_ -> toJSON r |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters