summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--dump.lisp31
-rw-r--r--json.asd3
-rw-r--r--load.lisp (renamed from json.lisp)0
-rw-r--r--package.lisp4
4 files changed, 36 insertions, 2 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))
diff --git a/json.asd b/json.asd
index 0af37a2..d6d5f66 100644
--- a/json.asd
+++ b/json.asd
@@ -4,4 +4,5 @@
#:monparser)
:components
((:file "package")
- (:file "json")))
+ (:file "dump")
+ (:file "load")))
diff --git a/json.lisp b/load.lisp
index 80fef99..80fef99 100644
--- a/json.lisp
+++ b/load.lisp
diff --git a/package.lisp b/package.lisp
index d2b1e6f..2c7c3a7 100644
--- a/package.lisp
+++ b/package.lisp
@@ -1,4 +1,6 @@
(defpackage #:json
(:use #:cl #:parser)
(:export #:parse-string
- #:parse-file))
+ #:parse-file
+ #:to-string
+ #:to-file))