From 117803c8ec8c7201043b7c2471e10a4de1ede0ed Mon Sep 17 00:00:00 2001 From: Simon Jakobi Date: Tue, 12 Oct 2021 00:49:51 +0200 Subject: [PATCH] Support aeson-2.0 https://hackage.haskell.org/package/aeson-2.0.1.0/changelog Addresses #16. --- aeson-yaml.cabal | 2 +- src/Data/Aeson/Yaml.hs | 26 +++++++++++++++++++++----- test/Test/Data/Aeson/Yaml.hs | 17 +++++++++++++++-- 3 files changed, 37 insertions(+), 8 deletions(-) diff --git a/aeson-yaml.cabal b/aeson-yaml.cabal index 3b610dc..2254bbe 100644 --- a/aeson-yaml.cabal +++ b/aeson-yaml.cabal @@ -37,7 +37,7 @@ library exposed-modules: Data.Aeson.Yaml build-depends: - aeson >= 0.4.0.0 && < 1.6 + aeson >= 0.4.0.0 && < 2.1 , base >= 4.8.2.0 && < 5 , bytestring >= 0.10.4.0 && < 0.12 , text >= 0.1 && < 1.3 diff --git a/src/Data/Aeson/Yaml.hs b/src/Data/Aeson/Yaml.hs index c9070c3..83effca 100644 --- a/src/Data/Aeson/Yaml.hs +++ b/src/Data/Aeson/Yaml.hs @@ -8,6 +8,7 @@ libyaml. It is also licensed under the BSD3 license. This module is meant to be imported qualified. -} +{-# LANGUAGE CPP #-} {-# LANGUAGE OverloadedStrings #-} module Data.Aeson.Yaml @@ -25,14 +26,22 @@ import qualified Data.ByteString.Builder as ByteString.Builder import qualified Data.ByteString.Lazy as ByteString.Lazy import qualified Data.ByteString.Short as ByteString.Short import Data.Char (isAlpha, isDigit) -import qualified Data.HashMap.Strict as HashMap -import Data.List (intersperse, sortOn) +import Data.List (intersperse) import Data.Monoid ((<>), mconcat, mempty) import qualified Data.Text as Text import Data.Text (Text) import qualified Data.Text.Encoding as Text.Encoding import qualified Data.Vector as Vector +#if MIN_VERSION_aeson(2,0,0) +import qualified Data.Aeson.Key as Key +import qualified Data.Aeson.KeyMap as KeyMap +import Data.Bifunctor (first) +#else +import qualified Data.HashMap.Strict as HashMap +import Data.List (sortOn) +#endif + b :: ByteString -> Builder b = ByteString.Builder.byteString @@ -81,15 +90,15 @@ encodeQuotedDocuments vs = encodeBuilder :: Bool -> Bool -> Int -> Data.Aeson.Value -> Builder encodeBuilder alwaysQuote newlineBeforeObject level value = case value of - Object hm - | null hm -> bs "{}" + Object km + | null km -> bs "{}" | otherwise -> mconcat $ (if newlineBeforeObject then (prefix :) else id) $ intersperse prefix $ - map (keyValue level) (sortOn fst $ HashMap.toList hm) + map (keyValue level) (objectToAscList km) where prefix = bs "\n" <> indent level Array vec | null vec -> bs "[]" @@ -173,3 +182,10 @@ encodeLines level ls = case ls of (line:_) -> " " `Text.isPrefixOf` line _ -> False + +objectToAscList :: Object -> [(Text, Value)] +#if MIN_VERSION_aeson(2,0,0) +objectToAscList = map (first Key.toText) . KeyMap.toAscList +#else +objectToAscList = sortOn fst . HashMap.toList +#endif diff --git a/test/Test/Data/Aeson/Yaml.hs b/test/Test/Data/Aeson/Yaml.hs index 55c5fc4..768cd9c 100644 --- a/test/Test/Data/Aeson/Yaml.hs +++ b/test/Test/Data/Aeson/Yaml.hs @@ -1,3 +1,4 @@ +{-# LANGUAGE CPP #-} {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE RecordWildCards #-} {-# LANGUAGE QuasiQuotes #-} @@ -7,10 +8,15 @@ module Test.Data.Aeson.Yaml where import qualified Data.Aeson import qualified Data.ByteString.Lazy import Data.Either (fromRight) -import qualified Data.HashMap.Strict as HashMap import Data.String.QQ (s) import qualified Data.Yaml +#if MIN_VERSION_aeson(2,0,0) +import qualified Data.Aeson.KeyMap as KeyMap +#else +import qualified Data.HashMap.Strict as HashMap +#endif + -- import Hedgehog -- import Hedgehog.Gen -- import Hedgehog.Gen.JSON (genJSONValue, sensibleRanges) @@ -243,7 +249,14 @@ pipelines: } foo :: Data.Aeson.Value -foo = Data.Aeson.Object $ HashMap.fromList [("foo", "bar")] +foo = + Data.Aeson.Object $ +#if MIN_VERSION_aeson(2,0,0) + KeyMap.fromList +#else + HashMap.fromList +#endif + [("foo", "bar")] test_testCases :: TestTree test_testCases = testGroup "Test Cases" $ map mkTestCase testCases