-- Hoogle documentation, generated by Haddock -- See Hoogle, http://www.haskell.org/hoogle/ -- | Abusing monadic syntax JSON objects generation. -- -- Generation of big, complex JSON objects with Text.JSON is -- painful. And autoderivation is not always possible. Check -- documentation of Text.JSON.Gen for more info. @package fields-json @version 0.4.0.0 -- | Interface for extracting data from JSValue. module Text.JSON.FromJSValue -- | Structures that can be parsed from JSON. Instances must -- declare either fromJSValue (parse directly from JSValue) -- or fromJSValueM (uses MonadReader). -- -- Example implementation: -- --
--   data D = D String Int
--   
--   instance FromJSValue D where
--     fromJSValue = do
--       s <- fromJSValueField "string_key"
--       i <- fromJSValueField "int_key"
--       return (D <$> s <*> i)
--   
-- -- Note that we make use of MonadReader instance for "(->)" and -- of Applicative programming style with <$> and -- <*>. -- -- Note: fromJSValueM is deprecated, in future fromJSValue -- will be generalized to work in any MonadReader JSValue. class FromJSValue a fromJSValue :: FromJSValue a => JSValue -> Maybe a fromJSValueM :: (FromJSValue a, MonadReader JSValue m) => m (Maybe a) -- | Structures that can be parsed from JSON, fields absent in the -- JSON will be filled in using (optional) original structure. -- -- By convention JSON null should be treated as a request to reset -- structure element to default value. class FromJSValueWithUpdate a fromJSValueWithUpdate :: FromJSValueWithUpdate a => Maybe a -> JSValue -> Maybe a fromJSValueWithUpdateM :: (FromJSValueWithUpdate a, MonadReader JSValue m) => Maybe a -> m (Maybe a) -- | Structures that can be matched with JSValue class MatchWithJSValue a matchesWithJSValue :: MatchWithJSValue a => a -> JSValue -> Bool matchesWithJSValueM :: (MatchWithJSValue a, MonadReader JSValue m) => a -> m Bool -- | Reading the value that is on some field. Returns Nothing if -- JSON is not an object or field is present but cannot be parsed, 'Just -- Nothing' if absent, and 'Just (Just a)' otherwise jsValueField :: (MonadReader JSValue m, FromJSValue a) => String -> m (Maybe (Maybe a)) -- | Reading the value that is on a field. Semantics are a bit involved, -- example GHCi session should clarify: -- --
--   Prelude> :set -XNoMonomorphismRestriction
--   Prelude> let x = withJSValue (JSObject (toJSObject [("key",JSString $ toJSString "value")]))
--   Prelude> x (fromJSValueField "key") :: IO (Maybe Int)
--   Nothing
--   Prelude> x (fromJSValueField "key") :: IO (Maybe (Maybe Int))
--   Just Nothing
--   Prelude> x (fromJSValueField "key") :: IO (Maybe (Maybe (Maybe Int)))
--   Just (Just Nothing)
--   Prelude> x (fromJSValueField "key") :: IO (Maybe String)
--   Just "value"
--   Prelude> x (fromJSValueField "key") :: IO (Maybe (Maybe String))
--   Just (Just "value")
--   Prelude> x (fromJSValueField "key") :: IO (Maybe (Maybe (Maybe String)))
--   Just (Just (Just "value"))
--   Prelude> let x = withJSValue (JSArray [])
--   Prelude> x (fromJSValueField "key") :: IO (Maybe String)
--   Nothing
--   Prelude> x (fromJSValueField "key") :: IO (Maybe (Maybe String))
--   Nothing
--   Prelude> x (fromJSValueField "key") :: IO (Maybe (Maybe (Maybe String)))
--   Nothing
--   
fromJSValueField :: (MonadReader JSValue m, FromJSValue a) => String -> m (Maybe a) -- | Version of fromJSValueField for Base64 encoded data to avoid -- memory leak. fromJSValueFieldBase64 :: MonadReader JSValue m => String -> m (Maybe ByteString) -- | Generalization of fromJSValueField. Does not use -- FromJSValue instances. fromJSValueFieldCustom :: MonadReader JSValue m => String -> m (Maybe a) -> m (Maybe a) -- | Runs parser on each element of underlaying json. Returns Just iff JSON -- is array. fromJSValueCustomMany :: MonadReader JSValue m => m (Maybe a) -> m (Maybe [a]) -- | Generalization of fromJSValueCustomMany, where each element of -- array can have different parser. fromJSValueCustomList :: MonadReader JSValue m => [m (Maybe a)] -> m (Maybe [a]) -- | Runs parser on each element of underlying json. Returns Just -- iff JSON is an array. -- -- Note: This method has quadratic complexity. It is better to write less -- general matching algorithms that use Maps. fromJSValueManyWithUpdate :: (MonadReader JSValue m, FromJSValueWithUpdate a, MatchWithJSValue a) => [a] -> m (Maybe [a]) -- | Simple runner. Example: -- --
--   let (v :: MyStruct) = runIdentity $ withJSValue js (fromJSValueM)
--   
-- -- or inline: -- --
--   let z = runIdentity $ withJSValue js $ do
--               a <- fromJSValueField "a"
--               b <- fromJSValueField "b"
--               c <- fromJSValueField "c"
--               return ((,,) <$> a <*> b <*> c)
--   
-- -- or using the monad transformer: -- --
--   z <- withJSValue js $ do
--               a <- fromJSValueField "a"
--               b <- fromJSValueField "b"
--               c <- fromJSValueField "c"
--               return ((,,) <$> a <*> b <*> c)
--   
withJSValue :: Monad m => JSValue -> ReaderT JSValue m a -> m a instance Text.JSON.FromJSValue.FromJSValue Text.JSON.Types.JSValue instance Text.JSON.FromJSValue.FromJSValue GHC.Base.String instance Text.JSON.FromJSValue.FromJSValue Data.ByteString.Internal.ByteString instance Text.JSON.FromJSValue.FromJSValue GHC.Integer.Type.Integer instance Text.JSON.FromJSValue.FromJSValue GHC.Types.Int instance Text.JSON.FromJSValue.FromJSValue GHC.Int.Int16 instance Text.JSON.FromJSValue.FromJSValue GHC.Int.Int32 instance Text.JSON.FromJSValue.FromJSValue GHC.Int.Int64 instance Text.JSON.FromJSValue.FromJSValue GHC.Types.Word instance Text.JSON.FromJSValue.FromJSValue GHC.Word.Word16 instance Text.JSON.FromJSValue.FromJSValue GHC.Word.Word32 instance Text.JSON.FromJSValue.FromJSValue GHC.Word.Word64 instance Text.JSON.FromJSValue.FromJSValue GHC.Types.Bool instance Text.JSON.FromJSValue.FromJSValue GHC.Types.Double instance Text.JSON.FromJSValue.FromJSValue GHC.Types.Float instance Text.JSON.FromJSValue.FromJSValue a => Text.JSON.FromJSValue.FromJSValue [a] instance Text.JSON.FromJSValue.FromJSValue a => Text.JSON.FromJSValue.FromJSValue (GHC.Maybe.Maybe a) instance (Text.JSON.FromJSValue.FromJSValue a, Text.JSON.FromJSValue.FromJSValue b) => Text.JSON.FromJSValue.FromJSValue (a, b) instance (Text.JSON.FromJSValue.FromJSValue a, Text.JSON.FromJSValue.FromJSValue b, Text.JSON.FromJSValue.FromJSValue c) => Text.JSON.FromJSValue.FromJSValue (a, b, c) instance (Text.JSON.FromJSValue.FromJSValue a, Text.JSON.FromJSValue.FromJSValue b, Text.JSON.FromJSValue.FromJSValue c, Text.JSON.FromJSValue.FromJSValue d) => Text.JSON.FromJSValue.FromJSValue (a, b, c, d) instance (Text.JSON.FromJSValue.FromJSValue a, Text.JSON.FromJSValue.FromJSValue b, Text.JSON.FromJSValue.FromJSValue c, Text.JSON.FromJSValue.FromJSValue d, Text.JSON.FromJSValue.FromJSValue e) => Text.JSON.FromJSValue.FromJSValue (a, b, c, d, e) instance (Text.JSON.FromJSValue.FromJSValue a, Text.JSON.FromJSValue.FromJSValue b, Text.JSON.FromJSValue.FromJSValue c, Text.JSON.FromJSValue.FromJSValue d, Text.JSON.FromJSValue.FromJSValue e, Text.JSON.FromJSValue.FromJSValue f) => Text.JSON.FromJSValue.FromJSValue (a, b, c, d, e, f) -- | Unifing some structures so they can be serialized to JSValue module Text.JSON.ToJSValue class ToJSValue a toJSValue :: ToJSValue a => a -> JSValue instance Text.JSON.ToJSValue.ToJSValue Text.JSON.Types.JSValue instance Text.JSON.ToJSValue.ToJSValue GHC.Types.Bool instance Text.JSON.ToJSValue.ToJSValue GHC.Base.String instance Text.JSON.ToJSValue.ToJSValue GHC.Integer.Type.Integer instance Text.JSON.ToJSValue.ToJSValue GHC.Types.Int instance Text.JSON.ToJSValue.ToJSValue GHC.Int.Int8 instance Text.JSON.ToJSValue.ToJSValue GHC.Int.Int16 instance Text.JSON.ToJSValue.ToJSValue GHC.Int.Int32 instance Text.JSON.ToJSValue.ToJSValue GHC.Int.Int64 instance Text.JSON.ToJSValue.ToJSValue GHC.Types.Word instance Text.JSON.ToJSValue.ToJSValue GHC.Word.Word8 instance Text.JSON.ToJSValue.ToJSValue GHC.Word.Word16 instance Text.JSON.ToJSValue.ToJSValue GHC.Word.Word32 instance Text.JSON.ToJSValue.ToJSValue GHC.Word.Word64 instance Text.JSON.ToJSValue.ToJSValue GHC.Types.Double instance Text.JSON.ToJSValue.ToJSValue GHC.Types.Float instance Text.JSON.ToJSValue.ToJSValue a => Text.JSON.ToJSValue.ToJSValue [a] instance Text.JSON.ToJSValue.ToJSValue a => Text.JSON.ToJSValue.ToJSValue (Data.Map.Internal.Map GHC.Base.String a) instance Text.JSON.ToJSValue.ToJSValue a => Text.JSON.ToJSValue.ToJSValue (GHC.Maybe.Maybe a) instance (Text.JSON.ToJSValue.ToJSValue a, Text.JSON.ToJSValue.ToJSValue b) => Text.JSON.ToJSValue.ToJSValue (a, b) -- | Abusing monadic 'do' notation library for generating JSON object. -- Hard-bound to JSValue from json package from hackage. -- -- Main ideas -- -- -- --
--   runJSONGen $ do
--       value "a" "a"
--       value "b" [1,2,3]
--       object "c" $ do
--           value "x" True
--           value "y" False
--   
-- -- Will generate json object: -- --
--   {a : "a", b: [1,2,3], c: {x: true, y : false}}
--   
module Text.JSON.Gen -- | Basic types type JSONGen = JSONGenT Identity -- | A monad that keeps currently constructed JSON. data JSONGenT m a -- | Runner. Example: -- --
--   let js = runJSONGen $ do
--              value "abc" "def"
--   
runJSONGen :: JSONGen () -> JSValue -- | Runner as monad transformer. Example: -- --
--   js <- runJSONGenT $ do
--              d <- lift $ getFromOuterMonad
--              value "abc" d
--   
runJSONGenT :: Monad m => JSONGenT m () -> m JSValue -- | Set pure value under given name in final JSON object. Example: -- --
--   value "key" "value"
--   
value :: (Monad m, ToJSValue a) => String -> a -> JSONGenT m () -- | Monadic verion of value using monad transformer. Example: -- --
--   js <- runJSONGenT $ do
--            valueM "abc" (getLine)
--   
valueM :: (Monad m, ToJSValue a) => String -> m a -> JSONGenT m () -- | Embed other JSON object as field in a resulting JSON object. Example: -- --
--   let js = runJSONGen $ do
--              object "nested" $ do
--                  value "abc" "def"
--   
object :: Monad m => String -> JSONGenT m () -> JSONGenT m () -- | Version for lists of objects. Example: -- --
--   let js = runJSONGen $ do
--              objects "nested" [ do
--                                   value "abc" "def"
--                                   value "x" "y",
--                                 do
--                                   value "qwe" "rty"
--                               ]
--   
objects :: Monad m => String -> [JSONGenT m ()] -> JSONGenT m () instance Control.Monad.Trans.Class.MonadTrans Text.JSON.Gen.JSONGenT instance GHC.Base.Monad m => GHC.Base.Monad (Text.JSON.Gen.JSONGenT m) instance GHC.Base.Functor m => GHC.Base.Functor (Text.JSON.Gen.JSONGenT m) instance GHC.Base.Monad m => GHC.Base.Applicative (Text.JSON.Gen.JSONGenT m) instance Control.Monad.IO.Class.MonadIO m => Control.Monad.IO.Class.MonadIO (Text.JSON.Gen.JSONGenT m)