[cl-json-devel] Encoding DOUBLE-FLOAT
Robert Goldman
rpgoldman at sift.info
Wed Jun 22 21:10:44 UTC 2011
On 6/22/11 Jun 22 -11:59 AM, Daniel Brunner wrote:
> (json:encode-json-to-string 0.0570714084012434D0)
I have replicated this error....
The bug is here:
(defun write-json-number (nr stream)
"Write the JSON representation of the number NR to STREAM."
(typecase nr
(integer (format stream "~d" nr))
(real (format stream "~f" nr))
(t (unencodable-value-error nr 'write-json-number))))
As can be seen by
CL-USER> (format t "~f" 0.0570714084012434D0)
0.0570714084012434D0
NIL
This suggests that we must avoid using FORMAT in translating floats to
strings for JSON, or that there's a recipe for getting FORMAT to shun
the D that I don't know about....
Not incredibly well thought out or tested patch:
(defun write-json-number (nr stream)
"Write the JSON representation of the number NR to STREAM."
(typecase nr
(integer (format stream "~d" nr))
(real (let* ((raw (format nil "~f" nr))
(dpos (position #\d raw :test 'char-equal)))
(princ (subseq raw 0 dpos) stream)))
(t (unencodable-value-error nr 'write-json-number))))
This checks for D in the output. I /believe/ that this is the only such
character that can creep in, but I am far from being an expert on this.
Best,
r
More information about the cl-json-devel
mailing list