on master help sign in
Add unary instances for vector
81f4e35Oleg Grenrus
Data/Aeson/Types/Instances.hs+19−2
@@ -656,6 +656,19 @@ instance (FromJSON a) => FromJSON (Seq.Seq a) where
656 parseJSON = parseJSON1
656 parseJSON = parseJSON1
657 {-# INLINE parseJSON #-}
657 {-# INLINE parseJSON #-}
658
658
659+instance ToJSON1 Vector where
660+ liftToJSON to = Array . V.map to
661+ {-# INLINE liftToJSON #-}
662+
663+ liftToEncoding to xs
664+ | V.null xs = E.emptyArray_
665+ | otherwise = Encoding $
666+ B.char7 '[' <> fromEncoding (to (V.unsafeHead xs)) <>
667+ V.foldr go (B.char7 ']') (V.unsafeTail xs)
668+ where
669+ go v b = B.char7 ',' <> fromEncoding (to v) <> b
670+ {-# INLINE liftToEncoding #-}
671+
659instance (ToJSON a) => ToJSON (Vector a) where
672instance (ToJSON a) => ToJSON (Vector a) where
660 toJSON = Array . V.map toJSON
673 toJSON = Array . V.map toJSON
661 {-# INLINE toJSON #-}
674 {-# INLINE toJSON #-}
@@ -672,9 +685,13 @@ encodeVector xs
672 where go v b = B.char7 ',' <> builder v <> b
685 where go v b = B.char7 ',' <> builder v <> b
673{-# INLINE encodeVector #-}
686{-# INLINE encodeVector #-}
674
687
688+instance FromJSON1 Vector where
689+ liftParseJSON p = withArray "Vector a" $
690+ V.mapM (uncurry $ parseIndexedJSON' p) . V.indexed
691+ {-# INLINE liftParseJSON #-}
692+
675instance (FromJSON a) => FromJSON (Vector a) where
693instance (FromJSON a) => FromJSON (Vector a) where
676− parseJSON = withArray "Vector a" $ V.mapM (uncurry parseIndexedJSON) .
694+ parseJSON = parseJSON1
677− V.indexed
678 {-# INLINE parseJSON #-}
695 {-# INLINE parseJSON #-}
679
696
680vectorToJSON :: (VG.Vector v a, ToJSON a) => v a -> Value
697vectorToJSON :: (VG.Vector v a, ToJSON a) => v a -> Value