on master help sign in
Safely use unsafeIndex in Data.Aeson.TH.parseProduct

because we have already checked for a valid length.

commit147e7746be90c0476eaae5cf725d55e1ed9f7988 authorBas van Dijk <v.dijk.bas@gmail.com> dateWed 2 Nov 2011, 20:47 (15 years ago) parentf44b517
1 file changed+8−8
Data/Aeson/TH.hs+8−8
Data/Aeson/TH.hs+8−8
@@ -89,9 +89,9 @@ instance 'FromJSON' a => 'FromJSON' (D a) where
8989 case conVal of
9090 'Array' arr ->
9191 if V.length arr == 3
92− then Product \<$\> 'parseJSON' (arr V.! 0)
93− \<*\> 'parseJSON' (arr V.! 1)
94− \<*\> 'parseJSON' (arr V.! 2)
92+ then Product \<$\> 'parseJSON' (arr `V.unsafeIndex` 0)
93+ \<*\> 'parseJSON' (arr `V.unsafeIndex` 1)
94+ \<*\> 'parseJSON' (arr `V.unsafeIndex` 2)
9595 else fail \"\<error message\>\"
9696 _ -> fail \"\<error message\>\"
9797 | conKey == T.pack \"Record\" ->
@@ -178,7 +178,7 @@ import Language.Haskell.TH
178178-- from text:
179179import qualified Data.Text as T ( Text, pack, unpack )
180180-- from vector:
181−import qualified Data.Vector as V ( (!), null, length )
181+import qualified Data.Vector as V ( unsafeIndex, null, length )
182182
183183
184184
@@ -385,8 +385,8 @@ encodeArgs withExp withField (ForallC _ _ con) =
385385-- \value -> case value of
386386-- 'Array' arr ->
387387-- if (V.length arr == 2)
388−-- then Foo \<$\> 'parseJSON' (arr V.! 0)
389−-- \<*\> 'parseJSON' (arr V.! 1)
388+-- then Foo \<$\> 'parseJSON' (arr `V.unsafeIndex` 0)
389+-- \<*\> 'parseJSON' (arr `V.unsafeIndex` 1)
390390-- else fail \"\<error message\>\"
391391-- other -> fail \"\<error message\>\"
392392-- @
@@ -600,11 +600,11 @@ parseProduct :: Name -- ^ Name of the type to which the constructor belongs.
600600 -> [Q Match]
601601parseProduct tName conName numArgs =
602602 [ do arr <- newName "arr"
603− -- List of: "parseJSON (arr V.! <IX>)"
603+ -- List of: "parseJSON (arr `V.unsafeIndex` <IX>)"
604604 let x:xs = [ [|parseJSON|]
605605 `appE`
606606 infixApp (varE arr)
607− [|(V.!)|]
607+ [|V.unsafeIndex|]
608608 (litE $ integerL ix)
609609 | ix <- [0 .. numArgs - 1]
610610 ]