Object serialization: Difference between revisions
Content added Content deleted
m (Added self-referencing note to EchoLisp) |
m (Fixed self-reference bug. EchoLisp 2.11) |
||
Line 705: | Line 705: | ||
=={{header|EchoLisp}}== |
=={{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. |
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. Auto-references and circular references are correctly maintained since EchoLisp version 2.11. |
||
<lang lisp> |
<lang lisp> |
||
(define (person->string self) (format "%a : person." (person-name self))) |
(define (person->string self) (format "%a : person." (person-name self))) |
||
Line 727: | Line 727: | ||
(local-put-value 'simon simon "objects.dat") |
(local-put-value 'simon simon "objects.dat") |
||
📕 local-db: local-put:unknown store : "objects.dat" |
📕 local-db: local-put:unknown store : "objects.dat" |
||
;; forgot to create the |
;; forgot to create the store. Create it : |
||
(local-make-store "objects.dat") → "objects.dat" |
(local-make-store "objects.dat") → "objects.dat" |
||
Line 742: | Line 742: | ||
<lang lisp> |
<lang lisp> |
||
;; reboot (close the browser window) |
;; reboot (close the browser window) |
||
; inspect objects.dat : |
|||
(local-keys 'objects.dat) → ("elvis" "papa" "simon") |
(local-keys 'objects.dat) → ("elvis" "papa" "simon") |
||
Line 759: | Line 760: | ||
papa → papa: father of (Antoinette Elvis). ; YES 😳 ! |
papa → papa: father of (Antoinette Elvis). ; YES 😳 ! |
||
;; |
;; - Self-referencing (EchoLisp version 2.11) |
||
;; add 'papa' to the chidren of 'papa' - whatever this means - and print it : |
;; add 'papa' to the chidren of 'papa' - whatever this means - and print it : |
||
(set-father-children! papa (list simon papa elvis)) |
(set-father-children! papa (list simon papa elvis)) |
||
papa → papa: father of (Antoinette papa Elvis). |
papa → papa: father of (Antoinette papa Elvis). |
||
; save/restore |
|||
; ❌ ... but ... (known bug in the current implementation) |
|||
(local-put-value 'papa papa "objects.dat") |
(local-put-value 'papa papa "objects.dat") |
||
(define papa (local-get-value 'papa "objects.dat")) |
|||
; will produce a "Too much recursion' error. |
|||
papa → papa: father of (Antoinette papa Elvis). |
|||
</lang> |
</lang> |
||