Object serialization: Difference between revisions
Content added Content deleted
m (→{{header|Racket}}: documentation references) |
(→{{header|Racket}}: actual implementation added) |
||
Line 1,527: | Line 1,527: | ||
Serialization is described in the Racket documentation in: [http://docs.racket-lang.org/reference/serialization.html?q=serialize Serialization], and more specifically with respect to object oriented programming classes: [http://docs.racket-lang.org/reference/objectserialize.html?q=serializable-class#%28form._%28%28lib._racket%2Fprivate%2Fclass-internal..rkt%29._define-serializable-class%29%29 Object Serialization]. |
Serialization is described in the Racket documentation in: [http://docs.racket-lang.org/reference/serialization.html?q=serialize Serialization], and more specifically with respect to object oriented programming classes: [http://docs.racket-lang.org/reference/objectserialize.html?q=serializable-class#%28form._%28%28lib._racket%2Fprivate%2Fclass-internal..rkt%29._define-serializable-class%29%29 Object Serialization]. |
||
The serialization needs to be included with |
The serialization needs to be included with |
||
<lang racket>(require racket/serialize)</lang> |
|||
The rest is covered by the Racket language. |
|||
<lang racket> |
<lang racket>#lang racket |
||
;; Object Serialization: Tim Brown, Oct. 2014 |
|||
(require racket/serialize) |
|||
(define (join-person-name-list persons) |
|||
</lang> |
|||
(string-join (map (λ (c) (send c ->string)) persons) ", ")) |
|||
(define-serializable-class person% object% |
|||
(init-field name [siblings null]) |
|||
(define/public (->string #:show (show null)) |
|||
(cond |
|||
[(and (member 'siblings show) (not (null? siblings))) |
|||
(format "~a (~a)" name (join-person-name-list siblings))] |
|||
[else name])) |
|||
(super-new)) |
|||
(define-serializable-class parent% person% |
|||
(init-field [children null]) |
|||
(define/override (->string #:show (show null)) |
|||
(cond |
|||
[(and (member 'children show) (not (null? children))) |
|||
(format "~a [~a]" (super ->string #:show show) (join-person-name-list children))] |
|||
[else (super ->string #:show show)])) |
|||
(super-new)) |
|||
;; horribly out of fashion and probaly no longer PC |
|||
(define-serializable-class nuclear-family% object% |
|||
(init-field father mother children) |
|||
(define/public (->string) |
|||
(string-append |
|||
(format "~a + ~a -> " (send father ->string) (send mother ->string)) |
|||
(format "~a" (join-person-name-list children)))) |
|||
(super-new)) |
|||
;; =| TESTS |========================================================================================= |
|||
(define jack (new person% [name "Jack"])) |
|||
(define joan (new person% [name "Joan"])) |
|||
(set-field! siblings jack (list joan)) |
|||
(set-field! siblings joan (list jack)) |
|||
(define the-kids (list jack joan)) |
|||
(define john (new parent% [name "John"] [children the-kids])) |
|||
(define jane (new parent% [name "Jane"] [children the-kids])) |
|||
(define the-family |
|||
(new nuclear-family% [father john] [mother jane] [children the-kids])) |
|||
(define (duplicate-object-through-file o f-name) |
|||
(with-output-to-file f-name #:exists 'replace (λ () (write (serialize o)))) |
|||
(with-input-from-file f-name (λ () (deserialize (read))))) |
|||
(define cloned-family (duplicate-object-through-file the-family "objects.dat")) |
|||
(printf "The original family:\t~a~%" (send the-family ->string)) |
|||
(printf "The cloned family:\t~a~%~%" (send cloned-family ->string)) |
|||
(printf "objects.dat contains ----~%~a~%-------------------~%~%" (file->string "objects.dat")) |
|||
(printf "Clones are different?~%") |
|||
(define cloned-jack (first (get-field children cloned-family))) |
|||
(set-field! name cloned-jack "JACK") |
|||
(printf "Jack's name is:\t~s~%" (get-field name jack)) |
|||
(printf "Clone's name is:\t~s~%~%" (get-field name cloned-jack)) |
|||
(printf "Relationships are maintained?~%") |
|||
(define cloned-joan (second (get-field children cloned-family))) |
|||
(printf "Joan's description with siblings:\t~s~%" (send joan ->string #:show '(siblings))) |
|||
(printf "Clone's description with siblings:\t~s~%~%" |
|||
(send cloned-joan ->string #:show '(siblings))) |
|||
(printf "After Jack's renaming the cloned family is: ~a~%~%" (send cloned-family ->string)) |
|||
(printf "Various descriptions of cloned John:~%") |
|||
(define cloned-john (get-field father cloned-family)) |
|||
(printf "Just the name:\t~s~%" (send cloned-john ->string)) |
|||
(printf "With siblings:\t~s (he hasn't any)~%" (send cloned-john ->string #:show '(siblings))) |
|||
(printf "With children:\t~s~%" (send cloned-john ->string #:show '(children))) |
|||
(printf "With both:\t~s~%" (send cloned-john ->string #:show '(siblings children)))</lang> |
|||
{{out}} |
{{out}} |
||
<pre>Language: racket [custom]; memory limit: 512 MB. |
|||
<pre> |
|||
The original family: John + Jane -> Jack, Joan |
|||
</pre> |
|||
The cloned family: John + Jane -> Jack, Joan |
|||
objects.dat contains ---- |
|||
((3) 3 ((#"C:\\Users\\Tim\\Dropbox\\rosettacode\\Serializable-Objects.rkt" . deserialize-info:person%) (#"C:\\Users\\Tim\\Dropbox\\rosettacode\\Serializable-Objects.rkt" . deserialize-info:nuclear-family%) (#"C:\\Users\\Tim\\Dropbox\\rosettacode\\Serializable-Objects.rkt" . deserialize-info:parent%)) 4 ((q . #(())) #&0 (0 (c (? . 0) c "Joan" c (c (? . 1)))) (c (? . 1) c (? . 2))) ((1 0 (c (? . 0) c "Jack" c (c (? . 2))))) (1 (c (? . 0) c (2 (c (v! (c (? . 0) q "John" ())) c (? . 3))) c (2 (c (v! (c (? . 0) q "Jane" ())) c (? . 3))) c (? . 3)))) |
|||
------------------- |
|||
Clones are different? |
|||
Jack's name is: "Jack" |
|||
Clone's name is: "JACK" |
|||
Relationships are maintained? |
|||
Joan's description with siblings: "Joan (Jack)" |
|||
Clone's description with siblings: "Joan (JACK)" |
|||
After Jack's renaming the cloned family is: John + Jane -> JACK, Joan |
|||
Various descriptions of cloned John: |
|||
Just the name: "John" |
|||
With siblings: "John" (he hasn't any) |
|||
With children: "John [JACK, Joan]" |
|||
With both: "John [JACK, Joan]"</pre> |
|||
=={{header|Ruby}}== |
=={{header|Ruby}}== |