The options are explained under MAKE-JSON-OUTPUT-STREAM. Target must be a character stream or NIL. If target is NIL output is written to a string that is returned, otherwise the values of body is returned.

The following functions and macros can only be used inside the body of WITH-JSON-OUTPUT:

WITH-JSON-ARRAY - Use any of the other functions/macros in the body to output elements of the array.

WITH-JSON-OBJECT - Use WITH-JSON-MEMBER or JSON-OUTPUT-MEMBER in the body.

WITH-JSON-MEMBER - The body must contain one call to one output function/macros to output the value.

JSON-OUTPUT-MEMBER - Outputs the member value using JSON-OUTPUT-VALUE.

JSON-OUTPUT-VALUE - string, number, sequence or hash-table.

JSON-OUTPUT-BOOLEAN - NIL outputs false, anything else outputs true.

JSON-OUTPUT-NULL - Always outputs null. No arguments.

JSON-OUTPUT-ALIST - Outputs an assoc list as a JSON object using JSON-OUTPUT-MEMBER.

indent - Default false. When true, the JSON text is broken into lines and indented.

escape-non-ascii - Default false. When true, any character outside the ASCII character set will be escaped using the syntax.

Common options

close-stream - Default false. When true, the source stream will be closed by JSON-CLOSE.

multiple - Default false. When true, only a single JSON text (object or array) is expected in the stream. When T, zero or more JSON texts can be read from or written to stream.

duplicate-key-check - Default true. When true, an error will be signaled when duplicate keys are detected in an object. Setting this to false avoids the added overhead of checking for duplicate keys.

Function
JSON-STREAMS:JSON-READ json-input-stream
=> token

Parses JSON text from the underlying stream and returns the next token.

Function
JSON-STREAMS:JSON-WRITE token json-output-stream
=> token

Outputs JSON text to underlying stream based on the given token.

Function
JSON-STREAMS:JSON-CLOSE json-stream &key abort

Closes the json-stream. Applications should always call JSON-CLOSE when finished with a JSON stream. This ensures that all syntax errors are detected. The value of abort is forwarded to CL:CLOSE of the underlying stream (if close-stream was true) and JSON syntax checking is suppressed.

Macro
JSON-STREAMS:WITH-OPEN-JSON-STREAM (var json-stream) &body

Binds var to json-stream, executes body, and ensures that JSON-CLOSE is called on json-stream.

Tokens

The possible tokens for input and output are

:BEGIN-OBJECT

:END-OBJECT

:BEGIN-ARRAY

:END-ARRAY

:NULL

:FALSE

:TRUE

A number

A string or (:STRING integer*)

:EOF

After a :BEGIN-OBJECT is returned, JSON-READ will alternate between returning keys and values of the object, until :END-OBJECT is returned instead of a key. Keys are always returned as strings. Values can be anything, except :END-OBJECT, :END-ARRAY and :EOF. Thus some kind of recursive or stack based algorithm is neccearry.

Handling of numbers

While JSON standard doesn't define any limits on the size of numbers, many progamming languages does. This library assumes the limits of JavaScript. In JavaScript there is only one number type, a 64-bit IEEE 754 double precision float. This has the following implications:

The syntax doesn't matter: 20, 20.0 or 2e1 are all the same number, the integer 20.

The integer range is +/- (expt 10 53). Numbers outside this range can't be represented accurately. This library will refuse to write an integer outside this range. If you need to output such large numbers, convert them to double-float or string.

At input the handling of numbers outside the integer range depends on syntax. If the number contains a decimal point or an exponent it will be returned as a double-float. Otherwise an error will be signalled. The option :USE-RATIOS disables this check.