/**
 * Serialization
 *
 * Demonstrates how to serialize an object to a file.
 */

class Item
{
    // custom serialize
    public method to_data (data)
    {
        data.write(@one)
        data.write(@two)
    }

    // custom deserialize
    public method initialize_data (data)
    {
        field @one = data.read()
        field @two = data.read()
    }

    method initialize (one, two)
    {
        field @one = one
        field @two = two
    }
}

method main ()
{
    path = "c:/temp/test.txt"
    System.IO.File.delete(path)

    // create object
    obj = Item.new("car", "truck")

    // serialize
    data = Data.new(obj)
    stream = System.IO.FileStream.new(path, "w", "c", "w")
    stream.write_data(data)
    stream.close()

    // deserialize
    obj = nil
    stream = System.IO.FileStream.new(path, "r", "e", "r")
    data = stream.read_data()
    obj = data.get()
    stream.close()

    // print object
    print(obj.type())
    print(obj.@one)
    print(obj.@two)
}