Skip to content

Commit b3b4fbf

Browse files
author
amirouche
committed
json-generator: improve spec, add examples and two tests.
1 parent 32b72ab commit b3b4fbf

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

srfi-180.html

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,25 @@ <h3 id="json-generator-port"><code>(json-generator [port]) → generator</code><
102102
<p>In cases where the JSON is invalid, the generator returned by <code>json-generator</code> should raise an object that satisfies the predicate <code>json-error?</code>.</p>
103103
<p>Otherwise, if <code>PORT</code> contains valid JSON text, the generator returned by <code>json-generator</code> must yield an end-of-file object in two situations:</p>
104104
<ul>
105-
<li>The first time <code>TOKENS</code> is called, it returns an object that is a boolean, a number, a string or the symbol <code>'null</code>.</li>
106-
<li>The first time <code>TOKENS</code> is called, it returns a symbol that is not the symbol <code>'null</code>. When the underlying JSON text is valid, it should be the symbol starting a structure: <code>'object-start</code> or <code>'array-start</code>. The end-of-file object is generated when that structure is finished.</li>
105+
<li>The first time the generator returned by <code>json-generator</code> is called, it returns an object that is a boolean, a number, a string or the symbol <code>'null</code>.</li>
106+
<li>The first time the generator returned by <code>json-generator</code> is called, it returns a symbol that is not the symbol <code>'null</code>. When the underlying JSON text is valid, it should be the symbol starting a structure: <code>'object-start</code> or <code>'array-start</code>. The end-of-file object is generated when that structure is finished.</li>
107107
</ul>
108108

109109
<p>In other words, the generator returned by <code>json-generator</code> will parse at most one JSON value or one top-level structure. If <code>PORT</code> is not finished, as in the case of <a href="http://jsonlines.org/">JSON lines</a>, the user should call <code>json-generator</code> again with the same <code>PORT</code>.</p>
110110

111-
<!-- TODO: add examples -->
111+
<h4>Examples</h4>
112112

113+
<pre>(assume
114+
(equal?
115+
(call-with-input-string "42 101 1337" (lambda (port) (generator->list (json-generator port))))
116+
'(42)))</pre>
117+
118+
<pre>(assume
119+
(equal?
120+
(call-with-input-string "[42] 101 1337" (lambda (port) (generator->list (json-generator port))))
121+
'(array-start 42 array-end)))</pre>
122+
123+
</pre>
113124
<h3 id="json-fold"><code>(json-fold proc array-start array-end object-start object-end seed [port-or-generator])</code></h3>
114125
<p>Fundamental JSON iterator.</p>
115126

srfi/180/checks.sld

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,9 @@
343343
;; json-sequence
344344
json-sequence.log
345345
json-sequence-with-one-broken-json.log
346+
;; others
347+
json-generator-single-top-level-value
348+
json-generator-single-top-level-value-structure
346349
)
347350

348351
(import (scheme base))
@@ -1583,4 +1586,14 @@
15831586
(call-with-input-file "./files/json-sequence-with-one-broken-json.log"
15841587
(lambda (port) (generator->list (json-sequence-read port))))))
15851588

1589+
(define json-generator-single-top-level-value
1590+
(check
1591+
(call-with-input-string "42 101 1337" (lambda (port) (generator->list (json-generator port))))
1592+
'(42)))
1593+
1594+
(define json-generator-single-top-level-value-structure
1595+
(check
1596+
(call-with-input-string "[42] 101 1337" (lambda (port) (generator->list (json-generator port))))
1597+
'(array-start 42 array-end)))
1598+
15861599
))

0 commit comments

Comments
 (0)