Clojure-Contrib

json end-of-file handling broken

Details

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

Description

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 "")
</code></pre>
which throws the exception
<pre><code>java.lang.IllegalArgumentException: Value out of range for char: -1
</code></pre>
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"
  [stream]
  (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.

Activity

Hide
Assembla Importer added a comment -

stuart.sierra said: [file:bYSqjYRuWr37WWeJe5cbCb]: fix, without auxiliary function

Show
Assembla Importer added a comment - stuart.sierra said: [file:bYSqjYRuWr37WWeJe5cbCb]: fix, without auxiliary function
Hide
Assembla Importer added a comment -

stuart.sierra said: (In revision:5a928e263ab88cb8d224de8585932f936aa30c8f) Fix EOF-handling in JSON reader; refs #91

Branch: master

Show
Assembla Importer added a comment - stuart.sierra said: (In revision:5a928e263ab88cb8d224de8585932f936aa30c8f) Fix EOF-handling in JSON reader; refs #91 Branch: master

People

Vote (0)
Watch (0)

Dates

  • Created:
    Updated:
    Resolved: