@@ -4,6 +4,7 @@ module Json.Decode.Internals
4 ( Value(..)
4 ( Value(..)
5 , Decoder(..)
5 , Decoder(..)
6 , Error(..)
6 , Error(..)
7 , string, text, bool, int
8 , string, text, bool, int
8 , list, dict, pairs
9 , list, dict, pairs
@@ -23,6 +24,7 @@ import qualified Data.HashMap.Strict as HashMap
23import qualified Data.Text as Text
24import qualified Data.Text as Text
24import qualified Data.Vector as Vector
25import qualified Data.Vector as Vector
25import Data.Vector ((!?))
26import Data.Vector ((!?))
27+import qualified Json.Encode as E
@@ -55,8 +57,8 @@ data Error e
55 = Field Text (Error e)
57 = Field Text (Error e)
56 | Index Int (Error e)
58 | Index Int (Error e)
57 | OneOf [Error e]
59 | OneOf [Error e]
58− | Failure Value e
60+ | Failure E.Value e
59− | Expecting Value Type
61+ | Expecting E.Value Type
@@ -69,13 +71,6 @@ data Type
69 | TArrayWith Int Int
71 | TArrayWith Int Int
72−mapLeft :: (e -> e') -> Either e a -> Either e' a
76− Left e -> Left (func e)
80-- PRIMITIVES
75-- PRIMITIVES
@@ -93,7 +88,7 @@ text =
96− Left (Expecting value TString)
91+ Left (expecting value TString)
99bool :: Decoder e Bool
94bool :: Decoder e Bool
@@ -107,7 +102,7 @@ bool =
107 Right False
102 Right False
110− Left (Expecting value TBool)
105+ Left (expecting value TBool)
113int :: Decoder e Int
108int :: Decoder e Int
@@ -118,7 +113,7 @@ int =
118 Right integer
113 Right integer
121− Left (Expecting value TInt)
116+ Left (expecting value TInt)
@@ -137,7 +132,7 @@ list (Decoder run) =
137 Vector.toList <$> Vector.imapM runHelp vector
132 Vector.toList <$> Vector.imapM runHelp vector
140− Left (Expecting value TArray)
135+ Left (expecting value TArray)
143dict :: Decoder e a -> Decoder e (HashMap.HashMap Text a)
138dict :: Decoder e a -> Decoder e (HashMap.HashMap Text a)
@@ -152,7 +147,7 @@ dict (Decoder run) =
152 HashMap.traverseWithKey runHelp hashMap
147 HashMap.traverseWithKey runHelp hashMap
155− Left (Expecting value TObject)
150+ Left (expecting value TObject)
158pairs :: Decoder e a -> Decoder e [(Text, a)]
153pairs :: Decoder e a -> Decoder e [(Text, a)]
@@ -169,7 +164,7 @@ pairs (Decoder run) =
169 traverse runHelp kvs
164 traverse runHelp kvs
172− Left (Expecting value TObject)
167+ Left (expecting value TObject)
175maybe :: Decoder e a -> Decoder e (Maybe a)
170maybe :: Decoder e a -> Decoder e (Maybe a)
@@ -197,10 +192,10 @@ field k (Decoder run) =
197 mapLeft (Field k) (run v)
192 mapLeft (Field k) (run v)
199 Nothing ->
194 Nothing ->
200− Left (Expecting value (TObjectWith k))
195+ Left (expecting value (TObjectWith k))
203− Left (Expecting value TObject)
198+ Left (expecting value TObject)
206at :: [Text] -> Decoder e a -> Decoder e a
201at :: [Text] -> Decoder e a -> Decoder e a
@@ -222,29 +217,14 @@ index i (Decoder run) =
222 mapLeft (Index i) (run v)
217 mapLeft (Index i) (run v)
224 Nothing ->
219 Nothing ->
225− Left (Expecting value (TArrayWith i (Vector.length vector)))
220+ Left (expecting value (TArrayWith i (Vector.length vector)))
228− Left (Expecting value TArray)
223+ Left (expecting value TArray)
235−map :: (a -> value) -> Decoder e a -> Decoder e value
227+-- ERRORS
236−map func (Decoder run) =
242−map2 :: (a -> b -> value) -> Decoder e a -> Decoder e b -> Decoder e value
243−map2 func (Decoder runA) (Decoder runB) =
250mapError :: (e -> e') -> Decoder e a -> Decoder e' a
230mapError :: (e -> e') -> Decoder e a -> Decoder e' a
@@ -263,33 +243,54 @@ mapErrorHelp func err =
263 Expecting v e -> Expecting v e
243 Expecting v e -> Expecting v e
266−apply :: Decoder e (a -> b) -> Decoder e a -> Decoder e b
246+mapLeft :: (e -> e') -> Either e a -> Either e' a
267−apply (Decoder runFunc) (Decoder runArg) =
247+mapLeft func eith =
268− Decoder $ \value ->
248+ case eith of
269− do func <- runFunc value
249+ Right a -> Right a
270− arg <- runArg value
250+ Left e -> Left (func e)
274−instance Functor (Decoder e) where
253+expecting :: Value -> Type -> Error e
275− fmap =
254+expecting value tipe =
276− map
255+ Expecting (toEncodeValue value) tipe
279−instance Applicative (Decoder e) where
258+toEncodeValue :: Value -> E.Value
280− pure =
259+toEncodeValue value =
281− succeed
260+ case value of
261+ Array vector -> E.array (Vector.toList (Vector.map toEncodeValue vector))
262+ Object fields _ -> E.object (fmap (\(k,v) -> (Text.unpack k, toEncodeValue v)) fields)
263+ String txt -> E.text txt
265+ FALSE -> E.bool False
266+ Integer n -> E.int n
287−instance Monad (Decoder e) where
271+-- MAPPING
291− (>>=) decoder callback =
273+
292− andThen callback decoder
274+map :: (a -> value) -> Decoder e a -> Decoder e value
275+map func (Decoder run) =
280+map2 :: (a -> b -> value) -> Decoder e a -> Decoder e b -> Decoder e value
281+map2 func (Decoder runA) (Decoder runB) =
288+apply :: Decoder e (a -> b) -> Decoder e a -> Decoder e b
289+apply (Decoder runFunc) (Decoder runArg) =
291+ do func <- runFunc value
@@ -304,7 +305,7 @@ succeed a =
304fail :: e -> Decoder e a
305fail :: e -> Decoder e a
305fail err =
306fail err =
306 Decoder $ \value ->
307 Decoder $ \value ->
307− Left (Failure value err)
308+ Left (Failure (toEncodeValue value) err)
310andThen :: (a -> Decoder e b) -> Decoder e a -> Decoder e b
311andThen :: (a -> Decoder e b) -> Decoder e a -> Decoder e b
@@ -337,3 +338,22 @@ oneOfHelp decoders errors value =
338 Left err ->
339 Left err ->
339 oneOfHelp otherDecoders (err:errors) value
340 oneOfHelp otherDecoders (err:errors) value
347+instance Functor (Decoder e) where
351+instance Applicative (Decoder e) where
356+instance Monad (Decoder e) where
358+ (>>=) decoder callback =
359+ andThen callback decoder