Skip to content

Commit

Permalink
json: rename read-json to read-jsons, add read-json that reads a sing…
Browse files Browse the repository at this point in the history
…le object.
  • Loading branch information
mrjbq7 committed Jul 21, 2024
1 parent 0d4745f commit f85eb07
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 14 deletions.
7 changes: 6 additions & 1 deletion basis/json/json-docs.factor
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@ HELP: json>
{ $notes "The full name of this word could be " { $snippet "json-string>object" } "." } ;

HELP: read-json
{ $values { "object" "deserialized object" } }
{ $description "Reads a JSON formatted strings into a Factor object. JSON objects are converted to Factor hashtables. All other JSON objects convert to their obvious Factor equivalents." } ;

HELP: read-jsons
{ $values { "objects" { $sequence "deserialized objects" } } }
{ $description "Reads JSON formatted strings into a vector of Factor object until the end of the stream is reached. JSON objects are converted to Factor hashtables. All other JSON objects convert to their obvious Factor equivalents." } ;

{ >json json> read-json write-json } related-words
{ >json json> read-json read-jsons write-json } related-words

HELP: path>json
{ $values
Expand Down Expand Up @@ -60,6 +64,7 @@ $nl
{ $subsections
json>
read-json
read-jsons
path>json
path>jsons
}
Expand Down
7 changes: 5 additions & 2 deletions basis/json/json-tests.factor
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
USING: hashtables io.encodings.utf8 io.files io.files.unique
USING: hashtables io io.encodings.utf8 io.files io.files.unique
io.streams.string json json.private kernel linked-assocs
literals math namespaces sequences strings tools.test ;
IN: json.tests
Expand Down Expand Up @@ -69,7 +69,10 @@ ${ { 0xabcd } >string } [ " \"\\uaBCd\" " json> ] unit-test
{ 0 } [ " 0 " json> ] unit-test

{ V{ LH{ { "a" "b" } } LH{ { "c" "d" } } } }
[ "{\"a\": \"b\"} {\"c\": \"d\"}" [ read-json ] with-string-reader ] unit-test
[ "{\"a\": \"b\"} {\"c\": \"d\"}" [ read-jsons ] with-string-reader ] unit-test

{ LH{ { "a" "b" } } " {\"c\": \"d\"}" }
[ "{\"a\": \"b\"} {\"c\": \"d\"}" [ read-json read-contents ] with-string-reader ] unit-test

! empty objects are allowed as values in objects
{ LH{ { "foo" LH{ } } } } [ "{ \"foo\" : {}}" json> ] unit-test
Expand Down
37 changes: 26 additions & 11 deletions basis/json/json.factor
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
! Copyright (C) 2006 Chris Double, 2008 Peter Burns, 2009 Philipp Winkler

USING: accessors ascii assocs combinators formatting hashtables
io io.encodings.utf16.private io.encodings.utf8 io.files
USING: accessors ascii assocs combinators formatting io
io.encodings.utf16.private io.encodings.utf8 io.files
io.streams.string kernel kernel.private linked-assocs make math
math.order math.parser mirrors namespaces sbufs sequences
sequences.private strings summary tr words vocabs.loader ;
sequences.private strings summary tr vectors vocabs.loader words
;
IN: json
Expand Down Expand Up @@ -144,21 +145,35 @@ DEFER: (read-json-string)
: get-json ( objects -- obj )
dup length 1 = [ first ] [ json-error ] if ;

: check-json-depth ( quot -- )
[ 0 json-depth ] dip '[
@ json-depth get zero? [ json-error ] unless
] with-variable ; inline

PRIVATE>

: stream-read-json ( stream -- objects )
0 json-depth [
: stream-read-jsons ( stream -- objects )
[
V{ } clone over '[ _ stream-read1 ] [ scan ] while* nip
json-depth get zero? [ json-error ] unless
] with-variable ;
] check-json-depth ;

: read-jsons ( -- objects )
input-stream get stream-read-jsons ;

: stream-read-json ( stream -- object )
[
V{ } clone over '[
_ stream-read1 [ scan dup first vector? ] [ f ] if*
] loop nip
] check-json-depth get-json ;

: read-json ( -- objects )
: read-json ( -- object )
input-stream get stream-read-json ;

GENERIC: json> ( string -- object )

M: string json>
[ read-json get-json ] with-string-reader ;
[ read-jsons get-json ] with-string-reader ;

SYMBOL: json-allow-fp-special?
f json-allow-fp-special? set-global
Expand Down Expand Up @@ -334,10 +349,10 @@ M: string jsonlines>
[ write-jsonlines ] with-string-writer ;

: path>json ( path -- json )
utf8 [ read-json get-json ] with-file-reader ;
utf8 [ read-jsons get-json ] with-file-reader ;

: path>jsons ( path -- jsons )
utf8 [ read-json ] with-file-reader ;
utf8 [ read-jsons ] with-file-reader ;

: json>path ( json path -- )
utf8 [ write-json ] with-file-writer ;
Expand Down

0 comments on commit f85eb07

Please sign in to comment.