top
up

8.4Datatypes and SerializationπŸ”— i

Prefab structure types (see Prefab Structure 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 (inout)(make-pipe ))
> (write #s(sproutbean)out)
> (read in)

'#s(sprout bean)

Other structure types created by 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 case can the result be read back in as an instance of the structure type:

> (struct posn(xy))
> (write (posn12))

#<posn>

> (define-values (inout)(make-pipe ))
> (write (posn12)out)
> (read in)

pipe::1: read: bad syntax `#<`

> (struct posn(xy)#:transparent)
> (write (posn12))

#(struct:posn 1 2)

> (define-values (inout)(make-pipe ))
> (write (posn12)out)
> (define v(read in))
> v

'#(struct:posn 1 2)

> (posn?v)

#f

> (vector? v)

#t

The serializable-struct form defines a structure type that can be serialize d to a value that can be printed using write and restored via read . The serialize d result can be deserialize d to get back an instance of the original structure type. The serialization form and functions are provided by the racket/serialize library.

Examples:
> (require racket/serialize)
> (serializable-struct posn(xy)#:transparent)
> (deserialize (serialize (posn12)))

(posn 1 2)

> (write (serialize (posn12)))

((3) 1 ((#f . deserialize-info:posn-v0)) 0 () () (0 1 2))

> (define-values (inout)(make-pipe ))
> (write (serialize (posn12))out)
> (deserialize (read in))

(posn 1 2)

In addition to the names bound by struct , serializable-struct binds an identifier with deserialization information, and it automatically provide s the deserialization identifier from a module context. This deserialization identifier is accessed reflectively when a value is deserialized.

top
up

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /