1{-# OPTIONS_GHC -Wall #-}
2{-# LANGUAGE OverloadedStrings #-}
4 ( Decoder, Error(..), decode, parse
5 , string, text, bool, int, float
9 , map, map2, succeed, fail, andThen, oneOf
15import Data.Text (Text)
16import Prelude hiding (fail, map, maybe)
18import qualified Control.Monad as Monad
19import qualified Data.Aeson as Aeson
20import qualified Data.ByteString.Lazy as BS
21import qualified Data.HashMap.Lazy as HashMap
22import qualified Data.Scientific as Scientific
23import qualified Data.Text as Text
24import qualified Data.Vector as Vector
25import Data.Vector ((!?))
34 { _run :: (Error -> Error) -> Aeson.Value -> Either Error a
42 | Failure Aeson.Value String
46decode :: Decoder a -> Aeson.Value -> Either Error a
51parse :: Decoder a -> BS.ByteString -> Either (Maybe Error) a
52parse decoder bytestring =
53 case Aeson.eitherDecode bytestring of
58 case decode decoder json of
70string :: Decoder String
77 Decoder $ \mkError value ->
83 Left (mkError (Failure value "Expecting a STRING"))
88 Decoder $ \mkError value ->
94 Left (mkError (Failure value "Expecting a BOOL"))
99 Decoder $ \mkError value ->
101 Aeson.Number number ->
102 case Scientific.toBoundedInteger number of
104 Left (mkError (Failure value "Expecting an INT"))
110 Left (mkError (Failure value "Expecting an INT"))
113float :: Decoder Float
115 Decoder $ \mkError value ->
117 Aeson.Number number ->
118 Right (Scientific.toRealFloat number)
121 Left (mkError (Failure value "Expecting a FLOAT"))
128list :: Decoder a -> Decoder [a]
130 Decoder $ \mkError value ->
132 Aeson.Array vector ->
134 Vector.imapM (\i v -> run (mkError . Index i) v) vector
137 Left (mkError (Failure value "Expecting an ARRAY"))
140dict :: Decoder a -> Decoder (HashMap.HashMap Text a)
142 Decoder $ \mkError value ->
144 Aeson.Object hashMap ->
145 HashMap.traverseWithKey (\k v -> run (mkError . Field k) v) hashMap
148 Left (mkError (Failure value "Expecting an OBJECT"))
151maybe :: Decoder a -> Decoder (Maybe a)
153 Decoder $ \mkError value ->
154 case run mkError value of
166field :: Text -> Decoder a -> Decoder a
167field name (Decoder run) =
168 Decoder $ \mkError value ->
170 Aeson.Object hashMap ->
171 case HashMap.lookup name hashMap of
173 run (mkError . Field name) v
176 Left $ mkError $ Failure value $
177 "Expecting a \"" ++ Text.unpack name ++ "\" field."
180 Left (mkError (Failure value "Expecting an OBJECT"))
183at :: [Text] -> Decoder a -> Decoder a
185 foldr field decoder names
192index :: Int -> Decoder a -> Decoder a
193index i (Decoder run) =
194 Decoder $ \mkError value ->
196 Aeson.Array vector ->
199 run (mkError . Index i) v
202 Left $ mkError $ Failure value $
203 "Expecting a longer ARRAY. I need index " ++ show i
204 ++ ", but the array only has " ++ show (Vector.length vector)
208 Left (mkError (Failure value "Expecting an ARRAY"))
215map :: (a -> value) -> Decoder a -> Decoder value
216map func (Decoder run) =
217 Decoder $ \mkError value ->
218 func <$> run mkError value
221map2 :: (a -> b -> value) -> Decoder a -> Decoder b -> Decoder value
222map2 func (Decoder runA) (Decoder runB) =
223 Decoder $ \mkError value ->
225 <$> runA mkError value
226 <*> runB mkError value
229apply :: Decoder (a -> b) -> Decoder a -> Decoder b
230apply (Decoder runFunc) (Decoder runArg) =
231 Decoder $ \mkError value ->
232 do func <- runFunc mkError value
233 arg <- runArg mkError value
237instance Functor Decoder where
242instance Applicative Decoder where
250instance Monad Decoder where
254 (>>=) decoder callback =
255 andThen callback decoder
265succeed :: a -> Decoder a
267 Decoder $ \_ _ -> Right a
270fail :: String -> Decoder a
272 Decoder $ \mkError value ->
273 Left (mkError (Failure value msg))
276andThen :: (a -> Decoder b) -> Decoder a -> Decoder b
277andThen callback (Decoder runA) =
278 Decoder $ \mkError value ->
279 do a <- runA mkError value
280 let (Decoder runB) = callback a
288oneOf :: [Decoder a] -> Decoder a
290 Decoder (oneOfHelp decoders [])
293oneOfHelp :: [Decoder a] -> [Error] -> (Error -> Error) -> Aeson.Value -> Either Error a
294oneOfHelp decoders errors mkError value =
297 Left (mkError (OneOf (reverse errors)))
299 Decoder run : otherDecoders ->
300 case run mkError value of
305 oneOfHelp otherDecoders (err:errors) mkError value
312aeson :: (Aeson.FromJSON a) => Decoder a
314 Decoder $ \mkError value ->
315 case Aeson.fromJSON value of
317 Left (mkError (Failure value msg))
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list