Skip to content

Commit f0190da

Browse files
committed
Provide Key as a type synonym, not abstract
1 parent 650e59d commit f0190da

File tree

4 files changed

+11
-63
lines changed

4 files changed

+11
-63
lines changed

src/Data/Aeson/Key.hs

Lines changed: 11 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
{-# LANGUAGE DeriveDataTypeable #-}
33
{-# LANGUAGE GADTs #-}
44
{-# LANGUAGE TemplateHaskellQuotes #-}
5+
{-# LANGUAGE PatternSynonyms #-}
56

67
module Data.Aeson.Key (
78
Key,
@@ -12,25 +13,22 @@ module Data.Aeson.Key (
1213
coercionToText,
1314
) where
1415

15-
import Prelude (Eq, Ord, (.), Show (..), String, Maybe (..))
16+
import Prelude ((.), String, Maybe (..), id)
1617

17-
import Control.Applicative ((<$>))
18-
import Control.DeepSeq (NFData(..))
19-
import Data.Data (Data)
20-
import Data.Hashable (Hashable(..))
21-
import Data.Monoid (Monoid(mempty, mappend))
22-
import Data.Semigroup (Semigroup((<>)))
2318
import Data.Text (Text)
2419
import Data.Type.Coercion (Coercion (..))
25-
import Data.Typeable (Typeable)
26-
import Text.Read (Read (..))
2720

28-
import qualified Data.String
2921
import qualified Data.Text as T
3022
import qualified Language.Haskell.TH.Syntax as TH
3123

32-
newtype Key = Key { unKey :: Text }
33-
deriving (Eq, Ord, Typeable, Data)
24+
type Key = Text
25+
26+
unKey :: Key -> Text
27+
unKey = id
28+
29+
pattern Key :: p -> p
30+
pattern Key a <- a
31+
where Key a = a
3432

3533
fromString :: String -> Key
3634
fromString = Key . T.pack
@@ -58,37 +56,4 @@ coercionToText = Just Coercion
5856
-- instances
5957
-------------------------------------------------------------------------------
6058

61-
instance Read Key where
62-
readPrec = fromString <$> readPrec
63-
64-
instance Show Key where
65-
showsPrec d (Key k) = showsPrec d k
66-
67-
instance Data.String.IsString Key where
68-
fromString = fromString
69-
70-
instance Hashable Key where
71-
hashWithSalt salt (Key k) = hashWithSalt salt k
72-
73-
instance NFData Key where
74-
rnf (Key k) = rnf k
75-
76-
instance Semigroup Key where
77-
Key x <> Key y = Key (x <> y)
78-
79-
instance Monoid Key where
80-
mempty = Key mempty
81-
mappend = (<>)
82-
83-
instance TH.Lift Key where
84-
#if MIN_VERSION_text(1,2,4)
85-
lift (Key k) = [| Key k |]
86-
#else
87-
lift k = [| fromString k' |] where k' = toString k
88-
#endif
89-
90-
#if MIN_VERSION_template_haskell(2,17,0)
91-
liftTyped = TH.unsafeCodeCoerce . TH.lift
92-
#elif MIN_VERSION_template_haskell(2,16,0)
93-
liftTyped = TH.unsafeTExpCoerce . TH.lift
94-
#endif
59+
-- No instances for type synonym

src/Data/Aeson/Types/FromJSON.hs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1966,13 +1966,6 @@ instance (FromJSON v, FromJSONKey k, Eq k, Hashable k) => FromJSON (H.HashMap k
19661966
-- aeson
19671967
-------------------------------------------------------------------------------
19681968

1969-
instance FromJSON Key where
1970-
parseJSON = withText "Key" (pure . Key.fromText)
1971-
1972-
instance FromJSONKey Key where
1973-
-- TODO: make me more efficient.
1974-
fromJSONKey = FromJSONKeyText Key.fromText
1975-
19761969
instance FromJSON Value where
19771970
parseJSON = pure
19781971

src/Data/Aeson/Types/ToJSON.hs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1763,13 +1763,6 @@ instance (ToJSON v, ToJSONKey k) => ToJSON (H.HashMap k v) where
17631763
-- aeson
17641764
-------------------------------------------------------------------------------
17651765

1766-
instance ToJSON Key where
1767-
toJSON = toJSON . Key.toText
1768-
toEncoding = E.key
1769-
1770-
instance ToJSONKey Key where
1771-
toJSONKey = ToJSONKeyText id E.key
1772-
17731766
instance ToJSON Value where
17741767
toJSON a = a
17751768
toEncoding = E.value

tests/Instances.hs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,6 @@ instance (ApproxEq a) => ApproxEq [a] where
169169
instance Arbitrary a => Arbitrary (DList.DList a) where
170170
arbitrary = DList.fromList <$> arbitrary
171171

172-
instance Arbitrary Key where
173-
arbitrary = Key.fromText <$> arbitrary
174-
175172
instance Arbitrary Value where
176173
arbitrary = sized arb where
177174
arb :: Int -> Gen Value

0 commit comments

Comments
 (0)