Skip to content

Commit

Permalink
Multidimensional array support
Browse files Browse the repository at this point in the history
  • Loading branch information
Zulu-Inuoe committed Feb 14, 2023
1 parent b25ca02 commit 04e7db1
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -263,11 +263,14 @@ When writing, the following type mappings are also available:
| character | string (`string`) |
| pathname | string (`uiop:native-namestring`) |
| real | number |
| array | array\* - multidimensional arrays are arrays-of-arrays |
| sequence | array |
| standard-object | object |
| structure-object\* | object |
| structure-object | object |

\*: On supported implementations where structure slots are available via the MOP.
\*: `#2A((1 2) (3 4))` becomes `[[1,2],[3,4]]`

†: On supported implementations where structure slots are available via the MOP.

If you have an alist/plist you wish to write, we recommend the use of either `alexandria:alist-hash-table` or `alexandria:plist-hash-table`, or use one of the methods in [Custom Serialization](#custom-serialization).

Expand Down
15 changes: 15 additions & 0 deletions src/jzon.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -1408,6 +1408,21 @@ see `write-values'"
(:method ((writer writer) (value pathname))
(%write-json-atom writer (uiop:native-namestring value)))

;; Multi-dimensional arrays
(:method ((writer writer) (value array))
(let ((dimensions (array-dimensions value)))
(if (null dimensions)
(write-value writer (aref value))
(labels ((recurse (dimensions acc)
(destructuring-bind (d . rest) dimensions
(with-array writer
(if (null rest)
(loop :for i :below d
:do (write-value writer (row-major-aref value (+ acc i))))
(loop :for i :below d
:do (recurse rest (+ acc (* i d)))))))))
(recurse dimensions 0)))))

;;; Sequence support
(:method ((writer writer) (value sequence))
(with-array writer
Expand Down
9 changes: 6 additions & 3 deletions test/jzon-tests.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -764,6 +764,12 @@
]
]" (jzon:stringify #(#(1 2) #(3 4)) :pretty t))))

(test stringify-multidimensional-array
(is (string= "[[1,2],[3,4]]" (jzon:stringify #2A((1 2) (3 4))))))

(test 0-dimension-array
(is (string= "42" (jzon:stringify #0A42))))

(defun recode (value)
"Shorthand for (jzon:parse (jzon:stringify value))"
(jzon:parse (jzon:stringify value)))
Expand Down Expand Up @@ -855,9 +861,6 @@
}
]" (jzon:stringify (vector (ph "x" 0)) :pretty t))))

(test stringify-no-slots-on-unknown-object ()
(is (string= "{}" (jzon:stringify (make-array '(2 2))))))

(test stringify-pretty-prints-keys
(is (string= "{\"#(1 2)\":0}" (jzon:stringify (ph #(1 2) 0)))))

Expand Down

0 comments on commit 04e7db1

Please sign in to comment.