Skip to content

Commit

Permalink
Merge pull request #875 from haskell/toJSONKeyText
Browse files Browse the repository at this point in the history
Add toJSONKeyKey, revert type of ToJSONKeyText
  • Loading branch information
phadej authored Oct 9, 2021
2 parents 80ec09c + 82e33e0 commit 4ad6b40
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 12 deletions.
1 change: 1 addition & 0 deletions src/Data/Aeson/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ module Data.Aeson.Types
, ToJSONKey(..)
, ToJSONKeyFunction(..)
, toJSONKeyText
, toJSONKeyKey
, contramapToJSONKeyFunction
, FromJSONKey(..)
, FromJSONKeyFunction(..)
Expand Down
1 change: 1 addition & 0 deletions src/Data/Aeson/Types/Class.hs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ module Data.Aeson.Types.Class
, ToJSONKey(..)
, ToJSONKeyFunction(..)
, toJSONKeyText
, toJSONKeyKey
, contramapToJSONKeyFunction
, FromJSONKey(..)
, FromJSONKeyFunction(..)
Expand Down
4 changes: 0 additions & 4 deletions src/Data/Aeson/Types/FromJSON.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2285,10 +2285,6 @@ instance FromJSON1 Proxy where
instance FromJSON (Proxy a) where
parseJSON _ = pure Proxy

fromNull :: String -> a -> Value -> Parser a
fromNull _ a Null = pure a
fromNull c _ v = prependContext c (typeMismatch "Null" v)

instance FromJSON2 Tagged where
liftParseJSON2 _ _ p _ = fmap Tagged . p

Expand Down
23 changes: 15 additions & 8 deletions src/Data/Aeson/Types/ToJSON.hs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ module Data.Aeson.Types.ToJSON
, ToJSONKey(..)
, ToJSONKeyFunction(..)
, toJSONKeyText
, toJSONKeyKey
, contramapToJSONKeyFunction

, GToJSONKey()
Expand Down Expand Up @@ -490,8 +491,14 @@ data ToJSONKeyFunction a
-- where
-- myKeyToText = Text.pack . show -- or showt from text-show
-- @
toJSONKeyText :: (a -> Key) -> ToJSONKeyFunction a
toJSONKeyText f = ToJSONKeyText f (E.key . f)
toJSONKeyText :: (a -> Text) -> ToJSONKeyFunction a
toJSONKeyText f = toJSONKeyKey (Key.fromText . f)

-- |
--
-- @since 2.0.0.0
toJSONKeyKey :: (a -> Key) -> ToJSONKeyFunction a
toJSONKeyKey f = ToJSONKeyText f (E.key . f)

-- | TODO: should this be exported?
toJSONKeyTextEnc :: (a -> Encoding' Key) -> ToJSONKeyFunction a
Expand Down Expand Up @@ -532,7 +539,7 @@ contramapToJSONKeyFunction h x = case x of
-- @
genericToJSONKey :: (Generic a, GToJSONKey (Rep a))
=> JSONKeyOptions -> ToJSONKeyFunction a
genericToJSONKey opts = toJSONKeyText (Key.fromString . keyModifier opts . getConName . from)
genericToJSONKey opts = toJSONKeyKey (Key.fromString . keyModifier opts . getConName . from)

class GetConName f => GToJSONKey f
instance GetConName f => GToJSONKey f
Expand Down Expand Up @@ -1448,23 +1455,23 @@ instance ToJSON Text where
toEncoding = E.text

instance ToJSONKey Text where
toJSONKey = toJSONKeyText Key.fromText
toJSONKey = toJSONKeyText id


instance ToJSON LT.Text where
toJSON = String . LT.toStrict
toEncoding = E.lazyText

instance ToJSONKey LT.Text where
toJSONKey = toJSONKeyText (Key.fromText . LT.toStrict)
toJSONKey = toJSONKeyText (LT.toStrict)


instance ToJSON Version where
toJSON = toJSON . showVersion
toEncoding = toEncoding . showVersion

instance ToJSONKey Version where
toJSONKey = toJSONKeyText (Key.fromString . showVersion)
toJSONKey = toJSONKeyKey (Key.fromString . showVersion)

-------------------------------------------------------------------------------
-- semigroups NonEmpty
Expand Down Expand Up @@ -2199,8 +2206,8 @@ instance (ToJSON a, ToJSON b, ToJSON c) => ToJSONKey (a,b,c)
instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d) => ToJSONKey (a,b,c,d)

instance ToJSONKey Char where
toJSONKey = ToJSONKeyText (Key.fromText . T.singleton) (E.key . Key.fromText . T.singleton)
toJSONKeyList = toJSONKeyText Key.fromString
toJSONKey = toJSONKeyText T.singleton
toJSONKeyList = toJSONKeyText T.pack

instance (ToJSONKey a, ToJSON a) => ToJSONKey [a] where
toJSONKey = toJSONKeyList
Expand Down

0 comments on commit 4ad6b40

Please sign in to comment.