@@ -5,6 +5,7 @@ module Json.Decode
55 , text, bool, int, float
66 , list, dict, maybe
77 , field, at
8+ , index
89 , map, map2, succeed, fail, andThen
910 )
1011 where
@@ -20,6 +21,7 @@ import qualified Data.HashMap.Lazy as HashMap
2021import qualified Data.Scientific as Scientific
2122import qualified Data.Text as Text
2223import qualified Data.Vector as Vector
24+import Data.Vector ((!?))
2325
2426
2527
@@ -176,6 +178,27 @@ at names decoder =
176178
177179
178180
181+-- ARRAY PRIMITIVES
182+
183+
184+index :: Int -> Decoder a -> Decoder a
185+index i (Decoder run) =
186+ Decoder $ \mkError value ->
187+ case value of
188+ Aeson.Array vector ->
189+ case vector !? i of
190+ Just v ->
191+ run (mkError . Index i) v
192+
193+ Nothing ->
194+ Left $ mkError $
195+ Failure value ("index " ++ show i ++ " of an array")
196+
197+ _ ->
198+ Left (mkError (Failure value "an array"))
199+
200+
201+
179202-- MAPPING
180203
181204