@@ -77,8 +77,8 @@ module Data.Aeson.Types.Instances
79import Control.Applicative (Const(..))
79import Control.Applicative (Const(..))
80−import Data.Aeson.Encode.Functions (brackets, builder, encode, foldable, list')
80+import Data.Aeson.Encode.Functions (brackets, builder, encode, foldable, foldable', list')
81−import Data.Aeson.Functions (mapHashKeyVal, mapKey, mapKeyVal)
81+import Data.Aeson.Functions (hashMapKey, mapHashKeyVal, mapKey, mapKeyVal)
82import Data.Aeson.Types.Class
82import Data.Aeson.Types.Class
83import Data.Aeson.Types.Internal
83import Data.Aeson.Types.Internal
84import Data.Attoparsec.Number (Number(..))
84import Data.Attoparsec.Number (Number(..))
@@ -150,7 +150,10 @@ import System.Locale (defaultTimeLocale)
152parseIndexedJSON :: FromJSON a => Int -> Value -> Parser a
152parseIndexedJSON :: FromJSON a => Int -> Value -> Parser a
153−parseIndexedJSON idx value = parseJSON value <?> Index idx
153+parseIndexedJSON = parseIndexedJSON' parseJSON
155+parseIndexedJSON' :: (Value -> Parser a) -> Int -> Value -> Parser a
156+parseIndexedJSON' p idx value = p value <?> Index idx
155parseIndexedJSONPair :: FromJSON b => (Value -> Parser a) -> Int -> Value -> Parser (a, b)
158parseIndexedJSONPair :: FromJSON b => (Value -> Parser a) -> Int -> Value -> Parser (a, b)
156parseIndexedJSONPair keyParser idx value = p value <?> Index idx
159parseIndexedJSONPair keyParser idx value = p value <?> Index idx
@@ -581,45 +584,76 @@ instance FromJSON LT.Text where
581 parseJSON = withText "Lazy Text" $ pure . LT.fromStrict
584 parseJSON = withText "Lazy Text" $ pure . LT.fromStrict
582 {-# INLINE parseJSON #-}
585 {-# INLINE parseJSON #-}
587+instance ToJSON1 NonEmpty where
588+ liftToJSON to = liftToJSON to . toList
589+ {-# INLINE liftToJSON #-}
591+ liftToEncoding = foldable'
592+ {-# INLINE liftToEncoding #-}
584instance (ToJSON a) => ToJSON (NonEmpty a) where
594instance (ToJSON a) => ToJSON (NonEmpty a) where
585− toJSON = Array . V.fromList . map toJSON . toList
595+ toJSON = toJSON1
586 {-# INLINE toJSON #-}
596 {-# INLINE toJSON #-}
588− toEncoding (x :| xs) = Encoding $
598+ toEncoding = toEncoding1
589− B.char7 '[' <> builder x <> commas xs <> B.char7 ']'
591− commas = Prelude.foldr (\v vs -> B.char7 ',' <> builder v <> vs) mempty
592 {-# INLINE toEncoding #-}
599 {-# INLINE toEncoding #-}
594−instance (FromJSON a) => FromJSON (NonEmpty a) where
601+instance FromJSON1 NonEmpty where
595− parseJSON = withArray "NonEmpty a" $
602+ liftParseJSON p = withArray "NonEmpty a" $
596− (>>= ne) . Tr.sequence . zipWith parseIndexedJSON [0..] . V.toList
603+ (>>= ne) . Tr.sequence . zipWith (parseIndexedJSON' p) [0..] . V.toList
598 ne [] = fail "Expected a NonEmpty but got an empty list"
605 ne [] = fail "Expected a NonEmpty but got an empty list"
599 ne (x:xs) = pure (x :| xs)
606 ne (x:xs) = pure (x :| xs)
607+ {-# INLINE liftParseJSON #-}
609+instance (FromJSON a) => FromJSON (NonEmpty a) where
610+ parseJSON = parseJSON1
612+instance ToJSON1 [] where
613+ liftToJSON to = Array . V.fromList . map to
614+ {-# INLINE liftToJSON #-}
616+ liftToEncoding = list'
617+ {-# INLINE liftToEncoding #-}
601instance (ToJSON a) => ToJSON [a] where
619instance (ToJSON a) => ToJSON [a] where
602− toJSON = toJSONList
620+ toJSON = toJSON1
603 {-# INLINE toJSON #-}
621 {-# INLINE toJSON #-}
605− toEncoding = toEncodingList
623+ toEncoding = toEncoding1
606 {-# INLINE toEncoding #-}
624 {-# INLINE toEncoding #-}
626+instance FromJSON1 [] where
627+ liftParseJSON p = withArray "[a]" $
628+ Tr.sequence . zipWith (parseIndexedJSON' p) [0..] . V.toList
629+ {-# INLINE liftParseJSON #-}
608instance (FromJSON a) => FromJSON [a] where
631instance (FromJSON a) => FromJSON [a] where
609− parseJSON = parseJSONList
632+ parseJSON = parseJSON1
610 {-# INLINE parseJSON #-}
633 {-# INLINE parseJSON #-}
635+instance ToJSON1 Seq.Seq where
636+ liftToJSON to = liftToJSON to . toList
637+ {-# INLINE liftToJSON #-}
639+ liftToEncoding = foldable'
640+ {-# INLINE liftToEncoding #-}
612instance (ToJSON a) => ToJSON (Seq.Seq a) where
642instance (ToJSON a) => ToJSON (Seq.Seq a) where
613− toJSON = toJSON . toList
643+ toJSON = toJSON1
614 {-# INLINE toJSON #-}
644 {-# INLINE toJSON #-}
616− toEncoding = foldable
646+ toEncoding = toEncoding1
617 {-# INLINE toEncoding #-}
647 {-# INLINE toEncoding #-}
619−instance (FromJSON a) => FromJSON (Seq.Seq a) where
649+instance FromJSON1 Seq.Seq where
620− parseJSON = withArray "Seq a" $
650+ liftParseJSON p = withArray "Seq a" $
621 fmap Seq.fromList .
651 fmap Seq.fromList .
622− Tr.sequence . zipWith parseIndexedJSON [0..] . V.toList
652+ Tr.sequence . zipWith (parseIndexedJSON' p) [0..] . V.toList
653+ {-# INLINE liftParseJSON #-}
655+instance (FromJSON a) => FromJSON (Seq.Seq a) where
656+ parseJSON = parseJSON1
623 {-# INLINE parseJSON #-}
657 {-# INLINE parseJSON #-}
625instance (ToJSON a) => ToJSON (Vector a) where
659instance (ToJSON a) => ToJSON (Vector a) where
@@ -1642,16 +1676,27 @@ instance FromJSON (Proxy a) where
1642 parseJSON Null = pure Proxy
1676 parseJSON Null = pure Proxy
1643 parseJSON v = typeMismatch "Proxy" v
1677 parseJSON v = typeMismatch "Proxy" v
1679+instance ToJSON1 (Tagged a) where
1680+ liftToJSON to (Tagged x) = to x
1681+ {-# INLINE liftToJSON #-}
1683+ liftToEncoding to (Tagged x) = to x
1684+ {-# INLINE liftToEncoding #-}
1645instance ToJSON b => ToJSON (Tagged a b) where
1686instance ToJSON b => ToJSON (Tagged a b) where
1646− toJSON (Tagged x) = toJSON x
1687+ toJSON = toJSON1
1647 {-# INLINE toJSON #-}
1688 {-# INLINE toJSON #-}
1649− toEncoding (Tagged x) = toEncoding x
1690+ toEncoding = toEncoding1
1650 {-# INLINE toEncoding #-}
1691 {-# INLINE toEncoding #-}
1693+instance FromJSON1 (Tagged a) where
1694+ liftParseJSON p = fmap Tagged . p
1695+ {-# INLINE liftParseJSON #-}
1652instance FromJSON b => FromJSON (Tagged a b) where
1697instance FromJSON b => FromJSON (Tagged a b) where
1698+ parseJSON = parseJSON1
1653 {-# INLINE parseJSON #-}
1699 {-# INLINE parseJSON #-}
1654− parseJSON = fmap Tagged . parseJSON
1656instance ToJSON a => ToJSON (Const a b) where
1701instance ToJSON a => ToJSON (Const a b) where
1657 toJSON (Const x) = toJSON x
1702 toJSON (Const x) = toJSON x