on master help sign in
Restore support for older Aeson (1.x)
ce478a2Sridhar Ratnakumar
CHANGELOG.md+4
@@ -1,5 +1,9 @@
1# Revision history for rec
1# Revision history for rec
2
2
3+## Unreleased
4+
5+- GHC 9.2 + Aeson 2.0 support
6+
3## 0.1.0.0 -- YYYY-mm-dd
7## 0.1.0.0 -- YYYY-mm-dd
4
8
5* First version. Released on an unsuspecting world.
9* First version. Released on an unsuspecting world.
src/JRec/Internal.hs+13−6
@@ -46,7 +46,14 @@ import GHC.TypeLits
46import qualified Optics.Lens as OL
46import qualified Optics.Lens as OL
47import Unsafe.Coerce
47import Unsafe.Coerce
48import Prelude
48import Prelude
49+#if MIN_VERSION_aeson(2,0,0)
49−import qualified Data.Aeson.Key as Key
50+import Data.Aeson.Key (fromText, fromString)
51+#else
52+fromText :: a -> a
53+fromText = id
54+fromString :: String -> T.Text
55+fromString = T.pack
56+#endif
50
57
51-- | Field named @l@ labels value of type @t@ adapted from the awesome /labels/ package.
58-- | Field named @l@ labels value of type @t@ adapted from the awesome /labels/ package.
52-- Example: @(#name := \"Chris\") :: (\"name\" := String)@
59-- Example: @(#name := \"Chris\") :: (\"name\" := String)@
@@ -559,16 +566,16 @@ class ToJSON a => EncodeField a where
559
566
560instance ToJSON a => EncodeField a where
567instance ToJSON a => EncodeField a where
561 encodeField = pure . toJSON
568 encodeField = pure . toJSON
562− encodeKV k v = Key.fromText k .= v
569+ encodeKV k v = fromText k .= v
563
570
564instance {-# OVERLAPS #-} ToJSON a => EncodeField (Maybe a) where
571instance {-# OVERLAPS #-} ToJSON a => EncodeField (Maybe a) where
565 encodeField = fmap toJSON
572 encodeField = fmap toJSON
566 encodeKV _ Nothing = mempty
573 encodeKV _ Nothing = mempty
567− encodeKV k v = Key.fromText k .= v
574+ encodeKV k v = fromText k .= v
568
575
569recToValue :: forall lts. (RecApply lts lts EncodeField) => JSONOptions -> Rec lts -> Value
576recToValue :: forall lts. (RecApply lts lts EncodeField) => JSONOptions -> Rec lts -> Value
570recToValue options r =
577recToValue options r =
571− object $ catMaybes $ reflectRec @EncodeField Proxy (\k v -> (Key.fromString (fieldTransform options k),) <$> encodeField v) r
578+ object $ catMaybes $ reflectRec @EncodeField Proxy (\k v -> (fromString (fieldTransform options k),) <$> encodeField v) r
572
579
573recToEncoding :: forall lts. (RecApply lts lts EncodeField) => JSONOptions -> Rec lts -> Encoding
580recToEncoding :: forall lts. (RecApply lts lts EncodeField) => JSONOptions -> Rec lts -> Encoding
574recToEncoding options r =
581recToEncoding options r =
@@ -673,10 +680,10 @@ class FromJSON a => ParseField a where
673 parseField :: Object -> T.Text -> Parser a
680 parseField :: Object -> T.Text -> Parser a
674
681
675instance FromJSON a => ParseField a where
682instance FromJSON a => ParseField a where
676− parseField o k = o .: Key.fromText k
683+ parseField o k = o .: fromText k
677
684
678instance {-# OVERLAPS #-} FromJSON a => ParseField (Maybe a) where
685instance {-# OVERLAPS #-} FromJSON a => ParseField (Maybe a) where
679− parseField o k = o .:? Key.fromText k
686+ parseField o k = o .:? fromText k
680
687
681instance
688instance
682 ( KnownSymbol l,
689 ( KnownSymbol l,