json end-of-file handling broken


  • Type: Defect Defect
  • Status: Closed Closed
  • Resolution: Completed
  • Affects Version/s: None
  • Fix Version/s: None
  • Component/s: None
  • Labels:


When read-json encounters an end-of-file, an IllegalArgumentException is thrown regardless of the user's eof preferences. This can easily be reproduced by running:

(read-json "")
which throws the exception
<pre><code>java.lang.IllegalArgumentException: Value out of range for char: -1
This is caused by the .read method of the Reader returning -1 when the end is reached. (.read returns an int.) This value is then passed to 'char', which tries to make the corresponding character from the code point number. However, -1 is not a valid code point, so it throws an exception.

A solution is to check for equality to -1 before trying to convert to a character. I also have an idea for a helper function that could be used:
<pre><code>(defn- read-char
  "Reads a character from a Reader. Returns nil if the end of the stream is reached"
  (let [x (.read stream)]
    (if (not= x -1)
      (char x))))

This function will always return either a Character or nil. Using it would make it possible to have the eof case along the other cases in the existing cond forms.



Vote (0)
Watch (0)


  • Created: