Add FSet/Jzon subsystem

This MR adds a new Quicklisp-loadable system, FSet/Jzon, wrapping the Jzon JSON parser/printer. It parses into FSet collections — JSON arrays parse into FSet seqs, and by default, JSON objects parse into FSet replay maps — and can also print these in JSON format. Two other FSet representations for JSON objects are also supported, plain maps and dynamic tuples.

FSet tuple keys have a new feature: you can optionally specify the type of the values stored under a key; FSet will signal an error if you try to assign a value to a key in a tuple that is not of the specified type. It will also signal an error if the default provided for the key is not of that type. (FSet/Jzon uses this feature to control how nil is printed in JSON; see below.)

Parsing

The FSet/Jzon parsing API exports many of the same names as Jzon, and is mostly used in the same way. The exception is when the input contains, potentially, more than one top-level JSON value. In this case, pass :allow-multiple-content t to make-parser or with-parser, as you would have with Jzon, but then call parse-top-level once for each top-level value you wish to parse. FSet/Jzon does not export parse-next.

As mentioned, JSON arrays parse into FSet seqs. For JSON objects, you have a choice. The default is to parse them into replay maps; these preserve the order of the keys of each object for printing purposes. Using the :object-type keyword argument to with-parser or make-parser, you can alternatively request plain maps, or tuples. The plain maps use the CHAMP implementation, so the keys will (usually) be reordered on printing, but they use less space than replay maps. In these cases the keys will be strings by default, though you can use other types by passing an appropriate :key-fn.

Tuples are FSet's most space-efficient representation of JSON objects, but require a bit more work to set up. You will need a tuple key for each key in the JSON you're parsing. The name of each tuple key should contain the corresponding JSON key string, normally upcased and with a prefix and/or suffix added; the use of a prefix and/or suffix is optional, but highly recommended. For example, if your JSON contains keys "size" and "color", the corresponding tuple key names could be +JSON-SIZE+ and +JSON-COLOR+ (written here in uppercase because that's how they'll print, but of course you can normally write them in lowercase in source code). Thus the prefix is "+JSON-" and the suffix is "+". With that in mind, the steps are:

  • For each key that you want to access in your program, write an fset2:define-tuple-key form.
  • To with-parser or make-parser, pass :object-type 'tuple; for :key-package, the package containing the tuple key names; and for :key-prefix and :key-suffix, the prefix and suffix to use. (If you're using a nonstandard reader case mode, or have mixed-case keys in your JSON input, you can also pass a :key-case-mode of :downcase or :preserve; :upcase is the default.)

