@@ -9,7 +9,7 @@ module Json.Decode.Internals
9 , list, dict, pairs
9 , list, dict, pairs
11 , field, at
11 , field, at
13 , map, map2, mapError
13 , map, map2, mapError
14 , succeed, fail
14 , succeed, fail
15 , andThen, oneOf
15 , andThen, oneOf
@@ -20,11 +20,10 @@ module Json.Decode.Internals
20import Data.Text (Text)
20import Data.Text (Text)
21import Prelude hiding (fail, map, maybe)
21import Prelude hiding (fail, map, maybe)
23−import qualified Data.HashMap.Strict as HashMap
23+import qualified Data.List as List
24+import qualified Data.Map.Strict as Map
24import qualified Data.Scientific as Sci
25import qualified Data.Scientific as Sci
25import qualified Data.Text as Text
26import qualified Data.Text as Text
26−import qualified Data.Vector as Vector
27−import Data.Vector ((!?))
28import qualified Elm.Name as N
27import qualified Elm.Name as N
29import qualified Json.Encode as E
28import qualified Json.Encode as E
@@ -34,8 +33,8 @@ import qualified Json.Encode as E
37− = Array (Vector.Vector Value)
36+ = Array [Value]
38− | Object [(Text, Value)] (HashMap.HashMap Text Value)
37+ | Object [(Text, Value)]
39 | String Text
38 | String Text
40 | Integer Int
39 | Integer Int
41 | Float Sci.Scientific
40 | Float Sci.Scientific
@@ -71,7 +70,7 @@ data Type
73 | TObjectWith Text
72 | TObjectWith Text
74− | TArrayWith Int Int
73+ | TArrayPair Int
@@ -132,27 +131,27 @@ list :: Decoder e a -> Decoder e [a]
132list (Decoder run) =
131list (Decoder run) =
133 Decoder $ \value ->
132 Decoder $ \value ->
134 case value of
133 case value of
135− Array vector ->
134+ Array vs ->
137 runHelp i v =
136 runHelp i v =
138 mapLeft (Index i) (run v)
137 mapLeft (Index i) (run v)
140− Vector.toList <$> Vector.imapM runHelp vector
139+ sequenceA (zipWith runHelp [0..] vs)
143 Left (expecting value TArray)
142 Left (expecting value TArray)
146−dict :: Decoder e a -> Decoder e (HashMap.HashMap Text a)
145+dict :: Decoder e a -> Decoder e (Map.Map Text a)
147dict (Decoder run) =
146dict (Decoder run) =
148 Decoder $ \value ->
147 Decoder $ \value ->
149 case value of
148 case value of
150− Object _ hashMap ->
149+ Object kvs ->
152 runHelp k v =
151 runHelp k v =
153 mapLeft (Field k) (run v)
152 mapLeft (Field k) (run v)
155− HashMap.traverseWithKey runHelp hashMap
154+ Map.traverseWithKey runHelp (Map.fromList kvs)
158 Left (expecting value TObject)
157 Left (expecting value TObject)
@@ -162,7 +161,7 @@ pairs :: Decoder e a -> Decoder e [(Text, a)]
162pairs (Decoder run) =
161pairs (Decoder run) =
163 Decoder $ \value ->
162 Decoder $ \value ->
164 case value of
163 case value of
165− Object kvs _ ->
164+ Object kvs ->
167 runHelp (k,v) =
166 runHelp (k,v) =
168 case run v of
167 case run v of
@@ -194,8 +193,8 @@ field :: Text -> Decoder e a -> Decoder e a
194field k (Decoder run) =
193field k (Decoder run) =
195 Decoder $ \value ->
194 Decoder $ \value ->
196 case value of
195 case value of
197− Object _ hashMap ->
196+ Object kvs ->
198− case HashMap.lookup k hashMap of
197+ case List.lookup k kvs of
199 Just v ->
198 Just v ->
200 mapLeft (Field k) (run v)
199 mapLeft (Field k) (run v)
@@ -215,17 +214,19 @@ at names decoder =
215-- ARRAY PRIMITIVES
214-- ARRAY PRIMITIVES
218−index :: Int -> Decoder e a -> Decoder e a
217+pair :: Decoder e a -> Decoder e b -> Decoder e (a,b)
219−index i (Decoder run) =
218+pair (Decoder runA) (Decoder runB) =
220 Decoder $ \value ->
219 Decoder $ \value ->
221 case value of
220 case value of
222− Array vector ->
221+ Array vs ->
223− case vector !? i of
222+ case vs of
224− Just v ->
223+ [a,b] ->
225+ <$> mapLeft (Index 0) (runA a)
225− mapLeft (Index i) (run v)
226+ <*> mapLeft (Index 1) (runB b)
228− Left (expecting value (TArrayWith i (Vector.length vector)))
229+ Left (expecting value (TArrayPair (length vs)))
231 Left (expecting value TArray)
232 Left (expecting value TArray)
@@ -266,14 +267,14 @@ expecting value tipe =
266toEncodeValue :: Value -> E.Value
267toEncodeValue :: Value -> E.Value
267toEncodeValue value =
268toEncodeValue value =
268 case value of
269 case value of
269− Array vector -> E.array (Vector.toList (Vector.map toEncodeValue vector))
270+ Array vs -> E.array (fmap toEncodeValue vs)
270− Object fields _ -> E.object (fmap (fmap toEncodeValue) fields)
271+ Object kvs -> E.object (fmap (fmap toEncodeValue) kvs)
271− String txt -> E.text txt
272+ String txt -> E.text txt
272− Integer n -> E.int n
273+ Integer n -> E.int n
273− Float n -> E.number n
274+ Float n -> E.number n
274− TRUE -> E.bool True
275+ TRUE -> E.bool True
275− FALSE -> E.bool False
276+ FALSE -> E.bool False
276− NULL -> E.null
277+ NULL -> E.null
@@ -297,9 +298,7 @@ map2 func (Decoder runA) (Decoder runB) =
297apply :: Decoder e (a -> b) -> Decoder e a -> Decoder e b
298apply :: Decoder e (a -> b) -> Decoder e a -> Decoder e b
298apply (Decoder runFunc) (Decoder runArg) =
299apply (Decoder runFunc) (Decoder runArg) =
299 Decoder $ \value ->
300 Decoder $ \value ->
300− do func <- runFunc value
301− arg <- runArg value
301+ runFunc value <*> runArg value