on master help sign in
Add docs for listEncoding, listValue and listParser
commitf899edcd119b69a27425cf87e1a6e9ac0da7983d authorOleg Grenrus <oleg.grenrus@iki.fi> dateMon 4 Jul 2016, 02:08 (10 years ago) parent334e5a2
2 files changed+17
Data/Aeson/Types/FromJSON.hs+1 Data/Aeson/Types/ToJSON.hs+16
Data/Aeson/Types/FromJSON.hs+1
@@ -548,6 +548,7 @@ parseJSON2 = liftParseJSON2 parseJSON parseJSONList parseJSON parseJSONList
548548-- List functions
549549-------------------------------------------------------------------------------
550550
551+-- | Helper function to use with 'liftParseJSON'. See 'Data.Aeson.ToJSON.listEncoding'.
551552listParser :: (Value -> Parser a) -> Value -> Parser [a]
552553listParser f (Array xs) = fmap V.toList (V.mapM f xs)
553554listParser _ v = typeMismatch "[a]" v
Data/Aeson/Types/ToJSON.hs+16
@@ -580,9 +580,25 @@ toEncoding2 = liftToEncoding2 toEncoding toEncodingList toEncoding toEncodingLis
580580-- Encoding functions
581581-------------------------------------------------------------------------------
582582
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+-- @
583597listEncoding :: (a -> Encoding) -> [a] -> Encoding
584598listEncoding = E.list
599+{-# INLINE listEncoding #-}
585600
601+-- | Helper function to use with 'liftToJSON', see 'listEncoding'.
586602listValue :: (a -> Value) -> [a] -> Value
587603listValue f = Array . V.fromList . map f
588604{-# INLINE listValue #-}