[rucksack-devel] Using rucksack for simple object serialization.

Klaus Harbo klaus at mu.dk
Sat Sep 30 08:50:32 UTC 2006


Arthur Lemmens wrote:
> Have you seen the code for serialization buffers in heap.lisp?  That also
> does serialization to a vector of (unsigned-byte 8).
No, I hadn't.  I think what I've done may be redundant.  See sketch below.

>> let me know if you're interested in a patch.
>
> Yes, I'd like to have a look at your approach.  Thanks.
>
> Arthur
>

I think what I did is straightforward, namely

     (defclass serializer () ())

     (defclass stream-serializer (serializer)
       ((stream :initarg :stream :reader serializer-stream
                :documentation "An (unsigned-byte 8) stream.")))

     (defclass vector-serializer (serializer)
       ((vector :initform (make-array '(256)
                                      :element-type '(unsigned-byte 8)
                                      :adjustable t
                                      :fill-pointer 0)
                :reader serializer-vector)
        (read-offset :initform 0 :accessor serializer-offset)))

combined with

     (defgeneric serialize-byte (byte serializer)
       (:method ((byte integer) (serializer stream-serializer))
        (write-byte byte (serializer-stream serializer)))
       (:method ((byte integer) (serializer vector-serializer))
         (vector-push-extend byte (serializer-vector serializer)))
       (:method ((byte integer) (stream stream))
        (write-byte byte stream))
       (:documentation "Writes an unsigned-byte to a serializer."))

     (defgeneric deserialize-byte (serializer &optional eof-error-p)
       (:method ((serializer stream-serializer) &optional (eof-error-p t))
        (read-byte (serializer-stream serializer) eof-error-p nil))
       (:method ((serializer vector-serializer) &optional (eof-error-p t))
         (with-slots (vector read-offset) serializer
           (when (>= read-offset (fill-pointer vector))
               (error "End of file in vector."))
           (prog1
              (aref vector read-offset)
             (incf read-offset))))
       (:method ((stream stream) &optional (eof-error-p t))
        (read-byte stream eof-error-p nil))
       (:documentation "Reads an unsigned-byte from a serializer.  
EOF-ERROR-P
     defaults to T."))

I think that is pretty much it; at least I don't remember making any 
other changes. 

A quick glance at heap.lisp suggests to me that it is pretty much 
equivalent to the code above. 

I obviously don't understand the overall organization of the files, but 
I never thought to look in heap.lisp for a vector-serializer!  Perhaps 
the location is due to your `serialization-buffer' inheriting from `buffer'?

-Klaus.



More information about the rucksack-devel mailing list