Version 1 of Serializing TclOO objects

Updated 2009-05-08 11:27:44 by glennj

In Playing with TclOO, Richard Suchenwirth provided a method to dump the class and variables of an object:

 define oo::object method dump {{pat *}} {
    set res [list class [info object class [self]]]
    foreach i [info object vars [self] $pat] {
        my variable $i
        lappend res $i [set $i]
    }
    set res
 }

This page should explore ways to serialize/unserialize TclOO objects.

My first thought would be to check if the self's variables themselves are objects:

 define oo::object method serialize {{pat *}} {
    set res [list class [info object class [self]]]
    foreach i [info object vars [self] $pat] {
        my variable $i
        if {[info object isa object [set $i]]} {
            set value [[set $i] serialize]
        } else {
            set value [set $i]
        }
        lappend res $i $value
    }
    set res
 }