8.4 Datatypes and Serialization
Prefab structure types (see Prefab Stucture Types) automatically support serialization: they can be written to an output stream, and a copy can be read back in from an input stream:
> (define-values (in out) (make-pipe)) |
> (write #s(sprout bean) out) |
> (read in) |
#s(sprout bean) |
Other structure types created by define-struct, which offer more abstraction than prefab structure types, normally write either using #<....> notation (for opaque structure types) or using #(....) vector notation (for transparent structure types). In neither can can the result be read back in as an instance of the structure type:
> (define-struct posn (x y)) |
> (write (make-posn 1 2)) |
#<posn> |
> (define-values (in out) (make-pipe)) |
> (write (make-posn 1 2) out) |
> (read in) |
UNKNOWN::0: read: bad syntax `#<’ |
> (define-struct posn (x y) #:transparent) |
> (write (make-posn 1 2)) |
#(struct:posn 1 2) |
> (define-values (in out) (make-pipe)) |
> (write (make-posn 1 2) out) |
> v |
#(struct:posn 1 2) |
> (posn? v) |
#f |
> (vector? v) |
#t |
The define-serializable-struct form defines a structure type that can be serialized to a value that can be printed using write and restored via read. The serialized result can be deserialized to get back an instance of the original structure type. The serialization form and functions are provided by the scheme/serialize library.
Examples: |
> (require scheme/serialize) |
> (define-serializable-struct posn (x y) #:transparent) |
> (deserialize (serialize (make-posn 1 2))) |
#(struct:posn 1 2) |
((1) 1 ((#f . deserialize-info:posn-v0)) 0 () () (0 1 2)) |
> (define-values (in out) (make-pipe)) |
> (deserialize (read in)) |
#(struct:posn 1 2) |
In addition to the names bound by define-struct, define-serializable-struct binds an identifier with deserialization information, and it automatically provides the deserialization identifier from a module context. This deserialization identifier is accessed reflectively when a value is deserialized.