CSC/ECE 517 Fall 2013/ch1 1w05 st: Difference between revisions
No edit summary |
|||
(29 intermediate revisions by 2 users not shown) | |||
Line 21: | Line 21: | ||
Let us consider a situation where two Ruby programs have to communicate with each other. One of the simplest way to do this is to convert the Ruby objects in the first programs into strings and writing these strings into a file. This is nothing but serialization. The second program can read this file and convert the strings back into Ruby objects. This is de-serialization. | Let us consider a situation where two Ruby programs have to communicate with each other. One of the simplest way to do this is to convert the Ruby objects in the first programs into strings and writing these strings into a file. This is nothing but serialization. The second program can read this file and convert the strings back into Ruby objects. This is de-serialization. | ||
== '''Types of Serialization''' == | == '''Types of Serialization''' == | ||
Serialization in Ruby can be done in two ways. During serialization, the object in memory can be converted into Human Readable formats like YAML (YAML Ain’t Markup Language) and JSON (JavaScript Object Notation), or the object can be converted into binary format. | Serialization in Ruby can be done in two ways. During serialization, the object in memory can be converted into Human Readable formats like YAML (YAML Ain’t Markup Language) and JSON (JavaScript Object Notation), or the object can be converted into binary format. | ||
=== Converting Ruby Objects in Human Readable Formats === | === Converting Ruby Objects in Human Readable Formats === | ||
The conversion of Ruby objects into YAML and JSON formats are explained below. | |||
==== Converting Ruby Objects to YAML format ==== | ==== Converting Ruby Objects to YAML format ==== | ||
YAML[http://yaml.org/] format is a human friendly data serialization standard for all programming languages. Any Ruby object can easily be serialized into YAML format. Let us consider the below code, | YAML[http://yaml.org/] format is a human friendly data serialization standard for all programming languages. YAML (YAML Ain't Markup Language) is perhaps the most common form of serialization in Ruby applications. It is used for configuration files in Rails and other projects, and is nearly ubiquitous. YAML is a plaintext format, as opposed to Marshal's[http://www.ruby-doc.org/core-2.0.0/Marshal.html] binary format. Immediately, this makes things easier. Objects stored as YAML are completely transparent and editable with nothing more than a text editor. It also has a simple, spartan syntax that's easy to look at and easy to type. It is not encumbered by excessive wordage and symbols seen in XML. Any Ruby object can easily be serialized into YAML format. Let us consider the below code,< | ||
< | <nowiki>require "yaml" | ||
class First | |||
def initialize(name, age, country) | def initialize(name, age, country) | ||
@name = name | @name = name | ||
Line 64: | Line 61: | ||
puts y</nowiki> | puts y</nowiki> | ||
We get the string representation of the object tree as the Output (because we have used the function to_s[http://ruby-doc.org/core-2.0.0/Object.html#method-i-to_s]). | We get the string representation of the object tree(object hierarchy) as the Output (because we have used the function to_s[http://ruby-doc.org/core-2.0.0/Object.html#method-i-to_s]). | ||
'''Output''': | '''Output''': | ||
<code> | <code> | ||
In Second: | In Second:<br> | ||
In First: | In First:<br> | ||
Tom, 25, USA | Tom, 25, USA<br> | ||
St. Marks Street</code> | St. Marks Street</code> | ||
Line 106: | Line 103: | ||
Thus we get back our original Object tree. | Thus we get back our original Object tree. | ||
==== Converting Ruby Objects to JSON format: ==== | ==== Converting Ruby Objects to JSON format: ==== | ||
JSON[http://www.json.org/] is a light-weight data interchange format. Any Ruby object can easily be serialized into JSON format. | JSON[http://www.json.org/] is a light-weight data interchange format. JSON is typically generated by web applications and can be quite daunting, with deep hierarchies that are difficult to navigate. Any Ruby object can easily be serialized into JSON format. On Ruby 1.8.7, you'll need to install a gem. However, in Ruby 1.9.2, the json gem is bundled with the core Ruby distribution. So, if you're using 1.9.2, you're probably all set. If you're on 1.8.7, you'll need to install a gem.[http://ruby.about.com/od/tasks/a/The-Json-Gem.htm] | ||
The JSON library can be installed using Ruby Gems[http://rubygems.org/] like shown below: | The JSON library can be installed using Ruby Gems[http://rubygems.org/] like shown below: | ||
Line 119: | Line 115: | ||
<code> | <code> | ||
require 'json' | require 'json' | ||
my_hash = {:Welcome => "Ruby"} | my_hash = {:Welcome => "Ruby"} | ||
puts JSON.generate(my_hash) => "{\"WELCOME\":\"RUBY\"}"</code> | puts JSON.generate(my_hash) => "{\"WELCOME\":\"RUBY\"}" | ||
</code> | |||
'''Output:''' | '''Output:''' | ||
Line 130: | Line 127: | ||
Ruby thus converts String to Hash. | Ruby thus converts String to Hash. | ||
<code>require 'json' | <code> | ||
require 'json' | |||
my_hash = JSON.parse('{"Welcome": "Ruby"}') | my_hash = JSON.parse('{"Welcome": "Ruby"}') | ||
puts my_hash["Welcome"] => "Ruby"</code> | puts my_hash["Welcome"] => "Ruby" | ||
</code> | |||
=== Converting Ruby Objects to Binary Formats === | |||
Binary Serialization is another form of serialization in Ruby which is not in human readable form. It is similar to YAML Serialization. Binary Serialization is done using Marshal[http://www.ruby-doc.org/core-2.0.0/Marshal.html]. Binary Serialization is used when high performance serialization and de-serialization process is required and when the contents are not required to be in readable format. | |||
Since the Binary Serialized data is not in human readable form, there are two essential guidelines that need to be followed. They are : | |||
1.Use print[http://ruby-doc.org/core-2.0.0/ARGF.html#method-i-print] instead of puts[http://ruby-doc.org/core-2.0.0/ARGF.html#method-i-puts] when serialized objects are written to a file in order to avoid new line characters to be written | |||
in the file. | |||
2.Use a record separator in order to differentiate between two objects. | |||
<code>Binary Serialization Example </code> | |||
<code> | |||
class Animal | |||
def initialize name, age | |||
@name = name | |||
@age=age | |||
puts "#{self.class.name}" | |||
end | |||
end | |||
class Cat < Animal | |||
def to_s | |||
"In Cat C: #{@name} \t #{@age}" | |||
end | |||
end | |||
class Dog < Animal | |||
def to_s | |||
puts "In Dog D: #{@name} \t #{@age}" | |||
end | |||
end | |||
d = Dog.new("Doggy Dig", 4) | |||
c = Cat.new("Kitty Kat",5) | |||
puts "Before Serialization" | |||
puts c | |||
puts d | |||
serialize_cat= Marshal.dump(c) #dumps the serialized cat object into serialize_cat | |||
serialize_dog= Marshal.dump(d) #dumps the serialized dog object into serialize_dog | |||
deserialize_cat= Marshal::load(serialize_cat) #deserializes the cat object and loads it back into deserialize_cat | |||
deserialize_dog= Marshal::load(serialize_dog) #deserializes the dog object and loads it back into deserialize_dog | |||
puts "After Serialization #{deserialize_cat}" | |||
puts "After Dog Serialization #{deserialize_dog}" | |||
</code> | |||
Output | |||
<code> | |||
Before Serialization | |||
In Cat C: Kitty Kat 5 | |||
In Dog D: Doggy Dig 4 | |||
After Serialization In Cat C: Kitty Kat 5 | |||
After Dog Serialization In Dog D: Doggy Dig 4 | |||
</code> | |||
== Serialization in OOLS Languages: Comparison == | |||
{| class="wikitable" | |||
|- | |||
! Sl.No !! Ruby !! Java !! .Net Framework | |||
|- | |||
| 1 || Ruby provides a module called [http://www.ruby-doc.org/core-2.0.0/Marshal.html Marshal] for serialization || Java uses an Interface named [http://docs.oracle.com/javase/6/docs/api/java/io/Serializable.html Serializable] interface for classes to implement || .Net provides a [http://msdn.microsoft.com/en-us/library/ms973893.aspx Serializable] Attribute | |||
|- | |||
| 2 || Ruby uses JSON to make it platform independent || An object can be serialized in one platform and de-serialized in another platform || .Net used Remoting technology to make it platform independent. | |||
|- | |||
| 3 || Ruby serializes an Object as a whole.|| Provides an option for serializing only the required methods/attributes to be serialized for an object. Use the keyword [http://docs.oracle.com/javase/7/docs/platform/serialization/spec/serial-arch.html Transient] to ignore certain methods that doesn’t need to be serialized || [http://msdn.microsoft.com/en-us/library/ms973893.aspx XML Serializer] sets [http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlattributes.xmlignore.aspx XmlIgnoreProperty] to true to ignore the default serialization of a field or a property | |||
|} | |||
== See Also== | |||
1. [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/json/rdoc/JSON.html Serialization in Ruby JSON] | |||
2. [http://api.rubyonrails.org/classes/ActiveModel/Serialization.html Serialization in Rails] | |||
3. [http://gregmoreno.wordpress.com/2011/01/27/preventing-model-explosion-via-rails-serialization/ Article on Rails Serialization] | |||
== References == | == References == | ||
2. http://yaml.org | 1. [http://en.wikipedia.org/wiki/Serialization Serilization in General] | ||
2. [http://yaml.org YAML] | |||
3. [http://www.w3schools.com/json/ JSON] | |||
4. [http://www.skorks.com/2010/04/serializing-and-deserializing-objects-with-ruby/ Serializing and De-serializing in Ruby] | |||
5. [http://www.codeproject.com/Articles/33296/Serialization-and-De-serialization Serialization and De-serialization] | |||
6. [http://www.ruby-doc.org/core-2.0.0/Marshal.html Marshal] | |||
7. [http://www.waset.org/journals/waset/v60/v60-39.pdf Object Serialization Techniques] | |||
8. [http://msdn.microsoft.com/en-us/library/182eeyhh.aspx XML Serialization] | |||
9. [http://www.tutorialspoint.com/java/java_serialization.htm Java Serialization] | |||
10.[http://www.codeproject.com/Articles/20962/Introducing-Serialization-in-NET Serialization in .Net] | |||
11.[http://json.org JSON] | |||
12.[http://ruby.about.com/od/advancedruby/ss/Serialization-In-Ruby-Yaml.html Serialization in Ruby YAML] | |||
13.[http://ruby.about.com/od/tasks/a/The-Json-Gem.html Installing JSON gem] |
Latest revision as of 01:38, 25 September 2013
Serialization
Serialization[1] is the process of converting an object or a group of objects into a stream of bytes or string to facilitate storage in memory or transmission over a network. The process of Serialization is also referred to as Marshalling. The stream of data has to be in a format that can be understood by both ends of a communication channel so that the object can be marshaled and reconstructed easily.
Basic Advantages of Serialization:
1.Facilitates the easy transportation of an object through a network.
2.Creates a clone of the object at the receiving end.
3.Ability to transmit data across the network in a cross-platform-compatible format.
4.Saving the data in a persistent or non-persistent storage medium in a non-proprietary format.
De-serialization is the process of converting the stream of bytes or string back to objects in memory. It is the process of reconstructing the object later.This process of de-serialization is also referred to as Unmarshalling.
Serialization in Ruby:
Let us consider a situation where two Ruby programs have to communicate with each other. One of the simplest way to do this is to convert the Ruby objects in the first programs into strings and writing these strings into a file. This is nothing but serialization. The second program can read this file and convert the strings back into Ruby objects. This is de-serialization.
Types of Serialization
Serialization in Ruby can be done in two ways. During serialization, the object in memory can be converted into Human Readable formats like YAML (YAML Ain’t Markup Language) and JSON (JavaScript Object Notation), or the object can be converted into binary format.
Converting Ruby Objects in Human Readable Formats
The conversion of Ruby objects into YAML and JSON formats are explained below.
Converting Ruby Objects to YAML format
YAML[2] format is a human friendly data serialization standard for all programming languages. YAML (YAML Ain't Markup Language) is perhaps the most common form of serialization in Ruby applications. It is used for configuration files in Rails and other projects, and is nearly ubiquitous. YAML is a plaintext format, as opposed to Marshal's[3] binary format. Immediately, this makes things easier. Objects stored as YAML are completely transparent and editable with nothing more than a text editor. It also has a simple, spartan syntax that's easy to look at and easy to type. It is not encumbered by excessive wordage and symbols seen in XML. Any Ruby object can easily be serialized into YAML format. Let us consider the below code,<
require "yaml" class First def initialize(name, age, country) @name = name @age = age @country=country end def to_s "In First:\n#{@name}, #{@age}, #{@country}\n" end end class Second def initialize(address, details) @address = address @details = details end def to_s "In Second:\n#{@details.to_s}#{@address}\n" end end x = First.new("Tom", 25, "USA") y = Second.new("St. Marks Street", x) puts y
We get the string representation of the object tree(object hierarchy) as the Output (because we have used the function to_s[4]).
Output:
In Second:
In First:
Tom, 25, USA
St. Marks Street
We use the below code to serialize out object tree.
serialized_object = YAML::dump(y)
puts serialized_object
The dump function serializes the object tree and stores the data in the YAML format in the variable serialized_object.
Data in the serialized (YAML) format looks like this:
--- !ruby/object:Second
address: St. Marks Street
details: !ruby/object:First
name: Tom
age: 25
country: USA
Now, to de-serialize the data, we use load function.
puts YAML::load(serialized_object)
The data is converted back to Ruby object tree.
Output:
In Second:
In First:
Tom, 25, USA
St. Marks Street
Thus we get back our original Object tree.
Converting Ruby Objects to JSON format:
JSON[5] is a light-weight data interchange format. JSON is typically generated by web applications and can be quite daunting, with deep hierarchies that are difficult to navigate. Any Ruby object can easily be serialized into JSON format. On Ruby 1.8.7, you'll need to install a gem. However, in Ruby 1.9.2, the json gem is bundled with the core Ruby distribution. So, if you're using 1.9.2, you're probably all set. If you're on 1.8.7, you'll need to install a gem.[6] The JSON library can be installed using Ruby Gems[7] like shown below:
# gem install json
We can create a JSON string for serialization by using the JSON.generate method as below:
require 'json'
my_hash = {:Welcome => "Ruby"}
puts JSON.generate(my_hash) => "{\"WELCOME\":\"RUBY\"}"
Output:
{"{\"Welcome\":\"Ruby\"}"=>"{\"WELCOME\":\"RUBY\"}"}
We can parse the JSON string received from another program by using JSON.parse Ruby thus converts String to Hash.
require 'json'
my_hash = JSON.parse('{"Welcome": "Ruby"}')
puts my_hash["Welcome"] => "Ruby"
Converting Ruby Objects to Binary Formats
Binary Serialization is another form of serialization in Ruby which is not in human readable form. It is similar to YAML Serialization. Binary Serialization is done using Marshal[8]. Binary Serialization is used when high performance serialization and de-serialization process is required and when the contents are not required to be in readable format.
Since the Binary Serialized data is not in human readable form, there are two essential guidelines that need to be followed. They are :
1.Use print[9] instead of puts[10] when serialized objects are written to a file in order to avoid new line characters to be written in the file. 2.Use a record separator in order to differentiate between two objects.
Binary Serialization Example
class Animal
def initialize name, age
@name = name
@age=age
puts "#{self.class.name}"
end
end
class Cat < Animal
def to_s
"In Cat C: #{@name} \t #{@age}"
end
end
class Dog < Animal
def to_s
puts "In Dog D: #{@name} \t #{@age}"
end
end
d = Dog.new("Doggy Dig", 4)
c = Cat.new("Kitty Kat",5)
puts "Before Serialization"
puts c
puts d
serialize_cat= Marshal.dump(c) #dumps the serialized cat object into serialize_cat
serialize_dog= Marshal.dump(d) #dumps the serialized dog object into serialize_dog
deserialize_cat= Marshal::load(serialize_cat) #deserializes the cat object and loads it back into deserialize_cat
deserialize_dog= Marshal::load(serialize_dog) #deserializes the dog object and loads it back into deserialize_dog
puts "After Serialization #{deserialize_cat}"
puts "After Dog Serialization #{deserialize_dog}"
Output
Before Serialization
In Cat C: Kitty Kat 5
In Dog D: Doggy Dig 4
After Serialization In Cat C: Kitty Kat 5
After Dog Serialization In Dog D: Doggy Dig 4
Serialization in OOLS Languages: Comparison
Sl.No | Ruby | Java | .Net Framework |
---|---|---|---|
1 | Ruby provides a module called Marshal for serialization | Java uses an Interface named Serializable interface for classes to implement | .Net provides a Serializable Attribute |
2 | Ruby uses JSON to make it platform independent | An object can be serialized in one platform and de-serialized in another platform | .Net used Remoting technology to make it platform independent. |
3 | Ruby serializes an Object as a whole. | Provides an option for serializing only the required methods/attributes to be serialized for an object. Use the keyword Transient to ignore certain methods that doesn’t need to be serialized | XML Serializer sets XmlIgnoreProperty to true to ignore the default serialization of a field or a property |
See Also
3. Article on Rails Serialization
References
2. YAML
3. JSON
4. Serializing and De-serializing in Ruby
5. Serialization and De-serialization
6. Marshal
7. Object Serialization Techniques
11.JSON