summaryrefslogtreecommitdiff
path: root/dump.lisp
diff options
context:
space:
mode:
authorJuan Manuel Tomás <jtomas1815@gmail.com>2022-12-26 03:10:17 -0300
committerJuan Manuel Tomás <jtomas1815@gmail.com>2022-12-26 03:10:17 -0300
commit2990f8c72975eb1de8be62ac043375fea47857eb (patch)
treedb7c8045b29bbb18255b7c05077e8f599fe070bd /dump.lisp
parentd7d3e70886554cb8350ea444adf384ca078673fb (diff)
downloadjson-2990f8c72975eb1de8be62ac043375fea47857eb.tar.gz
json-2990f8c72975eb1de8be62ac043375fea47857eb.zip
Introduce a way to serialize the parsed json data
Diffstat (limited to 'dump.lisp')
-rw-r--r--dump.lisp31
1 files changed, 31 insertions, 0 deletions
diff --git a/dump.lisp b/dump.lisp
new file mode 100644
index 0000000..d4f3fff
--- /dev/null
+++ b/dump.lisp
@@ -0,0 +1,31 @@
+(in-package #:json)
+
+(defun to-string (value)
+ (defun indent (str level)
+ (concatenate 'string (make-string (* 4 level) :initial-element #\Space) str))
+ (defun to-string-rec (value level)
+ (cond ((stringp value)
+ (format nil "\"~a\"" value))
+ ((symbolp value)
+ (string-downcase (symbol-name value)))
+ ((arrayp value)
+ (format nil
+ "[~&~{~a~^,~&~}]"
+ (map 'list (lambda (x) (indent (to-string-rec x (1+ level)) level))
+ value)))
+ ((hash-table-p value)
+ (let (items)
+ (maphash (lambda (k v)
+ (push (indent (format nil "\"~a\": ~a" k (to-string-rec v (1+ level))) level)
+ items))
+ value)
+ (format nil "{~&~{~a~^,~&~}}" items)))
+ (t value)))
+ (to-string-rec value 0))
+
+(defun to-file (value filename)
+ (with-open-file (s filename
+ :direction :output
+ :if-exists :supersede
+ :if-does-not-exist :create)
+ (princ (to-string value) s) t))