Skip to content

Commit 5d4bf62

Browse files
author
Amirouche
committed
json-write: more number checks.
1 parent f46ad4d commit 5d4bf62

File tree

4 files changed

+46
-5
lines changed

4 files changed

+46
-5
lines changed

srfi-180.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,8 @@ <h3 id="json-write-obj-port-unspecified"><code>(json-write obj [port]) → unspe
141141
<ul>
142142
<li>symbol <code>'null</code></li>
143143
<li>boolean</li>
144-
<li>real number</li>
144+
<li>number must be integers or inexact rational (that is they must not
145+
be complex, infinite, nan, or exact rational that are not integers)</li>
145146
<li>string</li>
146147
<li>vector</li>
147148
<li>association list</li>

srfi/json-checks.sld

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,16 @@
320320
y_structure_string_empty.json
321321
y_structure_trailing_newline.json
322322
y_structure_true_in_array.json
323-
y_structure_whitespace_array.json)
323+
y_structure_whitespace_array.json
324+
;; scheme specific
325+
n_+inf.0
326+
n_-inf.0
327+
n_complex
328+
n_-nan.0
329+
n_+nan.0
330+
n_exact_not_integer
331+
332+
)
324333

325334
(import (scheme base))
326335
(import (scheme file))
@@ -347,6 +356,12 @@
347356
(lambda (port)
348357
(json-read port))))
349358

359+
(define (json-string->obj string)
360+
(call-with-input-string string json-read))
361+
362+
(define (obj->json-string obj)
363+
(call-with-output-string (lambda (port) (json-write obj))))
364+
350365
(define parse json->obj->json->obj)
351366

352367
(define i_number_double_huge_neg_exp.json
@@ -1348,4 +1363,24 @@
13481363
(define y_structure_whitespace_array.json
13491364
(check #() (parse "./files/y_structure_whitespace_array.json")))
13501365

1366+
;; Scheme specific tests
1367+
1368+
(define n_+inf.0
1369+
(check-raise json-error? (obj->json-string +inf.0)))
1370+
1371+
(define n_-inf.0
1372+
(check-raise json-error? (obj->json-string -inf.0)))
1373+
1374+
(define n_complex
1375+
(check-raise json-error? (obj->json-string 3+14i)))
1376+
1377+
(define n_-nan.0
1378+
(check-raise json-error? (obj->json-string +nan.0)))
1379+
1380+
(define n_+nan.0
1381+
(check-raise json-error? (obj->json-string -nan.0)))
1382+
1383+
(define n_exact_not_integer
1384+
(check-raise json-error? (obj->json-string 314/100)))
1385+
13511386
))

srfi/json.scm

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -489,9 +489,13 @@
489489
((eq? obj 'null) (void))
490490
((boolean? obj) (void))
491491
((string? obj) (void))
492-
((number? obj)
493-
(when (= (abs obj) +inf.0)
494-
(raise (make-json-error "Infinity is not a valid JSON number"))))
492+
((and (number? obj)
493+
(not (infinite? obj))
494+
(not (nan? obj))
495+
(real? obj)
496+
(or (and (exact? obj) (= (denominator obj) 1))
497+
(inexact? obj)))
498+
(void))
495499
((vector? obj)
496500
(vector-for-each (lambda (obj) (raise-unless-valid? obj)) obj))
497501
;; XXX: pair? instead of list? because it is faster.

srfi/json.sld

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
json-write)
88

99
(import (scheme base)
10+
(scheme inexact)
1011
(scheme case-lambda)
1112
(scheme char)
1213
(scheme text)

0 commit comments

Comments
 (0)