9.2.2 JSON Printing

For printing, FSet/Jzon mostly just re-exports symbols from Jzon; you invoke the printer in the same way, but methods have been added to print the FSet collections that the parser can produce. So the tree you supply can contain anything that Jzon knows how to print, possibly with FSet collections mixed in. The printing-related Jzon symbols re-exported are: stringify, json-write-error, json-write-limit-error, json-recursive-write-error, coerced-fields, coerce-key, writer, make-writer, close-writer, and with-writer.

The one new printing-related function concerns tuple keys. As noted above, you may wish, when parsing JSON objects into tuples, to wrap the key strings with a prefix and/or suffix before interning them. When printing, you will probably want to strip these off. FSet/Jzon exports the generic function coerce-tuple-key (see below), which gives you a package-specific way to do that.

Jzon has three different ways it can print nil, depending on the type of the class slot holding the value: [] for a list; false for a boolean; or null for all other types. If you’re using tuples to represent JSON objects, you can get the same effect by using the :type keyword argument to define-tuple-key. (Of course, an empty seq will always print as [].) Examples:

(define-tuple-key +json-color+ :type (or null string))
(define-tuple-key +json-visible?+ :type boolean)
Function: coerce-tuple-key key key-name-package-as-keyword

Generic function that FSet/Jzon calls to turn a tuple-key into a string. coerce-tuple-key is called with the key as the first argument, and the key’s name’s package name, as a keyword symbol, as the second. This arrangement allows you to write methods like

(defmethod coerce-tuple-key (key (pkg (eql :my-package)))
  (let ((name (string (tuple-key-name key))))
    (assert (string= name "+JSON-" :end1 6))
    (string-downcase (subseq name 6 (1- (length name))))))

which will reverse the effect of passing "+JSON-" as key-prefix and "+" as key-suffix to make-parser or with-parser.