June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
25
Task/Object-serialization/Haskell/object-serialization.hs
Normal file
25
Task/Object-serialization/Haskell/object-serialization.hs
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
{-# LANGUAGE DeriveGeneric #-}
|
||||
|
||||
module Main (main) where
|
||||
|
||||
import qualified Data.ByteString.Lazy as ByteString (readFile, writeFile)
|
||||
import Data.Binary (Binary)
|
||||
import qualified Data.Binary as Binary (decode, encode)
|
||||
import GHC.Generics (Generic)
|
||||
|
||||
data Employee =
|
||||
Manager String String
|
||||
| IndividualContributor String String
|
||||
deriving (Generic, Show)
|
||||
instance Binary Employee
|
||||
|
||||
main :: IO ()
|
||||
main = do
|
||||
ByteString.writeFile "objects.dat" $ Binary.encode
|
||||
[ IndividualContributor "John Doe" "Sales"
|
||||
, Manager "Jane Doe" "Engineering"
|
||||
]
|
||||
|
||||
bytes <- ByteString.readFile "objects.dat"
|
||||
let employees = Binary.decode bytes
|
||||
print (employees :: [Employee])
|
||||
37
Task/Object-serialization/Julia/object-serialization.julia
Normal file
37
Task/Object-serialization/Julia/object-serialization.julia
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
abstract type Hello end
|
||||
|
||||
struct HelloWorld <: Hello
|
||||
name::String
|
||||
HelloWorld(s) = new(s)
|
||||
end
|
||||
|
||||
struct HelloTime <: Hello
|
||||
name::String
|
||||
tnew::DateTime
|
||||
HelloTime(s) = new(s, now())
|
||||
end
|
||||
|
||||
sayhello(hlo) = println("Hello to this world, $(hlo.name)!")
|
||||
|
||||
sayhello(hlo::HelloTime) = println("It is now $(now()). Hello from back in $(hlo.tnew), $(hlo.name)!")
|
||||
|
||||
h1 = HelloWorld("world")
|
||||
h2 = HelloTime("new world")
|
||||
|
||||
sayhello(h1)
|
||||
sayhello(h2)
|
||||
|
||||
fh = open("objects.dat", "w")
|
||||
serialize(fh, h1)
|
||||
serialize(fh,h2)
|
||||
close(fh)
|
||||
|
||||
sleep(10)
|
||||
|
||||
fh = open("objects.dat", "r")
|
||||
hh1 = deserialize(fh)
|
||||
hh2 = deserialize(fh)
|
||||
close(fh)
|
||||
|
||||
sayhello(hh1)
|
||||
sayhello(hh2)
|
||||
57
Task/Object-serialization/Kotlin/object-serialization.kotlin
Normal file
57
Task/Object-serialization/Kotlin/object-serialization.kotlin
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
// version 1.2.0
|
||||
|
||||
import java.io.*
|
||||
|
||||
open class Entity(val name: String = "Entity"): Serializable {
|
||||
override fun toString() = name
|
||||
|
||||
companion object {
|
||||
val serialVersionUID = 3504465751164822571L
|
||||
}
|
||||
}
|
||||
|
||||
class Person(name: String = "Brian"): Entity(name), Serializable {
|
||||
companion object {
|
||||
val serialVersionUID = -9170445713373959735L
|
||||
}
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val instance1 = Person()
|
||||
println(instance1)
|
||||
|
||||
val instance2 = Entity()
|
||||
println(instance2)
|
||||
|
||||
// serialize
|
||||
try {
|
||||
val out = ObjectOutputStream(FileOutputStream("objects.dat"))
|
||||
out.writeObject(instance1)
|
||||
out.writeObject(instance2)
|
||||
out.close()
|
||||
println("Serialized...")
|
||||
}
|
||||
catch (e: IOException) {
|
||||
println("Error occurred whilst serializing")
|
||||
System.exit(1)
|
||||
}
|
||||
|
||||
// deserialize
|
||||
try {
|
||||
val inp = ObjectInputStream(FileInputStream("objects.dat"))
|
||||
val readObject1 = inp.readObject()
|
||||
val readObject2 = inp.readObject()
|
||||
inp.close()
|
||||
println("Deserialized...")
|
||||
println(readObject1)
|
||||
println(readObject2)
|
||||
}
|
||||
catch (e: IOException) {
|
||||
println("Error occurred whilst deserializing")
|
||||
System.exit(1)
|
||||
}
|
||||
catch (e: ClassNotFoundException) {
|
||||
println("Unknown class for deserialized object")
|
||||
System.exit(1)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue