on master help sign in
Inline implementation of GToJSON
4cbe794Rémy Oudompheng
Data/Aeson/Types/ToJSON.hs+20
@@ -657,11 +657,13 @@ instance (ToJSON a) => ToJSON [a] where
657instance OVERLAPPABLE_ (GToJSON enc arity a) => GToJSON enc arity (M1 i c a) where
657instance OVERLAPPABLE_ (GToJSON enc arity a) => GToJSON enc arity (M1 i c a) where
658 -- Meta-information, which is not handled elsewhere, is ignored:
658 -- Meta-information, which is not handled elsewhere, is ignored:
659 gToJSON opts targs = gToJSON opts targs . unM1
659 gToJSON opts targs = gToJSON opts targs . unM1
660+ {-# INLINE gToJSON #-}
660
661
661instance GToJSON enc One Par1 where
662instance GToJSON enc One Par1 where
662 -- Direct occurrences of the last type parameter are encoded with the
663 -- Direct occurrences of the last type parameter are encoded with the
663 -- function passed in as an argument:
664 -- function passed in as an argument:
664 gToJSON _opts (To1Args tj _) = tj . unPar1
665 gToJSON _opts (To1Args tj _) = tj . unPar1
666+ {-# INLINE gToJSON #-}
665
667
666instance ( ConsToJSON enc arity a
668instance ( ConsToJSON enc arity a
667 , AllNullary (C1 c a) allNullary
669 , AllNullary (C1 c a) allNullary
@@ -674,11 +676,13 @@ instance ( ConsToJSON enc arity a
674 . sumToJSON opts targs
676 . sumToJSON opts targs
675 . unM1
677 . unM1
676 | otherwise = consToJSON opts targs . unM1 . unM1
678 | otherwise = consToJSON opts targs . unM1 . unM1
679+ {-# INLINE gToJSON #-}
677
680
678instance (ConsToJSON enc arity a) => GToJSON enc arity (C1 c a) where
681instance (ConsToJSON enc arity a) => GToJSON enc arity (C1 c a) where
679 -- Constructors need to be encoded differently depending on whether they're
682 -- Constructors need to be encoded differently depending on whether they're
680 -- a record or not. This distinction is made by 'consToJSON':
683 -- a record or not. This distinction is made by 'consToJSON':
681 gToJSON opts targs = consToJSON opts targs . unM1
684 gToJSON opts targs = consToJSON opts targs . unM1
685+ {-# INLINE gToJSON #-}
682
686
683instance ( AllNullary (a :+: b) allNullary
687instance ( AllNullary (a :+: b) allNullary
684 , SumToJSON enc arity (a :+: b) allNullary
688 , SumToJSON enc arity (a :+: b) allNullary
@@ -689,6 +693,7 @@ instance ( AllNullary (a :+: b) allNullary
689 -- strings. This distinction is made by 'sumToJSON':
693 -- strings. This distinction is made by 'sumToJSON':
690 gToJSON opts targs = (unTagged :: Tagged allNullary enc -> enc)
694 gToJSON opts targs = (unTagged :: Tagged allNullary enc -> enc)
691 . sumToJSON opts targs
695 . sumToJSON opts targs
696+ {-# INLINE gToJSON #-}
692
697
693--------------------------------------------------------------------------------
698--------------------------------------------------------------------------------
694-- Generic toJSON
699-- Generic toJSON
@@ -700,15 +705,18 @@ instance ( AllNullary (a :+: b) allNullary
700instance ToJSON a => GToJSON Value arity (K1 i a) where
705instance ToJSON a => GToJSON Value arity (K1 i a) where
701 -- Constant values are encoded using their ToJSON instance:
706 -- Constant values are encoded using their ToJSON instance:
702 gToJSON _opts _ = toJSON . unK1
707 gToJSON _opts _ = toJSON . unK1
708+ {-# INLINE gToJSON #-}
703
709
704instance ToJSON1 f => GToJSON Value One (Rec1 f) where
710instance ToJSON1 f => GToJSON Value One (Rec1 f) where
705 -- Recursive occurrences of the last type parameter are encoded using their
711 -- Recursive occurrences of the last type parameter are encoded using their
706 -- ToJSON1 instance:
712 -- ToJSON1 instance:
707 gToJSON _opts (To1Args tj tjl) = liftToJSON tj tjl . unRec1
713 gToJSON _opts (To1Args tj tjl) = liftToJSON tj tjl . unRec1
714+ {-# INLINE gToJSON #-}
708
715
709instance GToJSON Value arity U1 where
716instance GToJSON Value arity U1 where
710 -- Empty constructors are encoded to an empty array:
717 -- Empty constructors are encoded to an empty array:
711 gToJSON _opts _ _ = emptyArray
718 gToJSON _opts _ _ = emptyArray
719+ {-# INLINE gToJSON #-}
712
720
713instance ( WriteProduct arity a, WriteProduct arity b
721instance ( WriteProduct arity a, WriteProduct arity b
714 , ProductSize a, ProductSize b
722 , ProductSize a, ProductSize b
@@ -725,6 +733,7 @@ instance ( WriteProduct arity a, WriteProduct arity b
725 where
733 where
726 lenProduct = (unTagged2 :: Tagged2 (a :*: b) Int -> Int)
734 lenProduct = (unTagged2 :: Tagged2 (a :*: b) Int -> Int)
727 productSize
735 productSize
736+ {-# INLINE gToJSON #-}
728
737
729instance ( ToJSON1 f
738instance ( ToJSON1 f
730 , GToJSON Value One g
739 , GToJSON Value One g
@@ -736,6 +745,7 @@ instance ( ToJSON1 f
736 gToJSON opts targs =
745 gToJSON opts targs =
737 let gtj = gToJSON opts targs in
746 let gtj = gToJSON opts targs in
738 liftToJSON gtj (listValue gtj) . unComp1
747 liftToJSON gtj (listValue gtj) . unComp1
748+ {-# INLINE gToJSON #-}
739
749
740--------------------------------------------------------------------------------
750--------------------------------------------------------------------------------
741-- Generic toEncoding
751-- Generic toEncoding
@@ -743,15 +753,18 @@ instance ( ToJSON1 f
743instance ToJSON a => GToJSON Encoding arity (K1 i a) where
753instance ToJSON a => GToJSON Encoding arity (K1 i a) where
744 -- Constant values are encoded using their ToJSON instance:
754 -- Constant values are encoded using their ToJSON instance:
745 gToJSON _opts _ = toEncoding . unK1
755 gToJSON _opts _ = toEncoding . unK1
756+ {-# INLINE gToJSON #-}
746
757
747instance ToJSON1 f => GToJSON Encoding One (Rec1 f) where
758instance ToJSON1 f => GToJSON Encoding One (Rec1 f) where
748 -- Recursive occurrences of the last type parameter are encoded using their
759 -- Recursive occurrences of the last type parameter are encoded using their
749 -- ToEncoding1 instance:
760 -- ToEncoding1 instance:
750 gToJSON _opts (To1Args te tel) = liftToEncoding te tel . unRec1
761 gToJSON _opts (To1Args te tel) = liftToEncoding te tel . unRec1
762+ {-# INLINE gToJSON #-}
751
763
752instance GToJSON Encoding arity U1 where
764instance GToJSON Encoding arity U1 where
753 -- Empty constructors are encoded to an empty array:
765 -- Empty constructors are encoded to an empty array:
754 gToJSON _opts _ _ = E.emptyArray_
766 gToJSON _opts _ _ = E.emptyArray_
767+ {-# INLINE gToJSON #-}
755
768
756instance ( EncodeProduct arity a
769instance ( EncodeProduct arity a
757 , EncodeProduct arity b
770 , EncodeProduct arity b
@@ -761,6 +774,7 @@ instance ( EncodeProduct arity a
761 -- the same size as the product and write the product's elements to it using
774 -- the same size as the product and write the product's elements to it using
762 -- 'encodeProduct':
775 -- 'encodeProduct':
763 gToJSON opts targs p = E.list E.retagEncoding [encodeProduct opts targs p]
776 gToJSON opts targs p = E.list E.retagEncoding [encodeProduct opts targs p]
777+ {-# INLINE gToJSON #-}
764
778
765instance ( ToJSON1 f
779instance ( ToJSON1 f
766 , GToJSON Encoding One g
780 , GToJSON Encoding One g
@@ -772,6 +786,7 @@ instance ( ToJSON1 f
772 gToJSON opts targs =
786 gToJSON opts targs =
773 let gte = gToJSON opts targs in
787 let gte = gToJSON opts targs in
774 liftToEncoding gte (listEncoding gte) . unComp1
788 liftToEncoding gte (listEncoding gte) . unComp1
789+ {-# INLINE gToJSON #-}
775
790
776--------------------------------------------------------------------------------
791--------------------------------------------------------------------------------
777
792
@@ -983,6 +998,7 @@ instance ( RecordToPairs enc pairs arity f
983 ) => ConsToJSON' enc arity f True
998 ) => ConsToJSON' enc arity f True
984 where
999 where
985 consToJSON' opts targs = Tagged . fromPairs . recordToPairs opts targs
1000 consToJSON' opts targs = Tagged . fromPairs . recordToPairs opts targs
1001+ {-# INLINE consToJSON' #-}
986
1002
987instance GToJSON enc arity f => ConsToJSON' enc arity f False where
1003instance GToJSON enc arity f => ConsToJSON' enc arity f False where
988 consToJSON' opts targs = Tagged . gToJSON opts targs
1004 consToJSON' opts targs = Tagged . gToJSON opts targs
@@ -1074,10 +1090,12 @@ instance ( WriteProduct arity a
1074 lenL = len `unsafeShiftR` 1
1090 lenL = len `unsafeShiftR` 1
1075 lenR = len - lenL
1091 lenR = len - lenL
1076 ixR = ix + lenL
1092 ixR = ix + lenL
1093+ {-# INLINE writeProduct #-}
1077
1094
1078instance OVERLAPPABLE_ (GToJSON Value arity a) => WriteProduct arity a where
1095instance OVERLAPPABLE_ (GToJSON Value arity a) => WriteProduct arity a where
1079 writeProduct opts targs mv ix _ =
1096 writeProduct opts targs mv ix _ =
1080 VM.unsafeWrite mv ix . gToJSON opts targs
1097 VM.unsafeWrite mv ix . gToJSON opts targs
1098+ {-# INLINE writeProduct #-}
1081
1099
1082--------------------------------------------------------------------------------
1100--------------------------------------------------------------------------------
1083
1101
@@ -1095,9 +1113,11 @@ instance ( EncodeProduct arity a
1095 encodeProduct opts targs (a :*: b) =
1113 encodeProduct opts targs (a :*: b) =
1096 encodeProduct opts targs a >*<
1114 encodeProduct opts targs a >*<
1097 encodeProduct opts targs b
1115 encodeProduct opts targs b
1116+ {-# INLINE encodeProduct #-}
1098
1117
1099instance OVERLAPPABLE_ (GToJSON Encoding arity a) => EncodeProduct arity a where
1118instance OVERLAPPABLE_ (GToJSON Encoding arity a) => EncodeProduct arity a where
1100 encodeProduct opts targs a = E.retagEncoding $ gToJSON opts targs a
1119 encodeProduct opts targs a = E.retagEncoding $ gToJSON opts targs a
1120+ {-# INLINE encodeProduct #-}
1101
1121
1102--------------------------------------------------------------------------------
1122--------------------------------------------------------------------------------
1103
1123