1{-# OPTIONS_GHC -Wall #-}
2{-# LANGUAGE OverloadedStrings #-}
3module Json.Decode.Internals
7 , string, text, bool, int
19import Data.Text (Text)
20import Prelude hiding (fail, map, maybe)
22import qualified Data.HashMap.Strict as HashMap
23import qualified Data.Text as Text
24import qualified Data.Vector as Vector
25import Data.Vector ((!?))
33 = Array (Vector.Vector Value)
34 | Object [(Text, Value)] (HashMap.HashMap Text Value)
48 { _run :: (Error e -> Error e) -> Value -> Either (Error e) a
53 = Field Text (Error e)
57 | Expecting Value Type
74string :: Decoder e String
81 Decoder $ \mkError value ->
87 Left (mkError (Expecting value TString))
92 Decoder $ \mkError value ->
101 Left (mkError (Expecting value TBool))
106 Decoder $ \mkError value ->
112 Left (mkError (Expecting value TInt))
119list :: Decoder e a -> Decoder e [a]
121 Decoder $ \mkError value ->
125 Vector.imapM (\i v -> run (mkError . Index i) v) vector
128 Left (mkError (Expecting value TArray))
131dict :: Decoder e a -> Decoder e (HashMap.HashMap Text a)
133 Decoder $ \mkError value ->
136 HashMap.traverseWithKey (\k v -> run (mkError . Field k) v) hashMap
139 Left (mkError (Expecting value TObject))
142pairs :: Decoder e a -> Decoder e [(Text, a)]
144 Decoder $ \mkError value ->
147 traverse (\(k,v) -> (,) k <$> run (mkError . Field k) v) kvs
150 Left (mkError (Expecting value TObject))
153maybe :: Decoder e a -> Decoder e (Maybe a)
155 Decoder $ \mkError value ->
156 case run mkError value of
168field :: Text -> Decoder e a -> Decoder e a
169field name (Decoder run) =
170 Decoder $ \mkError value ->
173 case HashMap.lookup name hashMap of
175 run (mkError . Field name) v
178 Left $ mkError $ Expecting value (ObjectWith name)
181 Left (mkError (Expecting value TObject))
184at :: [Text] -> Decoder e a -> Decoder e a
186 foldr field decoder names
193index :: Int -> Decoder e a -> Decoder e a
194index i (Decoder run) =
195 Decoder $ \mkError value ->
200 run (mkError . Index i) v
203 Left $ mkError $ Expecting value $ ArrayWith i (Vector.length vector)
206 Left (mkError (Expecting value TArray))
213map :: (a -> value) -> Decoder e a -> Decoder e value
214map func (Decoder run) =
215 Decoder $ \mkError value ->
216 func <$> run mkError value
219map2 :: (a -> b -> value) -> Decoder e a -> Decoder e b -> Decoder e value
220map2 func (Decoder runA) (Decoder runB) =
221 Decoder $ \mkError value ->
223 <$> runA mkError value
224 <*> runB mkError value
227mapError :: (e -> e') -> Decoder e a -> Decoder e' a
228mapError func (Decoder run) =
229 Decoder $ \mkError value ->
230 run (mapErrorHelp func . mkError) value
233mapErrorHelp :: (e -> e') -> Error e -> Error e'
234mapErrorHelp func err =
236 Field f subErr -> Field f (mapErrorHelp func subErr)
237 Index i subErr -> Index i (mapErrorHelp func subErr)
238 OneOf errors -> OneOf (map (mapErrorHelp func) errors)
239 Failure v e -> Failure v (func e)
240 Expecting v e -> Expecting v e
243apply :: Decoder e (a -> b) -> Decoder e a -> Decoder e b
244apply (Decoder runFunc) (Decoder runArg) =
245 Decoder $ \mkError value ->
246 do func <- runFunc mkError value
247 arg <- runArg mkError value
251instance Functor (Decoder e) where
256instance Applicative (Decoder e) where
264instance Monad (Decoder e) where
268 (>>=) decoder callback =
269 andThen callback decoder
276succeed :: a -> Decoder e a
278 Decoder $ \_ _ -> Right a
281fail :: e -> Decoder e a
283 Decoder $ \mkError value ->
284 Left (mkError (Failure value err))
287andThen :: (a -> Decoder e b) -> Decoder e a -> Decoder e b
288andThen callback (Decoder runA) =
289 Decoder $ \mkError value ->
290 do a <- runA mkError value
291 let (Decoder runB) = callback a
299oneOf :: [Decoder e a] -> Decoder e a
301 Decoder (oneOfHelp decoders [])
304oneOfHelp :: [Decoder e a] -> [Error e] -> (Error e -> Error e) -> Value -> Either (Error e) a
305oneOfHelp decoders errors mkError value =
308 Left (mkError (OneOf (reverse errors)))
310 Decoder run : otherDecoders ->
311 case run mkError value of
316 oneOfHelp otherDecoders (err:errors) mkError value
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