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

read-value fails on empty string #31

Open fmnoise opened 4 years ago

fmnoise commented 4 years ago
(j/read-value "")
; => Exception: com.fasterxml.jackson.databind.exc.MismatchedInputException: No content to map due to end-of-input

Is that intentional? Cheshire returns nil for such case. Maybe that can be tuned with object-mapper? 🤔

fmnoise commented 4 years ago

Ahh, I see, empty string is not valid JSON, so probably jsonista implementation is more correct than cheshire. Good way to see if your project code if JSON-compliant though 🤦

ikitommi commented 4 years ago

Yes, it's a property of Jackson. There are lot of features that can be set, but with a quick test, at least this doesn't work:

(def allow-nil-object-mapper 
  (doto (j/object-mapper) 
    (.enable com.fasterxml.jackson.databind.DeserializationFeature/ACCEPT_EMPTY_STRING_AS_NULL_OBJECT)))

(j/read-value "" allow-nil-object-mapper)
; Execution error (MismatchedInputException) at com.fasterxml.jackson.databind.exc.MismatchedInputException/from (MismatchedInputException.java:59).
; No content to map due to end-of-input
;  at [Source: (String)""; line: 1, column: 0]

Would also like to know if there is existing thing for this in Jackson or would it require custom Java/Clojure code in jsonista.

KGOH commented 4 months ago

@ikitommi also relevant for me

ikitommi commented 4 months ago

PR welcome to add a optional option to enable this, e.g. :empty-string-as-nil. Let's not change the default, as it should not work. From JS:

JSON.parse("")
// VM154:1 Uncaught SyntaxError: Unexpected end of JSON input
//    at JSON.parse (<anonymous>)
//   at <anonymous>:1:6
KGOH commented 4 months ago

@ikitommi agree, however, as it is pointed out earlier, the option to disable this doesn't work https://github.com/metosin/jsonista/issues/31#issuecomment-630192885

(jsonista.core/read-value
  ""
  (doto (jsonista.core/object-mapper)
    (.enable com.fasterxml.jackson.databind.DeserializationFeature/ACCEPT_EMPTY_STRING_AS_NULL_OBJECT)))
;; MismatchedInputException: No content to map due to end-of-input at [Source: (String) "" ; line: 1, column: 0]
ikitommi commented 4 months ago

We can add our own options if Jackson doesn't support this. I would start by asking how to do this on the https://github.com/FasterXML/jackson repo how to do this.