Skip to content

Commit

Permalink
Support aeson-2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
sjakobi committed Oct 11, 2021
1 parent 4c89b1a commit 117803c
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 8 deletions.
2 changes: 1 addition & 1 deletion aeson-yaml.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 21 additions & 5 deletions src/Data/Aeson/Yaml.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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 "[]"
Expand Down Expand Up @@ -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
17 changes: 15 additions & 2 deletions test/Test/Data/Aeson/Yaml.hs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{-# LANGUAGE CPP #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE QuasiQuotes #-}
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 117803c

Please sign in to comment.