@@ -19,7 +19,6 @@ This is crucial to match what LSP clients expect.
19
19
module Data.Row.Aeson where
20
20
21
21
import Data.Aeson
22
- import Data.Aeson.KeyMap (singleton )
23
22
import Data.Aeson.Types (Parser , typeMismatch )
24
23
import Data.List (intercalate )
25
24
@@ -39,25 +38,27 @@ import Data.String
39
38
-- | Serialise a value as an entry in a JSON object. This allows customizing the
40
39
-- behaviour in the object context, in order to e.g. omit the field.
41
40
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
43
44
44
45
instance {-# OVERLAPPING #-} ToJSON a => ToJSONEntry (Maybe a ) where
45
46
-- Omit Nothing fields
46
47
toJSONEntry _ Nothing = mempty
47
- toJSONEntry k v = singleton k ( toJSON v)
48
+ toJSONEntry k v = fromString k .= toJSON v
48
49
49
50
instance {-# OVERLAPPABLE #-} ToJSON a => ToJSONEntry a where
50
- toJSONEntry k v = singleton k ( toJSON v)
51
+ toJSONEntry k v = fromString k .= toJSON v
51
52
52
53
class FromJSONEntry a where
53
- parseJSONEntry :: Object -> Key -> Parser a
54
+ parseJSONEntry :: Object -> String -> Parser a
54
55
55
56
instance {-# OVERLAPPING #-} FromJSON a => FromJSONEntry (Maybe a ) where
56
57
-- Parse Nothing fields as optional
57
- parseJSONEntry o k = o .:? k
58
+ parseJSONEntry o k = o .:? (fromString k)
58
59
59
60
instance {-# OVERLAPPABLE #-} FromJSON a => FromJSONEntry a where
60
- parseJSONEntry o k = o .: k
61
+ parseJSONEntry o k = o .: (fromString k)
61
62
62
63
------
63
64
0 commit comments