@@ -9,7 +9,7 @@ module Json.Decode.Internals
99 , list, dict, pairs
1010 , maybe
1111 , field, at
12− , index
12+ , pair
1313 , map, map2, mapError
1414 , succeed, fail
1515 , andThen, oneOf
@@ -20,11 +20,10 @@ module Json.Decode.Internals
2020import Data.Text (Text)
2121import Prelude hiding (fail, map, maybe)
2222
23−import qualified Data.HashMap.Strict as HashMap
23+import qualified Data.List as List
24+import qualified Data.Map.Strict as Map
2425import qualified Data.Scientific as Sci
2526import qualified Data.Text as Text
26−import qualified Data.Vector as Vector
27−import Data.Vector ((!?))
2827import qualified Elm.Name as N
2928import qualified Json.Encode as E
3029
@@ -34,8 +33,8 @@ import qualified Json.Encode as E
3433
3534
3635data Value
37− = Array (Vector.Vector Value)
38− | Object [(Text, Value)] (HashMap.HashMap Text Value)
36+ = Array [Value]
37+ | Object [(Text, Value)]
3938 | String Text
4039 | Integer Int
4140 | Float Sci.Scientific
@@ -71,7 +70,7 @@ data Type
7170 | TBool
7271 | TInt
7372 | TObjectWith Text
74− | TArrayWith Int Int
73+ | TArrayPair Int
7574
7675
7776
@@ -132,27 +131,27 @@ list :: Decoder e a -> Decoder e [a]
132131list (Decoder run) =
133132 Decoder $ \value ->
134133 case value of
135− Array vector ->
134+ Array vs ->
136135 let
137136 runHelp i v =
138137 mapLeft (Index i) (run v)
139138 in
140− Vector.toList <$> Vector.imapM runHelp vector
139+ sequenceA (zipWith runHelp [0..] vs)
141140
142141 _ ->
143142 Left (expecting value TArray)
144143
145144
146−dict :: Decoder e a -> Decoder e (HashMap.HashMap Text a)
145+dict :: Decoder e a -> Decoder e (Map.Map Text a)
147146dict (Decoder run) =
148147 Decoder $ \value ->
149148 case value of
150− Object _ hashMap ->
149+ Object kvs ->
151150 let
152151 runHelp k v =
153152 mapLeft (Field k) (run v)
154153 in
155− HashMap.traverseWithKey runHelp hashMap
154+ Map.traverseWithKey runHelp (Map.fromList kvs)
156155
157156 _ ->
158157 Left (expecting value TObject)
@@ -162,7 +161,7 @@ pairs :: Decoder e a -> Decoder e [(Text, a)]
162161pairs (Decoder run) =
163162 Decoder $ \value ->
164163 case value of
165− Object kvs _ ->
164+ Object kvs ->
166165 let
167166 runHelp (k,v) =
168167 case run v of
@@ -194,8 +193,8 @@ field :: Text -> Decoder e a -> Decoder e a
194193field k (Decoder run) =
195194 Decoder $ \value ->
196195 case value of
197− Object _ hashMap ->
198− case HashMap.lookup k hashMap of
196+ Object kvs ->
197+ case List.lookup k kvs of
199198 Just v ->
200199 mapLeft (Field k) (run v)
201200
@@ -215,17 +214,19 @@ at names decoder =
215214-- ARRAY PRIMITIVES
216215
217216
218−index :: Int -> Decoder e a -> Decoder e a
219−index i (Decoder run) =
217+pair :: Decoder e a -> Decoder e b -> Decoder e (a,b)
218+pair (Decoder runA) (Decoder runB) =
220219 Decoder $ \value ->
221220 case value of
222− Array vector ->
223− case vector !? i of
224− Just v ->
225− mapLeft (Index i) (run v)
221+ Array vs ->
222+ case vs of
223+ [a,b] ->
224+ (,)
225+ <$> mapLeft (Index 0) (runA a)
226+ <*> mapLeft (Index 1) (runB b)
226227
227− Nothing ->
228− Left (expecting value (TArrayWith i (Vector.length vector)))
228+ _ ->
229+ Left (expecting value (TArrayPair (length vs)))
229230
230231 _ ->
231232 Left (expecting value TArray)
@@ -266,14 +267,14 @@ expecting value tipe =
266267toEncodeValue :: Value -> E.Value
267268toEncodeValue value =
268269 case value of
269− Array vector -> E.array (Vector.toList (Vector.map toEncodeValue vector))
270− Object fields _ -> E.object (fmap (fmap toEncodeValue) fields)
271− String txt -> E.text txt
272− Integer n -> E.int n
273− Float n -> E.number n
274− TRUE -> E.bool True
275− FALSE -> E.bool False
276− NULL -> E.null
270+ Array vs -> E.array (fmap toEncodeValue vs)
271+ Object kvs -> E.object (fmap (fmap toEncodeValue) kvs)
272+ String txt -> E.text txt
273+ Integer n -> E.int n
274+ Float n -> E.number n
275+ TRUE -> E.bool True
276+ FALSE -> E.bool False
277+ NULL -> E.null
277278
278279
279280
@@ -297,9 +298,7 @@ map2 func (Decoder runA) (Decoder runB) =
297298apply :: Decoder e (a -> b) -> Decoder e a -> Decoder e b
298299apply (Decoder runFunc) (Decoder runArg) =
299300 Decoder $ \value ->
300− do func <- runFunc value
301− arg <- runArg value
302− return (func arg)
301+ runFunc value <*> runArg value
303302
304303
305304