on master help sign in
Provide `{To,From}JSON` instances for {5,6,7}-tuples

Providing this instances is motived by the Haskell Report suggesting that libraries ought to support tuples up to a size of 7 (see http://www.haskell.org/onlinereport/basic.html#basic-tuples): > The Prelude and libraries define tuple functions such as zip for > tuples up to a size of 7. This fixes #144

commit457b7ebff6025ec3882d7f4eb4282898b6767f1b authorHerbert Valerio Riedel <hvr@gnu.org> dateSun 22 Sep 2013, 10:47 (13 years ago) parentf984a59
1 file changed+78
Data/Aeson/Types/Class.hs+78
Data/Aeson/Types/Class.hs+78
@@ -748,6 +748,84 @@ instance (FromJSON a, FromJSON b, FromJSON c, FromJSON d) => FromJSON (a,b,c,d)
748748 show n ++ " into a 4-tuple"
749749 {-# INLINE parseJSON #-}
750750
751+instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e) => ToJSON (a,b,c,d,e) where
752+ toJSON (a,b,c,d,e) = Array $ V.create $ do
753+ mv <- VM.unsafeNew 5
754+ VM.unsafeWrite mv 0 (toJSON a)
755+ VM.unsafeWrite mv 1 (toJSON b)
756+ VM.unsafeWrite mv 2 (toJSON c)
757+ VM.unsafeWrite mv 3 (toJSON d)
758+ VM.unsafeWrite mv 4 (toJSON e)
759+ return mv
760+ {-# INLINE toJSON #-}
761+
762+instance (FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e) => FromJSON (a,b,c,d,e) where
763+ parseJSON = withArray "(a,b,c,d,e)" $ \abcde ->
764+ let n = V.length abcde
765+ in if n == 5
766+ then (,,,,) <$> parseJSON (V.unsafeIndex abcde 0)
767+ <*> parseJSON (V.unsafeIndex abcde 1)
768+ <*> parseJSON (V.unsafeIndex abcde 2)
769+ <*> parseJSON (V.unsafeIndex abcde 3)
770+ <*> parseJSON (V.unsafeIndex abcde 4)
771+ else fail $ "cannot unpack array of length " ++
772+ show n ++ " into a 5-tuple"
773+ {-# INLINE parseJSON #-}
774+
775+instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f) => ToJSON (a,b,c,d,e,f) where
776+ toJSON (a,b,c,d,e,f) = Array $ V.create $ do
777+ mv <- VM.unsafeNew 6
778+ VM.unsafeWrite mv 0 (toJSON a)
779+ VM.unsafeWrite mv 1 (toJSON b)
780+ VM.unsafeWrite mv 2 (toJSON c)
781+ VM.unsafeWrite mv 3 (toJSON d)
782+ VM.unsafeWrite mv 4 (toJSON e)
783+ VM.unsafeWrite mv 5 (toJSON f)
784+ return mv
785+ {-# INLINE toJSON #-}
786+
787+instance (FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e, FromJSON f) => FromJSON (a,b,c,d,e,f) where
788+ parseJSON = withArray "(a,b,c,d,e,f)" $ \abcdef ->
789+ let n = V.length abcdef
790+ in if n == 6
791+ then (,,,,,) <$> parseJSON (V.unsafeIndex abcdef 0)
792+ <*> parseJSON (V.unsafeIndex abcdef 1)
793+ <*> parseJSON (V.unsafeIndex abcdef 2)
794+ <*> parseJSON (V.unsafeIndex abcdef 3)
795+ <*> parseJSON (V.unsafeIndex abcdef 4)
796+ <*> parseJSON (V.unsafeIndex abcdef 5)
797+ else fail $ "cannot unpack array of length " ++
798+ show n ++ " into a 6-tuple"
799+ {-# INLINE parseJSON #-}
800+
801+instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g) => ToJSON (a,b,c,d,e,f,g) where
802+ toJSON (a,b,c,d,e,f,g) = Array $ V.create $ do
803+ mv <- VM.unsafeNew 7
804+ VM.unsafeWrite mv 0 (toJSON a)
805+ VM.unsafeWrite mv 1 (toJSON b)
806+ VM.unsafeWrite mv 2 (toJSON c)
807+ VM.unsafeWrite mv 3 (toJSON d)
808+ VM.unsafeWrite mv 4 (toJSON e)
809+ VM.unsafeWrite mv 5 (toJSON f)
810+ VM.unsafeWrite mv 6 (toJSON g)
811+ return mv
812+ {-# INLINE toJSON #-}
813+
814+instance (FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e, FromJSON f, FromJSON g) => FromJSON (a,b,c,d,e,f,g) where
815+ parseJSON = withArray "(a,b,c,d,e,f,g)" $ \abcdefg ->
816+ let n = V.length abcdefg
817+ in if n == 7
818+ then (,,,,,,) <$> parseJSON (V.unsafeIndex abcdefg 0)
819+ <*> parseJSON (V.unsafeIndex abcdefg 1)
820+ <*> parseJSON (V.unsafeIndex abcdefg 2)
821+ <*> parseJSON (V.unsafeIndex abcdefg 3)
822+ <*> parseJSON (V.unsafeIndex abcdefg 4)
823+ <*> parseJSON (V.unsafeIndex abcdefg 5)
824+ <*> parseJSON (V.unsafeIndex abcdefg 6)
825+ else fail $ "cannot unpack array of length " ++
826+ show n ++ " into a 7-tuple"
827+ {-# INLINE parseJSON #-}
828+
751829instance ToJSON a => ToJSON (Dual a) where
752830 toJSON = toJSON . getDual
753831 {-# INLINE toJSON #-}