ring-json icon indicating copy to clipboard operation
ring-json copied to clipboard

Cheshire lazy parsing lets some malformed JSON pass through

Open rwilson opened this issue 6 years ago • 1 comments

Specifically, when the malformed JSON is in an array.

It's easily demonstrated in a repl:

user> (def bad-json (cheshire.core/parse-string "[{\"foo\": 1 2}]"))
#'user/bad-json
user> (type bad-json)
clojure.lang.LazySeq
user> (first bad-json)
JsonParseException Unexpected character ('2' (code 50)): was expecting comma to separate Object entries
 at [Source: (StringReader); line: 1, column: 13]  com.fasterxml.jackson.core.JsonParser._constructError (JsonParser.java:1798)

The JsonParseException isn't thrown until the lazy sequence is realized, which may not be until subsequent middleware or an endpoint.

Options could be:

  1. Lazy parsing has some benefits, leave it and document for clarity.
  2. Always or optionally realize body to catch the exception

Thoughts?

rwilson avatar Mar 09 '18 02:03 rwilson

This was a puzzling behavior that i also had to track down. My opinion is that by default the library should not silently pass along invalid JSON until the lazy sequence is ultimately realized down the line, likely outside of whatever try/catch block one has put in place to handle decoding errors. Opt-in laziness would be better.

dco-lentz avatar Dec 27 '19 18:12 dco-lentz