From cc408da47ec53db05e86de4e451e96c919a2177a Mon Sep 17 00:00:00 2001 From: Joel Kaasinen Date: Fri, 9 Aug 2024 09:12:08 +0300 Subject: [PATCH 1/2] feat: :strip-empties encoding option --- src/clj/jsonista/core.clj | 2 ++ test/jsonista/core_test.clj | 3 +++ 2 files changed, 5 insertions(+) diff --git a/src/clj/jsonista/core.clj b/src/clj/jsonista/core.clj index ed5cfd6..9afff1b 100644 --- a/src/clj/jsonista/core.clj +++ b/src/clj/jsonista/core.clj @@ -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) | @@ -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)] diff --git a/test/jsonista/core_test.clj b/test/jsonista/core_test.clj index 5ac68a7..9d2d6f5 100644 --- a/test/jsonista/core_test.clj +++ b/test/jsonista/core_test.clj @@ -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" From e9bab817fb6098afe9d02fff9dacc59163d15c50 Mon Sep 17 00:00:00 2001 From: Joel Kaasinen Date: Fri, 9 Aug 2024 09:16:39 +0300 Subject: [PATCH 2/2] doc: update CHANGELOG.md --- CHANGELOG.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 92cd056..c0ec4af 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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)