Object serialization: Difference between revisions
Content added Content deleted
(Added EchoLisp) |
|||
Line 703: | Line 703: | ||
<file:objects.dat>.setBytes(surgeon.serialize(objs)) |
<file:objects.dat>.setBytes(surgeon.serialize(objs)) |
||
stdout.println(surgeon.unserialize(<file:objects.dat>.getBytes()))</lang> |
stdout.println(surgeon.unserialize(<file:objects.dat>.getBytes()))</lang> |
||
=={{header|EchoLisp}}== |
|||
Our classes will be 'struct' objects, with custom #:tostring procedures, as required. The instances are saved/restored to/from local storage. Serialization is performed by JSONifying the objects. References between instances of struct's are kept in the process. |
|||
<lang lisp> |
|||
(define (person->string self) (format "%a : person." (person-name self))) |
|||
(define (writer->string self) (format "%a: writer of %a." |
|||
(person-name self) |
|||
(writer-books self))) |
|||
(define (father->string self) (format "%a: father of %a." |
|||
(person-name self) |
|||
(map person-name (father-children self)))) |
|||
; 'classes' definition, with inheritance. |
|||
; a writer is a person, too. |
|||
(struct person (name) #:tostring person->string) |
|||
(struct writer person (books) #:tostring writer->string) |
|||
(struct father person (children) #:tostring father->string) |
|||
(define simon (writer "Simon" '(my-life my-wife my-bike))) |
|||
(define elvis (person "Elvis")) |
|||
(define papa (father "papa" (list simon elvis))) |
|||
(local-put-value 'simon simon "objects.dat") |
|||
π local-db: local-put:unknown store : "objects.dat" |
|||
;; forgot to create the 'file'. |
|||
(local-make-store "objects.dat") β "objects.dat" |
|||
(local-put-value 'simon simon "objects.dat") |
|||
(local-put-value 'elvis elvis "objects.dat") |
|||
(local-put-value 'papa papa "objects.dat") |
|||
;; inspect |
|||
simon β Simon: writer of (my-life my-wife my-bike). |
|||
papa β papa: father of (Simon Elvis). |
|||
elvis β Elvis : person. |
|||
</lang> |
|||
{{output}} |
|||
<lang lisp> |
|||
;; reboot (close the browser window) |
|||
(local-keys 'objects.dat) β ("elvis" "papa" "simon") |
|||
(define simon (local-get-value 'simon "objects.dat")) |
|||
(define elvis (local-get-value 'elvis "objects.dat")) |
|||
(define papa (local-get-value 'papa "objects.dat")) |
|||
; data are restored |
|||
simon β Simon: writer of (my-life my-wife my-bike). |
|||
papa β papa: father of (Simon Elvis). |
|||
;; check if references (pointers) are restored |
|||
(set-writer-name! simon "Antoinette") β "Antoinette" |
|||
simonβ Antoinette: writer of (my-life my-wife my-bike). |
|||
;; inspect |
|||
papa β papa: father of (Antoinette Elvis). ; YES π³ ! |
|||
</lang> |
|||
=={{header|Erlang}}== |
=={{header|Erlang}}== |
||
Erlang is not object oriented. This code is based upon my understanding of the Algol 68 example above. |
Erlang is not object oriented. This code is based upon my understanding of the Algol 68 example above. |