On printing, the keys will be in the order in which they were declared, or if some keys were found in the input that had not been declared, the order in which they were first encountered. (You can still call fset2:define-tuple-key on them after parsing, if you hadn't done it beforehand.)

There is one other reason you might want to use tuples for objects, which has to do with how nil is printed; see below.

Type json-atom
Re-exported from Jzon.
Type json-element
(or json-atom seq map tuple)
Macro with-parser (var in &rest kwd-args) &body body
Binds var around body to an FSet/Jzon parser that will read from in, which can be a vector, a stream, or a pathname. The keyword arguments are passed unchanged to make-parser.
Function make-parser in &key allow-comments allow-trailing-comma allow-multiple-content (max-string-length (min 1048576 (1- array-dimension-limit))) (max-depth 128) (key-fn (make-fset-string-pool)) (object-type 'replay-map) key-package (key-case-mode ':upcase) (key-prefix "") (key-suffix "")
If allow-comments is true, comments as in C (// for single-line or /* ... */ for block) will be accepted. If allow-trailing-comma, a comma will be permitted after the last element of an array or object. max-string-length limits the length of strings, and max-depth the maximum nesting depth; these help protect against DoS attacks. key-fn controls whether and how string pooling is done; the default uses an FSet set.
The returned parser should be closed after use; see close-parser, and macro with-parser which closes it automatically.
If allow-multiple-content is true, the input may contain multiple top-level JSON values, which you can read by calling parse-top-level multiple times.
JSON arrays are returned as FSet seqs; JSON objects are returned according to object-type, which can be replay-map, map, or tuple (symbols in package fset2). If key-package is supplied, keys will be interned in this package; this option overrides key-fn. If object-type is tuple, key-package is required, and may not be the CL keyword package. If key-package is supplied: (a) key-case-mode can be any of :upcase, :downcase, or :preserve; the default is :upcase. (b) If supplied, key-prefix is prepended and key-suffix is appended to the case-converted key before interning.

Other parsing-related symbols re-exported from Jzon are: json-atom, json-error, json-limit-error, json-parse-error, json-parse-limit-error, and json-eof-error.

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 new :type keyword argument to fset2: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 called to turn a `tuple-key' into a string. The first argument is the key; the second is the key's name's package name, as a keyword symbol. So you can write methods like
  (defmethod coerce-tuple-key (key (pkg (eql :my-package)))
    ...)
Edited by Scott L. Burson

Merge request reports

Loading
+3 −2
Changes for Code/defs.lisp: 3 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -85,7 +85,8 @@ (defpackage :fset
	   #:do-set #:do-bag #:do-bag-pairs #:do-map #:do-map-domain #:do-seq #:do-tuple
	   #:adjoinf #:removef #:includef #:excludef
	   #:unionf #:intersectf #:set-differencef #:map-unionf #:map-intersectf #:imagef #:updatef #:composef
	   #:define-tuple-key #:def-tuple-key #:get-tuple-key #:tuple-key-name #:tuple-key?
	   #:define-tuple-key #:def-tuple-key #:get-tuple-key
	   #:tuple-key #:tuple-key-name #:tuple-key-type #:tuple-key?
	   #:tuple-merge
	   #:fset-setup-readtable #:*fset-readtable*
	   #:fset-setup-rereading-readtable #:*fset-rereading-readtable*
@@ -222,7 +223,7 @@ (defpackage :fset2
	   #:do-set #:do-bag #:do-bag-pairs #:do-map #:do-map-domain #:do-seq #:do-tuple
	   #:adjoinf #:removef #:includef #:excludef
	   #:unionf #:intersectf #:set-differencef #:map-unionf #:map-intersectf #:imagef #:updatef #:composef
	   #:define-tuple-key #:get-tuple-key #:tuple-key-name #:tuple-key?
	   #:define-tuple-key #:get-tuple-key #:tuple-key #:tuple-key-name #:tuple-key-type #:tuple-key?
	   #:tuple-merge
	   #:fset-setup-readtable #:*fset-readtable*
	   #:fset-setup-rereading-readtable #:*fset-rereading-readtable*

Code/jzon-defs.lisp

0 → 100644
+35 −0
Changes for Code/jzon-defs.lisp: 35 added lines, 0 removed lines.
Original line number Diff line number Diff line
(in-package :cl-user)

(defpackage :FSet/Jzon
  (:use :cl :new-let)
  (:shadowing-import-from :new-let #:let #:cond)
  (:shadowing-import-from :fset2 #:set #:map)
  (:import-from :fset #:gensymx)
  (:import-from :fset2
		#:seq #:replay-map #:tuple #:push-last #:@ #:convert #:includef #:get-tuple-key
		#:do-seq #:do-map #:do-tuple #:tuple-key #:tuple-key-name #:tuple-key-type #:char-seq?)
  ;; Exports of names defined in this system
  (:export #:json-element #:with-parser #:make-parser #:close-parser #:parse #:parse-top-level
	   #:coerced-fields #:coerce-tuple-key)
  ;; We import and re-export (our guess at) the most commonly used parts of the Jzon API,
  ;; mostly writer-related.
  (:import-from :com.inuoe.jzon
		#:span #:stringify #:json-atom
		#:json-error #:json-limit-error #:json-parse-error #:json-parse-limit-error #:json-eof-error
		#:json-write-error #:json-write-limit-error #:json-recursive-write-error
		#:coerced-fields #:coerce-key
		#:writer #:make-writer #:close-writer #:with-writer)
  (:export #:span #:stringify #:json-atom
	   #:json-error #:json-limit-error #:json-parse-error #:json-parse-limit-error #:json-eof-error
	   #:json-write-error #:json-write-limit-error #:json-recursive-write-error
	   #:coerced-fields #:coerce-key
	   #:writer #:make-writer #:close-writer #:with-writer)
  (:local-nicknames (#:jzon #:com.inuoe.jzon)))

(defpackage :FSet/Jzon/Test
  (:use :cl :new-let :fset/jzon)
  (:shadowing-import-from :new-let #:let #:cond)
  (:shadowing-import-from :fset2 #:set #:map)
  (:import-from :fset2
		#:equal? #:seq #:replay-map #:tuple #:push-last #:@ #:includef #:define-tuple-key
		#:do-seq #:do-map #:do-tuple #:tuple-key-name))

Code/jzon-tests.lisp

0 → 100644
+49 −0
Changes for Code/jzon-tests.lisp: 49 added lines, 0 removed lines.
Original line number Diff line number Diff line
(in-package :FSet/Jzon/Test)

(define-tuple-key +json-color+ :type (or null string))
(define-tuple-key +json-sizes+ :default (seq) :type seq)
(define-tuple-key +json-sizes-list+ :default '() :type list)
(define-tuple-key +json-visible?+ :default nil :type boolean)

(defmethod coerce-tuple-key (key (pkg (eql :fset/jzon/test)))
  (let ((str (symbol-name (tuple-key-name key))))
    ;; Strip leading "+JSON-" and trailing "+", and downcase
    (string-downcase (subseq str 6 (1- (length str))))))

(defun test-fset/jzon ()
  (macrolet ((test (form)
               `(unless ,form
                  (error "Test failed: ~S" ',form))))
    (test (equal? (parse "2") 2))
    (test (equal? (parse "[2]") (seq 2)))
    (test (equal? (parse "[2, [7, 19, [42]]]") (seq 2 (seq 7 19 (seq 42)))))
    (test (equal? (parse "[2, [7, 19, [42],]]" :allow-trailing-comma t) (seq 2 (seq 7 19 (seq 42)))))
    (test (handler-case (progn (parse "[2, [7, 19, [42]]]" :max-depth 2)
			       nil)
	    (json-parse-limit-error () t)))
    (test (equal? (parse "{\"foo\": 42}") (replay-map ("foo" 42))))
    (test (equal? (parse "{\"foo\": /* a comment */ 42}" :allow-comments t) (replay-map ("foo" 42))))
    (test (equal? (parse "{\"foo\": 42, \"bar\": 27}") (replay-map ("foo" 42) ("bar" 27))))
    (test (equal? (with-parser (p "{\"foo\": 42, \"bar\": 27}[3, 17]" :allow-multiple-content t)
		    (list (parse-top-level p) (parse-top-level p)))
		  (list (replay-map ("foo" 42) ("bar" 27)) (seq 3 17))))
    (test (equal? (parse "[\"gubbish\", {\"color\": \"blue\", \"sizes\": [4, 7, 11]}]")
		  (seq "gubbish" (replay-map ("color" "blue") ("sizes" (seq 4 7 11))))))
    (test (equal? (parse "[\"gubbish\", {\"color\": \"blue\", \"sizes\": [4, 7, 11]}]" :object-type 'map)
		  (seq "gubbish" (map ("color" "blue") ("sizes" (seq 4 7 11))))))
    (test (equal? (parse "[\"gubbish\", {\"color\": \"blue\", \"sizes\": [4, 7, 11]}]"
			 :object-type 'tuple :key-package :fset/jzon/test
			 :key-prefix '#:+json- :key-suffix #\+)
		  (seq "gubbish" (tuple (+json-color+ "blue") (+json-sizes+ (seq 4 7 11))))))

    (test (equal? (stringify 2) "2"))
    (test (equal? (stringify (seq 2)) "[2]"))
    (test (equal? (stringify (seq 2 (seq 7 19 (seq 42)))) "[2,[7,19,[42]]]"))
    (test (equal? (stringify (replay-map ("foo" 42) ("bar" 27))) "{\"foo\":42,\"bar\":27}"))
    ;; We don't test on plain `map' because the ordering may be implementation-dependent,
    ;; and it goes through the same code path as `replay-map' anyway.
    (test (equal? (stringify (seq "gubbish" (replay-map ("color" "blue") ("sizes" (seq 4 7 11)))))
		  "[\"gubbish\",{\"color\":\"blue\",\"sizes\":[4,7,11]}]"))
    (test (equal? (stringify (seq "gubbish" (tuple (+json-color+ nil) (+json-sizes-list+ nil) (+json-visible?+ nil))))
		  "[\"gubbish\",{\"color\":null,\"sizes-list\":[],\"visible?\":false}]"))
    "Passed."))

Code/jzon.lisp

0 → 100644
+272 −0
Changes for Code/jzon.lisp: 272 added lines, 0 removed lines.
Original line number Diff line number Diff line
(in-package :fset/jzon)


;;; Exported insted of `jzon:json-element'.
(deftype json-element ()
  '(or json-atom seq map tuple))


;;; ================================================================================
;;; Parsing

(defstruct (parser
	     (:constructor raw-make-parser (jzon-parser allow-multiple-content max-depth object-type)))
  jzon-parser
  allow-multiple-content
  max-depth
  object-type)

(defmacro with-parser ((var in &rest kwd-args
			&key allow-comments allow-trailing-comma allow-multiple-content
			  max-string-length max-depth key-fn object-type
			  key-package key-case-mode key-prefix key-suffix)
		       &body body)
  "Binds `var' around `body' to an FSet/Jzon parser that will read from `in',
which can be a vector, a stream, or a pathname.  If `allow-comments' is true,
comments as in C \(`//' for single-line or `/* ... */' for block\) will be
accepted.  If `allow-trailing-comma', a comma will be permitted after the last
element of an array or object.  `max-string-length' limits the length of
strings, and `map-depth' the maximum nesting depth; these help protect against
DoS attacks.  `key-fn' controls whether and how string pooling is done; the
default uses an FSet set.

If `allow-multiple-content' is true, the input may contain multiple top-level
JSON values, which you can read by calling `parse-top-level' multiple times.

JSON arrays are returned as FSet seqs; JSON objects are returned according to
`object-type', which can be `replay-map', `map', or `tuple'.  If
`key-package' is supplied, keys will be interned in this package; this option
overrides `key-fn'.  If `object-type' is `tuple', `key-package' is
required, and may not be the CL keyword package.  If `key-package' is supplied:
\(a\) `key-case-mode' can be any of `:upcase', `:downcase', or `:preserve'; the
default is `:upcase'.  \(b\) If supplied, `key-prefix' is prepended and
`key-suffix' is appended to the case-converted key before interning."
  (declare (ignore allow-comments allow-trailing-comma allow-multiple-content
		   max-string-length max-depth key-fn object-type
		   key-package key-case-mode key-prefix key-suffix))
  `(let ((,var (make-parser ,in . ,kwd-args)))
     (unwind-protect (progn . ,body)
       (close-parser ,var))))

(defun make-parser (in &key allow-comments allow-trailing-comma allow-multiple-content
			 (max-string-length (min 1048576 (1- array-dimension-limit)))
			 (max-depth 128) (key-fn (make-fset-string-pool))
			 (object-type 'replay-map)
			 key-package (key-case-mode ':upcase) (key-prefix "") (key-suffix ""))
  "Constructs an FSet/Jzon parser that will read from `in', which can be a
vector, a stream, or a pathname.  If `allow-comments' is true, comments as in
C \(`//' for single-line or `/* ... */' for block\) will be accepted.  If
`allow-trailing-comma', a comma will be permitted after the last element of
an array or object.  `max-string-length' limits the length of strings, and
`map-depth' the maximum nesting depth; these help protect against DoS attacks.
`key-fn' controls whether and how string pooling is done; the default uses
an FSet set.

The returned parser should be closed after use; see `close-parser', and macro
`with-parser' which closes it automatically.

If `allow-multiple-content' is true, the input may contain multiple top-level
JSON values, which you can read by calling `parse-top-level' multiple times.

JSON arrays are returned as FSet seqs; JSON objects are returned according to
`object-type', which can be `replay-map', `map', or `tuple'.  If
`key-package' is supplied, keys will be interned in this package; this option
overrides `key-fn'.  If `object-type' is `tuple', `key-package' is
required, and may not be the CL keyword package.  If `key-package' is supplied:
\(a\) `key-case-mode' can be any of `:upcase', `:downcase', or `:preserve'; the
default is `:upcase'.  \(b\) If supplied, `key-prefix' is prepended and
`key-suffix' is appended to the case-converted key before interning."
  (check-type max-depth (integer 1 65535))
  (check-type object-type (member replay-map map tuple))
  (check-type key-package (or null string symbol package))
  (check-type key-case-mode (member :upcase :downcase :preserve))
  (when (eq object-type 'tuple)
    (when (null key-package)
      (error "To use object-type `tuple', you must also supply a `key-package'"))
    (unless (packagep key-package)
      (setq key-package (uiop:find-package* key-package)))
    (when (eq key-package (symbol-package ':upcase))
      ;; The problem is that keyword symbols are bound to themselves; we want to bind key names
      ;; to their key objects.
      (error "When object-type is `tuple', `key-package' may not be the keyword package")))
  (let ((key-prefix (string key-prefix))
	(key-suffix (string key-suffix))
	((jzon-parser
	   (com.inuoe.jzon:make-parser in :allow-comments allow-comments :allow-trailing-comma allow-trailing-comma
					  :allow-multiple-content allow-multiple-content
					  :max-string-length max-string-length
					  :key-fn (if key-package
						      (lambda (s)
							(declare (optimize (speed 3))
								 (string s))
							(let ((s (ecase key-case-mode
								   (:upcase (string-upcase s))
								   (:downcase (string-downcase s))
								   (:preserve s)))
							      ((s (if (and (string= key-prefix "")
									   (string= key-suffix ""))
								      s
								    (concatenate 'string key-prefix s key-suffix)))))
							  (intern s key-package)))
						    key-fn)))))
    (raw-make-parser jzon-parser allow-multiple-content max-depth object-type)))

(defun close-parser (parser)
  "Closes the parser.  If the input was specified as a pathname, closes the
input stream."
  (com.inuoe.jzon:close-parser (parser-jzon-parser parser)))

(defun parse-top-level (parser)
  "Reads one top-level JSON object from `parser'.  If `parser' was created
with `allow-multiple-content' true, this can be called repeatedly; it will
return `nil' when there are no more top-level objects in the input."
  (let ((ignore result (parse-value parser 0)))
    (declare (ignore ignore))
    (unless (parser-allow-multiple-content parser)
      (let ((event (com.inuoe.jzon:parse-next (parser-jzon-parser parser))))
	(when event
	  (error "Unexpected text following the top-level value"))))
    result))

(defun parse (in &key allow-comments allow-trailing-comma
		   (max-string-length (min 1048576 (1- array-dimension-limit)))
		   (max-depth 128) (key-fn (make-fset-string-pool))
		   (object-type 'replay-map)
		   key-package (key-case-mode ':upcase) (key-prefix "") (key-suffix ""))
  "Constructs an FSet/Jzon parser that will read from `in', which can be a
vector, a stream, or a pathname, and reads one top-level object from it.
If `allow-comments' is true, comments as in C \(`//' for single-line or
`/* ... */' for block\) will be accepted.  If `allow-trailing-comma', a
comma will be permitted after the last element of an array or object.
`max-string-length' limits the length of strings, and `map-depth' the maximum
nesting depth; these help protect against DoS attacks.  `key-fn' controls
whether and how string pooling is done; the default uses an FSet set.

JSON arrays are returned as FSet seqs; JSON objects are returned according to
`object-type', which can be `replay-map', `map', or `tuple'.  If
`key-package' is supplied, keys will be interned in this package; this option
overrides `key-fn'.  If `object-type' is `tuple', `key-package' is
required, and may not be the CL keyword package.  If `key-package' is supplied:
\(a\) `key-case-mode' can be any of `:upcase', `:downcase', or `:preserve'; the
default is `:preserve'.  \(b\) If supplied, `key-prefix' is prepended and
`key-suffix' is appended to the case-converted key before interning."
  (with-parser (parser in :allow-comments allow-comments :allow-trailing-comma allow-trailing-comma
			  :max-string-length max-string-length :max-depth max-depth :key-fn key-fn
			  :object-type object-type
			  :key-package key-package :key-case-mode key-case-mode
			  :key-prefix key-prefix :key-suffix key-suffix)
    (parse-top-level parser)))

(defun parse-value (parser depth)
  (declare (optimize (debug 3))
	   (fixnum depth))
  (flet ((check-depth ()
	   (when (= depth (parser-max-depth parser))
	     (signal-parse-error parser 'json-parse-limit-error (parser-max-depth parser)
				 "Maximum depth exceeded."))))
    (let ((event value (com.inuoe.jzon:parse-next (parser-jzon-parser parser))))
      (ecase event
	(:value (values event value))
	((nil) (values nil nil))
	(:begin-array
	  (check-depth)
	  (let ((result (seq)))
	    (loop
	      (let ((ev val (parse-value parser (1+ depth))))
		(when (eq ev ':end-array)
		  (return (values ':value result)))
		(push-last result val)))))
	(:begin-object
	  (check-depth)
	  (let ((result (ecase (parser-object-type parser)
			  (replay-map (replay-map))
			  (map (map))
			  (tuple (tuple)))))
	    (loop
	      (let ((kev kval (com.inuoe.jzon:parse-next (parser-jzon-parser parser))))
		(when (eq kev ':end-object)
		  (return (values ':value result)))
		(unless (eq kev ':object-key)
		  (error "Syntax error: expected object key; found ~S ~S" kev kval))
		(let ((ev val (parse-value parser (1+ depth)))
		      ((kval (if (eq (parser-object-type parser) 'tuple)
				 (if (boundp kval) (symbol-value kval)
				   (get-tuple-key kval))
			       kval))))
		  (unless (eq ev ':value)
		    (error "Internal error: expected value; found ~S ~S" ev val))
		  (setf (@ result kval) val))))))
	((:end-array :end-object)
	  event)))))

(defun signal-parse-error (parser error-class limit format &rest args)
  ;; Depends on Jzon internals.
  (let ((line column (funcall (slot-value (parser-jzon-parser parser) 'com.inuoe.jzon::%pos))))
    (error error-class :format-control format :format-arguments args
		       :line line :column column :limit limit)))

(defun make-fset-string-pool ()
  (let ((pool (set)))
    (lambda (k)
      (let ((present? prev-k (@ pool k)))
	(if present? prev-k
	  (progn
	    (includef pool k)
	    k))))))

;;; ================================================================================
;;; Writing

;;; We just re-export the main Jzon writing operations; the methods below handle
;;; writing the FSet types.

(defmethod coerce-key ((key seq))
  (convert 'string key))

(defgeneric coerce-tuple-key (key key-name-package-as-keyword)
  (:documentation
    "Generic function called to turn a `tuple-key' into a string.  The first
argument is the key; the second is the key's name's package name, as a
keyword symbol.  So you can write methods like

  (defmethod coerce-tuple-key (key (pkg (eql :my-package)))
    ...)")
  (:method (key pkg)
    "The default method returns the key's name in lowercase."
    (declare (ignore pkg))
    (string-downcase (tuple-key-name key))))

(defmethod coerce-key ((key tuple-key))
  (coerce-tuple-key key (intern (package-name (symbol-package (tuple-key-name key)))
				(symbol-package ':foo))))

(defmethod coerce-tuple-key (key pkg-name)
  (declare (ignore pkg-name))
  (symbol-name (tuple-key-name key)))

(defmethod com.inuoe.jzon:write-value ((writer writer) (s seq))
  (com.inuoe.jzon:with-array writer
    (do-seq (x s)
      (com.inuoe.jzon:write-value writer x))))

(defmethod com.inuoe.jzon:write-value ((writer writer) (m map))
  (com.inuoe.jzon:with-object writer
    (do-map (k v m)
      ;; Looks like `write-property' was intended to be exported.
      (com.inuoe.jzon::write-property writer k v))))

(defmethod com.inuoe.jzon:write-value ((writer writer) (tup tuple))
  (com.inuoe.jzon:with-object writer
    (do-tuple (k v tup)
      ;; Jzon will downcase the key, unless it is in mixed case already; there's no option
      ;; to override this behavior.
      (let ((type (tuple-key-type k))
	    ;; Jzon lets you further customize this behavior by specializing `coerced-fields'.
	    ;; A similar feature could be added here.
	    ((v (or v (cond ((and (subtypep 'boolean type) (subtypep type 'boolean))
			     nil)
			    ((and (subtypep 'list type) (subtypep type 'list))
			     (seq))
			    (t 'null))))))
	(com.inuoe.jzon::write-property writer k v)))))
+3 −1
Changes for Code/macros.lisp: 3 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -1343,7 +1343,9 @@ (gmap:def-gmap-arg-type tuple (tuple)
  `((convert 'list ,tuple)
    #'null
    (:values 2 #'(lambda (al) (values (caar al) (cdar al))))
    #'cdr))
    #'cdr
    nil nil
    (do-tuple ,tuple)))

(gmap:def-gmap-res-type tuple (&key filterp)
  `((empty-dyn-tuple) (:consume 2 #'Tuple-With) nil ,filterp))
Loading
Loading