JSON Parser
(import (macduffie json))
(macduffie json) is just a simple JSON parser. This library
is now based on the SchemeSpheres JSON parser, and no longer has any code from
Dominique Boucher. With this change, the license is now Expat
(MIT). Furthermore, the parser looks close to standards
compliant, but there remains a problem with Unicode surrogate
pairs.
This library depends on (srfi 69) to represent JSON objects.
Otherwise, only scheme-small libraries are used.
This library is regularly tested in Chibi, Kawa, and Gauche (in
R7RS-strict mode).
Library Procedures
- (json-read [port]) Reads a JSON file from port and converts it to
Scheme data. port defaults to (current-input-port).
- (json-write value [port]) Converts value to a JSON file and writes it to port. port defaults to (current-output-port).
- (json-read-string str) Reads a JSON string str and returns the corresponding
Scheme data.
- (json-read-file filepath) Reads a JSON file at filepath and returns the
corresponding Scheme data.
- (json-write-string value [prettify] [space-char space-count]) Writes value to a new JSON string which is returned. prettify writes the JSON in pretty-printed form, and defaults
to #f. space-char and space-count specify the
indentation for pretty-printing, and default to #\tab and 1.
- (json-write-file value filepath [prettify] [space-char
space-count]) Writes value to a JSON file at filepath. The optional arguments are the same as
json-write-string and have the same defaults.
- (json-null) Returns a record of the type json-null, for which
(eq? (json-null) (json-null)) always returns #t.
- (json-null? v) Returns #t if v is a record of
the type json-null.
To do:
- Correctly write Unicode surrogate pairs
Performance
This library will not work well for very large files. According
to my tests, here are the relative performance of Chibi, Kawa,
and Gauche:
- Kawa was ok with a 1,000-line JSON file. Performance was
unacceptable for a 5,000-line JSON file.
- Chibi was ok with a 5,000-line JSON file. Performance was
unacceptable for a 10,000-line JSON file.
- Gauche was ok with a 100,000-line JSON file. Performance
was unacceptable for a 500,000-line JSON file.
Data types
Note that keys are internally represented as symbols.
Scheme
|
JSON
|
Hash table (srfi 69) |
Object |
List |
Array |
String |
String |
Number |
Number |
#t |
true |
#f |
false |
json-null record type |
null |