@@ -4,6 +4,7 @@ module Json.Decode.Internals
44 ( Value(..)
55 , Decoder(..)
66 , Error(..)
7+ , Type(..)
78 , string, text, bool, int
89 , list, dict, pairs
910 , maybe
@@ -23,6 +24,7 @@ import qualified Data.HashMap.Strict as HashMap
2324import qualified Data.Text as Text
2425import qualified Data.Vector as Vector
2526import Data.Vector ((!?))
27+import qualified Json.Encode as E
2628
2729
2830
@@ -55,8 +57,8 @@ data Error e
5557 = Field Text (Error e)
5658 | Index Int (Error e)
5759 | OneOf [Error e]
58− | Failure Value e
59− | Expecting Value Type
60+ | Failure E.Value e
61+ | Expecting E.Value Type
6062
6163
6264data Type
@@ -69,13 +71,6 @@ data Type
6971 | TArrayWith Int Int
7072
7173
72−mapLeft :: (e -> e') -> Either e a -> Either e' a
73−mapLeft func eith =
74− case eith of
75− Right a -> Right a
76− Left e -> Left (func e)
77−
78−
7974
8075-- PRIMITIVES
8176
@@ -93,7 +88,7 @@ text =
9388 Right txt
9489
9590 _ ->
96− Left (Expecting value TString)
91+ Left (expecting value TString)
9792
9893
9994bool :: Decoder e Bool
@@ -107,7 +102,7 @@ bool =
107102 Right False
108103
109104 _ ->
110− Left (Expecting value TBool)
105+ Left (expecting value TBool)
111106
112107
113108int :: Decoder e Int
@@ -118,7 +113,7 @@ int =
118113 Right integer
119114
120115 _ ->
121− Left (Expecting value TInt)
116+ Left (expecting value TInt)
122117
123118
124119
@@ -137,7 +132,7 @@ list (Decoder run) =
137132 Vector.toList <$> Vector.imapM runHelp vector
138133
139134 _ ->
140− Left (Expecting value TArray)
135+ Left (expecting value TArray)
141136
142137
143138dict :: Decoder e a -> Decoder e (HashMap.HashMap Text a)
@@ -152,7 +147,7 @@ dict (Decoder run) =
152147 HashMap.traverseWithKey runHelp hashMap
153148
154149 _ ->
155− Left (Expecting value TObject)
150+ Left (expecting value TObject)
156151
157152
158153pairs :: Decoder e a -> Decoder e [(Text, a)]
@@ -169,7 +164,7 @@ pairs (Decoder run) =
169164 traverse runHelp kvs
170165
171166 _ ->
172− Left (Expecting value TObject)
167+ Left (expecting value TObject)
173168
174169
175170maybe :: Decoder e a -> Decoder e (Maybe a)
@@ -197,10 +192,10 @@ field k (Decoder run) =
197192 mapLeft (Field k) (run v)
198193
199194 Nothing ->
200− Left (Expecting value (TObjectWith k))
195+ Left (expecting value (TObjectWith k))
201196
202197 _ ->
203− Left (Expecting value TObject)
198+ Left (expecting value TObject)
204199
205200
206201at :: [Text] -> Decoder e a -> Decoder e a
@@ -222,29 +217,14 @@ index i (Decoder run) =
222217 mapLeft (Index i) (run v)
223218
224219 Nothing ->
225− Left (Expecting value (TArrayWith i (Vector.length vector)))
220+ Left (expecting value (TArrayWith i (Vector.length vector)))
226221
227222 _ ->
228− Left (Expecting value TArray)
229−
223+ Left (expecting value TArray)
230224
231225
232−-- MAPPING
233−
234226
235−map :: (a -> value) -> Decoder e a -> Decoder e value
236−map func (Decoder run) =
237− Decoder $ \value ->
238− func <$> run value
239−
240−
241−
242−map2 :: (a -> b -> value) -> Decoder e a -> Decoder e b -> Decoder e value
243−map2 func (Decoder runA) (Decoder runB) =
244− Decoder $ \value ->
245− func
246− <$> runA value
247− <*> runB value
227+-- ERRORS
248228
249229
250230mapError :: (e -> e') -> Decoder e a -> Decoder e' a
@@ -263,33 +243,54 @@ mapErrorHelp func err =
263243 Expecting v e -> Expecting v e
264244
265245
266−apply :: Decoder e (a -> b) -> Decoder e a -> Decoder e b
267−apply (Decoder runFunc) (Decoder runArg) =
268− Decoder $ \value ->
269− do func <- runFunc value
270− arg <- runArg value
271− return (func arg)
246+mapLeft :: (e -> e') -> Either e a -> Either e' a
247+mapLeft func eith =
248+ case eith of
249+ Right a -> Right a
250+ Left e -> Left (func e)
272251
273252
274−instance Functor (Decoder e) where
275− fmap =
276− map
253+expecting :: Value -> Type -> Error e
254+expecting value tipe =
255+ Expecting (toEncodeValue value) tipe
277256
278257
279−instance Applicative (Decoder e) where
280− pure =
281− succeed
258+toEncodeValue :: Value -> E.Value
259+toEncodeValue value =
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
264+ TRUE -> E.bool True
265+ FALSE -> E.bool False
266+ Integer n -> E.int n
267+ NULL -> E.null
282268
283− (<*>) =
284− apply
285269
286270
287−instance Monad (Decoder e) where
288− return =
289− succeed
271+-- MAPPING
290272
291− (>>=) decoder callback =
292− andThen callback decoder
273+
274+map :: (a -> value) -> Decoder e a -> Decoder e value
275+map func (Decoder run) =
276+ Decoder $ \value ->
277+ func <$> run value
278+
279+
280+map2 :: (a -> b -> value) -> Decoder e a -> Decoder e b -> Decoder e value
281+map2 func (Decoder runA) (Decoder runB) =
282+ Decoder $ \value ->
283+ func
284+ <$> runA value
285+ <*> runB value
286+
287+
288+apply :: Decoder e (a -> b) -> Decoder e a -> Decoder e b
289+apply (Decoder runFunc) (Decoder runArg) =
290+ Decoder $ \value ->
291+ do func <- runFunc value
292+ arg <- runArg value
293+ return (func arg)
293294
294295
295296
@@ -304,7 +305,7 @@ succeed a =
304305fail :: e -> Decoder e a
305306fail err =
306307 Decoder $ \value ->
307− Left (Failure value err)
308+ Left (Failure (toEncodeValue value) err)
308309
309310
310311andThen :: (a -> Decoder e b) -> Decoder e a -> Decoder e b
@@ -337,3 +338,22 @@ oneOfHelp decoders errors value =
337338
338339 Left err ->
339340 oneOfHelp otherDecoders (err:errors) value
341+
342+
343+
344+-- INSTANCES
345+
346+
347+instance Functor (Decoder e) where
348+ fmap = map
349+
350+
351+instance Applicative (Decoder e) where
352+ pure = succeed
353+ (<*>) = apply
354+
355+
356+instance Monad (Decoder e) where
357+ return = succeed
358+ (>>=) decoder callback =
359+ andThen callback decoder