<< Back to previous view

[CONTRIB-91] json end-of-file handling broken Created: 15/Aug/10  Updated: 17/Sep/10  Resolved: 17/Sep/10

Status: Closed
Project: Clojure-Contrib
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Defect
Reporter: Anonymous Assignee: rasmus.svensson
Resolution: Completed Votes: 0
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.



 Comments   
Comment by Assembla Importer [ 17/Sep/10 6:53 PM ]

Converted from http://www.assembla.com/spaces/clojure/tickets/91
Attachments:
fix91-1.patch - https://www.assembla.com/spaces/clojure/documents/bYSqjYRuWr37WWeJe5cbCb/download/bYSqjYRuWr37WWeJe5cbCb

Comment by Assembla Importer [ 17/Sep/10 6:53 PM ]

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

Comment by Assembla Importer [ 17/Sep/10 6:53 PM ]

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

Branch: master

Generated at Sun Sep 14 21:05:16 CDT 2014 using JIRA 4.4#649-r158309.