[plt-scheme] serializing /all/ of struct

From: George Herson (gherson at snet.net)
Date: Tue Feb 13 09:22:25 EST 2007

Thanks alot, Danny.  Your code worked great.  

In MzScheme, why doesn't serialize work, and fully, by
default??  Ie, (serialize my-object) should write out
everything scheme needs to deserialize the object,
without the special define, require'd file, and
inspector (this morning I seemed to need an inspector
of #f to get the field names printing to screen).

George
--- Daniel Yoo <dyoo at cs.wpi.edu> wrote:

> 
> 
> > How to serialize a structure to a file?
> >
> > Read/tested using the info in 
> >
>
http://download.plt-scheme.org/doc/360/html/mzlib/mzlib-Z-H-39.html#node_chap_39
> 
> > but so far can only deserialize a serialized
> structure with its original 
> > field names if the program that created the
> structure is still running. 
> > A structure inspector of (make-inspector) or #f
> isn't sufficient to get 
> > my structure's field names written to file.
> 
> 
> Hi George,
> 
> You can put the structure definition in a file:
> 
> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
> (module dept-struct mzscheme
>    (require (lib "serialize.ss"))
>    (provide (struct dept (id name)))
>    (define-serializable-struct dept (id name)))
> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
> 
> This module will need to be around for later on when
> we serialize and 
> deserialize structures.  I don't think inspectors
> need to be touched here.
> 
> 
> We can then write other modules that use this
> serializable dept structure:
> 
>
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
> (module test-dept-struct mzscheme
>    (require (lib "serialize.ss")
>             "dept-struct.ss")
> 
>    (define outp (open-output-string))
> 
>    (define test-value
>      (let ([content
>             (begin
>               (write (serialize (make-dept 42 "jane
> doe")) outp)
>               (get-output-string outp))])
> 
>        (deserialize (read (open-input-string
> content))))))
>
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
> 
> 
> 
> Another example using the serialized structure is:
> 
>
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
> (module test-dept-save mzscheme
>    (require (lib "serialize.ss")
>             "dept-struct.ss")
> 
>    (when (file-exists? "dept.dat")
>      (delete-file "dept.dat"))
>    (define outp (open-output-file "dept.dat"))
>    (write (serialize (make-dept 42 "jane doe"))
> outp)
>    (close-output-port outp))
>
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
> 
> 
> which writes out to a file called "dept.dat", and:
> 
> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
> (module test-dept-load mzscheme
>    (require (lib "serialize.ss")
>             "dept-struct.ss")
> 
>    (define inp (open-input-file "dept.dat"))
>    (define v (deserialize (read inp)))
>    (printf "~a ~a~n" (dept-id v) (dept-name v)))
> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
> 
> to show that we can load that serialized structure
> back into life.
> 
> 



Posted on the users mailing list.