Skip to content

Commit 82e9437

Browse files
author
amirouche
committed
add json-sequence-read with tests
1 parent 8b89303 commit 82e9437

File tree

6 files changed

+77
-2
lines changed

6 files changed

+77
-2
lines changed

srfi-180.html

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,11 @@ <h3 id="json-read-port-or-generator-object"><code>(json-read [port-or-generator]
219219

220220
<h3 id="json-lines-read-port-or-generator"><code>(json-lines-read [port-or-generator]) → generator</code></h3>
221221

222-
<p>JSON reader of <a href="http://jsonlines.org/">jsonlines</a> or <a href="http://ndjson.org/">ndjson</a>. As first and only argument, it takes a generator of characters or a textual input port. Its value is the value returned <code>current-input-port</code>. It will return a generator of scheme objects as specified in <code>json-fread</code>.</p>
222+
<p>JSON reader of <a href="http://jsonlines.org/">jsonlines</a> or <a href="http://ndjson.org/">ndjson</a>. As first and only argument, it takes a generator of characters or a textual input port. Its default value is the value returned by <code>current-input-port</code>. It will return a generator of scheme objects as specified in <code>json-read</code>.</p>
223+
224+
<h3 id="json-sequence-read-port-or-generator"><code>(json-sequence-read [port-or-generator]) → generator</code></h3>
225+
226+
<p>JSON reader of <a href="https://tools.ietf.org/html/rfc7464">JSON) Text Sequences (RFC7464)</a>. As first and only argument, it takes a generator of characters or a textual input port. Its default value is the value returned by <code>current-input-port</code>. It will return a generator of scheme objects as specified in <code>json-read</code>.</p>
223227

224228
<h3 id="json-accumulator-port-or-generator"><code>(json-accumulator port-or-accumulator) → procedure</code></h3>
225229
<p>Streaming event-based JSON writer. <code>PORT-OR-ACCUMULATOR</code> must be a textual output port, or an accumulator that accepts characters and strings. It returns an accumulator procedure that accept scheme objects as first and only argument that follows the same protocol as described in <code>json-generator</code>. Any deviation from the protocol must raise an error that statisfies <code>json-error?</code>. In particular, objects and arrays must be properly nested.</p>

srfi/180.sld

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
json-generator
1010
json-read
1111
json-lines-read
12+
json-sequence-read
1213
json-accumulator
1314
json-write)
1415

srfi/180/body.scm

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
((#\x20 ; Space
2020
#\x09 ; Horizontal tab
2121
#\x0A ; Line feed or New line
22-
#\x0D)
22+
#\x0D
23+
#\x1E ;; Record Separator
24+
)
2325
#t)
2426
(else #f)))
2527

@@ -510,6 +512,17 @@
510512
(lambda ()
511513
(json-read port-or-generator)))))
512514

515+
;; json-sequence-read
516+
517+
(define json-sequence-read
518+
(case-lambda
519+
(() (json-sequence-read (current-input-port)))
520+
((port-or-generator)
521+
(lambda ()
522+
(let loop ()
523+
(guard (ex ((json-error? ex) (loop)))
524+
(json-read port-or-generator)))))))
525+
513526
;; write procedures
514527

515528
(define (json-accumulator accumulator)

srfi/180/checks.sld

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,9 @@
340340
sample-crlf-line-separators.jsonl
341341
sample-no-eol-at-eof.jsonl
342342
sample.jsonl
343+
;; json-sequence
344+
json-sequence.log
345+
json-sequence-with-one-broken-json.log
343346
)
344347

345348
(import (scheme base))
@@ -1546,4 +1549,38 @@
15461549
(call-with-input-file "./files/sample-no-eol-at-eof.jsonl"
15471550
(lambda (port) (generator->list (json-lines-read port))))))
15481551

1552+
;; json-sequence.log was taken from:
1553+
;;
1554+
;; https://raw.githubusercontent.com/hildjj/json-text-sequence/
1555+
;;
1556+
;; License is MIT: Copyright (c) 2014 Joe Hildebrand
1557+
;;
1558+
(define json-sequence.log
1559+
(check '(((d . "2014-09-22T22:11:26.315Z") (count . 0))
1560+
((d . "2014-09-22T22:11:26.317Z") (count . 1))
1561+
((d . "2014-09-22T22:11:26.317Z") (count . 2))
1562+
((d . "2014-09-22T22:11:26.317Z") (count . 3))
1563+
((d . "2014-09-22T22:11:26.317Z") (count . 4))
1564+
((d . "2014-09-22T22:11:26.317Z") (count . 5))
1565+
((d . "2014-09-22T22:11:26.317Z") (count . 6))
1566+
((d . "2014-09-22T22:11:26.317Z") (count . 7))
1567+
((d . "2014-09-22T22:11:26.317Z") (count . 8))
1568+
((d . "2014-09-22T22:11:26.317Z") (count . 9)))
1569+
(call-with-input-file "./files/json-sequence.log"
1570+
(lambda (port) (generator->list (json-sequence-read port))))))
1571+
1572+
(define json-sequence-with-one-broken-json.log
1573+
(check '(((d . "2014-09-22T22:11:26.315Z") (count . 0))
1574+
((d . "2014-09-22T22:11:26.317Z") (count . 1))
1575+
((d . "2014-09-22T22:11:26.317Z") (count . 2))
1576+
((d . "2014-09-22T22:11:26.317Z") (count . 3))
1577+
((d . "2014-09-22T22:11:26.317Z") (count . 4))
1578+
((d . "2014-09-22T22:11:26.317Z") (count . 5))
1579+
((d . "2014-09-22T22:11:26.317Z") (count . 6))
1580+
((d . "2014-09-22T22:11:26.317Z") (count . 7))
1581+
;; ((d . "2014-09-22T22:11:26.317Z") (count . 8))
1582+
((d . "2014-09-22T22:11:26.317Z") (count . 9)))
1583+
(call-with-input-file "./files/json-sequence-with-one-broken-json.log"
1584+
(lambda (port) (generator->list (json-sequence-read port))))))
1585+
15491586
))
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{"d":"2014-09-22T22:11:26.315Z","count":0}
2+
{"d":"2014-09-22T22:11:26.317Z","count":1}
3+
{"d":"2014-09-22T22:11:26.317Z","count":2}
4+
{"d":"2014-09-22T22:11:26.317Z","count":3}
5+
{"d":"2014-09-22T22:11:26.317Z","count":4}
6+
{"d":"2014-09-22T22:11:26.317Z","count":5}
7+
{"d":"2014-09-22T22:11:26.317Z","count":6}
8+
{"d":"2014-09-22T22:11:26.317Z","count":7}
9+
{"d":"2014-09-22T22:11:26.317Z","
10+
{"d":"2014-09-22T22:11:26.317Z","count":9}

srfi/files/json-sequence.log

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{"d":"2014-09-22T22:11:26.315Z","count":0}
2+
{"d":"2014-09-22T22:11:26.317Z","count":1}
3+
{"d":"2014-09-22T22:11:26.317Z","count":2}
4+
{"d":"2014-09-22T22:11:26.317Z","count":3}
5+
{"d":"2014-09-22T22:11:26.317Z","count":4}
6+
{"d":"2014-09-22T22:11:26.317Z","count":5}
7+
{"d":"2014-09-22T22:11:26.317Z","count":6}
8+
{"d":"2014-09-22T22:11:26.317Z","count":7}
9+
{"d":"2014-09-22T22:11:26.317Z","count":8}
10+
{"d":"2014-09-22T22:11:26.317Z","count":9}

0 commit comments

Comments
 (0)