Skip to content

Commit 82e33e0

Browse files
committed
Add toJSONKeyKey, revert type of ToJSONKeyText
1 parent 80ec09c commit 82e33e0

File tree

4 files changed

+17
-12
lines changed

4 files changed

+17
-12
lines changed

src/Data/Aeson/Types.hs

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ module Data.Aeson.Types
4848
, ToJSONKey(..)
4949
, ToJSONKeyFunction(..)
5050
, toJSONKeyText
51+
, toJSONKeyKey
5152
, contramapToJSONKeyFunction
5253
, FromJSONKey(..)
5354
, FromJSONKeyFunction(..)

src/Data/Aeson/Types/Class.hs

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ module Data.Aeson.Types.Class
5353
, ToJSONKey(..)
5454
, ToJSONKeyFunction(..)
5555
, toJSONKeyText
56+
, toJSONKeyKey
5657
, contramapToJSONKeyFunction
5758
, FromJSONKey(..)
5859
, FromJSONKeyFunction(..)

src/Data/Aeson/Types/FromJSON.hs

-4
Original file line numberDiff line numberDiff line change
@@ -2285,10 +2285,6 @@ instance FromJSON1 Proxy where
22852285
instance FromJSON (Proxy a) where
22862286
parseJSON _ = pure Proxy
22872287

2288-
fromNull :: String -> a -> Value -> Parser a
2289-
fromNull _ a Null = pure a
2290-
fromNull c _ v = prependContext c (typeMismatch "Null" v)
2291-
22922288
instance FromJSON2 Tagged where
22932289
liftParseJSON2 _ _ p _ = fmap Tagged . p
22942290

src/Data/Aeson/Types/ToJSON.hs

+15-8
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ module Data.Aeson.Types.ToJSON
3939
, ToJSONKey(..)
4040
, ToJSONKeyFunction(..)
4141
, toJSONKeyText
42+
, toJSONKeyKey
4243
, contramapToJSONKeyFunction
4344

4445
, GToJSONKey()
@@ -490,8 +491,14 @@ data ToJSONKeyFunction a
490491
-- where
491492
-- myKeyToText = Text.pack . show -- or showt from text-show
492493
-- @
493-
toJSONKeyText :: (a -> Key) -> ToJSONKeyFunction a
494-
toJSONKeyText f = ToJSONKeyText f (E.key . f)
494+
toJSONKeyText :: (a -> Text) -> ToJSONKeyFunction a
495+
toJSONKeyText f = toJSONKeyKey (Key.fromText . f)
496+
497+
-- |
498+
--
499+
-- @since 2.0.0.0
500+
toJSONKeyKey :: (a -> Key) -> ToJSONKeyFunction a
501+
toJSONKeyKey f = ToJSONKeyText f (E.key . f)
495502

496503
-- | TODO: should this be exported?
497504
toJSONKeyTextEnc :: (a -> Encoding' Key) -> ToJSONKeyFunction a
@@ -532,7 +539,7 @@ contramapToJSONKeyFunction h x = case x of
532539
-- @
533540
genericToJSONKey :: (Generic a, GToJSONKey (Rep a))
534541
=> JSONKeyOptions -> ToJSONKeyFunction a
535-
genericToJSONKey opts = toJSONKeyText (Key.fromString . keyModifier opts . getConName . from)
542+
genericToJSONKey opts = toJSONKeyKey (Key.fromString . keyModifier opts . getConName . from)
536543

537544
class GetConName f => GToJSONKey f
538545
instance GetConName f => GToJSONKey f
@@ -1448,23 +1455,23 @@ instance ToJSON Text where
14481455
toEncoding = E.text
14491456

14501457
instance ToJSONKey Text where
1451-
toJSONKey = toJSONKeyText Key.fromText
1458+
toJSONKey = toJSONKeyText id
14521459

14531460

14541461
instance ToJSON LT.Text where
14551462
toJSON = String . LT.toStrict
14561463
toEncoding = E.lazyText
14571464

14581465
instance ToJSONKey LT.Text where
1459-
toJSONKey = toJSONKeyText (Key.fromText . LT.toStrict)
1466+
toJSONKey = toJSONKeyText (LT.toStrict)
14601467

14611468

14621469
instance ToJSON Version where
14631470
toJSON = toJSON . showVersion
14641471
toEncoding = toEncoding . showVersion
14651472

14661473
instance ToJSONKey Version where
1467-
toJSONKey = toJSONKeyText (Key.fromString . showVersion)
1474+
toJSONKey = toJSONKeyKey (Key.fromString . showVersion)
14681475

14691476
-------------------------------------------------------------------------------
14701477
-- semigroups NonEmpty
@@ -2199,8 +2206,8 @@ instance (ToJSON a, ToJSON b, ToJSON c) => ToJSONKey (a,b,c)
21992206
instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d) => ToJSONKey (a,b,c,d)
22002207

22012208
instance ToJSONKey Char where
2202-
toJSONKey = ToJSONKeyText (Key.fromText . T.singleton) (E.key . Key.fromText . T.singleton)
2203-
toJSONKeyList = toJSONKeyText Key.fromString
2209+
toJSONKey = toJSONKeyText T.singleton
2210+
toJSONKeyList = toJSONKeyText T.pack
22042211

22052212
instance (ToJSONKey a, ToJSON a) => ToJSONKey [a] where
22062213
toJSONKey = toJSONKeyList

0 commit comments

Comments
 (0)