on master help sign in
Restore support for older Aeson (1.x)
commitce478a27211696bd07779898a59a1f8259b42075 authorSridhar Ratnakumar <srid@srid.ca> dateSun 12 Mar 2023, 20:18 (4 years ago) parent407b588
2 files changed+17−6
CHANGELOG.md+4 src/JRec/Internal.hs+13−6
CHANGELOG.md+4
@@ -1,5 +1,9 @@
11# Revision history for rec
22
3+## Unreleased
4+
5+- GHC 9.2 + Aeson 2.0 support
6+
37## 0.1.0.0 -- YYYY-mm-dd
48
59* First version. Released on an unsuspecting world.
src/JRec/Internal.hs+13−6
@@ -46,7 +46,14 @@ import GHC.TypeLits
4646import qualified Optics.Lens as OL
4747import Unsafe.Coerce
4848import Prelude
49−import qualified Data.Aeson.Key as Key
49+#if MIN_VERSION_aeson(2,0,0)
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
5057
5158-- | Field named @l@ labels value of type @t@ adapted from the awesome /labels/ package.
5259-- Example: @(#name := \"Chris\") :: (\"name\" := String)@
@@ -559,16 +566,16 @@ class ToJSON a => EncodeField a where
559566
560567instance ToJSON a => EncodeField a where
561568 encodeField = pure . toJSON
562− encodeKV k v = Key.fromText k .= v
569+ encodeKV k v = fromText k .= v
563570
564571instance {-# OVERLAPS #-} ToJSON a => EncodeField (Maybe a) where
565572 encodeField = fmap toJSON
566573 encodeKV _ Nothing = mempty
567− encodeKV k v = Key.fromText k .= v
574+ encodeKV k v = fromText k .= v
568575
569576recToValue :: forall lts. (RecApply lts lts EncodeField) => JSONOptions -> Rec lts -> Value
570577recToValue 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
572579
573580recToEncoding :: forall lts. (RecApply lts lts EncodeField) => JSONOptions -> Rec lts -> Encoding
574581recToEncoding options r =
@@ -673,10 +680,10 @@ class FromJSON a => ParseField a where
673680 parseField :: Object -> T.Text -> Parser a
674681
675682instance FromJSON a => ParseField a where
676− parseField o k = o .: Key.fromText k
683+ parseField o k = o .: fromText k
677684
678685instance {-# OVERLAPS #-} FromJSON a => ParseField (Maybe a) where
679− parseField o k = o .:? Key.fromText k
686+ parseField o k = o .:? fromText k
680687
681688instance
682689 ( KnownSymbol l,