on master help sign in
Add docs for listEncoding, listValue and listParser
f899edcOleg Grenrus
Data/Aeson/Types/FromJSON.hs+1
@@ -548,6 +548,7 @@ parseJSON2 = liftParseJSON2 parseJSON parseJSONList parseJSON parseJSONList
548-- List functions
548-- List functions
549-------------------------------------------------------------------------------
549-------------------------------------------------------------------------------
550
550
551+-- | Helper function to use with 'liftParseJSON'. See 'Data.Aeson.ToJSON.listEncoding'.
551listParser :: (Value -> Parser a) -> Value -> Parser [a]
552listParser :: (Value -> Parser a) -> Value -> Parser [a]
552listParser f (Array xs) = fmap V.toList (V.mapM f xs)
553listParser f (Array xs) = fmap V.toList (V.mapM f xs)
553listParser _ v = typeMismatch "[a]" v
554listParser _ v = typeMismatch "[a]" v
Data/Aeson/Types/ToJSON.hs+16
@@ -580,9 +580,25 @@ toEncoding2 = liftToEncoding2 toEncoding toEncodingList toEncoding toEncodingLis
580-- Encoding functions
580-- Encoding functions
581-------------------------------------------------------------------------------
581-------------------------------------------------------------------------------
582
582
583+-- | Helper function to use with 'liftToEncoding'.
584+-- Useful when writing own 'ToJSON1' instances.
585+--
586+-- @
587+-- newtype F a = F [a]
588+--
589+-- -- This instance encodes String as an array of chars
590+-- instance 'ToJSON1' F where
591+-- 'liftToJSON' tj _ (F xs) = 'liftToJSON' tj ('listValue' tj) xs
592+-- 'liftToEncoding' te _ (F xs) = 'liftToEncoding' te ('listEncoding' te) xs
593+--
594+-- instance 'Data.Aeson.FromJSON.FromJSON1' F where
595+-- 'Data.Aeson.FromJSON.liftParseJSON' p _ v = F \<$\> 'Data.Aeson.FromJSON.liftParseJSON' p ('Data.Aeson.FromJSON.listParser' p) v
596+-- @
583listEncoding :: (a -> Encoding) -> [a] -> Encoding
597listEncoding :: (a -> Encoding) -> [a] -> Encoding
584listEncoding = E.list
598listEncoding = E.list
599+{-# INLINE listEncoding #-}
585
600
601+-- | Helper function to use with 'liftToJSON', see 'listEncoding'.
586listValue :: (a -> Value) -> [a] -> Value
602listValue :: (a -> Value) -> [a] -> Value
587listValue f = Array . V.fromList . map f
603listValue f = Array . V.fromList . map f
588{-# INLINE listValue #-}
604{-# INLINE listValue #-}