Serialisation.borg

Serialisation.borg


{
object(x,y)::
    {
    setxy(X,Y):: {x:=X; y:=Y};
    print():: display(x+y);
    clone()
    };


container()::
    {
    free:1;
    elements[10]:void;
    add(o)::
        {
        elements[free]:=o;
        free:=free+1
        };
    print()::
        for(i:1,i<=size(elements),i:=i+1,
            {
            o:elements[i];
            display(text(i)+": ");
            if (is_void(o),
                display(""+eoln),
                {o.print();
                display(eoln)})
            });
    clone()
    };


c:container();

serialize()::
    {
    ` first we create the container and add some objects
    c.add(object(1,100));
    c.add(object(2,200));
    c.add(object(3,300));
    c.add(object(4,400));
    c.add(object(5,500));
    c.add(object(6,600));
    c.add(object(7,700));
    c.print();
    ` now we serialize everything, but we unlink all dictionaries
    ` at the agent level.
    exp:ser(c,agentself);
    ` deserialize everything and relink at agentself again
    d:deser(exp,agentself);
    display("c~d = ",text(c~d),eoln);
    d.print();
    ` one of the nice features of this is that we can sandbox
    ` a deserialized expression this way. For example. We redefine
    ` the display operator and we link the deserialized expression
    ` to this redefined environment.
    olddisplay:display;
    display(txt):: olddisplay("--",txt);
    d:=deser(exp,clone());
    d.print()
    };

display("serialize()")
}