Skip to content

Commit

Permalink
Encode empty objects as {} and bump version to 1.0.5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickmn committed Nov 30, 2019
1 parent d9b6b10 commit 2b4abb1
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 18 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## 1.0.5.0 - 2019-11-30

### Fixed

- Encode empty objects as "{}" rather than ""

## 1.0.4.0 - 2019-11-06

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion aeson-yaml.cabal
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
cabal-version: 1.12

name: aeson-yaml
version: 1.0.4.0
version: 1.0.5.0
homepage: https://github.com/clovyr/aeson-yaml
bug-reports: https://github.com/clovyr/aeson-yaml/issues
author: Patrick Nielsen
Expand Down
37 changes: 20 additions & 17 deletions src/Data/Aeson/Yaml.hs
Original file line number Diff line number Diff line change
Expand Up @@ -82,22 +82,23 @@ encodeQuotedDocuments vs =
encodeBuilder :: Bool -> Bool -> Int -> Data.Aeson.Value -> Builder
encodeBuilder alwaysQuote newlineBeforeObject level value =
case value of
Object hm ->
mconcat $
(if newlineBeforeObject
then (prefix :)
else id) $
intersperse prefix $ map (keyValue level) (sortOn fst $ HashMap.toList hm)
Object hm
| null hm -> bs "{}"
| otherwise ->
mconcat $
(if newlineBeforeObject
then (prefix :)
else id) $
intersperse prefix $
map (keyValue level) (sortOn fst $ HashMap.toList hm)
where prefix = bs "\n" <> indent level
Array vec ->
if Vector.null vec
then bs "[]"
else mconcat $
(prefix :) $
intersperse prefix $
map
(encodeBuilder alwaysQuote False (level + 1))
(Vector.toList vec)
Array vec
| null vec -> bs "[]"
| otherwise ->
mconcat $
(prefix :) $
intersperse prefix $
map (encodeBuilder alwaysQuote False (level + 1)) (Vector.toList vec)
where prefix = bs "\n" <> indent level <> bs "- "
String s -> encodeText True alwaysQuote level s
Number n -> bl (Data.Aeson.encode n)
Expand All @@ -109,8 +110,10 @@ encodeBuilder alwaysQuote newlineBeforeObject level value =
[ encodeText False alwaysQuote level k
, ":"
, case v of
Object _ -> ""
Array xs | not (null xs) -> ""
Object hm
| not (null hm) -> ""
Array vec
| not (null vec) -> ""
_ -> " "
, encodeBuilder alwaysQuote True (level' + 1) v
]
Expand Down
2 changes: 2 additions & 0 deletions test/Test/Data/Aeson/Yaml.hs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ tcDataTypes =
[s|
{
"nullValue": null,
"emptyObject": {},
"isTrue": true,
"isFalse": false,
"numberString": "12345",
Expand All @@ -68,6 +69,7 @@ tcDataTypes =
[s|asteriskString: "*"
boolString: "true"
dateString: "2038-01-19"
emptyObject: {}
expString: "1e3"
isFalse: false
isTrue: true
Expand Down

0 comments on commit 2b4abb1

Please sign in to comment.