Skip to content

Commit 84b6973

Browse files
authored
Merge pull request #482 from haskell/mpj/fix-aeson-1
Fix compatibility with aeson 1
2 parents 9074106 + d76edf9 commit 84b6973

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

lsp-types/src/Data/Row/Aeson.hs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ This is crucial to match what LSP clients expect.
1919
module Data.Row.Aeson where
2020

2121
import Data.Aeson
22-
import Data.Aeson.KeyMap (singleton)
2322
import Data.Aeson.Types (Parser, typeMismatch)
2423
import Data.List (intercalate)
2524

@@ -39,25 +38,27 @@ import Data.String
3938
-- | Serialise a value as an entry in a JSON object. This allows customizing the
4039
-- behaviour in the object context, in order to e.g. omit the field.
4140
class ToJSONEntry a where
42-
toJSONEntry :: Key -> a -> Object
41+
-- We use String so we can use fromString on it to get a key that works
42+
-- in both aeson-1 and aeson-2
43+
toJSONEntry :: String -> a -> Object
4344

4445
instance {-# OVERLAPPING #-} ToJSON a => ToJSONEntry (Maybe a) where
4546
-- Omit Nothing fields
4647
toJSONEntry _ Nothing = mempty
47-
toJSONEntry k v = singleton k (toJSON v)
48+
toJSONEntry k v = fromString k .= toJSON v
4849

4950
instance {-# OVERLAPPABLE #-} ToJSON a => ToJSONEntry a where
50-
toJSONEntry k v = singleton k (toJSON v)
51+
toJSONEntry k v = fromString k .= toJSON v
5152

5253
class FromJSONEntry a where
53-
parseJSONEntry :: Object -> Key -> Parser a
54+
parseJSONEntry :: Object -> String -> Parser a
5455

5556
instance {-# OVERLAPPING #-} FromJSON a => FromJSONEntry (Maybe a) where
5657
-- Parse Nothing fields as optional
57-
parseJSONEntry o k = o .:? k
58+
parseJSONEntry o k = o .:? (fromString k)
5859

5960
instance {-# OVERLAPPABLE #-} FromJSON a => FromJSONEntry a where
60-
parseJSONEntry o k = o .: k
61+
parseJSONEntry o k = o .: (fromString k)
6162

6263
------
6364

lsp-types/src/Language/LSP/Protocol/Types/Common.hs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import Control.Lens
2727
import Data.Aeson hiding (Null)
2828
import qualified Data.Aeson as J
2929
import Data.Hashable
30+
import Data.String (fromString)
3031
import Data.Int (Int32)
3132
import Data.Mod.Word
3233
import GHC.Generics hiding (UInt)
@@ -122,8 +123,10 @@ nullToMaybe :: a |? Null -> Maybe a
122123
nullToMaybe (InL a) = Just a
123124
nullToMaybe (InR _) = Nothing
124125

126+
-- We use String so we can use fromString on it to get a key that works
127+
-- in both aeson-1 and aeson-2
125128
-- | Include a value in an JSON object optionally, omitting it if it is 'Nothing'.
126-
(.=?) :: (J.KeyValue kv, J.ToJSON v) => J.Key -> Maybe v -> [kv]
129+
(.=?) :: (J.KeyValue kv, J.ToJSON v) => String -> Maybe v -> [kv]
127130
k .=? v = case v of
128-
Just v' -> [k J..= v']
131+
Just v' -> [fromString k J..= v']
129132
Nothing -> mempty

0 commit comments

Comments
 (0)