summaryrefslogtreecommitdiff
path: root/save.lisp
diff options
context:
space:
mode:
authorJuan Manuel Tomás <jtomas1815@gmail.com>2023-02-03 14:39:39 -0300
committerJuan Manuel Tomás <jtomas1815@gmail.com>2023-02-03 14:39:39 -0300
commitfdeb8e5de32f21a4a016b99165d248a7d8c3abde (patch)
treea98a034636d7188dfd0b8c3acde2ff69dfdc5af9 /save.lisp
parente858144ddf1e50b3a390d33961945a9063d746f0 (diff)
downloadjson-fdeb8e5de32f21a4a016b99165d248a7d8c3abde.tar.gz
json-fdeb8e5de32f21a4a016b99165d248a7d8c3abde.zip
Change api and add object generation
Diffstat (limited to 'save.lisp')
-rw-r--r--save.lisp31
1 files changed, 31 insertions, 0 deletions
diff --git a/save.lisp b/save.lisp
new file mode 100644
index 0000000..d4f3fff
--- /dev/null
+++ b/save.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))