CSC/ECE 517 Fall 2013/ch1 1w05 st: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 26: Line 26:
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. Any Ruby object can easily be serialized into YAML format. Let us consider the below code,


require "yaml"
<code>require "yaml"</code>
     class First
     <nowiki>class First
     def initialize(name, age, country)
     def initialize(name, age, country)
    @name = name
@name = name
@age = age
@age = age
@country=country
@country=country
  end
    end


  def to_s
    def to_s
"In First:\n#{@name}, #{@age}, #{@country}\n"
"In First:\n#{@name}, #{@age}, #{@country}\n"
  end
    end
end
  end


class Second
  class Second
  def initialize(address, details)
    def initialize(address, details)
@address = address
@address = address
@details = details
@details = details
  end
    end
   
   
  def to_s
    def to_s
"In Second:\n#{@details.to_s}#{@address}\n"
"In Second:\n#{@details.to_s}#{@address}\n"
  end
    end
end
  end
   
   
x = First.new("Tom", 25, "USA")
  x = First.new("Tom", 25, "USA")
y = Second.new("St. Marks Street", x)
  y = Second.new("St. Marks Street", x)
puts y
  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 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]).
Line 58: Line 58:
'''Output''':
'''Output''':
   
   
In Second:
<nowiki>In Second:
In First:
In First:
Tom, 25, USA
Tom, 25, USA
St. Marks Street
St. Marks Street</nowiki>
   
   
We use the below code to serialize out object tree.
We use the below code to serialize out object tree.
   
   
serialized_object = YAML::dump(y)
<nowiki>serialized_object = YAML::dump(y)
puts serialized_object
puts serialized_object</nowiki>
   
   
The dump function serializes the object tree and stores the data in the YAML format in the variable serialized_object.
The dump function serializes the object tree and stores the data in the YAML format in the variable serialized_object.
Line 72: Line 72:
Data in the serialized (YAML) format looks like this:
Data in the serialized (YAML) format looks like this:
   
   
--- !ruby/object:Second
<nowiki>--- !ruby/object:Second
address: St. Marks Street
address: St. Marks Street
details: !ruby/object:First
details: !ruby/object:First
   name: Tom
   name: Tom
   age: 25
   age: 25
   country: USA
   country: USA</nowiki>
   
   
Now, to de-serialize the data, we use load function.
Now, to de-serialize the data, we use load function.
   
   
puts YAML::load(serialized_object)
<code>puts YAML::load(serialized_object)</code>
   
   
The data is converted back to Ruby object tree.The output is as below:
The data is converted back to Ruby object tree.
The output is as below:
   
   
In Second:
<nowiki>In Second:
In First:
In First:
Tom, 25, USA
Tom, 25, USA
St. Marks Street
St. Marks Street
</nowiki>
 
Thus we get back our original Object tree.
Thus we get back our original Object tree.
    
    
Line 103: Line 105:
We can create a JSON string for serialization by using the JSON.generate method as below:
We can create a JSON string for serialization by using the JSON.generate method as below:
   
   
<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\"}"
   
   
The output is as below:
The output is as below:
   
   
{"{\"Welcome\":\"Ruby\"}"=>"{\"WELCOME\":\"RUBY\"}"}
<code>{"{\"Welcome\":\"Ruby\"}"=>"{\"WELCOME\":\"RUBY\"}"}</code>
   
   
We can parse the JSON string received from another program by using JSON.parse
We can parse the JSON string received from another program by using JSON.parse
Ruby thus converts String to Hash.
Ruby thus converts String to Hash.
   
   
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"

Revision as of 21:33, 17 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. De-serialization is the process of converting the stream of bytes or string back to objects in memory. 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

Converting Ruby Objects to YAML format

YAML[2] 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,

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 as the Output (because we have used the function to_s[3]).

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. The output is as below:

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[4] is a light-weight data interchange format. Any Ruby object can easily be serialized into JSON format. The JSON library can be installed using Ruby Gems[5] 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\"}"

The output is as below:

{"{\"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"