-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutil.lisp
46 lines (35 loc) · 1.45 KB
/
util.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
(in-package :postgres-json)
;;;; JSON related small functions
(defun obj (&rest args)
"Return an 'equal key/value hash-table consisting of pairs of ARGS.
For JSON use your keys must be Common Lisp strings."
(let ((hash (make-hash-table :test #'equal)))
(loop for (key val) on args by #'cddr do
(setf (gethash key hash) val))
hash))
(defun from-json (string)
"Parse the JSON string STRING and return the resulting lisp object."
(yason:parse string :json-arrays-as-vectors t))
(defun to-json (object)
"Convert a lisp OBJECT to a string of JSON."
(with-output-to-string (s)
(yason:encode object s)))
(defun pp-json (object &key (stream *standard-output*) (indent 4))
"Pretty print lisp OBJECT as JSON to STREAM with specified INDENT."
(fresh-line stream)
(let ((s (yason:make-json-output-stream stream :indent indent)))
(yason:encode object s)))
;;;; True utility functions and macros, waiting for a real home
(defmacro first-value (form)
`(nth-value 0 ,form))
(defun sym (package-name &rest args)
"Return symbol being the concatenation of upcasing ARGS. See
ALEXANDRIA:FORMAT-SYMBOL for effect of PACKAGE-NAME."
(format-symbol package-name "~:@(~{~A~}~)" args))
(defun sym-prefix (prefix symbol)
(sym t prefix "-" symbol))
(defun sym-suffix (symbol suffix)
(sym t symbol "-" suffix))
(defun walk-tree (fun tree)
"Walk TREE and call FUN at each node. Thanks to Lisp Tips."
(subst-if t (constantly nil) tree :key fun))