Skip to content

Commit

Permalink
Merge pull request #71 from lehins/main
Browse files Browse the repository at this point in the history
Fix aeson compatibility and update upper bounds
  • Loading branch information
newhoggy committed Dec 18, 2023
2 parents 4099c04 + 95b26c0 commit a7ec159
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
10 changes: 5 additions & 5 deletions hw-aeson.cabal
Expand Up @@ -22,13 +22,13 @@ source-repository head

common base { build-depends: base >= 4.11 && < 5 }

common aeson { build-depends: aeson >= 1.4 && < 2.2 }
common bytestring { build-depends: bytestring >= 0.10 && < 0.12 }
common containers { build-depends: containers >= 0.6 && < 0.7 }
common doctest { build-depends: doctest >= 0.16.2 && < 0.21 }
common aeson { build-depends: aeson >= 1.4 && < 2.3 }
common bytestring { build-depends: bytestring >= 0.10 && < 0.13 }
common containers { build-depends: containers >= 0.6 && < 0.8 }
common doctest { build-depends: doctest >= 0.16.2 && < 0.23 }
common doctest-discover { build-depends: doctest-discover >= 0.2 && < 0.3 }
common hashable { build-depends: hashable >= 1.3 && < 1.5 }
common hedgehog { build-depends: hedgehog >= 0.6 && < 1.3 }
common hedgehog { build-depends: hedgehog >= 0.6 && < 1.5 }
common hspec { build-depends: hspec >= 2.4 && < 3 }
common text { build-depends: text >= 1.2 && < 3 }
common text-short { build-depends: text-short >= 0.1.3 && < 0.2 }
Expand Down
26 changes: 24 additions & 2 deletions src/HaskellWorks/Data/Aeson.hs
@@ -1,5 +1,10 @@
{-# LANGUAGE CPP #-}
{-# LANGUAGE DerivingVia #-}

#if MIN_VERSION_aeson(2,2,0)
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE UndecidableInstances #-}
#endif
module HaskellWorks.Data.Aeson
( JsonEndo(..)
, WithJsonKeyValues(..)
Expand Down Expand Up @@ -30,8 +35,13 @@ newtype JsonEndo a = JsonEndo
}
deriving (Semigroup, Monoid) via (Endo [a])

#if MIN_VERSION_aeson(2,2,0)
instance (ToJSON e, KeyValue e a) => KeyValue e (JsonEndo a) where
explicitToField f k v = JsonEndo (k .= f v :)
#else
instance KeyValue a => KeyValue (JsonEndo a) where
k .= v = JsonEndo (k .= v:)
#endif
k .= v = JsonEndo (k .= v :)

objectWithoutNulls :: [Pair] -> Value
objectWithoutNulls = object . Prelude.filter (not . isNull . snd)
Expand All @@ -45,13 +55,21 @@ readJson t s = case readMaybe s of
Nothing -> fail $ "Could not parse " <> t

-- | Render optional fields as missing in JSON output.
#if MIN_VERSION_aeson(2,2,0)
(.?=) :: (KeyValue e p, ToJSON v, Monoid p) => Key -> Maybe v -> p
#else
(.?=) :: (KeyValue p, ToJSON v, Monoid p) => Key -> Maybe v -> p
#endif
(.?=) k mv = case mv of
Just v -> k .= v
Nothing -> mempty

-- | Same as '.=', but with lower precedence to work well with lens.
#if MIN_VERSION_aeson(2,2,0)
(.!=) :: (KeyValue e kv, ToJSON v) => Key -> v -> kv
#else
(.!=) :: (KeyValue kv, ToJSON v) => Key -> v -> kv
#endif
(.!=) = (.=)

-- | Same as 'object' except used in combination with '.?=' and '.!=' instead of '.='.
Expand Down Expand Up @@ -81,7 +99,11 @@ objectEndo es = object $ unJsonEndo (mconcat es) []
-- ]
-- @
class ToJsonKeyValues a where
#if MIN_VERSION_aeson(2,2,0)
toJsonKeyValues :: (KeyValue e kv, Monoid kv) => a -> [kv]
#else
toJsonKeyValues :: (KeyValue kv, Monoid kv) => a -> [kv]
#endif

-- | For use with language extension DerivingVia. This derivation provides
-- a ToJSON instance that delegates to the ToJsonKeyValues instance.
Expand Down

0 comments on commit a7ec159

Please sign in to comment.