@@ -71,6 +71,7 @@ import Data.Foldable (toList)
7171import Data.Functor.Identity (Identity(..))
7272import Data.Hashable (Hashable(..))
7373import Data.Int (Int8, Int16, Int32, Int64)
74+import Data.List.NonEmpty (NonEmpty(..))
7475import Data.Maybe (fromMaybe)
7576import Data.Monoid ((<>))
7677import Data.Monoid (Dual(..), First(..), Last(..))
@@ -491,6 +492,20 @@ instance FromJSON LT.Text where
491492 parseJSON = withText "Lazy Text" $ pure . LT.fromStrict
492493 {-# INLINE parseJSON #-}
493494
495+instance (ToJSON a) => ToJSON (NonEmpty a) where
496+ toJSON = toJSON . toList
497+ {-# INLINE toJSON #-}
498+
499+ toEncoding = toEncoding . toList
500+ {-# INLINE toEncoding #-}
501+
502+instance (FromJSON a) => FromJSON (NonEmpty a) where
503+ parseJSON = withArray "NonEmpty a" $
504+ (>>= ne) . Tr.sequence . zipWith parseIndexedJSON [0..] . V.toList
505+ where
506+ ne [] = fail "Expected a NonEmpty but got an empty list"
507+ ne (x:xs) = pure (x :| xs)
508+
494509instance OVERLAPPABLE_ (ToJSON a) => ToJSON [a] where
495510 toJSON = Array . V.fromList . map toJSON
496511 {-# INLINE toJSON #-}