on master help sign in
Make a 4-tuple an instance of ToJSON and FromJSON
commit12a9daf6fe2dce30bdf4ce6768fb3bea991149b1 authorBas van Dijk <v.dijk.bas@gmail.com> dateFri 18 Nov 2011, 03:17 (15 years ago) parent5fa5f24
1 file changed+16
Data/Aeson/Types/Class.hs+16
Data/Aeson/Types/Class.hs+16
@@ -647,6 +647,22 @@ instance (FromJSON a, FromJSON b, FromJSON c) => FromJSON (a,b,c) where
647647 parseJSON v = typeMismatch "(a,b,c)" v
648648 {-# INLINE parseJSON #-}
649649
650+instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d) => ToJSON (a,b,c,d) where
651+ toJSON (a,b,c,d) = toJSON [toJSON a, toJSON b, toJSON c, toJSON d]
652+ {-# INLINE toJSON #-}
653+
654+instance (FromJSON a, FromJSON b, FromJSON c, FromJSON d) => FromJSON (a,b,c,d) where
655+ parseJSON (Array abcd) =
656+ case V.toList abcd of
657+ [a,b,c,d] -> (,,,) <$> parseJSON a
658+ <*> parseJSON b
659+ <*> parseJSON c
660+ <*> parseJSON d
661+ _ -> fail $ "cannot unpack array of length " ++
662+ show (V.length abcd) ++ " into a 4-tuple"
663+ parseJSON v = typeMismatch "(a,b,c,d)" v
664+ {-# INLINE parseJSON #-}
665+
650666instance ToJSON a => ToJSON (Dual a) where
651667 toJSON = toJSON . getDual
652668 {-# INLINE toJSON #-}