RosettaCodeData/Task/Object-serialization/Neko/object-serialization.neko

42 lines
1.1 KiB
Plaintext

/* Object serialization, in Neko */
var file_open = $loader.loadprim("std@file_open", 2)
var file_write = $loader.loadprim("std@file_write", 4)
var file_read = $loader.loadprim("std@file_read", 4)
var file_close = $loader.loadprim("std@file_close", 1)
var serialize = $loader.loadprim("std@serialize", 1)
var unserialize = $loader.loadprim("std@unserialize", 2)
/* Inheritance by prototype */
proto = $new(null)
proto.print = function () { $print(this, "\n") }
obj = $new(null)
obj.msg = "Hello"
obj.dest = $array("Town", "Country", "World")
$objsetproto(obj, proto)
$print("Original:\n")
obj.print()
/* Serialize the object */
var thing = serialize(obj)
var len = $ssize(thing)
/* To disk */
var f = file_open("object-serialization.bin", "w")
file_write(f, thing, 0, len)
file_close(f)
/* Load the binary data into a new string space */
f = file_open("object-serialization.bin", "r")
var buff = $smake(len)
file_read(f, buff, 0, len)
file_close(f)
/* Unserialize the object into a new variable */
var other = unserialize(buff, $loader)
$print("deserialized:\n")
other.print()