Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

:strip-empties encoding option #79

Merged
merged 2 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading