@@ -32,6 +32,7 @@ import qualified Control.Monad.State as S
32import Data.Aeson
32import Data.Aeson
33import Data.Aeson.Types (Parser)
33import Data.Aeson.Types (Parser)
34import Data.Constraint
34import Data.Constraint
35+import Data.List (intercalate)
35import Data.Maybe (catMaybes)
36import Data.Maybe (catMaybes)
36import Data.Proxy
37import Data.Proxy
37import qualified Data.Text as T
38import qualified Data.Text as T
@@ -42,11 +43,17 @@ import GHC.OverloadedLabels
42import GHC.Prim
43import GHC.Prim
43import GHC.ST (ST (..), runST)
44import GHC.ST (ST (..), runST)
44import GHC.TypeLits
45import GHC.TypeLits
45−import Generic.Data (gshowsPrec)
46−import Generic.Data.Internal.Show (GShow)
47import qualified Optics.Lens as OL
46import qualified Optics.Lens as OL
48import Unsafe.Coerce
47import Unsafe.Coerce
49import Prelude
48import Prelude
49+#if MIN_VERSION_aeson(2,0,0)
50+import Data.Aeson.Key (fromText, fromString)
54+fromString :: String -> T.Text
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)@
@@ -102,13 +109,12 @@ defaultJSONOptions = JSONOptions {fieldTransform = id}
102type role Rec representational
109type role Rec representational
105− ( RecApply lts lts Show,
112+ ( RecApply lts lts Show
106− GShow Proxy (Rep (Rec lts)),
109 Show (Rec lts)
114 Show (Rec lts)
111− show = (\x -> if null x then "{}" else x) . unwords . drop 1 . words . flip (gshowsPrec 0) ""
116+ show rec = "{" ++ formatted ++ "}"
117+ where formatted = intercalate ", " $ map (\(k, v) -> k ++ " = " ++ v) $ showRec rec
113instance RecEq lts lts => Eq (Rec lts) where
119instance RecEq lts lts => Eq (Rec lts) where
114 (==) (a :: Rec lts) (b :: Rec lts) = recEq a b (Proxy :: Proxy lts)
120 (==) (a :: Rec lts) (b :: Rec lts) = recEq a b (Proxy :: Proxy lts)
@@ -403,26 +409,20 @@ type family RecDeepTy (ps :: r) (lts :: [*]) :: * where
403-- NOTE: changed from original superrecord to not require a 'Sort'
409-- NOTE: changed from original superrecord to not require a 'Sort'
404combine ::
410combine ::
405 forall lhs rhs res.
411 forall lhs rhs res.
406− ( KnownNat (RecSize lhs),
407− KnownNat (RecSize rhs),
408− KnownNat (RecSize lhs + RecSize rhs),
409− res ~ RecAppend lhs rhs,
412+ res ~ RecAppend lhs rhs =>
410− RecCopy lhs lhs res,
413 Rec lhs ->
413 Rec lhs ->
414 Rec rhs ->
414 Rec rhs ->
416−combine lts rts =
416+combine (MkRec arr0#) (MkRec arr1#) =
417− let !(I# size#) =
417+ let !size0# = sizeofSmallArray# arr0#
418− fromIntegral $ natVal' (proxy# :: Proxy# (RecSize lhs + RecSize rhs))
418+ !size1# = sizeofSmallArray# arr1#
419 in runST' $
419 in runST' $
420 ST $ \s# ->
420 ST $ \s# ->
421− case newSmallArray# size# (error "No value") s# of
421+ case newSmallArray# (size0# +# size1#) (error "No value") s# of
422 (# s'#, arr# #) ->
422 (# s'#, arr# #) ->
423− case recCopyInto (Proxy :: Proxy lhs) lts (Proxy :: Proxy res) arr# s'# of
423+ case copySmallArray# arr1# 0# arr# 0# size1# s'# of
425− case recCopyInto (Proxy :: Proxy rhs) rts (Proxy :: Proxy res) arr# s''# of
425+ case copySmallArray# arr0# 0# arr# size1# size0# s''# of
427 case unsafeFreezeSmallArray# arr# s'''# of
427 case unsafeFreezeSmallArray# arr# s'''# of
428 (# s''''#, a# #) -> (# s''''#, MkRec a# #)
428 (# s''''#, a# #) -> (# s''''#, MkRec a# #)
@@ -492,13 +492,7 @@ insert (l := v) rts =
492-- | Alias for 'combine'
492-- | Alias for 'combine'
494 forall lhs rhs res.
494 forall lhs rhs res.
495− ( KnownNat (RecSize lhs),
496− KnownNat (RecSize rhs),
497− KnownNat (RecSize lhs + RecSize rhs),
498− res ~ RecAppend lhs rhs,
495+ res ~ RecAppend lhs rhs =>
499− RecCopy lhs lhs res,
502 Rec lhs ->
496 Rec lhs ->
503 Rec rhs ->
497 Rec rhs ->
@@ -572,16 +566,16 @@ class ToJSON a => EncodeField a where
573instance ToJSON a => EncodeField a where
567instance ToJSON a => EncodeField a where
574 encodeField = pure . toJSON
568 encodeField = pure . toJSON
575− encodeKV = (.=)
569+ encodeKV k v = fromText k .= v
577instance {-# OVERLAPS #-} ToJSON a => EncodeField (Maybe a) where
571instance {-# OVERLAPS #-} ToJSON a => EncodeField (Maybe a) where
578 encodeField = fmap toJSON
572 encodeField = fmap toJSON
579 encodeKV _ Nothing = mempty
573 encodeKV _ Nothing = mempty
580− encodeKV k v = k .= v
574+ encodeKV k v = fromText k .= v
582recToValue :: forall lts. (RecApply lts lts EncodeField) => JSONOptions -> Rec lts -> Value
576recToValue :: forall lts. (RecApply lts lts EncodeField) => JSONOptions -> Rec lts -> Value
583recToValue options r =
577recToValue options r =
584− object $ catMaybes $ reflectRec @EncodeField Proxy (\k v -> (T.pack (fieldTransform options k),) <$> encodeField v) r
578+ object $ catMaybes $ reflectRec @EncodeField Proxy (\k v -> (fromString (fieldTransform options k),) <$> encodeField v) r
586recToEncoding :: forall lts. (RecApply lts lts EncodeField) => JSONOptions -> Rec lts -> Encoding
580recToEncoding :: forall lts. (RecApply lts lts EncodeField) => JSONOptions -> Rec lts -> Encoding
587recToEncoding options r =
581recToEncoding options r =
@@ -686,10 +680,10 @@ class FromJSON a => ParseField a where
686 parseField :: Object -> T.Text -> Parser a
680 parseField :: Object -> T.Text -> Parser a
688instance FromJSON a => ParseField a where
682instance FromJSON a => ParseField a where
689− parseField = (.:)
683+ parseField o k = o .: fromText k
691instance {-# OVERLAPS #-} FromJSON a => ParseField (Maybe a) where
685instance {-# OVERLAPS #-} FromJSON a => ParseField (Maybe a) where
692− parseField = (.:?)
686+ parseField o k = o .:? fromText k
695 ( KnownSymbol l,
689 ( KnownSymbol l,