{-# OPTIONS_GHC -Wno-orphans #-}
module Yamlet.Aeson
( ViaAeson (..)
) where
import Control.Monad
import Data.Aeson qualified as A
import Data.Aeson.Decoding.ByteString.Lazy
import Data.Aeson.Decoding.Tokens
import Data.Aeson.Key qualified as K
import Data.Aeson.KeyMap qualified as KM
import Data.Aeson.Types qualified as A
import Data.ByteString.Lazy.Char8 qualified as LBS8
import Data.List qualified as L
import Data.Map.Strict qualified as M
import Data.Scientific qualified as Sci
import Data.Set qualified as Set
import Data.Text qualified as T
import Data.Vector qualified as V
import Yamlet
import Yamlet.Syntax qualified as S
newtype ViaAeson a = ViaAeson {forall a. ViaAeson a -> a
value :: a}
deriving stock (ViaAeson a -> ViaAeson a -> Bool
(ViaAeson a -> ViaAeson a -> Bool)
-> (ViaAeson a -> ViaAeson a -> Bool) -> Eq (ViaAeson a)
forall a. Eq a => ViaAeson a -> ViaAeson a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: forall a. Eq a => ViaAeson a -> ViaAeson a -> Bool
== :: ViaAeson a -> ViaAeson a -> Bool
$c/= :: forall a. Eq a => ViaAeson a -> ViaAeson a -> Bool
/= :: ViaAeson a -> ViaAeson a -> Bool
Eq, Eq (ViaAeson a)
Eq (ViaAeson a) =>
(ViaAeson a -> ViaAeson a -> Ordering)
-> (ViaAeson a -> ViaAeson a -> Bool)
-> (ViaAeson a -> ViaAeson a -> Bool)
-> (ViaAeson a -> ViaAeson a -> Bool)
-> (ViaAeson a -> ViaAeson a -> Bool)
-> (ViaAeson a -> ViaAeson a -> ViaAeson a)
-> (ViaAeson a -> ViaAeson a -> ViaAeson a)
-> Ord (ViaAeson a)
ViaAeson a -> ViaAeson a -> Bool
ViaAeson a -> ViaAeson a -> Ordering
ViaAeson a -> ViaAeson a -> ViaAeson a
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall a. Ord a => Eq (ViaAeson a)
forall a. Ord a => ViaAeson a -> ViaAeson a -> Bool
forall a. Ord a => ViaAeson a -> ViaAeson a -> Ordering
forall a. Ord a => ViaAeson a -> ViaAeson a -> ViaAeson a
$ccompare :: forall a. Ord a => ViaAeson a -> ViaAeson a -> Ordering
compare :: ViaAeson a -> ViaAeson a -> Ordering
$c< :: forall a. Ord a => ViaAeson a -> ViaAeson a -> Bool
< :: ViaAeson a -> ViaAeson a -> Bool
$c<= :: forall a. Ord a => ViaAeson a -> ViaAeson a -> Bool
<= :: ViaAeson a -> ViaAeson a -> Bool
$c> :: forall a. Ord a => ViaAeson a -> ViaAeson a -> Bool
> :: ViaAeson a -> ViaAeson a -> Bool
$c>= :: forall a. Ord a => ViaAeson a -> ViaAeson a -> Bool
>= :: ViaAeson a -> ViaAeson a -> Bool
$cmax :: forall a. Ord a => ViaAeson a -> ViaAeson a -> ViaAeson a
max :: ViaAeson a -> ViaAeson a -> ViaAeson a
$cmin :: forall a. Ord a => ViaAeson a -> ViaAeson a -> ViaAeson a
min :: ViaAeson a -> ViaAeson a -> ViaAeson a
Ord, Int -> ViaAeson a -> ShowS
[ViaAeson a] -> ShowS
ViaAeson a -> String
(Int -> ViaAeson a -> ShowS)
-> (ViaAeson a -> String)
-> ([ViaAeson a] -> ShowS)
-> Show (ViaAeson a)
forall a. Show a => Int -> ViaAeson a -> ShowS
forall a. Show a => [ViaAeson a] -> ShowS
forall a. Show a => ViaAeson a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: forall a. Show a => Int -> ViaAeson a -> ShowS
showsPrec :: Int -> ViaAeson a -> ShowS
$cshow :: forall a. Show a => ViaAeson a -> String
show :: ViaAeson a -> String
$cshowList :: forall a. Show a => [ViaAeson a] -> ShowS
showList :: [ViaAeson a] -> ShowS
Show)
instance FromYaml A.Value where
parseYaml :: Node -> Parser Value
parseYaml = (Node -> Parser Value) -> Node -> Parser Value
forall a. (Node -> Parser a) -> Node -> Parser a
parseNode ((Node -> Parser Value) -> Node -> Parser Value)
-> (Node -> Parser Value) -> Node -> Parser Value
forall a b. (a -> b) -> a -> b
$ \Node
n -> case Node -> View
view Node
n of
View
NullView -> Value -> Parser Value
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Value
A.Null
BoolView Bool
b -> Value -> Parser Value
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Value -> Parser Value) -> Value -> Parser Value
forall a b. (a -> b) -> a -> b
$! Bool -> Value
A.Bool Bool
b
IntView Integer
i -> Value -> Parser Value
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Value -> Parser Value) -> Value -> Parser Value
forall a b. (a -> b) -> a -> b
$! Scientific -> Value
A.Number (Integer -> Scientific
forall a. Num a => Integer -> a
fromInteger Integer
i)
FloatView FloatValue
f ->
Value -> Parser Value
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Value -> Parser Value) -> Value -> Parser Value
forall a b. (a -> b) -> a -> b
$! case FloatValue
f of
Finite Scientific
s
| Scientific
s Scientific -> Scientific -> Bool
forall a. Eq a => a -> a -> Bool
== Scientific
0 -> Value
zero
| Bool
otherwise -> Scientific -> Value
A.Number Scientific
s
FloatValue
NegativeZero -> Value
zero
FloatValue
Infinity -> Text -> Value
A.String Text
"+inf"
FloatValue
NegativeInfinity -> Text -> Value
A.String Text
"-inf"
FloatValue
NaN -> Value
A.Null
StringView Text
t -> Value -> Parser Value
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Value -> Parser Value) -> Value -> Parser Value
forall a b. (a -> b) -> a -> b
$! Text -> Value
A.String (Text -> Text
T.copy Text
t)
SequenceView [Node]
xs -> Array -> Value
A.Array (Array -> Value) -> ([Value] -> Array) -> [Value] -> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Value] -> Array
forall a. [a] -> Vector a
V.fromList ([Value] -> Value) -> Parser [Value] -> Parser Value
forall (m :: * -> *) a b. Monad m => (a -> b) -> m a -> m b
<$!> (Node -> Parser Value) -> [Node] -> Parser [Value]
forall a. (Node -> Parser a) -> [Node] -> Parser [a]
parseItems Node -> Parser Value
forall a. FromYaml a => Node -> Parser a
parseYaml [Node]
xs
MappingView [(Node, Node)]
_ -> Map KeyText Value -> Value
object (Map KeyText Value -> Value)
-> Parser (Map KeyText Value) -> Parser Value
forall (m :: * -> *) a b. Monad m => (a -> b) -> m a -> m b
<$!> Node -> Parser (Map KeyText Value)
forall a. FromYaml a => Node -> Parser a
parseYaml Node
n
AliasView Text
_ -> String -> Node -> Parser Value
forall a. String -> Node -> Parser a
typeMismatch String
"a value" Node
n
where
zero :: A.Value
zero :: Value
zero = Scientific -> Value
A.Number (Integer -> Int -> Scientific
Sci.scientific Integer
0 (-Int
1))
object :: M.Map KeyText A.Value -> A.Value
object :: Map KeyText Value -> Value
object = Object -> Value
A.Object (Object -> Value)
-> (Map KeyText Value -> Object) -> Map KeyText Value -> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Map Key Value -> Object
forall v. Map Key v -> KeyMap v
KM.fromMap (Map Key Value -> Object)
-> (Map KeyText Value -> Map Key Value)
-> Map KeyText Value
-> Object
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (KeyText -> Key) -> Map KeyText Value -> Map Key Value
forall k1 k2 a. (k1 -> k2) -> Map k1 a -> Map k2 a
M.mapKeysMonotonic (\(KeyText Text
t) -> Text -> Key
K.fromText Text
t)
instance ToYaml A.Value where
toYaml :: Value -> Node
toYaml = Value -> Node
forall a. ToYaml a => a -> Node
toYaml (Value -> Node) -> (Value -> Value) -> Value -> Node
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Value -> Value
aesonValue
instance A.FromJSON a => FromYaml (ViaAeson a) where
parseYaml :: Node -> Parser (ViaAeson a)
parseYaml Node
n = do
v <- Node -> Parser Value
forall a. FromYaml a => Node -> Parser a
parseYaml Node
n
case A.ifromJSON @a v of
A.ISuccess a
a -> ViaAeson a -> Parser (ViaAeson a)
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (a -> ViaAeson a
forall a. a -> ViaAeson a
ViaAeson a
a)
A.IError JSONPath
path String
msg -> JSONPath -> String -> Node -> Parser (ViaAeson a)
forall b. JSONPath -> String -> Node -> Parser b
failAtPath JSONPath
path String
msg Node
n
where
failAtPath :: A.JSONPath -> String -> S.Node -> Parser b
failAtPath :: forall b. JSONPath -> String -> Node -> Parser b
failAtPath JSONPath
path String
msg Node
node = case (JSONPath
path, Node
node.content) of
([], Content
_) -> Node -> String -> Parser b
forall a. Node -> String -> Parser a
failAt Node
node String
msg
(A.Key Key
key : JSONPath
rest, S.MappingContent CollectionStyle
_ [(Node, Node)]
kvs)
| Just (Node
_, Node
v) <- ((Node, Node) -> Bool) -> [(Node, Node)] -> Maybe (Node, Node)
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
L.find (Text -> Node -> Bool
isKey (Key -> Text
K.toText Key
key) (Node -> Bool) -> ((Node, Node) -> Node) -> (Node, Node) -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Node, Node) -> Node
forall a b. (a, b) -> a
fst) [(Node, Node)]
kvs ->
JSONPath -> String -> Node -> Parser b
forall b. JSONPath -> String -> Node -> Parser b
failAtPath JSONPath
rest String
msg Node
v
(A.Index Int
i : JSONPath
rest, S.SequenceContent CollectionStyle
_ [Node]
xs)
| Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
0
, Node
x : [Node]
_ <- Int -> [Node] -> [Node]
forall a. Int -> [a] -> [a]
drop Int
i [Node]
xs ->
JSONPath -> String -> Node -> Parser b
forall b. JSONPath -> String -> Node -> Parser b
failAtPath JSONPath
rest String
msg Node
x
(JSONPath, Content)
_ ->
Node -> String -> Parser b
forall a. Node -> String -> Parser a
failAt Node
node (String
msg String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
" at " String -> ShowS
forall a. [a] -> [a] -> [a]
++ Path -> String
renderPath ([PathElement] -> Path
pathFromElements ((JSONPathElement -> PathElement) -> JSONPath -> [PathElement]
forall a b. (a -> b) -> [a] -> [b]
map JSONPathElement -> PathElement
element JSONPath
path)))
isKey :: T.Text -> S.Node -> Bool
isKey :: Text -> Node -> Bool
isKey Text
key Node
k = case Node
k.content of
S.ScalarContent ScalarStyle
_ Text
t -> Text
t Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
key
Content
_ -> Bool
False
element :: A.JSONPathElement -> PathElement
element :: JSONPathElement -> PathElement
element = \case
A.Key Key
key -> Text -> PathElement
Key (Key -> Text
K.toText Key
key)
A.Index Int
i -> Int -> PathElement
Index Int
i
instance A.ToJSON a => ToYaml (ViaAeson a) where
toYaml :: ViaAeson a -> Node
toYaml (ViaAeson a
a) = Value -> Node
forall a. ToYaml a => a -> Node
toYaml (Value -> Node) -> Value -> Node
forall a b. (a -> b) -> a -> b
$ case Tokens ByteString String -> Either String (Value, ByteString)
forall t. Tokens t String -> Either String (Value, t)
value (ByteString -> Tokens ByteString String
lbsToTokens (a -> ByteString
forall a. ToJSON a => a -> ByteString
A.encode a
a)) of
Left String
err -> String -> Value
forall b. String -> b
invalid String
err
Right (Value
v, ByteString
rest)
| (Char -> Bool) -> ByteString -> Bool
LBS8.all (Char -> String -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` String
jsonSpace) ByteString
rest -> Value
v
| Bool
otherwise -> String -> Value
forall b. String -> b
invalid (String -> Value) -> String -> Value
forall a b. (a -> b) -> a -> b
$ String
"unexpected " String -> ShowS
forall a. [a] -> [a] -> [a]
++ ShowS
forall a. Show a => a -> String
show (ByteString -> String
LBS8.unpack ByteString
rest) String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
" after the value"
where
jsonSpace :: String
jsonSpace :: String
jsonSpace = String
" \t\n\r"
invalid :: String -> b
invalid :: forall b. String -> b
invalid String
err =
String -> b
forall a. String -> a
errorWithoutStackTrace (String -> b) -> String -> b
forall a b. (a -> b) -> a -> b
$
String
"Yamlet.Aeson.ViaAeson: the toEncoding is not valid JSON: " String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
err
value :: Tokens t String -> Either String (Value, t)
value :: forall t. Tokens t String -> Either String (Value, t)
value = \case
TkLit Lit
l t
rest -> (Value, t) -> Either String (Value, t)
forall a b. b -> Either a b
Right (Lit -> Value
lit Lit
l, t
rest)
TkText Text
t t
rest -> (Value, t) -> Either String (Value, t)
forall a b. b -> Either a b
Right (Text -> Value
String Text
t, t
rest)
TkNumber Number
n t
rest -> (Value, t) -> Either String (Value, t)
forall a b. b -> Either a b
Right (Number -> Value
number Number
n, t
rest)
TkArrayOpen TkArray t String
arr -> [Value] -> TkArray t String -> Either String (Value, t)
forall t. [Value] -> TkArray t String -> Either String (Value, t)
items [] TkArray t String
arr
TkRecordOpen TkRecord t String
r -> [(Key, Value)] -> TkRecord t String -> Either String (Value, t)
forall t.
[(Key, Value)] -> TkRecord t String -> Either String (Value, t)
pairs [] TkRecord t String
r
TkErr String
err -> String -> Either String (Value, t)
forall a b. a -> Either a b
Left String
err
items :: [Value] -> TkArray t String -> Either String (Value, t)
items :: forall t. [Value] -> TkArray t String -> Either String (Value, t)
items [Value]
acc = \case
TkItem Tokens (TkArray t String) String
toks -> Tokens (TkArray t String) String
-> Either String (Value, TkArray t String)
forall t. Tokens t String -> Either String (Value, t)
value Tokens (TkArray t String) String
toks Either String (Value, TkArray t String)
-> ((Value, TkArray t String) -> Either String (Value, t))
-> Either String (Value, t)
forall a b.
Either String a -> (a -> Either String b) -> Either String b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \(Value
v, TkArray t String
rest) -> [Value] -> TkArray t String -> Either String (Value, t)
forall t. [Value] -> TkArray t String -> Either String (Value, t)
items (Value
v Value -> [Value] -> [Value]
forall a. a -> [a] -> [a]
: [Value]
acc) TkArray t String
rest
TkArrayEnd t
rest -> (Value, t) -> Either String (Value, t)
forall a b. b -> Either a b
Right ([Value] -> Value
Sequence ([Value] -> [Value]
forall a. [a] -> [a]
reverse [Value]
acc), t
rest)
TkArrayErr String
err -> String -> Either String (Value, t)
forall a b. a -> Either a b
Left String
err
pairs :: [(A.Key, Value)] -> TkRecord t String -> Either String (Value, t)
pairs :: forall t.
[(Key, Value)] -> TkRecord t String -> Either String (Value, t)
pairs [(Key, Value)]
acc = \case
TkPair Key
key Tokens (TkRecord t String) String
toks -> Tokens (TkRecord t String) String
-> Either String (Value, TkRecord t String)
forall t. Tokens t String -> Either String (Value, t)
value Tokens (TkRecord t String) String
toks Either String (Value, TkRecord t String)
-> ((Value, TkRecord t String) -> Either String (Value, t))
-> Either String (Value, t)
forall a b.
Either String a -> (a -> Either String b) -> Either String b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \(Value
v, TkRecord t String
rest) -> [(Key, Value)] -> TkRecord t String -> Either String (Value, t)
forall t.
[(Key, Value)] -> TkRecord t String -> Either String (Value, t)
pairs ((Key
key, Value
v) (Key, Value) -> [(Key, Value)] -> [(Key, Value)]
forall a. a -> [a] -> [a]
: [(Key, Value)]
acc) TkRecord t String
rest
TkRecordEnd t
rest -> (Value, t) -> Either String (Value, t)
forall a b. b -> Either a b
Right ([(Value, Value)] -> Value
Mapping (Set Key -> [(Key, Value)] -> [(Value, Value)]
firstKeys Set Key
forall a. Set a
Set.empty ([(Key, Value)] -> [(Key, Value)]
forall a. [a] -> [a]
reverse [(Key, Value)]
acc)), t
rest)
TkRecordErr String
err -> String -> Either String (Value, t)
forall a b. a -> Either a b
Left String
err
firstKeys :: Set.Set A.Key -> [(A.Key, Value)] -> [(Value, Value)]
firstKeys :: Set Key -> [(Key, Value)] -> [(Value, Value)]
firstKeys Set Key
seen = \case
[] -> []
(Key
key, Value
v) : [(Key, Value)]
rest
| Key
key Key -> Set Key -> Bool
forall a. Ord a => a -> Set a -> Bool
`Set.member` Set Key
seen -> Set Key -> [(Key, Value)] -> [(Value, Value)]
firstKeys Set Key
seen [(Key, Value)]
rest
| Bool
otherwise -> (Text -> Value
String (Key -> Text
K.toText Key
key), Value
v) (Value, Value) -> [(Value, Value)] -> [(Value, Value)]
forall a. a -> [a] -> [a]
: Set Key -> [(Key, Value)] -> [(Value, Value)]
firstKeys (Key -> Set Key -> Set Key
forall a. Ord a => a -> Set a -> Set a
Set.insert Key
key Set Key
seen) [(Key, Value)]
rest
lit :: Lit -> Value
lit :: Lit -> Value
lit = \case
Lit
LitNull -> Value
Null
Lit
LitTrue -> Bool -> Value
Bool Bool
True
Lit
LitFalse -> Bool -> Value
Bool Bool
False
number :: Number -> Value
number :: Number -> Value
number = \case
NumInteger Integer
i -> Integer -> Value
Int Integer
i
NumDecimal Scientific
s -> FloatValue -> Value
Float (Scientific -> FloatValue
Finite Scientific
s)
NumScientific Scientific
s -> FloatValue -> Value
Float (Scientific -> FloatValue
Finite Scientific
s)
newtype KeyText = KeyText T.Text
deriving stock (KeyText -> KeyText -> Bool
(KeyText -> KeyText -> Bool)
-> (KeyText -> KeyText -> Bool) -> Eq KeyText
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: KeyText -> KeyText -> Bool
== :: KeyText -> KeyText -> Bool
$c/= :: KeyText -> KeyText -> Bool
/= :: KeyText -> KeyText -> Bool
Eq, Eq KeyText
Eq KeyText =>
(KeyText -> KeyText -> Ordering)
-> (KeyText -> KeyText -> Bool)
-> (KeyText -> KeyText -> Bool)
-> (KeyText -> KeyText -> Bool)
-> (KeyText -> KeyText -> Bool)
-> (KeyText -> KeyText -> KeyText)
-> (KeyText -> KeyText -> KeyText)
-> Ord KeyText
KeyText -> KeyText -> Bool
KeyText -> KeyText -> Ordering
KeyText -> KeyText -> KeyText
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: KeyText -> KeyText -> Ordering
compare :: KeyText -> KeyText -> Ordering
$c< :: KeyText -> KeyText -> Bool
< :: KeyText -> KeyText -> Bool
$c<= :: KeyText -> KeyText -> Bool
<= :: KeyText -> KeyText -> Bool
$c> :: KeyText -> KeyText -> Bool
> :: KeyText -> KeyText -> Bool
$c>= :: KeyText -> KeyText -> Bool
>= :: KeyText -> KeyText -> Bool
$cmax :: KeyText -> KeyText -> KeyText
max :: KeyText -> KeyText -> KeyText
$cmin :: KeyText -> KeyText -> KeyText
min :: KeyText -> KeyText -> KeyText
Ord)
instance FromYaml KeyText where
parseYaml :: Node -> Parser KeyText
parseYaml = (Node -> Parser KeyText) -> Node -> Parser KeyText
forall a. (Node -> Parser a) -> Node -> Parser a
parseNode ((Node -> Parser KeyText) -> Node -> Parser KeyText)
-> (Node -> Parser KeyText) -> Node -> Parser KeyText
forall a b. (a -> b) -> a -> b
$ \Node
k -> case Node
k.content of
S.ScalarContent ScalarStyle
_ Text
t -> KeyText -> Parser KeyText
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (KeyText -> Parser KeyText) -> KeyText -> Parser KeyText
forall a b. (a -> b) -> a -> b
$! Text -> KeyText
KeyText (Text -> Text
T.copy Text
t)
Content
_ -> String -> Node -> Parser KeyText
forall a. String -> Node -> Parser a
typeMismatch String
"a scalar key" Node
k
aesonValue :: A.Value -> Value
aesonValue :: Value -> Value
aesonValue = \case
Value
A.Null -> Value
Null
A.Bool Bool
b -> Bool -> Value
Bool Bool
b
A.Number Scientific
s -> Scientific -> Value
number Scientific
s
A.String Text
t -> Text -> Value
String Text
t
A.Array Array
xs -> [Value] -> Value
Sequence ((Value -> Value) -> [Value] -> [Value]
forall a b. (a -> b) -> [a] -> [b]
map Value -> Value
aesonValue (Array -> [Value]
forall a. Vector a -> [a]
V.toList Array
xs))
A.Object Object
o -> [(Value, Value)] -> Value
Mapping [(Text -> Value
String (Key -> Text
K.toText Key
k), Value -> Value
aesonValue Value
v) | (Key
k, Value
v) <- Object -> [(Key, Value)]
forall v. KeyMap v -> [(Key, v)]
KM.toList Object
o]
where
number :: Sci.Scientific -> Value
number :: Scientific -> Value
number Scientific
s
| Int
e Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0 Bool -> Bool -> Bool
|| Int
e Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
1024 = FloatValue -> Value
Float (Scientific -> FloatValue
Finite Scientific
s)
| Bool
otherwise = Integer -> Value
Int (Scientific -> Integer
Sci.coefficient Scientific
s Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Integer
10 Integer -> Int -> Integer
forall a b. (Num a, Integral b) => a -> b -> a
^ Int
e)
where
e :: Int
e :: Int
e = Scientific -> Int
Sci.base10Exponent Scientific
s