Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
61
Task/Object-serialization/Wren/object-serialization.wren
Normal file
61
Task/Object-serialization/Wren/object-serialization.wren
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
import "/json" for JSON
|
||||
import "io" for File, FileFlags
|
||||
|
||||
class Entity {
|
||||
construct new(name) {
|
||||
_name = name
|
||||
}
|
||||
|
||||
name { _name }
|
||||
|
||||
// JSON representation
|
||||
toString { "{\"name\": \"%(_name)\"}" }
|
||||
|
||||
// mimics the JSON output
|
||||
print() { System.print(this.toString.replace("\"", "")) }
|
||||
|
||||
serialize(fileName) {
|
||||
var o = JSON.parse(this.toString)
|
||||
File.openWithFlags(fileName, FileFlags.writeOnly) { |file|
|
||||
file.writeBytes("%(o)\n")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class Person is Entity {
|
||||
construct new(name, age) {
|
||||
super(name)
|
||||
_age = age
|
||||
}
|
||||
|
||||
// JSON representation
|
||||
toString { "{\"name\": \"%(name)\", \"age\": \"%(_age)\"}" }
|
||||
|
||||
// mimics the JSON output
|
||||
print() { System.print(this.toString.replace("\"", "")) }
|
||||
|
||||
serialize(fileName) {
|
||||
var o = JSON.parse(this.toString)
|
||||
File.openWithFlags(fileName, FileFlags.writeOnly) { |file|
|
||||
file.writeBytes("%(o)\n")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// create file for serialization
|
||||
var fileName = "objects.dat"
|
||||
var file = File.create(fileName)
|
||||
file.close()
|
||||
|
||||
System.print("Calling print methods gives:")
|
||||
|
||||
var e = Entity.new("John")
|
||||
e.print()
|
||||
e.serialize(fileName)
|
||||
|
||||
var p = Person.new("Fred", 35)
|
||||
p.print()
|
||||
p.serialize(fileName)
|
||||
|
||||
System.print("\nContents of objects.dat are:")
|
||||
System.print(File.read(fileName))
|
||||
Loading…
Add table
Add a link
Reference in a new issue