{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE Trustworthy #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE DefaultSignatures #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE ViewPatterns #-}
{-# OPTIONS_GHC -Wno-orphans #-}
module Data.Aeson.Lens
(
AsNumber(..)
, _Integral
, nonNull
, AsValue(..)
, key, atKey, members
, nth, values
, IsKey(..)
, AsJSON(..)
, _JSON'
, pattern JSON
, pattern Value_
, pattern Number_
, pattern Double
, pattern Integer
, pattern Integral
, pattern Bool_
, pattern String_
, pattern Null_
, pattern Key_
) where
import Control.Applicative
import Control.Lens
import Data.Aeson
import qualified Data.Aeson.Key as Key
import qualified Data.Aeson.KeyMap as KM
import Data.Aeson.KeyMap (KeyMap)
import Data.Scientific (Scientific)
import qualified Data.Scientific as Scientific
import qualified Data.ByteString as Strict
import Data.ByteString.Lazy.Char8 as Lazy hiding (putStrLn)
import Data.Text as Text
import qualified Data.Text.Encoding.Error as Encoding
import qualified Data.Text.Lazy as LazyText
import Data.Text.Short (ShortText)
import Data.Text.Lens (packed)
import qualified Data.Text.Encoding as StrictText
import qualified Data.Text.Lazy.Encoding as LazyText
import Data.Vector (Vector)
import Prelude hiding (null)
class AsNumber t where
_Number :: Prism' t Scientific
default _Number :: AsValue t => Prism' t Scientific
_Number = forall t. AsValue t => Prism' t Value
_Valueforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall t. AsNumber t => Prism' t Scientific
_Number
{-# INLINE _Number #-}
_Double :: Prism' t Double
_Double = forall t. AsNumber t => Prism' t Scientific
_Numberforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso forall a. RealFloat a => Scientific -> a
Scientific.toRealFloat forall a b. (Real a, Fractional b) => a -> b
realToFrac
{-# INLINE _Double #-}
_Integer :: Prism' t Integer
_Integer = forall t. AsNumber t => Prism' t Scientific
_Numberforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso forall a b. (RealFrac a, Integral b) => a -> b
floor forall a b. (Integral a, Num b) => a -> b
fromIntegral
{-# INLINE _Integer #-}
instance AsNumber Value where
_Number :: Prism' Value Scientific
_Number = forall b t s a. (b -> t) -> (s -> Either t a) -> Prism s t a b
prism Scientific -> Value
Number forall a b. (a -> b) -> a -> b
$ \Value
v -> case Value
v of Number Scientific
n -> forall a b. b -> Either a b
Right Scientific
n; Value
_ -> forall a b. a -> Either a b
Left Value
v
{-# INLINE _Number #-}
instance AsNumber Scientific where
_Number :: Prism' Scientific Scientific
_Number = forall a. a -> a
id
{-# INLINE _Number #-}
instance AsNumber Strict.ByteString
instance AsNumber Lazy.ByteString
instance AsNumber Text
instance AsNumber LazyText.Text
instance AsNumber String
_Integral :: (AsNumber t, Integral a) => Prism' t a
_Integral :: forall t a. (AsNumber t, Integral a) => Prism' t a
_Integral = forall t. AsNumber t => Prism' t Scientific
_Number forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso forall a b. (RealFrac a, Integral b) => a -> b
floor forall a b. (Integral a, Num b) => a -> b
fromIntegral
{-# INLINE _Integral #-}
nonNull :: Prism' Value Value
nonNull :: Prism' Value Value
nonNull = forall b t s a. (b -> t) -> (s -> Either t a) -> Prism s t a b
prism forall a. a -> a
id (\Value
v -> if forall s t a b. APrism s t a b -> s -> Bool
isn't forall t. AsValue t => Prism' t ()
_Null Value
v then forall a b. b -> Either a b
Right Value
v else forall a b. a -> Either a b
Left Value
v)
{-# INLINE nonNull #-}
class AsNumber t => AsValue t where
_Value :: Prism' t Value
_String :: Prism' t Text
_String = forall t. AsValue t => Prism' t Value
_Valueforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall b t s a. (b -> t) -> (s -> Either t a) -> Prism s t a b
prism Text -> Value
String (\Value
v -> case Value
v of String Text
s -> forall a b. b -> Either a b
Right Text
s; Value
_ -> forall a b. a -> Either a b
Left Value
v)
{-# INLINE _String #-}
_Bool :: Prism' t Bool
_Bool = forall t. AsValue t => Prism' t Value
_Valueforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall b t s a. (b -> t) -> (s -> Either t a) -> Prism s t a b
prism Bool -> Value
Bool (\Value
v -> case Value
v of Bool Bool
b -> forall a b. b -> Either a b
Right Bool
b; Value
_ -> forall a b. a -> Either a b
Left Value
v)
{-# INLINE _Bool #-}
_Null :: Prism' t ()
_Null = forall t. AsValue t => Prism' t Value
_Valueforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall b t s a. (b -> t) -> (s -> Either t a) -> Prism s t a b
prism (forall a b. a -> b -> a
const Value
Null) (\Value
v -> case Value
v of Value
Null -> forall a b. b -> Either a b
Right (); Value
_ -> forall a b. a -> Either a b
Left Value
v)
{-# INLINE _Null #-}
_Object :: Prism' t (KeyMap Value)
_Object = forall t. AsValue t => Prism' t Value
_Valueforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall b t s a. (b -> t) -> (s -> Either t a) -> Prism s t a b
prism KeyMap Value -> Value
Object (\Value
v -> case Value
v of Object KeyMap Value
o -> forall a b. b -> Either a b
Right KeyMap Value
o; Value
_ -> forall a b. a -> Either a b
Left Value
v)
{-# INLINE _Object #-}
_Array :: Prism' t (Vector Value)
_Array = forall t. AsValue t => Prism' t Value
_Valueforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall b t s a. (b -> t) -> (s -> Either t a) -> Prism s t a b
prism Vector Value -> Value
Array (\Value
v -> case Value
v of Array Vector Value
a -> forall a b. b -> Either a b
Right Vector Value
a; Value
_ -> forall a b. a -> Either a b
Left Value
v)
{-# INLINE _Array #-}
instance AsValue Value where
_Value :: Prism' Value Value
_Value = forall a. a -> a
id
{-# INLINE _Value #-}
instance AsValue Strict.ByteString where
_Value :: Prism' ByteString Value
_Value = forall t a b. (AsJSON t, FromJSON a, ToJSON b) => Prism t t a b
_JSON
{-# INLINE _Value #-}
instance AsValue Lazy.ByteString where
_Value :: Prism' ByteString Value
_Value = forall t a b. (AsJSON t, FromJSON a, ToJSON b) => Prism t t a b
_JSON
{-# INLINE _Value #-}
instance AsValue String where
_Value :: Prism' String Value
_Value = Iso' String ByteString
strictUtf8forall b c a. (b -> c) -> (a -> b) -> a -> c
.forall t a b. (AsJSON t, FromJSON a, ToJSON b) => Prism t t a b
_JSON
{-# INLINE _Value #-}
instance AsValue Text where
_Value :: Prism' Text Value
_Value = Iso' Text ByteString
strictTextUtf8forall b c a. (b -> c) -> (a -> b) -> a -> c
.forall t a b. (AsJSON t, FromJSON a, ToJSON b) => Prism t t a b
_JSON
{-# INLINE _Value #-}
instance AsValue LazyText.Text where
_Value :: Prism' Text Value
_Value = Iso' Text ByteString
lazyTextUtf8forall b c a. (b -> c) -> (a -> b) -> a -> c
.forall t a b. (AsJSON t, FromJSON a, ToJSON b) => Prism t t a b
_JSON
{-# INLINE _Value #-}
key :: AsValue t => Key -> Traversal' t Value
key :: forall t. AsValue t => Key -> Traversal' t Value
key Key
i = forall t. AsValue t => Prism' t (KeyMap Value)
_Object forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall m. Ixed m => Index m -> Traversal' m (IxValue m)
ix Key
i
{-# INLINE key #-}
atKey :: AsValue t => Key -> Traversal' t (Maybe Value)
atKey :: forall t. AsValue t => Key -> Traversal' t (Maybe Value)
atKey Key
i = forall t. AsValue t => Prism' t (KeyMap Value)
_Object forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall m. At m => Index m -> Lens' m (Maybe (IxValue m))
at Key
i
{-# INLINE atKey #-}
members :: AsValue t => IndexedTraversal' Key t Value
members :: forall t. AsValue t => IndexedTraversal' Key t Value
members = forall t. AsValue t => Prism' t (KeyMap Value)
_Object forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall i (t :: * -> *) a b.
TraversableWithIndex i t =>
IndexedTraversal i (t a) (t b) a b
itraversed
{-# INLINE members #-}
nth :: AsValue t => Int -> Traversal' t Value
nth :: forall t. AsValue t => Int -> Traversal' t Value
nth Int
i = forall t. AsValue t => Prism' t (Vector Value)
_Array forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall m. Ixed m => Index m -> Traversal' m (IxValue m)
ix Int
i
{-# INLINE nth #-}
values :: AsValue t => IndexedTraversal' Int t Value
values :: forall t. AsValue t => IndexedTraversal' Int t Value
values = forall t. AsValue t => Prism' t (Vector Value)
_Array forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a b.
Traversable f =>
IndexedTraversal Int (f a) (f b) a b
traversed
{-# INLINE values #-}
strictUtf8 :: Iso' String Strict.ByteString
strictUtf8 :: Iso' String ByteString
strictUtf8 = forall t. IsText t => Iso' String t
packed forall b c a. (b -> c) -> (a -> b) -> a -> c
. Iso' Text ByteString
strictTextUtf8
strictTextUtf8 :: Iso' Text.Text Strict.ByteString
strictTextUtf8 :: Iso' Text ByteString
strictTextUtf8 = forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso Text -> ByteString
StrictText.encodeUtf8 (OnDecodeError -> ByteString -> Text
StrictText.decodeUtf8With OnDecodeError
Encoding.lenientDecode)
lazyTextUtf8 :: Iso' LazyText.Text Lazy.ByteString
lazyTextUtf8 :: Iso' Text ByteString
lazyTextUtf8 = forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso Text -> ByteString
LazyText.encodeUtf8 (OnDecodeError -> ByteString -> Text
LazyText.decodeUtf8With OnDecodeError
Encoding.lenientDecode)
_JSON' :: (AsJSON t, FromJSON a, ToJSON a) => Prism' t a
_JSON' :: forall t a. (AsJSON t, FromJSON a, ToJSON a) => Prism' t a
_JSON' = forall t a b. (AsJSON t, FromJSON a, ToJSON b) => Prism t t a b
_JSON
class IsKey t where
_Key :: Iso' t Key
instance IsKey Key where
_Key :: Iso' Key Key
_Key = forall a. a -> a
id
{-# INLINE _Key #-}
instance IsKey String where
_Key :: Iso' String Key
_Key = forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso String -> Key
Key.fromString Key -> String
Key.toString
{-# INLINE _Key #-}
instance IsKey Text.Text where
_Key :: Iso' Text Key
_Key = forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso Text -> Key
Key.fromText Key -> Text
Key.toText
{-# INLINE _Key #-}
instance IsKey LazyText.Text where
_Key :: Iso' Text Key
_Key = forall lazy strict. Strict lazy strict => Iso' lazy strict
strictforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall t. IsKey t => Iso' t Key
_Key
{-# INLINE _Key #-}
instance IsKey ShortText where
_Key :: Iso' ShortText Key
_Key = forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso ShortText -> Key
Key.fromShortText Key -> ShortText
Key.toShortText
{-# INLINE _Key #-}
instance IsKey Strict.ByteString where
_Key :: Iso' ByteString Key
_Key = forall s t a b. AnIso s t a b -> Iso b a t s
from Iso' Text ByteString
strictTextUtf8forall b c a. (b -> c) -> (a -> b) -> a -> c
.forall t. IsKey t => Iso' t Key
_Key
{-# INLINE _Key #-}
instance IsKey Lazy.ByteString where
_Key :: Iso' ByteString Key
_Key = forall s t a b. AnIso s t a b -> Iso b a t s
from Iso' Text ByteString
lazyTextUtf8forall b c a. (b -> c) -> (a -> b) -> a -> c
.forall t. IsKey t => Iso' t Key
_Key
{-# INLINE _Key #-}
class AsJSON t where
_JSON :: (FromJSON a, ToJSON b) => Prism t t a b
instance AsJSON Strict.ByteString where
_JSON :: forall a b.
(FromJSON a, ToJSON b) =>
Prism ByteString ByteString a b
_JSON = forall lazy strict. Strict lazy strict => Iso' strict lazy
lazyforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall t a b. (AsJSON t, FromJSON a, ToJSON b) => Prism t t a b
_JSON
{-# INLINE _JSON #-}
instance AsJSON Lazy.ByteString where
_JSON :: forall a b.
(FromJSON a, ToJSON b) =>
Prism ByteString ByteString a b
_JSON = forall b s a. (b -> s) -> (s -> Maybe a) -> Prism s s a b
prism' forall a. ToJSON a => a -> ByteString
encode forall a. FromJSON a => ByteString -> Maybe a
decode
{-# INLINE _JSON #-}
instance AsJSON String where
_JSON :: forall a b. (FromJSON a, ToJSON b) => Prism String String a b
_JSON = Iso' String ByteString
strictUtf8forall b c a. (b -> c) -> (a -> b) -> a -> c
.forall t a b. (AsJSON t, FromJSON a, ToJSON b) => Prism t t a b
_JSON
{-# INLINE _JSON #-}
instance AsJSON Text where
_JSON :: forall a b. (FromJSON a, ToJSON b) => Prism Text Text a b
_JSON = Iso' Text ByteString
strictTextUtf8forall b c a. (b -> c) -> (a -> b) -> a -> c
.forall t a b. (AsJSON t, FromJSON a, ToJSON b) => Prism t t a b
_JSON
{-# INLINE _JSON #-}
instance AsJSON LazyText.Text where
_JSON :: forall a b. (FromJSON a, ToJSON b) => Prism Text Text a b
_JSON = Iso' Text ByteString
lazyTextUtf8forall b c a. (b -> c) -> (a -> b) -> a -> c
.forall t a b. (AsJSON t, FromJSON a, ToJSON b) => Prism t t a b
_JSON
{-# INLINE _JSON #-}
instance AsJSON Value where
_JSON :: forall a b. (FromJSON a, ToJSON b) => Prism Value Value a b
_JSON = forall b t s a. (b -> t) -> (s -> Either t a) -> Prism s t a b
prism forall a. ToJSON a => a -> Value
toJSON forall a b. (a -> b) -> a -> b
$ \Value
x -> case forall a. FromJSON a => Value -> Result a
fromJSON Value
x of
Success a
y -> forall a b. b -> Either a b
Right a
y;
Result a
_ -> forall a b. a -> Either a b
Left Value
x
{-# INLINE _JSON #-}
type instance Index Value = Key
type instance IxValue Value = Value
instance Ixed Value where
ix :: Index Value -> Traversal' Value (IxValue Value)
ix Index Value
i IxValue Value -> f (IxValue Value)
f (Object KeyMap Value
o) = KeyMap Value -> Value
Object forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall m. Ixed m => Index m -> Traversal' m (IxValue m)
ix Index Value
i IxValue Value -> f (IxValue Value)
f KeyMap Value
o
ix Index Value
_ IxValue Value -> f (IxValue Value)
_ Value
v = forall (f :: * -> *) a. Applicative f => a -> f a
pure Value
v
{-# INLINE ix #-}
instance Plated Value where
plate :: Traversal' Value Value
plate Value -> f Value
f (Object KeyMap Value
o) = KeyMap Value -> Value
Object forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse Value -> f Value
f KeyMap Value
o
plate Value -> f Value
f (Array Vector Value
a) = Vector Value -> Value
Array forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse Value -> f Value
f Vector Value
a
plate Value -> f Value
_ Value
xs = forall (f :: * -> *) a. Applicative f => a -> f a
pure Value
xs
{-# INLINE plate #-}
type instance Index (KM.KeyMap v) = Key.Key
type instance IxValue (KM.KeyMap v) = v
instance Ixed (KM.KeyMap v)
instance At (KM.KeyMap v) where
at :: Index (KeyMap v) -> Lens' (KeyMap v) (Maybe (IxValue (KeyMap v)))
at Index (KeyMap v)
k Maybe (IxValue (KeyMap v)) -> f (Maybe (IxValue (KeyMap v)))
f = forall (f :: * -> *) v.
Functor f =>
(Maybe v -> f (Maybe v)) -> Key -> KeyMap v -> f (KeyMap v)
KM.alterF Maybe (IxValue (KeyMap v)) -> f (Maybe (IxValue (KeyMap v)))
f Index (KeyMap v)
k
{-# INLINE at #-}
instance Each (KM.KeyMap a) (KM.KeyMap b) a b where
each :: Traversal (KeyMap a) (KeyMap b) a b
each = forall (f :: * -> *) a b.
Traversable f =>
IndexedTraversal Int (f a) (f b) a b
traversed
{-# INLINE each #-}
instance (t ~ KeyMap v') => Rewrapped (KeyMap v) t
instance Wrapped (KeyMap v) where
type Unwrapped (KeyMap v) = [(Key, v)]
_Wrapped' :: Iso' (KeyMap v) (Unwrapped (KeyMap v))
_Wrapped' = forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso forall v. KeyMap v -> [(Key, v)]
KM.toList forall v. [(Key, v)] -> KeyMap v
KM.fromList
{-# INLINE _Wrapped' #-}
pattern JSON :: (FromJSON a, ToJSON a, AsJSON t) => () => a -> t
pattern $bJSON :: forall a t. (FromJSON a, ToJSON a, AsJSON t) => a -> t
$mJSON :: forall {r} {a} {t}.
(FromJSON a, ToJSON a, AsJSON t) =>
t -> (a -> r) -> ((# #) -> r) -> r
JSON a <- (preview _JSON -> Just a) where
JSON a
a = forall t a b. (AsJSON t, FromJSON a, ToJSON b) => Prism t t a b
_JSON forall t b. AReview t b -> b -> t
# a
a
pattern Value_ :: (FromJSON a, ToJSON a) => () => a -> Value
pattern $bValue_ :: forall a. (FromJSON a, ToJSON a) => a -> Value
$mValue_ :: forall {r} {a}.
(FromJSON a, ToJSON a) =>
Value -> (a -> r) -> ((# #) -> r) -> r
Value_ a <- (fromJSON -> Success a) where
Value_ a
a = forall a. ToJSON a => a -> Value
toJSON a
a
pattern Number_ :: AsNumber t => Scientific -> t
pattern $bNumber_ :: forall t. AsNumber t => Scientific -> t
$mNumber_ :: forall {r} {t}.
AsNumber t =>
t -> (Scientific -> r) -> ((# #) -> r) -> r
Number_ n <- (preview _Number -> Just n) where
Number_ Scientific
n = forall t. AsNumber t => Prism' t Scientific
_Number forall t b. AReview t b -> b -> t
# Scientific
n
pattern Double :: AsNumber t => Double -> t
pattern $bDouble :: forall t. AsNumber t => Double -> t
$mDouble :: forall {r} {t}.
AsNumber t =>
t -> (Double -> r) -> ((# #) -> r) -> r
Double d <- (preview _Double -> Just d) where
Double Double
d = forall t. AsNumber t => Prism' t Double
_Double forall t b. AReview t b -> b -> t
# Double
d
pattern Integer :: AsNumber t => Integer -> t
pattern $bInteger :: forall t. AsNumber t => Integer -> t
$mInteger :: forall {r} {t}.
AsNumber t =>
t -> (Integer -> r) -> ((# #) -> r) -> r
Integer i <- (preview _Integer -> Just i) where
Integer Integer
i = forall t. AsNumber t => Prism' t Integer
_Integer forall t b. AReview t b -> b -> t
# Integer
i
pattern Integral :: (AsNumber t, Integral a) => a -> t
pattern $bIntegral :: forall t a. (AsNumber t, Integral a) => a -> t
$mIntegral :: forall {r} {t} {a}.
(AsNumber t, Integral a) =>
t -> (a -> r) -> ((# #) -> r) -> r
Integral d <- (preview _Integral -> Just d) where
Integral a
d = forall t a. (AsNumber t, Integral a) => Prism' t a
_Integral forall t b. AReview t b -> b -> t
# a
d
pattern Bool_ :: AsValue t => Bool -> t
pattern $bBool_ :: forall t. AsValue t => Bool -> t
$mBool_ :: forall {r} {t}. AsValue t => t -> (Bool -> r) -> ((# #) -> r) -> r
Bool_ b <- (preview _Bool -> Just b) where
Bool_ Bool
b = forall t. AsValue t => Prism' t Bool
_Bool forall t b. AReview t b -> b -> t
# Bool
b
pattern String_ :: AsValue t => Text -> t
pattern $bString_ :: forall t. AsValue t => Text -> t
$mString_ :: forall {r} {t}. AsValue t => t -> (Text -> r) -> ((# #) -> r) -> r
String_ p <- (preview _String -> Just p) where
String_ Text
p = forall t. AsValue t => Prism' t Text
_String forall t b. AReview t b -> b -> t
# Text
p
pattern Null_ :: AsValue t => t
pattern $bNull_ :: forall t. AsValue t => t
$mNull_ :: forall {r} {t}. AsValue t => t -> ((# #) -> r) -> ((# #) -> r) -> r
Null_ <- (preview _Null -> Just ()) where
Null_ = forall t. AsValue t => Prism' t ()
_Null forall t b. AReview t b -> b -> t
# ()
pattern Key_ :: IsKey t => Key -> t
pattern $bKey_ :: forall t. IsKey t => Key -> t
$mKey_ :: forall {r} {t}. IsKey t => t -> (Key -> r) -> ((# #) -> r) -> r
Key_ k <- (preview _Key -> Just k) where
Key_ Key
k = forall t. IsKey t => Iso' t Key
_Key forall t b. AReview t b -> b -> t
# Key
k