[bknr-cvs] hans changed trunk/libraries/yason/

BKNR Commits bknr at bknr.net
Tue Jan 27 07:12:46 UTC 2009


Revision: 4169
Author: hans
URL: http://bknr.net/trac/changeset/4169

ENCODE-PLIST and ENCODE-ALIST contributed by Nathan Froyd.

U   trunk/libraries/yason/encode.lisp
U   trunk/libraries/yason/package.lisp

Modified: trunk/libraries/yason/encode.lisp
===================================================================
--- trunk/libraries/yason/encode.lisp	2009-01-26 14:23:00 UTC (rev 4168)
+++ trunk/libraries/yason/encode.lisp	2009-01-27 07:12:45 UTC (rev 4169)
@@ -52,6 +52,11 @@
 (defmethod encode ((object integer) &optional (stream *standard-output*))
   (princ object stream))
 
+(defun encode-key/value (key value stream)
+  (encode string stream)
+  (write-char #\: stream)
+  (encode value stream))
+
 (defmethod encode ((object hash-table) &optional (stream *standard-output*))
   (write-char #\{ stream)
   (let (printed)
@@ -59,9 +64,7 @@
                (if printed
                    (write-char #\, stream)
                    (setf printed t))
-               (encode key stream)
-               (write-char #\: stream)
-               (encode value stream))
+               (encode-key/value key value stream))
              object))
   (write-char #\} stream)
   object)
@@ -90,6 +93,32 @@
   (write-char #\] stream)
   object)
 
+(defun encode-symbol/value (symbol value stream)
+  (let ((string (symbol-name symbol)))
+    (encode-key/value string value stream)))
+
+(defun encode-alist (object &optional (stream *standard-output*))
+  (loop initially (write-char #\{ stream)
+     with printed = nil
+     for (key . value) in object
+     do (if printed
+            (write-char #\, stream)
+            (setf printed t))
+       (encode-symbol/value key value stream)
+     finally (write-char #\} stream)
+       (return object)))
+
+(defun encode-plist (object &optional (stream *standard-output*))
+  (loop initially (write-char #\{ stream)
+     with printed = nil
+     for (key value . rest) on object by #'cddr
+     do (if printed
+            (write-char #\, stream)
+            (setf printed t))
+       (encode-symbol/value key value stream)
+     finally (write-char #\} stream)
+       (return object)))
+
 (defmethod encode ((object (eql 'true)) &optional (stream *standard-output*))
   (write-string "true" stream)
   object)
@@ -187,9 +216,7 @@
 encoded using the ENCODE generic function, so they both must be of a
 type for which an ENCODE method is defined."
   (next-aggregate-element)
-  (encode key (output-stream *json-output*))
-  (write-char #\: (output-stream *json-output*))
-  (encode value (output-stream *json-output*))
+  (encode-key/value key value (output-stream *json-output*))
   value)
 
 (defun encode-object-elements (&rest elements)
@@ -226,4 +253,4 @@
    the ENCODE-SLOTS method as appropriate.")
   (:method (object)
     (with-object ()
-      (json:encode-slots object))))
\ No newline at end of file
+      (json:encode-slots object))))

Modified: trunk/libraries/yason/package.lisp
===================================================================
--- trunk/libraries/yason/package.lisp	2009-01-26 14:23:00 UTC (rev 4168)
+++ trunk/libraries/yason/package.lisp	2009-01-27 07:12:45 UTC (rev 4169)
@@ -25,6 +25,8 @@
    #:encode
    #:encode-slots
    #:encode-object
+   #:encode-plist
+   #:encode-alist
 
    ;; Streaming encoder interface
    #:with-output





More information about the Bknr-cvs mailing list