Skip to content

Commit

Permalink
adding support for Newline delimited JSON (application/x-ndjson) & JS…
Browse files Browse the repository at this point in the history
…ON Lines (application/jsonl) with NeoJSONWriter>>#nextPutAll: and NeoJSONReader>>#upToEnd exemplified in NeoJSONExamplesTests>>#testMultiInstances
  • Loading branch information
svenvc committed Apr 24, 2024
1 parent 19bb4ca commit 17efabc
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
10 changes: 10 additions & 0 deletions repository/Neo-JSON-Core/NeoJSONReader.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -515,3 +515,13 @@ NeoJSONReader >> unescapeChar: char [
ifTrue: [ ^ Character tab ].
self error: 'Unknown escape char: ', (String with: char)
]
{ #category : #accessing }
NeoJSONReader >> upToEnd [
"Parse and collect JSON values from my stream until it ends, returning the collection.
This supports Newline delimited JSON (application/x-ndjson) & JSON Lines (application/jsonl).
Warning: though each element is valid JSON, taken as a whole the result is non-valid JSON."
^ Array streamContents: [ :out |
[ self atEnd ] whileFalse: [ out nextPut: self next ] ]
]
11 changes: 11 additions & 0 deletions repository/Neo-JSON-Core/NeoJSONWriter.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,17 @@ NeoJSONWriter >> nextPut: anObject as: schema [
^ mapping writeObject: anObject on: self
]

{ #category : #accessing }
NeoJSONWriter >> nextPutAll: aCollection [
"Write a separate JSON representation of all objects in aCollection to my stream.
This supports Newline delimited JSON (application/x-ndjson) & JSON Lines (application/jsonl).
Warning: though each element is valid JSON, taken as a whole the result is non-valid JSON."

aCollection do: [ :each |
self nextPut: each.
self newline ]
]

{ #category : #'initialize-release' }
NeoJSONWriter >> on: aWriteStream [
"Initialize on aWriteStream, which should be a character stream that
Expand Down
21 changes: 20 additions & 1 deletion repository/Neo-JSON-Tests/NeoJSONExamplesTests.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,28 @@ NeoJSONExamplesTests runs a couple of examples as tests
Class {
#name : #NeoJSONExamplesTests,
#superclass : #TestCase,
#category : 'Neo-JSON-Tests'
#category : #'Neo-JSON-Tests'
}

{ #category : #tests }
NeoJSONExamplesTests >> testMultiInstances [
"Newline delimited JSON (application/x-ndjson) - https://github.com/ndjson/ndjson-spec"
"JSON Lines (application/jsonl) - https://jsonlines.org"

| data json |

data := (1 to: 10) collect: [ :each |
{ #number -> each } asDictionary ].

json := String streamContents: [ :out |
(NeoJSONWriter on: out) nextPutAll: data ].

self assert: (NeoJSONReader on: json readStream) upToEnd equals: data.

self assert: (NeoJSONReader on: json readStream) next equals: data first.
self deny: ((NeoJSONReader on: json readStream) next; atEnd)
]

{ #category : #tests }
NeoJSONExamplesTests >> testPoints [
| points json result |
Expand Down

0 comments on commit 17efabc

Please sign in to comment.