[DJSON-17] Double "positive infinity" value leads to json strings that can not be decoded Created: 21/May/14 Updated: 13/Jun/14 Resolved: 13/Jun/14
|Reporter:||Anton Logvinenko||Assignee:||Stuart Sierra|
If you put a Double/POSITIVE_INFINITY as a value somewhere in your Clojure-collection and convert it to JSON then it will become impossible to decode that JSON back to Clojure-collection. Double/POSITIVE_INFINITY is encoded as "Infinity" string. When you try to parse that JSON, you get an Exception "Exception JSON error (unexpected character): I clojure.data.json/-read".
Can't say whether this really is a bug, but the thing that looks bad is that I couldn't decode what I encoded with the same library, it's not always "x == (read-str (write-str x))".
Here is a simple test to reproduce this behaviour:
Originally I encountered this problem with clojure.data.json v. 0.1.2:
|Comment by Tim Clemons [ 22/May/14 7:29 PM ]|
JSON currently does not support any representation of infinity. In RFC4627, section 2.4 it reads:
A potential workaround would be to decide on a convention whereby :value-fn functions passed to write and read translate Infinity to and from a given String. However, that's an application specific solution that doesn't really belong in the library.
|Comment by Anton Logvinenko [ 23/May/14 6:13 AM ]|
Maybe json/write-str should just fail if the collection contains Nan or Infinity? Not sure if this will be the best behavior in all possible use cases, but it would be better in the application where I've encountered it. The server was processing messages from ActiveMQ and silently converting some Doubles to "Infinity" string. If it failed, the server would retry, then log the problem, put messages to DLQ queue and we would know about it. Instead the problem manifested itself only when someone requested the data, plus the stored data was already "corrupted" by the time.
|Comment by Stuart Sierra [ 06/Jun/14 4:04 PM ]|
Fixed on Git master branch as of b4ba1fc0.
Holding release 0.2.5 for possible feedback.
|Comment by Anton Logvinenko [ 07/Jun/14 4:26 PM ]|
I've installed local 0.2.5-SNAPSHOT and ran the code. Having NaN inside makes json/write throw an exception "Exception JSON error: cannot write Double NaN clojure.data.json/write-double (json.clj:368)" (similar for Infinity).
On the other hand, json/write with :value-fn works fine and I'm able to replace values I need to.
Seems to me everything works great!
|Comment by Stuart Sierra [ 13/Jun/14 4:17 PM ]|
Released as part of version 0.2.5