From fdeb8e5de32f21a4a016b99165d248a7d8c3abde Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Manuel=20Tom=C3=A1s?= Date: Fri, 3 Feb 2023 14:39:39 -0300 Subject: Change api and add object generation --- dump.lisp | 31 ------------------------------- generate.lisp | 9 +++++++++ json.asd | 5 +++-- load.lisp | 4 ++-- package.lisp | 7 ++++--- save.lisp | 31 +++++++++++++++++++++++++++++++ 6 files changed, 49 insertions(+), 38 deletions(-) delete mode 100644 dump.lisp create mode 100644 generate.lisp create mode 100644 save.lisp diff --git a/dump.lisp b/dump.lisp deleted file mode 100644 index d4f3fff..0000000 --- a/dump.lisp +++ /dev/null @@ -1,31 +0,0 @@ -(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/generate.lisp b/generate.lisp new file mode 100644 index 0000000..de213a4 --- /dev/null +++ b/generate.lisp @@ -0,0 +1,9 @@ +(in-package #:json) + +(defmacro obj (&rest fields) + (let ((o (gensym)) + (f (gensym))) + `(let ((,o (make-hash-table :test #'equal :size ,(length fields)))) + (dolist (,f ',fields) + (setf (gethash (car ,f) ,o) (cadr ,f))) + ,o))) diff --git a/json.asd b/json.asd index d6d5f66..13436f6 100644 --- a/json.asd +++ b/json.asd @@ -4,5 +4,6 @@ #:monparser) :components ((:file "package") - (:file "dump") - (:file "load"))) + (:file "save") + (:file "load") + (:file "generate"))) diff --git a/load.lisp b/load.lisp index c8c1194..c808774 100644 --- a/load.lisp +++ b/load.lisp @@ -113,8 +113,8 @@ (_ whitespace)) v)) -(defun parse-string (str) +(defun from-string (str) (run json-value (input:from-string str))) -(defun parse-file (file) +(defun from-file (file) (run json-value (input:from-file file))) diff --git a/package.lisp b/package.lisp index 2c7c3a7..d4024f8 100644 --- a/package.lisp +++ b/package.lisp @@ -1,6 +1,7 @@ (defpackage #:json (:use #:cl #:parser) - (:export #:parse-string - #:parse-file + (:export #:from-string + #:from-file #:to-string - #:to-file)) + #:to-file + #:obj)) 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)) -- cgit v1.2.3