@@ -71,6 +71,7 @@ import Data.Foldable (toList)
71import Data.Functor.Identity (Identity(..))
71import Data.Functor.Identity (Identity(..))
72import Data.Hashable (Hashable(..))
72import Data.Hashable (Hashable(..))
73import Data.Int (Int8, Int16, Int32, Int64)
73import Data.Int (Int8, Int16, Int32, Int64)
74+import Data.List.NonEmpty (NonEmpty(..))
74import Data.Maybe (fromMaybe)
75import Data.Maybe (fromMaybe)
75import Data.Monoid ((<>))
76import Data.Monoid ((<>))
76import Data.Monoid (Dual(..), First(..), Last(..))
77import Data.Monoid (Dual(..), First(..), Last(..))
@@ -491,6 +492,20 @@ instance FromJSON LT.Text where
491 parseJSON = withText "Lazy Text" $ pure . LT.fromStrict
492 parseJSON = withText "Lazy Text" $ pure . LT.fromStrict
492 {-# INLINE parseJSON #-}
493 {-# INLINE parseJSON #-}
495+instance (ToJSON a) => ToJSON (NonEmpty a) where
496+ toJSON = toJSON . toList
497+ {-# INLINE toJSON #-}
499+ toEncoding = toEncoding . toList
500+ {-# INLINE toEncoding #-}
502+instance (FromJSON a) => FromJSON (NonEmpty a) where
503+ parseJSON = withArray "NonEmpty a" $
504+ (>>= ne) . Tr.sequence . zipWith parseIndexedJSON [0..] . V.toList
506+ ne [] = fail "Expected a NonEmpty but got an empty list"
507+ ne (x:xs) = pure (x :| xs)
494instance OVERLAPPABLE_ (ToJSON a) => ToJSON [a] where
509instance OVERLAPPABLE_ (ToJSON a) => ToJSON [a] where
495 toJSON = Array . V.fromList . map toJSON
510 toJSON = Array . V.fromList . map toJSON
496 {-# INLINE toJSON #-}
511 {-# INLINE toJSON #-}