metosin / jsonista

Clojure library for fast JSON encoding and decoding.
https://cljdoc.org/d/metosin/jsonista
Eclipse Public License 2.0
422 stars 30 forks source link

Support String Deserialization for explicit null values #74

Closed VigneshwaranJheyaraman closed 5 months ago

VigneshwaranJheyaraman commented 1 year ago

Context

(def parsed-json (jsonista.core/read-value json-string)) => dev/parsed-json

;; Instead of {"user-name" nil}, which we desire We get {"user-name" "null"} ;; This is because, the Object deserialization for String doesn't handle this explicit scenario. Thus this fix

;; After the current fix, we will get the output as {"user-name" nil}



 ## Fixes done
* Created a `StringDeserializer` that handles the _explicit_ `null` scneario
* Added _deserializer_ to `SimpleModule` for `String`
* Added some test cases for verify the same

 ## Unit testing
* All test cases passes :100:
<img width="802" alt="Screenshot 2023-10-28 at 2 32 26 AM" src="https://github.com/metosin/jsonista/assets/42887658/56c9a7c8-1d60-4170-a416-415d8ac4db9c">
ikitommi commented 5 months ago

Thanks, but I don't think this is correct. "null" is a valid string, so it should not be mapped to nil.

(require '[jsonista.core :as j])

(j/read-value "{\"user-name\": \"null\"}")
; => {"user-name" "null"}

(j/read-value "{\"user-name\": null}")
; => {"user-name" nil}