{-# LANGUAGE DeriveFoldable        #-}
{-# LANGUAGE DeriveFunctor         #-}
{-# LANGUAGE DeriveTraversable     #-}
{-# LANGUAGE FlexibleInstances     #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE NoImplicitPrelude     #-}
{-# LANGUAGE RankNTypes            #-}
{-# LANGUAGE TypeFamilies          #-}
-- |
--
-- JSON Array representation and functions.
--
module Waargonaut.Types.JArray
  (
    -- * Types
    JArray (..)

    -- * Parser
  , parseJArray
  ) where

import           Prelude                   (Eq, Show, Int)

import           Control.Category          ((.))
import           Control.Error.Util        (note)
import           Control.Lens              (AsEmpty (..), Cons (..), Rewrapped, Ixed (..), Index, IxValue,
                                            Wrapped (..), cons, iso,
                                            nearly, over, prism, to, ( # ),
                                            (^.), (^?), _2, _Wrapped)
import           Control.Lens.Extras       (is)
import           Control.Monad             (Monad)

import           Data.Bifoldable           (Bifoldable (bifoldMap))
import           Data.Bifunctor            (Bifunctor (bimap))
import           Data.Bitraversable        (Bitraversable (bitraverse))
import           Data.Foldable             (Foldable)
import           Data.Function             (($))
import           Data.Functor              (Functor, (<$>))
import           Data.Monoid               (Monoid (..), mempty)
import           Data.Semigroup            (Semigroup (..))
import           Data.Traversable          (Traversable)

import           Text.Parser.Char          (CharParsing, char)

import           Waargonaut.Types.CommaSep (CommaSeparated,
                                            parseCommaSeparated)

-- $setup
-- >>> :set -XOverloadedStrings
-- >>> import Utils
-- >>> import Waargonaut.Types.Json
-- >>> import Waargonaut.Types.Whitespace
-- >>> import Control.Monad (return)
-- >>> import Data.Either (Either (..), isLeft)
-- >>> import Waargonaut.Decode.Error (DecodeError)
----

-- | Conveniently, a JSON array is a 'CommaSeparated' list with an optional
-- trailing comma, some instances and other functions need to work differently so
-- we wrap it up in a newtype.
newtype JArray ws a =
  JArray (CommaSeparated ws a)
  deriving (JArray ws a -> JArray ws a -> Bool
(JArray ws a -> JArray ws a -> Bool)
-> (JArray ws a -> JArray ws a -> Bool) -> Eq (JArray ws a)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall ws a. (Eq ws, Eq a) => JArray ws a -> JArray ws a -> Bool
/= :: JArray ws a -> JArray ws a -> Bool
$c/= :: forall ws a. (Eq ws, Eq a) => JArray ws a -> JArray ws a -> Bool
== :: JArray ws a -> JArray ws a -> Bool
$c== :: forall ws a. (Eq ws, Eq a) => JArray ws a -> JArray ws a -> Bool
Eq, Int -> JArray ws a -> ShowS
[JArray ws a] -> ShowS
JArray ws a -> String
(Int -> JArray ws a -> ShowS)
-> (JArray ws a -> String)
-> ([JArray ws a] -> ShowS)
-> Show (JArray ws a)
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall ws a. (Show ws, Show a) => Int -> JArray ws a -> ShowS
forall ws a. (Show ws, Show a) => [JArray ws a] -> ShowS
forall ws a. (Show ws, Show a) => JArray ws a -> String
showList :: [JArray ws a] -> ShowS
$cshowList :: forall ws a. (Show ws, Show a) => [JArray ws a] -> ShowS
show :: JArray ws a -> String
$cshow :: forall ws a. (Show ws, Show a) => JArray ws a -> String
showsPrec :: Int -> JArray ws a -> ShowS
$cshowsPrec :: forall ws a. (Show ws, Show a) => Int -> JArray ws a -> ShowS
Show, a -> JArray ws b -> JArray ws a
(a -> b) -> JArray ws a -> JArray ws b
(forall a b. (a -> b) -> JArray ws a -> JArray ws b)
-> (forall a b. a -> JArray ws b -> JArray ws a)
-> Functor (JArray ws)
forall a b. a -> JArray ws b -> JArray ws a
forall a b. (a -> b) -> JArray ws a -> JArray ws b
forall ws a b. a -> JArray ws b -> JArray ws a
forall ws a b. (a -> b) -> JArray ws a -> JArray ws b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: a -> JArray ws b -> JArray ws a
$c<$ :: forall ws a b. a -> JArray ws b -> JArray ws a
fmap :: (a -> b) -> JArray ws a -> JArray ws b
$cfmap :: forall ws a b. (a -> b) -> JArray ws a -> JArray ws b
Functor, JArray ws a -> Bool
(a -> m) -> JArray ws a -> m
(a -> b -> b) -> b -> JArray ws a -> b
(forall m. Monoid m => JArray ws m -> m)
-> (forall m a. Monoid m => (a -> m) -> JArray ws a -> m)
-> (forall m a. Monoid m => (a -> m) -> JArray ws a -> m)
-> (forall a b. (a -> b -> b) -> b -> JArray ws a -> b)
-> (forall a b. (a -> b -> b) -> b -> JArray ws a -> b)
-> (forall b a. (b -> a -> b) -> b -> JArray ws a -> b)
-> (forall b a. (b -> a -> b) -> b -> JArray ws a -> b)
-> (forall a. (a -> a -> a) -> JArray ws a -> a)
-> (forall a. (a -> a -> a) -> JArray ws a -> a)
-> (forall a. JArray ws a -> [a])
-> (forall a. JArray ws a -> Bool)
-> (forall a. JArray ws a -> Int)
-> (forall a. Eq a => a -> JArray ws a -> Bool)
-> (forall a. Ord a => JArray ws a -> a)
-> (forall a. Ord a => JArray ws a -> a)
-> (forall a. Num a => JArray ws a -> a)
-> (forall a. Num a => JArray ws a -> a)
-> Foldable (JArray ws)
forall a. Eq a => a -> JArray ws a -> Bool
forall a. Num a => JArray ws a -> a
forall a. Ord a => JArray ws a -> a
forall m. Monoid m => JArray ws m -> m
forall a. JArray ws a -> Bool
forall a. JArray ws a -> Int
forall a. JArray ws a -> [a]
forall a. (a -> a -> a) -> JArray ws a -> a
forall ws a. Eq a => a -> JArray ws a -> Bool
forall ws a. Num a => JArray ws a -> a
forall ws a. Ord a => JArray ws a -> a
forall m a. Monoid m => (a -> m) -> JArray ws a -> m
forall ws m. Monoid m => JArray ws m -> m
forall ws a. JArray ws a -> Bool
forall ws a. JArray ws a -> Int
forall ws a. JArray ws a -> [a]
forall b a. (b -> a -> b) -> b -> JArray ws a -> b
forall a b. (a -> b -> b) -> b -> JArray ws a -> b
forall ws a. (a -> a -> a) -> JArray ws a -> a
forall ws m a. Monoid m => (a -> m) -> JArray ws a -> m
forall ws b a. (b -> a -> b) -> b -> JArray ws a -> b
forall ws a b. (a -> b -> b) -> b -> JArray ws a -> b
forall (t :: * -> *).
(forall m. Monoid m => t m -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. t a -> [a])
-> (forall a. t a -> Bool)
-> (forall a. t a -> Int)
-> (forall a. Eq a => a -> t a -> Bool)
-> (forall a. Ord a => t a -> a)
-> (forall a. Ord a => t a -> a)
-> (forall a. Num a => t a -> a)
-> (forall a. Num a => t a -> a)
-> Foldable t
product :: JArray ws a -> a
$cproduct :: forall ws a. Num a => JArray ws a -> a
sum :: JArray ws a -> a
$csum :: forall ws a. Num a => JArray ws a -> a
minimum :: JArray ws a -> a
$cminimum :: forall ws a. Ord a => JArray ws a -> a
maximum :: JArray ws a -> a
$cmaximum :: forall ws a. Ord a => JArray ws a -> a
elem :: a -> JArray ws a -> Bool
$celem :: forall ws a. Eq a => a -> JArray ws a -> Bool
length :: JArray ws a -> Int
$clength :: forall ws a. JArray ws a -> Int
null :: JArray ws a -> Bool
$cnull :: forall ws a. JArray ws a -> Bool
toList :: JArray ws a -> [a]
$ctoList :: forall ws a. JArray ws a -> [a]
foldl1 :: (a -> a -> a) -> JArray ws a -> a
$cfoldl1 :: forall ws a. (a -> a -> a) -> JArray ws a -> a
foldr1 :: (a -> a -> a) -> JArray ws a -> a
$cfoldr1 :: forall ws a. (a -> a -> a) -> JArray ws a -> a
foldl' :: (b -> a -> b) -> b -> JArray ws a -> b
$cfoldl' :: forall ws b a. (b -> a -> b) -> b -> JArray ws a -> b
foldl :: (b -> a -> b) -> b -> JArray ws a -> b
$cfoldl :: forall ws b a. (b -> a -> b) -> b -> JArray ws a -> b
foldr' :: (a -> b -> b) -> b -> JArray ws a -> b
$cfoldr' :: forall ws a b. (a -> b -> b) -> b -> JArray ws a -> b
foldr :: (a -> b -> b) -> b -> JArray ws a -> b
$cfoldr :: forall ws a b. (a -> b -> b) -> b -> JArray ws a -> b
foldMap' :: (a -> m) -> JArray ws a -> m
$cfoldMap' :: forall ws m a. Monoid m => (a -> m) -> JArray ws a -> m
foldMap :: (a -> m) -> JArray ws a -> m
$cfoldMap :: forall ws m a. Monoid m => (a -> m) -> JArray ws a -> m
fold :: JArray ws m -> m
$cfold :: forall ws m. Monoid m => JArray ws m -> m
Foldable, Functor (JArray ws)
Foldable (JArray ws)
Functor (JArray ws)
-> Foldable (JArray ws)
-> (forall (f :: * -> *) a b.
    Applicative f =>
    (a -> f b) -> JArray ws a -> f (JArray ws b))
-> (forall (f :: * -> *) a.
    Applicative f =>
    JArray ws (f a) -> f (JArray ws a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> JArray ws a -> m (JArray ws b))
-> (forall (m :: * -> *) a.
    Monad m =>
    JArray ws (m a) -> m (JArray ws a))
-> Traversable (JArray ws)
(a -> f b) -> JArray ws a -> f (JArray ws b)
forall ws. Functor (JArray ws)
forall ws. Foldable (JArray ws)
forall ws (m :: * -> *) a.
Monad m =>
JArray ws (m a) -> m (JArray ws a)
forall ws (f :: * -> *) a.
Applicative f =>
JArray ws (f a) -> f (JArray ws a)
forall ws (m :: * -> *) a b.
Monad m =>
(a -> m b) -> JArray ws a -> m (JArray ws b)
forall ws (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> JArray ws a -> f (JArray ws b)
forall (t :: * -> *).
Functor t
-> Foldable t
-> (forall (f :: * -> *) a b.
    Applicative f =>
    (a -> f b) -> t a -> f (t b))
-> (forall (f :: * -> *) a. Applicative f => t (f a) -> f (t a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> t a -> m (t b))
-> (forall (m :: * -> *) a. Monad m => t (m a) -> m (t a))
-> Traversable t
forall (m :: * -> *) a.
Monad m =>
JArray ws (m a) -> m (JArray ws a)
forall (f :: * -> *) a.
Applicative f =>
JArray ws (f a) -> f (JArray ws a)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> JArray ws a -> m (JArray ws b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> JArray ws a -> f (JArray ws b)
sequence :: JArray ws (m a) -> m (JArray ws a)
$csequence :: forall ws (m :: * -> *) a.
Monad m =>
JArray ws (m a) -> m (JArray ws a)
mapM :: (a -> m b) -> JArray ws a -> m (JArray ws b)
$cmapM :: forall ws (m :: * -> *) a b.
Monad m =>
(a -> m b) -> JArray ws a -> m (JArray ws b)
sequenceA :: JArray ws (f a) -> f (JArray ws a)
$csequenceA :: forall ws (f :: * -> *) a.
Applicative f =>
JArray ws (f a) -> f (JArray ws a)
traverse :: (a -> f b) -> JArray ws a -> f (JArray ws b)
$ctraverse :: forall ws (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> JArray ws a -> f (JArray ws b)
$cp2Traversable :: forall ws. Foldable (JArray ws)
$cp1Traversable :: forall ws. Functor (JArray ws)
Traversable)

instance JArray ws a ~ t => Rewrapped (JArray ws a) t
instance Wrapped (JArray ws a) where
  type Unwrapped (JArray ws a) = CommaSeparated ws a
  _Wrapped' :: p (Unwrapped (JArray ws a)) (f (Unwrapped (JArray ws a)))
-> p (JArray ws a) (f (JArray ws a))
_Wrapped' = (JArray ws a -> CommaSeparated ws a)
-> (CommaSeparated ws a -> JArray ws a)
-> Iso
     (JArray ws a)
     (JArray ws a)
     (CommaSeparated ws a)
     (CommaSeparated ws a)
forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso (\(JArray CommaSeparated ws a
x) -> CommaSeparated ws a
x) CommaSeparated ws a -> JArray ws a
forall ws a. CommaSeparated ws a -> JArray ws a
JArray

instance Monoid ws => Cons (JArray ws a) (JArray ws a) a a where
  _Cons :: p (a, JArray ws a) (f (a, JArray ws a))
-> p (JArray ws a) (f (JArray ws a))
_Cons = ((a, JArray ws a) -> JArray ws a)
-> (JArray ws a -> Either (JArray ws a) (a, JArray ws a))
-> Prism
     (JArray ws a) (JArray ws a) (a, JArray ws a) (a, JArray ws a)
forall b t s a. (b -> t) -> (s -> Either t a) -> Prism s t a b
prism
    (\(a
a,JArray ws a
j) -> ASetter
  (JArray ws a)
  (JArray ws a)
  (CommaSeparated ws a)
  (CommaSeparated ws a)
-> (CommaSeparated ws a -> CommaSeparated ws a)
-> JArray ws a
-> JArray ws a
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
over ASetter
  (JArray ws a)
  (JArray ws a)
  (CommaSeparated ws a)
  (CommaSeparated ws a)
forall s t. Rewrapping s t => Iso s t (Unwrapped s) (Unwrapped t)
_Wrapped (a -> CommaSeparated ws a -> CommaSeparated ws a
forall s a. Cons s s a a => a -> s -> s
cons a
a) JArray ws a
j)
    (\JArray ws a
j -> JArray ws a
-> Maybe (a, JArray ws a) -> Either (JArray ws a) (a, JArray ws a)
forall a b. a -> Maybe b -> Either a b
note JArray ws a
j (Maybe (a, JArray ws a) -> Either (JArray ws a) (a, JArray ws a))
-> Maybe (a, JArray ws a) -> Either (JArray ws a) (a, JArray ws a)
forall a b. (a -> b) -> a -> b
$ ASetter
  (a, CommaSeparated ws a)
  (a, JArray ws a)
  (CommaSeparated ws a)
  (JArray ws a)
-> (CommaSeparated ws a -> JArray ws a)
-> (a, CommaSeparated ws a)
-> (a, JArray ws a)
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
over ASetter
  (a, CommaSeparated ws a)
  (a, JArray ws a)
  (CommaSeparated ws a)
  (JArray ws a)
forall s t a b. Field2 s t a b => Lens s t a b
_2 (Tagged (CommaSeparated ws a) (Identity (CommaSeparated ws a))
-> Tagged (JArray ws a) (Identity (JArray ws a))
forall s t. Rewrapping s t => Iso s t (Unwrapped s) (Unwrapped t)
_Wrapped (Tagged (CommaSeparated ws a) (Identity (CommaSeparated ws a))
 -> Tagged (JArray ws a) (Identity (JArray ws a)))
-> CommaSeparated ws a -> JArray ws a
forall t b. AReview t b -> b -> t
#) ((a, CommaSeparated ws a) -> (a, JArray ws a))
-> Maybe (a, CommaSeparated ws a) -> Maybe (a, JArray ws a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> JArray ws a
j JArray ws a
-> Getting
     (First (a, CommaSeparated ws a))
     (JArray ws a)
     (a, CommaSeparated ws a)
-> Maybe (a, CommaSeparated ws a)
forall s a. s -> Getting (First a) s a -> Maybe a
^? (CommaSeparated ws a
 -> Const (First (a, CommaSeparated ws a)) (CommaSeparated ws a))
-> JArray ws a
-> Const (First (a, CommaSeparated ws a)) (JArray ws a)
forall s t. Rewrapping s t => Iso s t (Unwrapped s) (Unwrapped t)
_Wrapped ((CommaSeparated ws a
  -> Const (First (a, CommaSeparated ws a)) (CommaSeparated ws a))
 -> JArray ws a
 -> Const (First (a, CommaSeparated ws a)) (JArray ws a))
-> (((a, CommaSeparated ws a)
     -> Const (First (a, CommaSeparated ws a)) (a, CommaSeparated ws a))
    -> CommaSeparated ws a
    -> Const (First (a, CommaSeparated ws a)) (CommaSeparated ws a))
-> Getting
     (First (a, CommaSeparated ws a))
     (JArray ws a)
     (a, CommaSeparated ws a)
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. ((a, CommaSeparated ws a)
 -> Const (First (a, CommaSeparated ws a)) (a, CommaSeparated ws a))
-> CommaSeparated ws a
-> Const (First (a, CommaSeparated ws a)) (CommaSeparated ws a)
forall s t a b. Cons s t a b => Prism s t (a, s) (b, t)
_Cons)
  {-# INLINE _Cons #-}

instance (Semigroup ws, Monoid ws) => AsEmpty (JArray ws a) where
  _Empty :: p () (f ()) -> p (JArray ws a) (f (JArray ws a))
_Empty = JArray ws a -> (JArray ws a -> Bool) -> Prism' (JArray ws a) ()
forall a. a -> (a -> Bool) -> Prism' a ()
nearly (CommaSeparated ws a -> JArray ws a
forall ws a. CommaSeparated ws a -> JArray ws a
JArray CommaSeparated ws a
forall a. Monoid a => a
mempty) (JArray ws a -> Getting Bool (JArray ws a) Bool -> Bool
forall s a. s -> Getting a s a -> a
^. (CommaSeparated ws a -> Const Bool (CommaSeparated ws a))
-> JArray ws a -> Const Bool (JArray ws a)
forall s t. Rewrapping s t => Iso s t (Unwrapped s) (Unwrapped t)
_Wrapped ((CommaSeparated ws a -> Const Bool (CommaSeparated ws a))
 -> JArray ws a -> Const Bool (JArray ws a))
-> ((Bool -> Const Bool Bool)
    -> CommaSeparated ws a -> Const Bool (CommaSeparated ws a))
-> Getting Bool (JArray ws a) Bool
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. (CommaSeparated ws a -> Bool)
-> (Bool -> Const Bool Bool)
-> CommaSeparated ws a
-> Const Bool (CommaSeparated ws a)
forall (p :: * -> * -> *) (f :: * -> *) s a.
(Profunctor p, Contravariant f) =>
(s -> a) -> Optic' p f s a
to (APrism (CommaSeparated ws a) (CommaSeparated ws a) () ()
-> CommaSeparated ws a -> Bool
forall s t a b. APrism s t a b -> s -> Bool
is APrism (CommaSeparated ws a) (CommaSeparated ws a) () ()
forall a. AsEmpty a => Prism' a ()
_Empty))
  {-# INLINE _Empty #-}

instance (Monoid ws, Semigroup ws) => Semigroup (JArray ws a) where
  (JArray CommaSeparated ws a
a) <> :: JArray ws a -> JArray ws a -> JArray ws a
<> (JArray CommaSeparated ws a
b) = CommaSeparated ws a -> JArray ws a
forall ws a. CommaSeparated ws a -> JArray ws a
JArray (CommaSeparated ws a
a CommaSeparated ws a -> CommaSeparated ws a -> CommaSeparated ws a
forall a. Semigroup a => a -> a -> a
<> CommaSeparated ws a
b)

instance (Semigroup ws, Monoid ws) => Monoid (JArray ws a) where
  mempty :: JArray ws a
mempty = CommaSeparated ws a -> JArray ws a
forall ws a. CommaSeparated ws a -> JArray ws a
JArray CommaSeparated ws a
forall a. Monoid a => a
mempty
  mappend :: JArray ws a -> JArray ws a -> JArray ws a
mappend = JArray ws a -> JArray ws a -> JArray ws a
forall a. Semigroup a => a -> a -> a
(<>)

type instance IxValue (JArray ws a) = a
type instance Index (JArray ws a)   = Int

instance Ixed (JArray ws a) where
  ix :: Index (JArray ws a)
-> Traversal' (JArray ws a) (IxValue (JArray ws a))
ix Index (JArray ws a)
i IxValue (JArray ws a) -> f (IxValue (JArray ws a))
f (JArray CommaSeparated ws a
cs) = CommaSeparated ws a -> JArray ws a
forall ws a. CommaSeparated ws a -> JArray ws a
JArray (CommaSeparated ws a -> JArray ws a)
-> f (CommaSeparated ws a) -> f (JArray ws a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Index (CommaSeparated ws a)
-> (IxValue (CommaSeparated ws a)
    -> f (IxValue (CommaSeparated ws a)))
-> CommaSeparated ws a
-> f (CommaSeparated ws a)
forall m. Ixed m => Index m -> Traversal' m (IxValue m)
ix Index (CommaSeparated ws a)
Index (JArray ws a)
i IxValue (CommaSeparated ws a) -> f (IxValue (CommaSeparated ws a))
IxValue (JArray ws a) -> f (IxValue (JArray ws a))
f CommaSeparated ws a
cs

instance Bifunctor JArray where
  bimap :: (a -> b) -> (c -> d) -> JArray a c -> JArray b d
bimap a -> b
f c -> d
g (JArray CommaSeparated a c
cs) = CommaSeparated b d -> JArray b d
forall ws a. CommaSeparated ws a -> JArray ws a
JArray ((a -> b) -> (c -> d) -> CommaSeparated a c -> CommaSeparated b d
forall (p :: * -> * -> *) a b c d.
Bifunctor p =>
(a -> b) -> (c -> d) -> p a c -> p b d
bimap a -> b
f c -> d
g CommaSeparated a c
cs)

instance Bifoldable JArray where
  bifoldMap :: (a -> m) -> (b -> m) -> JArray a b -> m
bifoldMap a -> m
f b -> m
g (JArray CommaSeparated a b
cs) = (a -> m) -> (b -> m) -> CommaSeparated a b -> m
forall (p :: * -> * -> *) m a b.
(Bifoldable p, Monoid m) =>
(a -> m) -> (b -> m) -> p a b -> m
bifoldMap a -> m
f b -> m
g CommaSeparated a b
cs

instance Bitraversable JArray where
  bitraverse :: (a -> f c) -> (b -> f d) -> JArray a b -> f (JArray c d)
bitraverse a -> f c
f b -> f d
g (JArray CommaSeparated a b
cs) = CommaSeparated c d -> JArray c d
forall ws a. CommaSeparated ws a -> JArray ws a
JArray (CommaSeparated c d -> JArray c d)
-> f (CommaSeparated c d) -> f (JArray c d)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (a -> f c)
-> (b -> f d) -> CommaSeparated a b -> f (CommaSeparated c d)
forall (t :: * -> * -> *) (f :: * -> *) a c b d.
(Bitraversable t, Applicative f) =>
(a -> f c) -> (b -> f d) -> t a b -> f (t c d)
bitraverse a -> f c
f b -> f d
g CommaSeparated a b
cs

-- | Parse a single JSON array
--
-- >>> testparse (parseJArray parseWhitespace parseWaargonaut) "[null ]"
-- Right (JArray (CommaSeparated (WS []) (Just (Elems {_elemsElems = [], _elemsLast = Elem {_elemVal = Json (JNull (WS [Space])), _elemTrailing = Nothing}}))))
--
-- >>> testparse (parseJArray parseWhitespace parseWaargonaut) "[null,]"
-- Right (JArray (CommaSeparated (WS []) (Just (Elems {_elemsElems = [], _elemsLast = Elem {_elemVal = Json (JNull (WS [])), _elemTrailing = Just (Comma,WS [])}}))))
--
parseJArray
  :: ( Monad f
     , CharParsing f
     )
  => f ws
  -> f a
  -> f (JArray ws a)
parseJArray :: f ws -> f a -> f (JArray ws a)
parseJArray f ws
ws f a
a = CommaSeparated ws a -> JArray ws a
forall ws a. CommaSeparated ws a -> JArray ws a
JArray (CommaSeparated ws a -> JArray ws a)
-> f (CommaSeparated ws a) -> f (JArray ws a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
  f Char -> f Char -> f ws -> f a -> f (CommaSeparated ws a)
forall (f :: * -> *) open close ws a.
(Monad f, CharParsing f) =>
f open -> f close -> f ws -> f a -> f (CommaSeparated ws a)
parseCommaSeparated (Char -> f Char
forall (m :: * -> *). CharParsing m => Char -> m Char
char Char
'[') (Char -> f Char
forall (m :: * -> *). CharParsing m => Char -> m Char
char Char
']') f ws
ws f a
a