Skip to content

Commit

Permalink
Merge pull request #79 from metosin/strip-empties
Browse files Browse the repository at this point in the history
:strip-empties encoding option
  • Loading branch information
opqdonut authored Aug 9, 2024
2 parents dae5171 + e9bab81 commit b86a719
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
## Unreleased

* The `:strip-nils` option now doesn't strip empty values like `{}` or `""`.
Use the new `:strip-empties` option if you want the old behaviour.
Thanks to [@dominicfreeston](https://github.com/dominicfreeston)!
[#78](https://github.com/metosin/jsonista/pull/78),
[#79](https://github.com/metosin/jsonista/pull/79)

## 0.3.9 (2023-06-29)

* add `:do-not-fail-on-empty-beans` option [#75](https://github.com/metosin/jsonista/pull/75)
Expand Down
2 changes: 2 additions & 0 deletions src/clj/jsonista/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@
| `:pretty` | set to true use Jacksons pretty-printing defaults |
| `:escape-non-ascii` | set to true to escape non ascii characters |
| `:strip-nils` | remove any keys that have nil values |
| `:strip-empties` | remove any keys that have nil or empty (\"\", {}, [], etc) values |
| `:do-not-fail-on-empty-beans` | serialize objects with no accessors as empty objects instead of throwing an exception |
| `:date-format` | string for custom date formatting. default: `yyyy-MM-dd'T'HH:mm:ss'Z'` |
| `:encode-key-fn` | true to coerce keyword keys to strings, false to leave them as keywords, or a function to provide custom coercion (default: true) |
Expand Down Expand Up @@ -155,6 +156,7 @@
(:pretty options) (.enable SerializationFeature/INDENT_OUTPUT)
(:bigdecimals options) (.enable DeserializationFeature/USE_BIG_DECIMAL_FOR_FLOATS)
(:strip-nils options) (.setSerializationInclusion JsonInclude$Include/NON_NULL)
(:strip-empties options) (.setSerializationInclusion JsonInclude$Include/NON_EMPTY)
(:do-not-fail-on-empty-beans options) (.disable SerializationFeature/FAIL_ON_EMPTY_BEANS)
(:escape-non-ascii options) (doto (-> .getFactory (.enable JsonGenerator$Feature/ESCAPE_NON_ASCII)))))]
(doseq [module (:modules options)]
Expand Down
3 changes: 3 additions & 0 deletions test/jsonista/core_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@
(testing ":strip-nils doesn't strip other empties"
(let [data-with-nils {:hello "world" :goodbye nil :empty-string "" :empty-map {}}]
(is (= "{\"hello\":\"world\",\"empty-string\":\"\",\"empty-map\":{}}" (j/write-value-as-string data-with-nils (j/object-mapper {:strip-nils true}))))))
(testing ":strip-empties"
(let [data-with-nils {:hello "world" :goodbye nil :empty-string "" :empty-map {}}]
(is (= "{\"hello\":\"world\"}" (j/write-value-as-string data-with-nils (j/object-mapper {:strip-empties true}))))))
(testing ":escape-non-ascii"
(is (= "{\"imperial-money\":\"\\u00A3\"}" (j/write-value-as-string {:imperial-money "£"} (j/object-mapper {:escape-non-ascii true})))))
(testing ":date-format"
Expand Down

0 comments on commit b86a719

Please sign in to comment.