Object serialization: Difference between revisions
Content added Content deleted
m (→{{header|Ol}}) |
Thundergnat (talk | contribs) (Rename Perl 6 -> Raku, alphabetize, minor clean-up) |
||
Line 210: | Line 210: | ||
Name: Entity, Created: +20111111 |
Name: Entity, Created: +20111111 |
||
</pre> |
</pre> |
||
=={{header|C sharp|C#}}== |
|||
<lang csharp>using System; |
|||
using System.IO; |
|||
using System.Collections.Generic; |
|||
using System.Runtime.Serialization.Formatters.Binary; |
|||
namespace Object_serialization |
|||
{ |
|||
[Serializable] public class Being |
|||
{ |
|||
public bool Alive { get; set; } |
|||
} |
|||
[Serializable] public class Animal: Being |
|||
{ |
|||
public Animal() { } |
|||
public Animal(long id, string name, bool alive = true) |
|||
{ |
|||
Id = id; |
|||
Name = name; |
|||
Alive = alive; |
|||
} |
|||
public long Id { get; set; } |
|||
public string Name { get; set; } |
|||
public void Print() { Console.WriteLine("{0}, id={1} is {2}", |
|||
Name, Id, Alive ? "alive" : "dead"); } |
|||
} |
|||
internal class Program |
|||
{ |
|||
private static void Main() |
|||
{ |
|||
string path = |
|||
Environment.GetFolderPath(Environment.SpecialFolder.Desktop)+"\\objects.dat"; |
|||
var n = new List<Animal> |
|||
{ |
|||
new Animal(1, "Fido"), |
|||
new Animal(2, "Lupo"), |
|||
new Animal(7, "Wanda"), |
|||
new Animal(3, "Kiki", alive: false) |
|||
}; |
|||
foreach(Animal animal in n) |
|||
animal.Print(); |
|||
using(var stream = new FileStream(path, FileMode.Create, FileAccess.Write)) |
|||
new BinaryFormatter().Serialize(stream, n); |
|||
n.Clear(); |
|||
Console.WriteLine("---------------"); |
|||
List<Animal> m; |
|||
using(var stream = new FileStream(path, FileMode.Open, FileAccess.Read)) |
|||
m = (List<Animal>) new BinaryFormatter().Deserialize(stream); |
|||
foreach(Animal animal in m) |
|||
animal.Print(); |
|||
} |
|||
} |
|||
}</lang> |
|||
<pre>Fido, id=1 is alive |
|||
Lupo, id=2 is alive |
|||
Wanda, id=7 is alive |
|||
Kiki, id=3 is dead |
|||
--------------- |
|||
Fido, id=1 is alive |
|||
Lupo, id=2 is alive |
|||
Wanda, id=7 is alive |
|||
Kiki, id=3 is dead</pre> |
|||
=={{header|C++}}== |
=={{header|C++}}== |
||
Line 347: | Line 422: | ||
wage per hour: 55.35 |
wage per hour: 55.35 |
||
</pre> |
</pre> |
||
=={{header|C sharp|C#}}== |
|||
<lang csharp>using System; |
|||
using System.IO; |
|||
using System.Collections.Generic; |
|||
using System.Runtime.Serialization.Formatters.Binary; |
|||
namespace Object_serialization |
|||
{ |
|||
[Serializable] public class Being |
|||
{ |
|||
public bool Alive { get; set; } |
|||
} |
|||
[Serializable] public class Animal: Being |
|||
{ |
|||
public Animal() { } |
|||
public Animal(long id, string name, bool alive = true) |
|||
{ |
|||
Id = id; |
|||
Name = name; |
|||
Alive = alive; |
|||
} |
|||
public long Id { get; set; } |
|||
public string Name { get; set; } |
|||
public void Print() { Console.WriteLine("{0}, id={1} is {2}", |
|||
Name, Id, Alive ? "alive" : "dead"); } |
|||
} |
|||
internal class Program |
|||
{ |
|||
private static void Main() |
|||
{ |
|||
string path = |
|||
Environment.GetFolderPath(Environment.SpecialFolder.Desktop)+"\\objects.dat"; |
|||
var n = new List<Animal> |
|||
{ |
|||
new Animal(1, "Fido"), |
|||
new Animal(2, "Lupo"), |
|||
new Animal(7, "Wanda"), |
|||
new Animal(3, "Kiki", alive: false) |
|||
}; |
|||
foreach(Animal animal in n) |
|||
animal.Print(); |
|||
using(var stream = new FileStream(path, FileMode.Create, FileAccess.Write)) |
|||
new BinaryFormatter().Serialize(stream, n); |
|||
n.Clear(); |
|||
Console.WriteLine("---------------"); |
|||
List<Animal> m; |
|||
using(var stream = new FileStream(path, FileMode.Open, FileAccess.Read)) |
|||
m = (List<Animal>) new BinaryFormatter().Deserialize(stream); |
|||
foreach(Animal animal in m) |
|||
animal.Print(); |
|||
} |
|||
} |
|||
}</lang> |
|||
<pre>Fido, id=1 is alive |
|||
Lupo, id=2 is alive |
|||
Wanda, id=7 is alive |
|||
Kiki, id=3 is dead |
|||
--------------- |
|||
Fido, id=1 is alive |
|||
Lupo, id=2 is alive |
|||
Wanda, id=7 is alive |
|||
Kiki, id=3 is dead</pre> |
|||
=={{header|Caché ObjectScript}}== |
=={{header|Caché ObjectScript}}== |
||
Line 1,449: | Line 1,450: | ||
00000050: 7269 616c 697a 6174 696f 6e02 0000 0000 rialization..... |
00000050: 7269 616c 697a 6174 696f 6e02 0000 0000 rialization..... |
||
00000060: 0000 0061 0000 0000 0000 0000 7a ...a........z</pre> |
00000060: 0000 0061 0000 0000 0000 0000 7a ...a........z</pre> |
||
=={{header|Objeck}}== |
|||
<lang objeck> |
|||
bundle Default { |
|||
class Thingy { |
|||
@id : Int; |
|||
New(id : Int) { |
|||
@id := id; |
|||
} |
|||
method : public : Print() ~ Nil { |
|||
@id->PrintLine(); |
|||
} |
|||
} |
|||
class Person from Thingy { |
|||
@name : String; |
|||
New(id : Int, name : String) { |
|||
Parent(id); |
|||
@name := name; |
|||
} |
|||
method : public : Print() ~ Nil { |
|||
@id->PrintLine(); |
|||
@name->PrintLine(); |
|||
} |
|||
} |
|||
class Serial { |
|||
function : Main(args : String[]) ~ Nil { |
|||
t := Thingy->New(7); |
|||
p := Person->New(13, "Bush"); |
|||
s := IO.Serializer->New(); |
|||
s->Write(t->As(Base)); |
|||
s->Write(p->As(Base)); |
|||
writer := IO.FileWriter->New("objects.dat"); |
|||
writer->WriteBuffer(s->Serialize()); |
|||
writer->Close(); |
|||
buffer := IO.FileReader->ReadBinaryFile("objects.dat"); |
|||
d := IO.Deserializer->New(buffer); |
|||
t2 := d->ReadObject()->As(Thingy); |
|||
t2->Print(); |
|||
p2 := d->ReadObject()->As(Person); |
|||
p2->Print(); |
|||
} |
|||
} |
|||
} |
|||
</lang> |
|||
=={{header|Objective-C}}== |
=={{header|Objective-C}}== |
||
Line 1,616: | Line 1,671: | ||
return EXIT_SUCCESS; |
return EXIT_SUCCESS; |
||
}</lang> |
}</lang> |
||
=={{header|Objeck}}== |
|||
<lang objeck> |
|||
bundle Default { |
|||
class Thingy { |
|||
@id : Int; |
|||
New(id : Int) { |
|||
@id := id; |
|||
} |
|||
method : public : Print() ~ Nil { |
|||
@id->PrintLine(); |
|||
} |
|||
} |
|||
class Person from Thingy { |
|||
@name : String; |
|||
New(id : Int, name : String) { |
|||
Parent(id); |
|||
@name := name; |
|||
} |
|||
method : public : Print() ~ Nil { |
|||
@id->PrintLine(); |
|||
@name->PrintLine(); |
|||
} |
|||
} |
|||
class Serial { |
|||
function : Main(args : String[]) ~ Nil { |
|||
t := Thingy->New(7); |
|||
p := Person->New(13, "Bush"); |
|||
s := IO.Serializer->New(); |
|||
s->Write(t->As(Base)); |
|||
s->Write(p->As(Base)); |
|||
writer := IO.FileWriter->New("objects.dat"); |
|||
writer->WriteBuffer(s->Serialize()); |
|||
writer->Close(); |
|||
buffer := IO.FileReader->ReadBinaryFile("objects.dat"); |
|||
d := IO.Deserializer->New(buffer); |
|||
t2 := d->ReadObject()->As(Thingy); |
|||
t2->Print(); |
|||
p2 := d->ReadObject()->As(Person); |
|||
p2->Print(); |
|||
} |
|||
} |
|||
} |
|||
</lang> |
|||
=={{header|OCaml}}== |
=={{header|OCaml}}== |
||
Line 1,820: | Line 1,821: | ||
</lang> |
</lang> |
||
This time the objects were serialized to the [http://www.json.org/ JSON] format. Other supported formats are [http://search.cpan.org/perldoc?Storable Storable] and [http://www.yaml.org/ YAML]. |
This time the objects were serialized to the [http://www.json.org/ JSON] format. Other supported formats are [http://search.cpan.org/perldoc?Storable Storable] and [http://www.yaml.org/ YAML]. |
||
=={{header|Perl 6}}== |
|||
<lang perl6>#!/usr/bin/env perl6 |
|||
# Reference: |
|||
# https://docs.perl6.org/language/classtut |
|||
# https://github.com/teodozjan/perl-store |
|||
use v6; |
|||
use PerlStore::FileStore; |
|||
class Point { |
|||
has Int $.x; |
|||
has Int $.y; |
|||
} |
|||
class Rectangle does FileStore { |
|||
has Point $.lower; |
|||
has Point $.upper; |
|||
method area() returns Int { |
|||
($!upper.x - $!lower.x) * ( $!upper.y - $!lower.y); |
|||
} |
|||
} |
|||
my $r1 = Rectangle.new(lower => Point.new(x => 0, y => 0), |
|||
upper => Point.new(x => 10, y => 10)); |
|||
say "Create Rectangle1 with area ",$r1.area(); |
|||
say "Serialize Rectangle1 to object.dat"; |
|||
$r1.to_file('./objects.dat'); |
|||
say ""; |
|||
say "take a peek on object.dat .."; |
|||
say slurp "./objects.dat"; |
|||
say ""; |
|||
say "Deserialize to Rectangle2"; |
|||
my $r2 = from_file('objects.dat'); |
|||
say "Rectangle2 is of type ", $r2.WHAT; |
|||
say "Rectangle2 area is ", $r2.area();</lang> |
|||
{{out}} |
|||
<pre>Create Rectangle1 with area 100 |
|||
Serialize Rectangle1 to object.dat |
|||
take a peek on object.dat .. |
|||
Rectangle.new(lower => Point.new(x => 0, y => 0), upper => Point.new(x => 10, y => 10)) |
|||
Deserialize to Rectangle2 |
|||
Rectangle2 is of type (Rectangle) |
|||
Rectangle2 area is 100</pre> |
|||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
Line 2,119: | Line 2,071: | ||
With children: "John [JACK, Joan]" |
With children: "John [JACK, Joan]" |
||
With both: "John [JACK, Joan]"</pre> |
With both: "John [JACK, Joan]"</pre> |
||
=={{header|Raku}}== |
|||
(formerly Perl 6) |
|||
<lang perl6>#!/usr/bin/env perl6 |
|||
# Reference: |
|||
# https://docs.perl6.org/language/classtut |
|||
# https://github.com/teodozjan/perl-store |
|||
use v6; |
|||
use PerlStore::FileStore; |
|||
class Point { |
|||
has Int $.x; |
|||
has Int $.y; |
|||
} |
|||
class Rectangle does FileStore { |
|||
has Point $.lower; |
|||
has Point $.upper; |
|||
method area() returns Int { |
|||
($!upper.x - $!lower.x) * ( $!upper.y - $!lower.y); |
|||
} |
|||
} |
|||
my $r1 = Rectangle.new(lower => Point.new(x => 0, y => 0), |
|||
upper => Point.new(x => 10, y => 10)); |
|||
say "Create Rectangle1 with area ",$r1.area(); |
|||
say "Serialize Rectangle1 to object.dat"; |
|||
$r1.to_file('./objects.dat'); |
|||
say ""; |
|||
say "take a peek on object.dat .."; |
|||
say slurp "./objects.dat"; |
|||
say ""; |
|||
say "Deserialize to Rectangle2"; |
|||
my $r2 = from_file('objects.dat'); |
|||
say "Rectangle2 is of type ", $r2.WHAT; |
|||
say "Rectangle2 area is ", $r2.area();</lang> |
|||
{{out}} |
|||
<pre>Create Rectangle1 with area 100 |
|||
Serialize Rectangle1 to object.dat |
|||
take a peek on object.dat .. |
|||
Rectangle.new(lower => Point.new(x => 0, y => 0), upper => Point.new(x => 10, y => 10)) |
|||
Deserialize to Rectangle2 |
|||
Rectangle2 is of type (Rectangle) |
|||
Rectangle2 area is 100</pre> |
|||
=={{header|Ruby}}== |
=={{header|Ruby}}== |