<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.expertiza.ncsu.edu/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Mmehta2</id>
	<title>Expertiza_Wiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.expertiza.ncsu.edu/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Mmehta2"/>
	<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Special:Contributions/Mmehta2"/>
	<updated>2026-05-07T01:07:40Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.41.0</generator>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1d_mm&amp;diff=83432</id>
		<title>CSC/ECE 517 Spring 2014/ch1a 1d mm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1d_mm&amp;diff=83432"/>
		<updated>2014-02-18T03:58:34Z</updated>

		<summary type="html">&lt;p&gt;Mmehta2: /* Serialization in Java */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page discusses Serialization, focusing on Ruby programming language. It describes different methods of Serialization in Ruby. In addition, it also compares serialization in different Object Oriented languages.&lt;br /&gt;
&lt;br /&gt;
== '''Introduction''' ==&lt;br /&gt;
&lt;br /&gt;
==='''Serialization'''===&lt;br /&gt;
&lt;br /&gt;
'''Serialization'''[http://en.wikipedia.org/wiki/Serialization] is a process of converting a data structure or an object into a stream of bytes or string to facilitate storage in memory, file (persistence storage) or transmission over a network. The process of Serialization is also referred to as ''Marshalling''[http://en.wikipedia.org/wiki/Marshalling_%28computer_science%29]. 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.&lt;br /&gt;
&lt;br /&gt;
==='''De-serialization'''===&lt;br /&gt;
&lt;br /&gt;
'''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''.&lt;br /&gt;
&lt;br /&gt;
The process of Serialization/De-serialization is not a replacement for database systems. Database Systems are used for their relational and transactional semantics.&lt;br /&gt;
&lt;br /&gt;
==='''Basic Advantages of Serialization'''===&lt;br /&gt;
&lt;br /&gt;
1. Communication between two or more processes on same machine. Object state can be saved and shared in a persistent or in-memory store.&lt;br /&gt;
&lt;br /&gt;
2. Communication between processes on different machines. Serialization facilitates the transmission of an object through a network.&lt;br /&gt;
&lt;br /&gt;
3. Creating a clone of an object. &lt;br /&gt;
&lt;br /&gt;
4. Cross-platform compatibility. Object can be serialized in a common format that is understood by multiple platforms. For example, JSON[http://www.json.org/] and XML.&lt;br /&gt;
&lt;br /&gt;
==='''Practical Applications for Serialization'''===&lt;br /&gt;
&lt;br /&gt;
1. [http://docs.oracle.com/cd/E13222_01/wls/docs81/webapp/sessions.html#139726 HTTP Session] Replication by sharing session objects across web servers for load balancing and handling failover scenarios.&lt;br /&gt;
&lt;br /&gt;
2. Serialization facilitates communication in [http://en.wikipedia.org/wiki/Java_remote_method_invocation Remote Method Invocation] or [http://en.wikipedia.org/wiki/Remote_procedure_call Remote procedure calls]&lt;br /&gt;
&lt;br /&gt;
3. Rails Cookie Handling[http://techbrahmana.blogspot.com/2012/03/rails-cookie-handling-serialization-and.html]. Cookies are stored marshalled/unmarshalled to and from client machines.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== '''Serialization in Ruby:''' ==&lt;br /&gt;
&lt;br /&gt;
Ruby provides serialization capabilities through its module, [http://www.ruby-doc.org/core-2.0.0/Marshal.html Marshal]. There are also other libraries like YAML[http://yaml.org/] and JSON which can be used in Ruby to generate serialized objects for purposes like platform independence and human readable formats.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:SerializationChart.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== '''Types of Serialization''' ==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
=== Converting Ruby Objects in Human Readable Formats ===&lt;br /&gt;
----&lt;br /&gt;
The conversion of Ruby objects into YAML and JSON formats are explained below.&lt;br /&gt;
&lt;br /&gt;
==== Converting Ruby Objects to YAML format ====&lt;br /&gt;
&lt;br /&gt;
YAML 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 binary format. 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. In order to use it in Ruby, the yaml.rb file is required to be loaded which provides methods for converting objects into yaml format and creating .yml files.&lt;br /&gt;
&lt;br /&gt;
=====Serialization using YAML=====&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;nowiki&amp;gt; #Serialization using YAML's to_yaml method&lt;br /&gt;
 &lt;br /&gt;
  require &amp;quot;yaml&amp;quot;&lt;br /&gt;
     class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
	@name = name&lt;br /&gt;
	@age = age&lt;br /&gt;
	@country=country&lt;br /&gt;
     end&lt;br /&gt;
      def to_s&lt;br /&gt;
	&amp;quot;In First:\n#{@name}, #{@age}, #{@country}\n&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
  x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
  puts x&lt;br /&gt;
  puts x.to_yaml&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   In First:&lt;br /&gt;
   Tom, 25, USA&lt;br /&gt;
   --- !ruby/object:First&lt;br /&gt;
   name: Tom&lt;br /&gt;
   age: 25&lt;br /&gt;
   country: USA&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The above code displays the object x, first as a string and then in the yaml format.&lt;br /&gt;
&lt;br /&gt;
====='''Saving YAML data into a file:'''=====&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt; # Serialization using YAML::dump&lt;br /&gt;
&lt;br /&gt;
require 'yaml'&lt;br /&gt;
&lt;br /&gt;
f = File.open( 'first.yml', 'w' )&lt;br /&gt;
YAML.dump( [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;], f )&lt;br /&gt;
f.close&lt;br /&gt;
		&lt;br /&gt;
File.open( 'first.yml' ){ |f|	&lt;br /&gt;
    $arr= YAML.load(f)&lt;br /&gt;
}	&lt;br /&gt;
&lt;br /&gt;
p( $arr )&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;]&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The dump function can be used to serialize the data and save it into a file in YAML format. As shown in the above example the data in YAML format can be de-serialized using the load function.&lt;br /&gt;
&lt;br /&gt;
YAML libraries also provides an option of selecting only those variables of the object that are needed to be serialized. This is done using the to_yaml_properties method as shown in the below example.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=====Custom Serialization using YAML=====&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;nowiki&amp;gt;&lt;br /&gt;
    require &amp;quot;yaml&amp;quot;&lt;br /&gt;
     class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
	@name = name&lt;br /&gt;
	@age = age&lt;br /&gt;
	@country=country&lt;br /&gt;
     end&lt;br /&gt;
     def to_s&lt;br /&gt;
	&amp;quot;In First:\n#{@name}, #{@age}, #{@country}\n&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
      def to_yaml_properties&lt;br /&gt;
	   [&amp;quot;@name&amp;quot;,&amp;quot;@age&amp;quot;]  #@country will not be serialized&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
  x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
  puts x&lt;br /&gt;
  puts x.to_yaml&lt;br /&gt;
  &amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   In First:&lt;br /&gt;
   Tom, 25, USA&lt;br /&gt;
   --- !ruby/object:First&lt;br /&gt;
   name: Tom&lt;br /&gt;
   age: 25&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Converting Ruby Objects to JSON format: ====&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
JSON 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. Ruby 1.8.7 distribution is not bundled with json gem. However, in Ruby 1.9.2, the json gem is bundled with the core Ruby distribution.&lt;br /&gt;
The JSON library can be installed using Ruby Gems[http://rubygems.org/] as shown below:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;# gem install json&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A JSON string for serialization can be created by using the JSON.generate method:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
        require 'json'&lt;br /&gt;
        my_hash = {:Welcome =&amp;gt; &amp;quot;Ruby&amp;quot;}&lt;br /&gt;
        puts JSON.generate(my_hash) =&amp;gt; &amp;quot;{\&amp;quot;WELCOME\&amp;quot;:\&amp;quot;RUBY\&amp;quot;}&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
'''Output:'''&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;{&amp;quot;{\&amp;quot;Welcome\&amp;quot;:\&amp;quot;Ruby\&amp;quot;}&amp;quot;=&amp;gt;&amp;quot;{\&amp;quot;WELCOME\&amp;quot;:\&amp;quot;RUBY\&amp;quot;}&amp;quot;}&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A JSON string received from another program can be parsed by using JSON.parse&lt;br /&gt;
Ruby thus converts String to Hash.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
        require 'json'&lt;br /&gt;
        my_hash = JSON.parse('{&amp;quot;Welcome&amp;quot;: &amp;quot;Ruby&amp;quot;}')&lt;br /&gt;
        puts my_hash[&amp;quot;Welcome&amp;quot;] =&amp;gt; &amp;quot;Ruby&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Converting Ruby Objects to Binary Formats ===&lt;br /&gt;
----&lt;br /&gt;
Binary Serialization is another form of serialization in Ruby which is not in human readable form. 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. Binary Serialization  is done using Marshal which is built into Ruby and the code for it is written in Ruby's Marshal module(marshal.c) and thus no additional files are required in order to use it. The _dump and _load methods defined in marshal are used for serialization. Using marshal module the following types of objects can not be serialized: bindings, procedure objects, singleton objects, instances of IO objects and interfaces.&lt;br /&gt;
&lt;br /&gt;
Since the Binary Serialized data is not in human readable form, the following guidelines need to be followed.&lt;br /&gt;
     1.Use print instead of puts[http://ruby-doc.org/core-2.0.0/ARGF.html#] when serialized objects are written to a file in order to avoid new line characters to be written &lt;br /&gt;
       in the file.&lt;br /&gt;
     &lt;br /&gt;
     2.Use a record separator in order to differentiate between two objects.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====='''Binary Serialization Example:'''=====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    class Animal&lt;br /&gt;
      def initialize  name, age&lt;br /&gt;
      @name = name&lt;br /&gt;
      @age=age&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
   class Cat &amp;lt; Animal&lt;br /&gt;
    def to_s&lt;br /&gt;
     &amp;quot;In Cat C: #{@name} \t #{@age}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
  c = Cat.new(&amp;quot;Kitty Kat&amp;quot;,5)&lt;br /&gt;
  puts &amp;quot;Before Serialization&amp;quot;&lt;br /&gt;
  puts c&lt;br /&gt;
  #puts d&lt;br /&gt;
  serialize_cat= Marshal.dump(c) #dumps the serialized cat object into serialize_cat&lt;br /&gt;
  puts &amp;quot;\nAfter Serialization:\n #{serialize_cat}&amp;quot;&lt;br /&gt;
  deserialize_cat= Marshal::load(serialize_cat) #deserializes the cat object and loads it back into deserialize_cat&lt;br /&gt;
  puts &amp;quot;\nAfter Deserialization\n #{deserialize_cat}&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   Before Serialization&lt;br /&gt;
   In Cat C: Kitty Kat 	 5&lt;br /&gt;
   After Serialization:&lt;br /&gt;
   oCat:&lt;br /&gt;
   @nameI&amp;quot;Kitty Kat:ET:	@agei&lt;br /&gt;
   After Deserialization&lt;br /&gt;
   In Cat C: Kitty Kat 	 5&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====='''Saving Marshal data into a file'''=====&lt;br /&gt;
&lt;br /&gt;
Similar to YAML, Marshal can also be used to dump data into a file. The above example showing the serialization using YAML::dump can be written using marshal as shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    f = File.open( 'first.yml', 'w' )&lt;br /&gt;
    Marshal.dump( [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;], f )&lt;br /&gt;
    f.close	&lt;br /&gt;
    File.open( 'first.yml' ){ |f|	&lt;br /&gt;
    $arr= Marshal.load(f)&lt;br /&gt;
    }	&lt;br /&gt;
    p( $arr )&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;]&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice that in the above example there is no &amp;quot;require&amp;quot; statement as opposed to the earlier example of writing serialized data into files using YAML. This is because unlike YAML, marshal is built-in Ruby and  no external libraries is required in order to use its functionality.&lt;br /&gt;
&lt;br /&gt;
====='''Custom serialization using Marshal'''=====&lt;br /&gt;
&lt;br /&gt;
Marshal can also be used to custom serialize an object i.e. it provides an option to omit the variables of an object that are not required in the serialized data. The following program shows the use of marshal_dump method for achieving custom serialization.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
      class First&lt;br /&gt;
      def initialize(name, age, country)&lt;br /&gt;
         @name = name&lt;br /&gt;
         @age = age&lt;br /&gt;
         @country=country&lt;br /&gt;
      end&lt;br /&gt;
      def to_s&lt;br /&gt;
            &amp;quot;In First: #{@name}, #{@age}, #{@country}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
      def marshal_dump&lt;br /&gt;
            [@name,@age]  #@country will not be serialized&lt;br /&gt;
      end&lt;br /&gt;
      def marshal_load(data)&lt;br /&gt;
       @name=data[0]&lt;br /&gt;
       @age=data[1]&lt;br /&gt;
       @country=&amp;quot;United States of America&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
      end&lt;br /&gt;
 &lt;br /&gt;
      x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
      puts x&lt;br /&gt;
  &lt;br /&gt;
      marshal_data = Marshal.dump( x ) &lt;br /&gt;
      y = Marshal.load( marshal_data ) &lt;br /&gt;
      p( y.to_s )&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    In First: Tom, 25, USA&lt;br /&gt;
    &amp;quot;In First: Tom, 25, United States of America&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== '''Serialization Performance in Ruby''' ==&lt;br /&gt;
The different serialization formats described above differ in the efficiency at which they can serialize and deserialize data and thus while serializing large amount of data their efficiency is taken into account. The .report[http://www.ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html] method in Ruby can be used to evaluate the performance of these serialization patterns. Marshal, as it handles data in binary format, is the most efficient form of serialization in Ruby. The following example compares the performance of marshal, JSON and YAML format.[http://www.pauldix.net/2008/08/serializing-dat.html]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   require 'benchmark'&lt;br /&gt;
   require 'rubygems'&lt;br /&gt;
   require 'json'&lt;br /&gt;
   require 'yaml'&lt;br /&gt;
   include Benchmark&lt;br /&gt;
   class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
      @name=name&lt;br /&gt;
      @age=age&lt;br /&gt;
      @country=country&lt;br /&gt;
     end&lt;br /&gt;
   end  &lt;br /&gt;
    x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
    puts &amp;quot;            User     System     Total      Real&amp;quot;&lt;br /&gt;
   benchmark do |t|&lt;br /&gt;
    print &amp;quot;Marshal:&amp;quot;&lt;br /&gt;
    t.report{1000.times do; Marshal.load(Marshal.dump(x));end}&lt;br /&gt;
    print &amp;quot;JSON:   &amp;quot;&lt;br /&gt;
    t.report{1000.times do; JSON.load(JSON.dump(x));end}&lt;br /&gt;
    print &amp;quot;YAML:   &amp;quot;&lt;br /&gt;
    t.report{1000.times do; YAML.load(YAML.dump(x));end}&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
              User     System     Total      Real&lt;br /&gt;
  Marshal:   0.010000   0.000000   0.010000 (  0.015408)&lt;br /&gt;
  JSON:      0.020000   0.010000   0.030000 (  0.022462)&lt;br /&gt;
  YAML:      0.460000   0.010000   0.470000 (  0.472149)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output of the above example shows that, given large chunks of data, marshal is comparatively more efficient format for serialization in Ruby.&lt;br /&gt;
&lt;br /&gt;
== Serialization in other Object-Oriented Languages ==&lt;br /&gt;
&lt;br /&gt;
===Comparison===&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Sl.No !! Ruby !! Java !! .Net Framework !! C++&lt;br /&gt;
|-&lt;br /&gt;
| 1 || Ruby provides a built in module called 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 || Although, there is no built in support for serialization in C++, it can be achieved by using Boost libraries[http://www.boost.org/doc/libs/1_36_0/libs/serialization/example/demo.cpp]&lt;br /&gt;
|-&lt;br /&gt;
| 2 || The built in module of Ruby (Marshal) does not support platform independence, however, it can be achieved by using external libraries like YAML and JSON || Similarly, Java's built in serialization is also not platform independent and in order to use serialization in Java across Ruby platform, jruby library should be used.  || .Net used Remoting technology to make it platform independent. || Serialization using the Boost libraries is not platform independent.&lt;br /&gt;
|-&lt;br /&gt;
| 3 || YAML provides a method [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/syck/rdoc/Object.html (to_yaml_properties)] which can be used to select the variables who's value is need to be serialized. With Marshal, we need to write a method named marshal_dump defining the variables of an object that has to be serialized. || Provides an option for serializing only the required 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 data 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 || Serialization using the Boost libraries is custom and thus the user can specify the part of the objects to be serialized.&lt;br /&gt;
|-&lt;br /&gt;
| 4 || Bindings, procedure objects, singleton objects, instances of IO objects and interfaces can not be serialized. Serializing these objects throws TypeError exceptions. || Thread, OutputStream and its subclasses, and Socket are not serializable in Java.[http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html] || Objects like DataRow are non serializable in .NET. Whether an object is serializable or not can be checked using the [http://msdn.microsoft.com/en-us/library/system.type.isserializable(v=vs.110).aspx?cs-save-lang=1&amp;amp;cs-lang=cpp#code-snippet-1 Type.IsSerializable] || There are no such objects in C++.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===Serialization in Java===&lt;br /&gt;
&lt;br /&gt;
To serialize an object in Java, the class needs to implements Serializable interface and use ObjectOutputStream to serialize an object.&lt;br /&gt;
Similarly, we can use ObjectInputStream to deserialize an object. Below is an example to serialize an object of Class First.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;  &lt;br /&gt;
  import java.io.FileInputStream;&lt;br /&gt;
  import java.io.FileOutputStream;&lt;br /&gt;
  import java.io.IOException;&lt;br /&gt;
  import java.io.ObjectInputStream;&lt;br /&gt;
  import java.io.ObjectOutputStream;&lt;br /&gt;
  import java.io.Serializable;&lt;br /&gt;
           &lt;br /&gt;
 public class First implements Serializable {&lt;br /&gt;
 &lt;br /&gt;
 private static final long serialVersionUID = 1L;&lt;br /&gt;
 public String name;&lt;br /&gt;
 public int age;&lt;br /&gt;
 public String country;&lt;br /&gt;
 &lt;br /&gt;
 private static void serialize(First f, String path) throws IOException {&lt;br /&gt;
  &lt;br /&gt;
       FileOutputStream fos = new FileOutputStream(path);&lt;br /&gt;
       ObjectOutputStream oos = new ObjectOutputStream(fos);&lt;br /&gt;
    &lt;br /&gt;
       // Serializing&lt;br /&gt;
       oos.writeObject(f);&lt;br /&gt;
       oos.close();&lt;br /&gt;
       fos.close();&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
 private static First deserialize(String path) throws IOException,&lt;br /&gt;
  		ClassNotFoundException {&lt;br /&gt;
  	// tom.ser will deserialize in object f&lt;br /&gt;
  	First f = null;&lt;br /&gt;
  	FileInputStream fin = new FileInputStream(path);&lt;br /&gt;
  	ObjectInputStream oin = new ObjectInputStream(fin);&lt;br /&gt;
  	// DeSerializing and Casting to Class First&lt;br /&gt;
  	f = (First) oin.readObject();&lt;br /&gt;
  	oin.close();&lt;br /&gt;
  	fin.close();&lt;br /&gt;
  	return f;&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  public static void main(String[] args) {&lt;br /&gt;
  	try {&lt;br /&gt;
  		// Creating object for Tom&lt;br /&gt;
  		First f1 = new First();&lt;br /&gt;
  		f1.name = &amp;quot;Tom&amp;quot;;&lt;br /&gt;
  		f1.age = 25;&lt;br /&gt;
  		f1.country = &amp;quot;USA&amp;quot;;&lt;br /&gt;
  		First f2 = null;&lt;br /&gt;
  		long start = System.currentTimeMillis();&lt;br /&gt;
  		for (int i = 0; i &amp;lt; 1000; i++){&lt;br /&gt;
  		// serializing&lt;br /&gt;
  		serialize(f1, &amp;quot;/path/to/files/&amp;quot; + i + &amp;quot;.ser&amp;quot;);&lt;br /&gt;
  		// deserializing&lt;br /&gt;
  		f2 = deserialize(&amp;quot;/path/to/files&amp;quot; + i + &amp;quot;.ser&amp;quot;);&lt;br /&gt;
  		}&lt;br /&gt;
  		long end = System.currentTimeMillis();&lt;br /&gt;
  		long totalDuration = end - start;&lt;br /&gt;
  		System.out.println(&amp;quot;Serialization/Deserialization for 1000 Iterations duration: &amp;quot; + totalDuration + &amp;quot; Milliseconds&amp;quot;);&lt;br /&gt;
  		// Printing deserialized object&lt;br /&gt;
  		System.out.println(&amp;quot;Deserialized =&amp;gt; Name: &amp;quot; + f2.name + &amp;quot;, Age: &amp;quot; + f2.age + &amp;quot;, Country: &amp;quot; + f2.country);&lt;br /&gt;
  	} catch (IOException ex) {&lt;br /&gt;
  		e.printStackTrace();&lt;br /&gt;
  	} catch (ClassNotFoundException ex) {&lt;br /&gt;
  		e.printStackTrace();&lt;br /&gt;
  	}&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 Serialization/Deserialization for 1000 Iterations duration: 1651 Milliseconds&lt;br /&gt;
 Deserialized =&amp;gt; Name: Tom, Age: 25, Country: USA&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== See Also==&lt;br /&gt;
&lt;br /&gt;
1. [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/json/rdoc/JSON.html Serialization in Ruby JSON]&lt;br /&gt;
&lt;br /&gt;
2. [http://api.rubyonrails.org/classes/ActiveModel/Serialization.html Serialization in Rails]&lt;br /&gt;
&lt;br /&gt;
3. [http://gregmoreno.wordpress.com/2011/01/27/preventing-model-explosion-via-rails-serialization/ Article on Rails Serialization]&lt;br /&gt;
&lt;br /&gt;
4. [http://en.wikipedia.org/wiki/Google_Protocol_Buffers Protocol Buffers]&lt;br /&gt;
&lt;br /&gt;
5. [https://rubygems.org/gems/ruby-protocol-buffers Ruby Protocol Buffers Gem]&lt;br /&gt;
&lt;br /&gt;
6. [http://en.wikipedia.org/wiki/Apache_Avro Avro]&lt;br /&gt;
&lt;br /&gt;
7. [http://rubygems.org/gems/avro Ruby Avro Gem]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
1. [http://en.wikipedia.org/wiki/Serialization Serilization in General]&lt;br /&gt;
&lt;br /&gt;
2. [http://en.wikipedia.org/wiki/Marshalling_%28computer_science%29 Marshaling]&lt;br /&gt;
&lt;br /&gt;
3. [http://json.org/ JSON]&lt;br /&gt;
&lt;br /&gt;
4. [http://techbrahmana.blogspot.com/2012/03/rails-cookie-handling-serialization-and.html Rails Cookie Handling]&lt;br /&gt;
&lt;br /&gt;
5. [http://yaml.org/ YAML]&lt;br /&gt;
&lt;br /&gt;
6. [http://rubygems.org/ Ruby Gems]&lt;br /&gt;
&lt;br /&gt;
7. [http://ruby-doc.org/core-2.0.0/ARGF.html# Ruby ARGF Documentation]&lt;br /&gt;
&lt;br /&gt;
8. [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html Ruby Documentation for Benchmark Gem]&lt;br /&gt;
&lt;br /&gt;
9. [http://www.pauldix.net/2008/08/serializing-dat.html Serialization Performance Comparison]&lt;br /&gt;
&lt;br /&gt;
10. [http://www.boost.org/doc/libs/1_36_0/libs/serialization/example/demo.cpp Boost Libraries]&lt;br /&gt;
&lt;br /&gt;
11. [http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html Java Serialization]&lt;br /&gt;
&lt;br /&gt;
12. [http://www.w3schools.com/json/ JSON Tutorial]&lt;br /&gt;
&lt;br /&gt;
13. [http://www.skorks.com/2010/04/serializing-and-deserializing-objects-with-ruby/ Serializing and De-serializing in Ruby]&lt;br /&gt;
&lt;br /&gt;
14. [http://www.codeproject.com/Articles/33296/Serialization-and-De-serialization Serialization and De-serialization]&lt;br /&gt;
&lt;br /&gt;
15. [http://msdn.microsoft.com/en-us/library/182eeyhh.aspx XML Serialization]&lt;br /&gt;
&lt;br /&gt;
16. [http://www.tutorialspoint.com/java/java_serialization.htm Java Serialization Tutorial]&lt;br /&gt;
&lt;br /&gt;
17. [http://www.sapphiresteel.com/ruby-programming/The-Book-Of-Ruby The Book of Ruby]&lt;br /&gt;
&lt;br /&gt;
18. [http://www.codeproject.com/Articles/20962/Introducing-Serialization-in-NET Serialization in .Net]&lt;br /&gt;
&lt;br /&gt;
19. [http://ruby.about.com/od/advancedruby/ss/Serialization-In-Ruby-Yaml.html Serialization in Ruby YAML]&lt;br /&gt;
&lt;br /&gt;
20. [http://ruby.about.com/od/tasks/a/The-Json-Gem.html Installing JSON gem]&lt;br /&gt;
&lt;br /&gt;
21. [http://www.ruby-doc.org/stdlib-2.0.0/libdoc/json/rdoc/JSON.html Serialization in Ruby JSON]&lt;/div&gt;</summary>
		<author><name>Mmehta2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1d_mm&amp;diff=83430</id>
		<title>CSC/ECE 517 Spring 2014/ch1a 1d mm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1d_mm&amp;diff=83430"/>
		<updated>2014-02-18T03:30:47Z</updated>

		<summary type="html">&lt;p&gt;Mmehta2: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page discusses Serialization, focusing on Ruby programming language. It describes different methods of Serialization in Ruby. In addition, it also compares serialization in different Object Oriented languages.&lt;br /&gt;
&lt;br /&gt;
== '''Introduction''' ==&lt;br /&gt;
&lt;br /&gt;
==='''Serialization'''===&lt;br /&gt;
&lt;br /&gt;
'''Serialization'''[http://en.wikipedia.org/wiki/Serialization] is a process of converting a data structure or an object into a stream of bytes or string to facilitate storage in memory, file (persistence storage) or transmission over a network. The process of Serialization is also referred to as ''Marshalling''[http://en.wikipedia.org/wiki/Marshalling_%28computer_science%29]. 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.&lt;br /&gt;
&lt;br /&gt;
==='''De-serialization'''===&lt;br /&gt;
&lt;br /&gt;
'''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''.&lt;br /&gt;
&lt;br /&gt;
The process of Serialization/De-serialization is not a replacement for database systems. Database Systems are used for their relational and transactional semantics.&lt;br /&gt;
&lt;br /&gt;
==='''Basic Advantages of Serialization'''===&lt;br /&gt;
&lt;br /&gt;
1. Communication between two or more processes on same machine. Object state can be saved and shared in a persistent or in-memory store.&lt;br /&gt;
&lt;br /&gt;
2. Communication between processes on different machines. Serialization facilitates the transmission of an object through a network.&lt;br /&gt;
&lt;br /&gt;
3. Creating a clone of an object. &lt;br /&gt;
&lt;br /&gt;
4. Cross-platform compatibility. Object can be serialized in a common format that is understood by multiple platforms. For example, JSON[http://www.json.org/] and XML.&lt;br /&gt;
&lt;br /&gt;
==='''Practical Applications for Serialization'''===&lt;br /&gt;
&lt;br /&gt;
1. [http://docs.oracle.com/cd/E13222_01/wls/docs81/webapp/sessions.html#139726 HTTP Session] Replication by sharing session objects across web servers for load balancing and handling failover scenarios.&lt;br /&gt;
&lt;br /&gt;
2. Serialization facilitates communication in [http://en.wikipedia.org/wiki/Java_remote_method_invocation Remote Method Invocation] or [http://en.wikipedia.org/wiki/Remote_procedure_call Remote procedure calls]&lt;br /&gt;
&lt;br /&gt;
3. Rails Cookie Handling[http://techbrahmana.blogspot.com/2012/03/rails-cookie-handling-serialization-and.html]. Cookies are stored marshalled/unmarshalled to and from client machines.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== '''Serialization in Ruby:''' ==&lt;br /&gt;
&lt;br /&gt;
Ruby provides serialization capabilities through its module, [http://www.ruby-doc.org/core-2.0.0/Marshal.html Marshal]. There are also other libraries like YAML[http://yaml.org/] and JSON which can be used in Ruby to generate serialized objects for purposes like platform independence and human readable formats.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:SerializationChart.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== '''Types of Serialization''' ==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
=== Converting Ruby Objects in Human Readable Formats ===&lt;br /&gt;
----&lt;br /&gt;
The conversion of Ruby objects into YAML and JSON formats are explained below.&lt;br /&gt;
&lt;br /&gt;
==== Converting Ruby Objects to YAML format ====&lt;br /&gt;
&lt;br /&gt;
YAML 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 binary format. 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. In order to use it in Ruby, the yaml.rb file is required to be loaded which provides methods for converting objects into yaml format and creating .yml files.&lt;br /&gt;
&lt;br /&gt;
=====Serialization using YAML=====&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;nowiki&amp;gt; #Serialization using YAML's to_yaml method&lt;br /&gt;
 &lt;br /&gt;
  require &amp;quot;yaml&amp;quot;&lt;br /&gt;
     class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
	@name = name&lt;br /&gt;
	@age = age&lt;br /&gt;
	@country=country&lt;br /&gt;
     end&lt;br /&gt;
      def to_s&lt;br /&gt;
	&amp;quot;In First:\n#{@name}, #{@age}, #{@country}\n&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
  x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
  puts x&lt;br /&gt;
  puts x.to_yaml&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   In First:&lt;br /&gt;
   Tom, 25, USA&lt;br /&gt;
   --- !ruby/object:First&lt;br /&gt;
   name: Tom&lt;br /&gt;
   age: 25&lt;br /&gt;
   country: USA&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The above code displays the object x, first as a string and then in the yaml format.&lt;br /&gt;
&lt;br /&gt;
====='''Saving YAML data into a file:'''=====&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt; # Serialization using YAML::dump&lt;br /&gt;
&lt;br /&gt;
require 'yaml'&lt;br /&gt;
&lt;br /&gt;
f = File.open( 'first.yml', 'w' )&lt;br /&gt;
YAML.dump( [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;], f )&lt;br /&gt;
f.close&lt;br /&gt;
		&lt;br /&gt;
File.open( 'first.yml' ){ |f|	&lt;br /&gt;
    $arr= YAML.load(f)&lt;br /&gt;
}	&lt;br /&gt;
&lt;br /&gt;
p( $arr )&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;]&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The dump function can be used to serialize the data and save it into a file in YAML format. As shown in the above example the data in YAML format can be de-serialized using the load function.&lt;br /&gt;
&lt;br /&gt;
YAML libraries also provides an option of selecting only those variables of the object that are needed to be serialized. This is done using the to_yaml_properties method as shown in the below example.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=====Custom Serialization using YAML=====&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;nowiki&amp;gt;&lt;br /&gt;
    require &amp;quot;yaml&amp;quot;&lt;br /&gt;
     class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
	@name = name&lt;br /&gt;
	@age = age&lt;br /&gt;
	@country=country&lt;br /&gt;
     end&lt;br /&gt;
     def to_s&lt;br /&gt;
	&amp;quot;In First:\n#{@name}, #{@age}, #{@country}\n&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
      def to_yaml_properties&lt;br /&gt;
	   [&amp;quot;@name&amp;quot;,&amp;quot;@age&amp;quot;]  #@country will not be serialized&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
  x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
  puts x&lt;br /&gt;
  puts x.to_yaml&lt;br /&gt;
  &amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   In First:&lt;br /&gt;
   Tom, 25, USA&lt;br /&gt;
   --- !ruby/object:First&lt;br /&gt;
   name: Tom&lt;br /&gt;
   age: 25&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Converting Ruby Objects to JSON format: ====&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
JSON 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. Ruby 1.8.7 distribution is not bundled with json gem. However, in Ruby 1.9.2, the json gem is bundled with the core Ruby distribution.&lt;br /&gt;
The JSON library can be installed using Ruby Gems[http://rubygems.org/] as shown below:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;# gem install json&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A JSON string for serialization can be created by using the JSON.generate method:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
        require 'json'&lt;br /&gt;
        my_hash = {:Welcome =&amp;gt; &amp;quot;Ruby&amp;quot;}&lt;br /&gt;
        puts JSON.generate(my_hash) =&amp;gt; &amp;quot;{\&amp;quot;WELCOME\&amp;quot;:\&amp;quot;RUBY\&amp;quot;}&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
'''Output:'''&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;{&amp;quot;{\&amp;quot;Welcome\&amp;quot;:\&amp;quot;Ruby\&amp;quot;}&amp;quot;=&amp;gt;&amp;quot;{\&amp;quot;WELCOME\&amp;quot;:\&amp;quot;RUBY\&amp;quot;}&amp;quot;}&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A JSON string received from another program can be parsed by using JSON.parse&lt;br /&gt;
Ruby thus converts String to Hash.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
        require 'json'&lt;br /&gt;
        my_hash = JSON.parse('{&amp;quot;Welcome&amp;quot;: &amp;quot;Ruby&amp;quot;}')&lt;br /&gt;
        puts my_hash[&amp;quot;Welcome&amp;quot;] =&amp;gt; &amp;quot;Ruby&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Converting Ruby Objects to Binary Formats ===&lt;br /&gt;
----&lt;br /&gt;
Binary Serialization is another form of serialization in Ruby which is not in human readable form. 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. Binary Serialization  is done using Marshal which is built into Ruby and the code for it is written in Ruby's Marshal module(marshal.c) and thus no additional files are required in order to use it. The _dump and _load methods defined in marshal are used for serialization. Using marshal module the following types of objects can not be serialized: bindings, procedure objects, singleton objects, instances of IO objects and interfaces.&lt;br /&gt;
&lt;br /&gt;
Since the Binary Serialized data is not in human readable form, the following guidelines need to be followed.&lt;br /&gt;
     1.Use print instead of puts[http://ruby-doc.org/core-2.0.0/ARGF.html#] when serialized objects are written to a file in order to avoid new line characters to be written &lt;br /&gt;
       in the file.&lt;br /&gt;
     &lt;br /&gt;
     2.Use a record separator in order to differentiate between two objects.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====='''Binary Serialization Example:'''=====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    class Animal&lt;br /&gt;
      def initialize  name, age&lt;br /&gt;
      @name = name&lt;br /&gt;
      @age=age&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
   class Cat &amp;lt; Animal&lt;br /&gt;
    def to_s&lt;br /&gt;
     &amp;quot;In Cat C: #{@name} \t #{@age}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
  c = Cat.new(&amp;quot;Kitty Kat&amp;quot;,5)&lt;br /&gt;
  puts &amp;quot;Before Serialization&amp;quot;&lt;br /&gt;
  puts c&lt;br /&gt;
  #puts d&lt;br /&gt;
  serialize_cat= Marshal.dump(c) #dumps the serialized cat object into serialize_cat&lt;br /&gt;
  puts &amp;quot;\nAfter Serialization:\n #{serialize_cat}&amp;quot;&lt;br /&gt;
  deserialize_cat= Marshal::load(serialize_cat) #deserializes the cat object and loads it back into deserialize_cat&lt;br /&gt;
  puts &amp;quot;\nAfter Deserialization\n #{deserialize_cat}&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   Before Serialization&lt;br /&gt;
   In Cat C: Kitty Kat 	 5&lt;br /&gt;
   After Serialization:&lt;br /&gt;
   oCat:&lt;br /&gt;
   @nameI&amp;quot;Kitty Kat:ET:	@agei&lt;br /&gt;
   After Deserialization&lt;br /&gt;
   In Cat C: Kitty Kat 	 5&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====='''Saving Marshal data into a file'''=====&lt;br /&gt;
&lt;br /&gt;
Similar to YAML, Marshal can also be used to dump data into a file. The above example showing the serialization using YAML::dump can be written using marshal as shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    f = File.open( 'first.yml', 'w' )&lt;br /&gt;
    Marshal.dump( [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;], f )&lt;br /&gt;
    f.close	&lt;br /&gt;
    File.open( 'first.yml' ){ |f|	&lt;br /&gt;
    $arr= Marshal.load(f)&lt;br /&gt;
    }	&lt;br /&gt;
    p( $arr )&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;]&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice that in the above example there is no &amp;quot;require&amp;quot; statement as opposed to the earlier example of writing serialized data into files using YAML. This is because unlike YAML, marshal is built-in Ruby and  no external libraries is required in order to use its functionality.&lt;br /&gt;
&lt;br /&gt;
====='''Custom serialization using Marshal'''=====&lt;br /&gt;
&lt;br /&gt;
Marshal can also be used to custom serialize an object i.e. it provides an option to omit the variables of an object that are not required in the serialized data. The following program shows the use of marshal_dump method for achieving custom serialization.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
      class First&lt;br /&gt;
      def initialize(name, age, country)&lt;br /&gt;
         @name = name&lt;br /&gt;
         @age = age&lt;br /&gt;
         @country=country&lt;br /&gt;
      end&lt;br /&gt;
      def to_s&lt;br /&gt;
            &amp;quot;In First: #{@name}, #{@age}, #{@country}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
      def marshal_dump&lt;br /&gt;
            [@name,@age]  #@country will not be serialized&lt;br /&gt;
      end&lt;br /&gt;
      def marshal_load(data)&lt;br /&gt;
       @name=data[0]&lt;br /&gt;
       @age=data[1]&lt;br /&gt;
       @country=&amp;quot;United States of America&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
      end&lt;br /&gt;
 &lt;br /&gt;
      x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
      puts x&lt;br /&gt;
  &lt;br /&gt;
      marshal_data = Marshal.dump( x ) &lt;br /&gt;
      y = Marshal.load( marshal_data ) &lt;br /&gt;
      p( y.to_s )&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    In First: Tom, 25, USA&lt;br /&gt;
    &amp;quot;In First: Tom, 25, United States of America&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== '''Serialization Performance in Ruby''' ==&lt;br /&gt;
The different serialization formats described above differ in the efficiency at which they can serialize and deserialize data and thus while serializing large amount of data their efficiency is taken into account. The .report[http://www.ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html] method in Ruby can be used to evaluate the performance of these serialization patterns. Marshal, as it handles data in binary format, is the most efficient form of serialization in Ruby. The following example compares the performance of marshal, JSON and YAML format.[http://www.pauldix.net/2008/08/serializing-dat.html]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   require 'benchmark'&lt;br /&gt;
   require 'rubygems'&lt;br /&gt;
   require 'json'&lt;br /&gt;
   require 'yaml'&lt;br /&gt;
   include Benchmark&lt;br /&gt;
   class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
      @name=name&lt;br /&gt;
      @age=age&lt;br /&gt;
      @country=country&lt;br /&gt;
     end&lt;br /&gt;
   end  &lt;br /&gt;
    x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
    puts &amp;quot;            User     System     Total      Real&amp;quot;&lt;br /&gt;
   benchmark do |t|&lt;br /&gt;
    print &amp;quot;Marshal:&amp;quot;&lt;br /&gt;
    t.report{1000.times do; Marshal.load(Marshal.dump(x));end}&lt;br /&gt;
    print &amp;quot;JSON:   &amp;quot;&lt;br /&gt;
    t.report{1000.times do; JSON.load(JSON.dump(x));end}&lt;br /&gt;
    print &amp;quot;YAML:   &amp;quot;&lt;br /&gt;
    t.report{1000.times do; YAML.load(YAML.dump(x));end}&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
              User     System     Total      Real&lt;br /&gt;
  Marshal:   0.010000   0.000000   0.010000 (  0.015408)&lt;br /&gt;
  JSON:      0.020000   0.010000   0.030000 (  0.022462)&lt;br /&gt;
  YAML:      0.460000   0.010000   0.470000 (  0.472149)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output of the above example shows that, given large chunks of data, marshal is comparatively more efficient format for serialization in Ruby.&lt;br /&gt;
&lt;br /&gt;
== Serialization in other Object-Oriented Languages ==&lt;br /&gt;
&lt;br /&gt;
===Comparison===&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Sl.No !! Ruby !! Java !! .Net Framework !! C++&lt;br /&gt;
|-&lt;br /&gt;
| 1 || Ruby provides a built in module called 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 || Although, there is no built in support for serialization in C++, it can be achieved by using Boost libraries[http://www.boost.org/doc/libs/1_36_0/libs/serialization/example/demo.cpp]&lt;br /&gt;
|-&lt;br /&gt;
| 2 || The built in module of Ruby (Marshal) does not support platform independence, however, it can be achieved by using external libraries like YAML and JSON || Similarly, Java's built in serialization is also not platform independent and in order to use serialization in Java across Ruby platform, jruby library should be used.  || .Net used Remoting technology to make it platform independent. || Serialization using the Boost libraries is not platform independent.&lt;br /&gt;
|-&lt;br /&gt;
| 3 || YAML provides a method [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/syck/rdoc/Object.html (to_yaml_properties)] which can be used to select the variables who's value is need to be serialized. With Marshal, we need to write a method named marshal_dump defining the variables of an object that has to be serialized. || Provides an option for serializing only the required 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 data 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 || Serialization using the Boost libraries is custom and thus the user can specify the part of the objects to be serialized.&lt;br /&gt;
|-&lt;br /&gt;
| 4 || Bindings, procedure objects, singleton objects, instances of IO objects and interfaces can not be serialized. Serializing these objects throws TypeError exceptions. || Thread, OutputStream and its subclasses, and Socket are not serializable in Java.[http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html] || Objects like DataRow are non serializable in .NET. Whether an object is serializable or not can be checked using the [http://msdn.microsoft.com/en-us/library/system.type.isserializable(v=vs.110).aspx?cs-save-lang=1&amp;amp;cs-lang=cpp#code-snippet-1 Type.IsSerializable] || There are no such objects in C++.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===Serialization in Java===&lt;br /&gt;
&lt;br /&gt;
To serialize an object in Java, the class needs to implements Serializable interface and use ObjectOutputStream to serialize an object.&lt;br /&gt;
Similarly, we can use ObjectInputStream to deserialize an object. Below is an example to serialize an object of Class First.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;  &lt;br /&gt;
  import java.io.FileInputStream;&lt;br /&gt;
  import java.io.FileOutputStream;&lt;br /&gt;
  import java.io.IOException;&lt;br /&gt;
  import java.io.ObjectInputStream;&lt;br /&gt;
  import java.io.ObjectOutputStream;&lt;br /&gt;
  import java.io.Serializable;&lt;br /&gt;
           &lt;br /&gt;
 public class First implements Serializable {&lt;br /&gt;
 &lt;br /&gt;
 private static final long serialVersionUID = 1L;&lt;br /&gt;
 public String name;&lt;br /&gt;
 public int age;&lt;br /&gt;
 public String country;&lt;br /&gt;
 &lt;br /&gt;
 private static void serialize(First f, String path) throws IOException {&lt;br /&gt;
  &lt;br /&gt;
       FileOutputStream fos = new FileOutputStream(path);&lt;br /&gt;
       ObjectOutputStream oos = new ObjectOutputStream(fos);&lt;br /&gt;
    &lt;br /&gt;
       // Serializing&lt;br /&gt;
       oos.writeObject(f);&lt;br /&gt;
       oos.close();&lt;br /&gt;
       fos.close();&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
 private static First deserialize(String path) throws IOException,&lt;br /&gt;
  		ClassNotFoundException {&lt;br /&gt;
  	// tom.ser will deserialize in object f&lt;br /&gt;
  	First f = null;&lt;br /&gt;
  	FileInputStream fin = new FileInputStream(path);&lt;br /&gt;
  	ObjectInputStream oin = new ObjectInputStream(fin);&lt;br /&gt;
  	// DeSerializing and Casting to Class First&lt;br /&gt;
  	f = (First) oin.readObject();&lt;br /&gt;
  	oin.close();&lt;br /&gt;
  	fin.close();&lt;br /&gt;
  	return f;&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  public static void main(String[] args) {&lt;br /&gt;
  	try {&lt;br /&gt;
  		// Creating object for Tom&lt;br /&gt;
  		First f1 = new First();&lt;br /&gt;
  		f1.name = &amp;quot;Tom&amp;quot;;&lt;br /&gt;
  		f1.age = 25;&lt;br /&gt;
  		f1.country = &amp;quot;USA&amp;quot;;&lt;br /&gt;
  		First f2 = null;&lt;br /&gt;
  		long start = System.currentTimeMillis();&lt;br /&gt;
  		for (int i = 0; i &amp;lt; 1000; i++){&lt;br /&gt;
  		// serializing&lt;br /&gt;
  		serialize(f1, &amp;quot;/path/to/files/&amp;quot; + i + &amp;quot;.ser&amp;quot;);&lt;br /&gt;
  		// deserializing&lt;br /&gt;
  		f2 = deserialize(&amp;quot;/path/to/files&amp;quot; + i + &amp;quot;.ser&amp;quot;);&lt;br /&gt;
  		}&lt;br /&gt;
  		long end = System.currentTimeMillis();&lt;br /&gt;
  		long totalDuration = end - start;&lt;br /&gt;
  		System.out.println(&amp;quot;Serialization/Deserialization for 1000 Iterations duration: &amp;quot; + totalDuration + &amp;quot; Milliseconds&amp;quot;);&lt;br /&gt;
  		// Printing deserialized object&lt;br /&gt;
  		System.out.println(&amp;quot;Deserialized =&amp;gt; Name: &amp;quot; + f2.name + &amp;quot;, Age: &amp;quot; + f2.age + &amp;quot;, Country: &amp;quot; + f2.country);&lt;br /&gt;
  	} catch (IOException ex) {&lt;br /&gt;
  		e.printStackTrace();&lt;br /&gt;
  	} catch (ClassNotFoundException ex) {&lt;br /&gt;
  		e.printStackTrace();&lt;br /&gt;
  	}&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 Serialization/Deserialization for 1000 Iterations duration: 1651335109 Nano Seconds&lt;br /&gt;
 Deserialized =&amp;gt; Name: Tom, Age: 25, Country: USA&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== See Also==&lt;br /&gt;
&lt;br /&gt;
1. [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/json/rdoc/JSON.html Serialization in Ruby JSON]&lt;br /&gt;
&lt;br /&gt;
2. [http://api.rubyonrails.org/classes/ActiveModel/Serialization.html Serialization in Rails]&lt;br /&gt;
&lt;br /&gt;
3. [http://gregmoreno.wordpress.com/2011/01/27/preventing-model-explosion-via-rails-serialization/ Article on Rails Serialization]&lt;br /&gt;
&lt;br /&gt;
4. [http://en.wikipedia.org/wiki/Google_Protocol_Buffers Protocol Buffers]&lt;br /&gt;
&lt;br /&gt;
5. [https://rubygems.org/gems/ruby-protocol-buffers Ruby Protocol Buffers Gem]&lt;br /&gt;
&lt;br /&gt;
6. [http://en.wikipedia.org/wiki/Apache_Avro Avro]&lt;br /&gt;
&lt;br /&gt;
7. [http://rubygems.org/gems/avro Ruby Avro Gem]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
1. [http://en.wikipedia.org/wiki/Serialization Serilization in General]&lt;br /&gt;
&lt;br /&gt;
2. [http://en.wikipedia.org/wiki/Marshalling_%28computer_science%29 Marshaling]&lt;br /&gt;
&lt;br /&gt;
3. [http://json.org/ JSON]&lt;br /&gt;
&lt;br /&gt;
4. [http://techbrahmana.blogspot.com/2012/03/rails-cookie-handling-serialization-and.html Rails Cookie Handling]&lt;br /&gt;
&lt;br /&gt;
5. [http://yaml.org/ YAML]&lt;br /&gt;
&lt;br /&gt;
6. [http://rubygems.org/ Ruby Gems]&lt;br /&gt;
&lt;br /&gt;
7. [http://ruby-doc.org/core-2.0.0/ARGF.html# Ruby ARGF Documentation]&lt;br /&gt;
&lt;br /&gt;
8. [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html Ruby Documentation for Benchmark Gem]&lt;br /&gt;
&lt;br /&gt;
9. [http://www.pauldix.net/2008/08/serializing-dat.html Serialization Performance Comparison]&lt;br /&gt;
&lt;br /&gt;
10. [http://www.boost.org/doc/libs/1_36_0/libs/serialization/example/demo.cpp Boost Libraries]&lt;br /&gt;
&lt;br /&gt;
11. [http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html Java Serialization]&lt;br /&gt;
&lt;br /&gt;
12. [http://www.w3schools.com/json/ JSON Tutorial]&lt;br /&gt;
&lt;br /&gt;
13. [http://www.skorks.com/2010/04/serializing-and-deserializing-objects-with-ruby/ Serializing and De-serializing in Ruby]&lt;br /&gt;
&lt;br /&gt;
14. [http://www.codeproject.com/Articles/33296/Serialization-and-De-serialization Serialization and De-serialization]&lt;br /&gt;
&lt;br /&gt;
15. [http://msdn.microsoft.com/en-us/library/182eeyhh.aspx XML Serialization]&lt;br /&gt;
&lt;br /&gt;
16. [http://www.tutorialspoint.com/java/java_serialization.htm Java Serialization Tutorial]&lt;br /&gt;
&lt;br /&gt;
17. [http://www.sapphiresteel.com/ruby-programming/The-Book-Of-Ruby The Book of Ruby]&lt;br /&gt;
&lt;br /&gt;
18. [http://www.codeproject.com/Articles/20962/Introducing-Serialization-in-NET Serialization in .Net]&lt;br /&gt;
&lt;br /&gt;
19. [http://ruby.about.com/od/advancedruby/ss/Serialization-In-Ruby-Yaml.html Serialization in Ruby YAML]&lt;br /&gt;
&lt;br /&gt;
20. [http://ruby.about.com/od/tasks/a/The-Json-Gem.html Installing JSON gem]&lt;br /&gt;
&lt;br /&gt;
21. [http://www.ruby-doc.org/stdlib-2.0.0/libdoc/json/rdoc/JSON.html Serialization in Ruby JSON]&lt;/div&gt;</summary>
		<author><name>Mmehta2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1d_mm&amp;diff=83429</id>
		<title>CSC/ECE 517 Spring 2014/ch1a 1d mm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1d_mm&amp;diff=83429"/>
		<updated>2014-02-18T03:21:45Z</updated>

		<summary type="html">&lt;p&gt;Mmehta2: /* Serialization in Java */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page discusses Serialization, focusing on Ruby programming language. It describes different methods of Serialization in Ruby. In addition, it also compares serialization in different Object Oriented languages.&lt;br /&gt;
&lt;br /&gt;
== '''Introduction''' ==&lt;br /&gt;
&lt;br /&gt;
==='''Serialization'''===&lt;br /&gt;
&lt;br /&gt;
'''Serialization'''[http://en.wikipedia.org/wiki/Serialization] is a process of converting a data structure or an object into a stream of bytes or string to facilitate storage in memory, file (persistence storage) or transmission over a network. The process of Serialization is also referred to as ''Marshalling''[http://en.wikipedia.org/wiki/Marshalling_%28computer_science%29]. 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.&lt;br /&gt;
&lt;br /&gt;
==='''De-serialization'''===&lt;br /&gt;
&lt;br /&gt;
'''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''.&lt;br /&gt;
&lt;br /&gt;
The process of Serialization/De-serialization is not a replacement for database systems. Database Systems are used for their relational and transactional semantics.&lt;br /&gt;
&lt;br /&gt;
==='''Basic Advantages of Serialization'''===&lt;br /&gt;
&lt;br /&gt;
1. Communication between two or more processes on same machine. Object state can be saved and shared in a persistent or in-memory store.&lt;br /&gt;
&lt;br /&gt;
2. Communication between processes on different machines. Serialization facilitates the transmission of an object through a network.&lt;br /&gt;
&lt;br /&gt;
3. Creating a clone of an object. &lt;br /&gt;
&lt;br /&gt;
4. Cross-platform compatibility. Object can be serialized in a common format that is understood by multiple platforms. For example, JSON[http://www.json.org/] and XML.&lt;br /&gt;
&lt;br /&gt;
==='''Practical Applications for Serialization'''===&lt;br /&gt;
&lt;br /&gt;
1. [http://docs.oracle.com/cd/E13222_01/wls/docs81/webapp/sessions.html#139726 HTTP Session] Replication by sharing session objects across web servers for load balancing and handling failover scenarios.&lt;br /&gt;
&lt;br /&gt;
2. Serialization facilitates communication in [http://en.wikipedia.org/wiki/Java_remote_method_invocation Remote Method Invocation] or [http://en.wikipedia.org/wiki/Remote_procedure_call Remote procedure calls]&lt;br /&gt;
&lt;br /&gt;
3. Rails Cookie Handling[http://techbrahmana.blogspot.com/2012/03/rails-cookie-handling-serialization-and.html]. Cookies are stored marshalled/unmarshalled to and from client machines.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== '''Serialization in Ruby:''' ==&lt;br /&gt;
&lt;br /&gt;
Ruby provides serialization capabilities through its module, [http://www.ruby-doc.org/core-2.0.0/Marshal.html Marshal]. There are also other libraries like YAML[http://yaml.org/] and JSON which can be used in Ruby to generate serialized objects for purposes like platform independence and human readable formats.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:SerializationChart.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== '''Types of Serialization''' ==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
=== Converting Ruby Objects in Human Readable Formats ===&lt;br /&gt;
----&lt;br /&gt;
The conversion of Ruby objects into YAML and JSON formats are explained below.&lt;br /&gt;
&lt;br /&gt;
==== Converting Ruby Objects to YAML format ====&lt;br /&gt;
&lt;br /&gt;
YAML 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 binary format. 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. In order to use it in Ruby, the yaml.rb file is required to be loaded which provides methods for converting objects into yaml format and creating .yml files.&lt;br /&gt;
&lt;br /&gt;
=====Serialization using YAML=====&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;nowiki&amp;gt; #Serialization using YAML's to_yaml method&lt;br /&gt;
 &lt;br /&gt;
  require &amp;quot;yaml&amp;quot;&lt;br /&gt;
     class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
	@name = name&lt;br /&gt;
	@age = age&lt;br /&gt;
	@country=country&lt;br /&gt;
     end&lt;br /&gt;
      def to_s&lt;br /&gt;
	&amp;quot;In First:\n#{@name}, #{@age}, #{@country}\n&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
  x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
  puts x&lt;br /&gt;
  puts x.to_yaml&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   In First:&lt;br /&gt;
   Tom, 25, USA&lt;br /&gt;
   --- !ruby/object:First&lt;br /&gt;
   name: Tom&lt;br /&gt;
   age: 25&lt;br /&gt;
   country: USA&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The above code displays the object x, first as a string and then in the yaml format.&lt;br /&gt;
&lt;br /&gt;
====='''Saving YAML data into a file:'''=====&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt; # Serialization using YAML::dump&lt;br /&gt;
&lt;br /&gt;
require 'yaml'&lt;br /&gt;
&lt;br /&gt;
f = File.open( 'first.yml', 'w' )&lt;br /&gt;
YAML.dump( [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;], f )&lt;br /&gt;
f.close&lt;br /&gt;
		&lt;br /&gt;
File.open( 'first.yml' ){ |f|	&lt;br /&gt;
    $arr= YAML.load(f)&lt;br /&gt;
}	&lt;br /&gt;
&lt;br /&gt;
p( $arr )&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;]&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The dump function can be used to serialize the data and save it into a file in YAML format. As shown in the above example the data in YAML format can be de-serialized using the load function.&lt;br /&gt;
&lt;br /&gt;
YAML libraries also provides an option of selecting only those variables of the object that are needed to be serialized. This is done using the to_yaml_properties method as shown in the below example.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=====Custom Serialization using YAML=====&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;nowiki&amp;gt;&lt;br /&gt;
    require &amp;quot;yaml&amp;quot;&lt;br /&gt;
     class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
	@name = name&lt;br /&gt;
	@age = age&lt;br /&gt;
	@country=country&lt;br /&gt;
     end&lt;br /&gt;
     def to_s&lt;br /&gt;
	&amp;quot;In First:\n#{@name}, #{@age}, #{@country}\n&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
      def to_yaml_properties&lt;br /&gt;
	   [&amp;quot;@name&amp;quot;,&amp;quot;@age&amp;quot;]  #@country will not be serialized&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
  x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
  puts x&lt;br /&gt;
  puts x.to_yaml&lt;br /&gt;
  &amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   In First:&lt;br /&gt;
   Tom, 25, USA&lt;br /&gt;
   --- !ruby/object:First&lt;br /&gt;
   name: Tom&lt;br /&gt;
   age: 25&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Converting Ruby Objects to JSON format: ====&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
JSON 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. Ruby 1.8.7 distribution is not bundled with json gem. However, in Ruby 1.9.2, the json gem is bundled with the core Ruby distribution.&lt;br /&gt;
The JSON library can be installed using Ruby Gems[http://rubygems.org/] as shown below:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;# gem install json&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A JSON string for serialization can be created by using the JSON.generate method:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
        require 'json'&lt;br /&gt;
        my_hash = {:Welcome =&amp;gt; &amp;quot;Ruby&amp;quot;}&lt;br /&gt;
        puts JSON.generate(my_hash) =&amp;gt; &amp;quot;{\&amp;quot;WELCOME\&amp;quot;:\&amp;quot;RUBY\&amp;quot;}&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
'''Output:'''&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;{&amp;quot;{\&amp;quot;Welcome\&amp;quot;:\&amp;quot;Ruby\&amp;quot;}&amp;quot;=&amp;gt;&amp;quot;{\&amp;quot;WELCOME\&amp;quot;:\&amp;quot;RUBY\&amp;quot;}&amp;quot;}&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A JSON string received from another program can be parsed by using JSON.parse&lt;br /&gt;
Ruby thus converts String to Hash.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
        require 'json'&lt;br /&gt;
        my_hash = JSON.parse('{&amp;quot;Welcome&amp;quot;: &amp;quot;Ruby&amp;quot;}')&lt;br /&gt;
        puts my_hash[&amp;quot;Welcome&amp;quot;] =&amp;gt; &amp;quot;Ruby&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Converting Ruby Objects to Binary Formats ===&lt;br /&gt;
----&lt;br /&gt;
Binary Serialization is another form of serialization in Ruby which is not in human readable form. 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. Binary Serialization  is done using Marshal which is built into Ruby and the code for it is written in Ruby's Marshal module(marshal.c) and thus no additional files are required in order to use it. The _dump and _load methods defined in marshal are used for serialization. Using marshal module the following types of objects can not be serialized: bindings, procedure objects, singleton objects, instances of IO objects and interfaces.&lt;br /&gt;
&lt;br /&gt;
Since the Binary Serialized data is not in human readable form, the following guidelines need to be followed.&lt;br /&gt;
     1.Use print instead of puts[http://ruby-doc.org/core-2.0.0/ARGF.html#] when serialized objects are written to a file in order to avoid new line characters to be written &lt;br /&gt;
       in the file.&lt;br /&gt;
     &lt;br /&gt;
     2.Use a record separator in order to differentiate between two objects.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====='''Binary Serialization Example:'''=====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    class Animal&lt;br /&gt;
      def initialize  name, age&lt;br /&gt;
      @name = name&lt;br /&gt;
      @age=age&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
   class Cat &amp;lt; Animal&lt;br /&gt;
    def to_s&lt;br /&gt;
     &amp;quot;In Cat C: #{@name} \t #{@age}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
  c = Cat.new(&amp;quot;Kitty Kat&amp;quot;,5)&lt;br /&gt;
  puts &amp;quot;Before Serialization&amp;quot;&lt;br /&gt;
  puts c&lt;br /&gt;
  #puts d&lt;br /&gt;
  serialize_cat= Marshal.dump(c) #dumps the serialized cat object into serialize_cat&lt;br /&gt;
  puts &amp;quot;\nAfter Serialization:\n #{serialize_cat}&amp;quot;&lt;br /&gt;
  deserialize_cat= Marshal::load(serialize_cat) #deserializes the cat object and loads it back into deserialize_cat&lt;br /&gt;
  puts &amp;quot;\nAfter Deserialization\n #{deserialize_cat}&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   Before Serialization&lt;br /&gt;
   In Cat C: Kitty Kat 	 5&lt;br /&gt;
   After Serialization:&lt;br /&gt;
   oCat:&lt;br /&gt;
   @nameI&amp;quot;Kitty Kat:ET:	@agei&lt;br /&gt;
   After Deserialization&lt;br /&gt;
   In Cat C: Kitty Kat 	 5&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====='''Saving Marshal data into a file'''=====&lt;br /&gt;
&lt;br /&gt;
Similar to YAML, Marshal can also be used to dump data into a file. The above example showing the serialization using YAML::dump can be written using marshal as shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    f = File.open( 'first.yml', 'w' )&lt;br /&gt;
    Marshal.dump( [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;], f )&lt;br /&gt;
    f.close	&lt;br /&gt;
    File.open( 'first.yml' ){ |f|	&lt;br /&gt;
    $arr= Marshal.load(f)&lt;br /&gt;
    }	&lt;br /&gt;
    p( $arr )&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;]&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice that in the above example there is no &amp;quot;require&amp;quot; statement as opposed to the earlier example of writing serialized data into files using YAML. This is because unlike YAML, marshal is built-in Ruby and  no external libraries is required in order to use its functionality.&lt;br /&gt;
&lt;br /&gt;
====='''Custom serialization using Marshal'''=====&lt;br /&gt;
&lt;br /&gt;
Marshal can also be used to custom serialize an object i.e. it provides an option to omit the variables of an object that are not required in the serialized data. The following program shows the use of marshal_dump method for achieving custom serialization.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
      class First&lt;br /&gt;
      def initialize(name, age, country)&lt;br /&gt;
         @name = name&lt;br /&gt;
         @age = age&lt;br /&gt;
         @country=country&lt;br /&gt;
      end&lt;br /&gt;
      def to_s&lt;br /&gt;
            &amp;quot;In First: #{@name}, #{@age}, #{@country}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
      def marshal_dump&lt;br /&gt;
            [@name,@age]  #@country will not be serialized&lt;br /&gt;
      end&lt;br /&gt;
      def marshal_load(data)&lt;br /&gt;
       @name=data[0]&lt;br /&gt;
       @age=data[1]&lt;br /&gt;
       @country=&amp;quot;United States of America&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
      end&lt;br /&gt;
 &lt;br /&gt;
      x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
      puts x&lt;br /&gt;
  &lt;br /&gt;
      marshal_data = Marshal.dump( x ) &lt;br /&gt;
      y = Marshal.load( marshal_data ) &lt;br /&gt;
      p( y.to_s )&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    In First: Tom, 25, USA&lt;br /&gt;
    &amp;quot;In First: Tom, 25, United States of America&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== '''Serialization Performance in Ruby''' ==&lt;br /&gt;
The different serialization formats described above differ in the efficiency at which they can serialize and deserialize data and thus while serializing large amount of data their efficiency is taken into account. The .report[http://www.ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html] method in Ruby can be used to evaluate the performance of these serialization patterns. Marshal, as it handles data in binary format, is the most efficient form of serialization in Ruby. The following example compares the performance of marshal, JSON and YAML format.[http://www.pauldix.net/2008/08/serializing-dat.html]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   require 'benchmark'&lt;br /&gt;
   require 'rubygems'&lt;br /&gt;
   require 'json'&lt;br /&gt;
   require 'yaml'&lt;br /&gt;
   include Benchmark&lt;br /&gt;
   class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
      @name=name&lt;br /&gt;
      @age=age&lt;br /&gt;
      @country=country&lt;br /&gt;
     end&lt;br /&gt;
   end  &lt;br /&gt;
    x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
    puts &amp;quot;            User     System     Total      Real&amp;quot;&lt;br /&gt;
   benchmark do |t|&lt;br /&gt;
    print &amp;quot;Marshal:&amp;quot;&lt;br /&gt;
    t.report{1000.times do; Marshal.load(Marshal.dump(x));end}&lt;br /&gt;
    print &amp;quot;JSON:   &amp;quot;&lt;br /&gt;
    t.report{1000.times do; JSON.load(JSON.dump(x));end}&lt;br /&gt;
    print &amp;quot;YAML:   &amp;quot;&lt;br /&gt;
    t.report{1000.times do; YAML.load(YAML.dump(x));end}&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
              User     System     Total      Real&lt;br /&gt;
  Marshal:   0.010000   0.000000   0.010000 (  0.015408)&lt;br /&gt;
  JSON:      0.020000   0.010000   0.030000 (  0.022462)&lt;br /&gt;
  YAML:      0.460000   0.010000   0.470000 (  0.472149)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output of the above example shows that, given large chunks of data, marshal is comparatively more efficient format for serialization in Ruby.&lt;br /&gt;
&lt;br /&gt;
== Serialization in other Object-Oriented Languages ==&lt;br /&gt;
&lt;br /&gt;
===Comparison===&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Sl.No !! Ruby !! Java !! .Net Framework !! C++&lt;br /&gt;
|-&lt;br /&gt;
| 1 || Ruby provides a built in module called 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 || Although, there is no built in support for serialization in C++, it can be achieved by using Boost libraries[http://www.boost.org/doc/libs/1_36_0/libs/serialization/example/demo.cpp]&lt;br /&gt;
|-&lt;br /&gt;
| 2 || The built in module of Ruby (Marshal) does not support platform independence, however, it can be achieved by using external libraries like YAML and JSON || Similarly, Java's built in serialization is also not platform independent and in order to use serialization in Java across Ruby platform, jruby library should be used.  || .Net used Remoting technology to make it platform independent. || Serialization using the Boost libraries is not platform independent.&lt;br /&gt;
|-&lt;br /&gt;
| 3 || YAML provides a method [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/syck/rdoc/Object.html (to_yaml_properties)] which can be used to select the variables who's value is need to be serialized. With Marshal, we need to write a method named marshal_dump defining the variables of an object that has to be serialized. || Provides an option for serializing only the required 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 data 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 || Serialization using the Boost libraries is custom and thus the user can specify the part of the objects to be serialized.&lt;br /&gt;
|-&lt;br /&gt;
| 4 || Bindings, procedure objects, singleton objects, instances of IO objects and interfaces can not be serialized. Serializing these objects throws TypeError exceptions. || Thread, OutputStream and its subclasses, and Socket are not serializable in Java.[http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html] || Objects like DataRow are non serializable in .NET. Whether an object is serializable or not can be checked using the [http://msdn.microsoft.com/en-us/library/system.type.isserializable(v=vs.110).aspx?cs-save-lang=1&amp;amp;cs-lang=cpp#code-snippet-1 Type.IsSerializable] || There are no such objects in C++.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===Serialization in Java===&lt;br /&gt;
&lt;br /&gt;
To serialize an object in Java, the class needs to implements Serializable interface and use ObjectOutputStream to serialize an object.&lt;br /&gt;
Similarly, we can use ObjectInputStream to deserialize an object. Below is an example to serialize an object of Class First.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;  &lt;br /&gt;
  import java.io.FileInputStream;&lt;br /&gt;
  import java.io.FileOutputStream;&lt;br /&gt;
  import java.io.IOException;&lt;br /&gt;
  import java.io.ObjectInputStream;&lt;br /&gt;
  import java.io.ObjectOutputStream;&lt;br /&gt;
  import java.io.Serializable;&lt;br /&gt;
           &lt;br /&gt;
 public class First implements Serializable {&lt;br /&gt;
 &lt;br /&gt;
 private static final long serialVersionUID = 1L;&lt;br /&gt;
 public String name;&lt;br /&gt;
 public int age;&lt;br /&gt;
 public String country;&lt;br /&gt;
 &lt;br /&gt;
 private static void serialize(First f, String path) throws IOException {&lt;br /&gt;
  &lt;br /&gt;
       FileOutputStream fos = new FileOutputStream(path);&lt;br /&gt;
       ObjectOutputStream oos = new ObjectOutputStream(fos);&lt;br /&gt;
    &lt;br /&gt;
       // Serializing&lt;br /&gt;
       oos.writeObject(f);&lt;br /&gt;
       oos.close();&lt;br /&gt;
       fos.close();&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
 private static First deserialize(String path) throws IOException,&lt;br /&gt;
  		ClassNotFoundException {&lt;br /&gt;
  	// tom.ser will deserialize in object f&lt;br /&gt;
  	First f = null;&lt;br /&gt;
  	FileInputStream fin = new FileInputStream(path);&lt;br /&gt;
  	ObjectInputStream oin = new ObjectInputStream(fin);&lt;br /&gt;
  	// DeSerializing and Casting to Class First&lt;br /&gt;
  	f = (First) oin.readObject();&lt;br /&gt;
  	oin.close();&lt;br /&gt;
  	fin.close();&lt;br /&gt;
  	return f;&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  public static void main(String[] args) {&lt;br /&gt;
  	try {&lt;br /&gt;
  		// Creating object for Tom&lt;br /&gt;
  		First f1 = new First();&lt;br /&gt;
  		f1.name = &amp;quot;Tom&amp;quot;;&lt;br /&gt;
  		f1.age = 25;&lt;br /&gt;
  		f1.country = &amp;quot;USA&amp;quot;;&lt;br /&gt;
  		First f2 = null;&lt;br /&gt;
  		long start = System.currentTimeMillis();&lt;br /&gt;
  		for (int i = 0; i &amp;lt; 1000; i++){&lt;br /&gt;
  		// serializing&lt;br /&gt;
  		serialize(f1, &amp;quot;/path/to/files/&amp;quot; + i + &amp;quot;.ser&amp;quot;);&lt;br /&gt;
  		// deserializing&lt;br /&gt;
  		f2 = deserialize(&amp;quot;/path/to/files&amp;quot; + i + &amp;quot;.ser&amp;quot;);&lt;br /&gt;
  		}&lt;br /&gt;
  		long end = System.currentTimeMillis();&lt;br /&gt;
  		long totalDuration = end - start;&lt;br /&gt;
  		System.out.println(&amp;quot;Serialization/Deserialization for 1000 Iterations duration: &amp;quot; + totalDuration + &amp;quot; Milliseconds&amp;quot;);&lt;br /&gt;
  		// Printing deserialized object&lt;br /&gt;
  		System.out.println(&amp;quot;Deserialized =&amp;gt; Name: &amp;quot; + f2.name + &amp;quot;, Age: &amp;quot; + f2.age + &amp;quot;, Country: &amp;quot; + f2.country);&lt;br /&gt;
  	} catch (IOException ex) {&lt;br /&gt;
  		e.printStackTrace();&lt;br /&gt;
  	} catch (ClassNotFoundException ex) {&lt;br /&gt;
  		e.printStackTrace();&lt;br /&gt;
  	}&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 Serialization/Deserialization for 1000 Iterations duration: 1651335109 Nano Seconds&lt;br /&gt;
 Deserialized =&amp;gt; Name: Tom, Age: 25, Country: USA&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== See Also==&lt;br /&gt;
&lt;br /&gt;
1. [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/json/rdoc/JSON.html Serialization in Ruby JSON]&lt;br /&gt;
&lt;br /&gt;
2. [http://api.rubyonrails.org/classes/ActiveModel/Serialization.html Serialization in Rails]&lt;br /&gt;
&lt;br /&gt;
3. [http://gregmoreno.wordpress.com/2011/01/27/preventing-model-explosion-via-rails-serialization/ Article on Rails Serialization]&lt;br /&gt;
&lt;br /&gt;
4. [http://en.wikipedia.org/wiki/Google_Protocol_Buffers Protocol Buffers]&lt;br /&gt;
&lt;br /&gt;
5. [https://rubygems.org/gems/ruby-protocol-buffers Ruby Protocol Buffers Gem]&lt;br /&gt;
&lt;br /&gt;
6. [http://en.wikipedia.org/wiki/Apache_Avro Avro]&lt;br /&gt;
&lt;br /&gt;
7. [http://rubygems.org/gems/avro Ruby Avro Gem]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
1. [http://en.wikipedia.org/wiki/Serialization Serilization in General]&lt;br /&gt;
&lt;br /&gt;
2. [http://en.wikipedia.org/wiki/Marshalling_%28computer_science%29 Marshaling]&lt;br /&gt;
&lt;br /&gt;
3. [http://techbrahmana.blogspot.com/2012/03/rails-cookie-handling-serialization-and.html Rails Cookie Handling]&lt;br /&gt;
&lt;br /&gt;
4. [http://yaml.org/ YAML]&lt;br /&gt;
&lt;br /&gt;
5. [http://www.ruby-doc.org/core-2.1.0/Marshal.html Marshal module]&lt;br /&gt;
&lt;br /&gt;
6. [http://json.org/ JSON]&lt;br /&gt;
&lt;br /&gt;
7. [http://rubygems.org/ Ruby Gems]&lt;br /&gt;
&lt;br /&gt;
8. [http://ruby-doc.org/core-2.0.0/ARGF.html# Ruby ARGF Documentation]&lt;br /&gt;
&lt;br /&gt;
9. [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html Ruby Documentation for Benchmark Gem]&lt;br /&gt;
&lt;br /&gt;
10. [http://www.pauldix.net/2008/08/serializing-dat.html Serialization Performance Comparison]&lt;br /&gt;
&lt;br /&gt;
11. [http://www.boost.org/doc/libs/1_36_0/libs/serialization/example/demo.cpp Boost Libraries]&lt;br /&gt;
&lt;br /&gt;
12. [http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html Java Serialization]&lt;br /&gt;
&lt;br /&gt;
13. [http://www.w3schools.com/json/ JSON Tutorial]&lt;br /&gt;
&lt;br /&gt;
14. [http://www.skorks.com/2010/04/serializing-and-deserializing-objects-with-ruby/ Serializing and De-serializing in Ruby]&lt;br /&gt;
&lt;br /&gt;
15. [http://www.codeproject.com/Articles/33296/Serialization-and-De-serialization Serialization and De-serialization]&lt;br /&gt;
&lt;br /&gt;
16. [http://msdn.microsoft.com/en-us/library/182eeyhh.aspx XML Serialization]&lt;br /&gt;
&lt;br /&gt;
17. [http://www.tutorialspoint.com/java/java_serialization.htm Java Serialization Tutorial]&lt;br /&gt;
&lt;br /&gt;
18. [http://www.sapphiresteel.com/ruby-programming/The-Book-Of-Ruby The Book of Ruby]&lt;br /&gt;
&lt;br /&gt;
19. [http://www.codeproject.com/Articles/20962/Introducing-Serialization-in-NET Serialization in .Net]&lt;br /&gt;
&lt;br /&gt;
20. [http://ruby.about.com/od/advancedruby/ss/Serialization-In-Ruby-Yaml.html Serialization in Ruby YAML]&lt;br /&gt;
&lt;br /&gt;
21. [http://ruby.about.com/od/tasks/a/The-Json-Gem.html Installing JSON gem]&lt;br /&gt;
&lt;br /&gt;
22. [http://www.ruby-doc.org/stdlib-2.0.0/libdoc/json/rdoc/JSON.html Serialization in Ruby JSON]&lt;/div&gt;</summary>
		<author><name>Mmehta2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1d_mm&amp;diff=83428</id>
		<title>CSC/ECE 517 Spring 2014/ch1a 1d mm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1d_mm&amp;diff=83428"/>
		<updated>2014-02-18T03:17:58Z</updated>

		<summary type="html">&lt;p&gt;Mmehta2: /* Serialization in Java */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page discusses Serialization, focusing on Ruby programming language. It describes different methods of Serialization in Ruby. In addition, it also compares serialization in different Object Oriented languages.&lt;br /&gt;
&lt;br /&gt;
== '''Introduction''' ==&lt;br /&gt;
&lt;br /&gt;
==='''Serialization'''===&lt;br /&gt;
&lt;br /&gt;
'''Serialization'''[http://en.wikipedia.org/wiki/Serialization] is a process of converting a data structure or an object into a stream of bytes or string to facilitate storage in memory, file (persistence storage) or transmission over a network. The process of Serialization is also referred to as ''Marshalling''[http://en.wikipedia.org/wiki/Marshalling_%28computer_science%29]. 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.&lt;br /&gt;
&lt;br /&gt;
==='''De-serialization'''===&lt;br /&gt;
&lt;br /&gt;
'''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''.&lt;br /&gt;
&lt;br /&gt;
The process of Serialization/De-serialization is not a replacement for database systems. Database Systems are used for their relational and transactional semantics.&lt;br /&gt;
&lt;br /&gt;
==='''Basic Advantages of Serialization'''===&lt;br /&gt;
&lt;br /&gt;
1. Communication between two or more processes on same machine. Object state can be saved and shared in a persistent or in-memory store.&lt;br /&gt;
&lt;br /&gt;
2. Communication between processes on different machines. Serialization facilitates the transmission of an object through a network.&lt;br /&gt;
&lt;br /&gt;
3. Creating a clone of an object. &lt;br /&gt;
&lt;br /&gt;
4. Cross-platform compatibility. Object can be serialized in a common format that is understood by multiple platforms. For example, JSON[http://www.json.org/] and XML.&lt;br /&gt;
&lt;br /&gt;
==='''Practical Applications for Serialization'''===&lt;br /&gt;
&lt;br /&gt;
1. [http://docs.oracle.com/cd/E13222_01/wls/docs81/webapp/sessions.html#139726 HTTP Session] Replication by sharing session objects across web servers for load balancing and handling failover scenarios.&lt;br /&gt;
&lt;br /&gt;
2. Serialization facilitates communication in [http://en.wikipedia.org/wiki/Java_remote_method_invocation Remote Method Invocation] or [http://en.wikipedia.org/wiki/Remote_procedure_call Remote procedure calls]&lt;br /&gt;
&lt;br /&gt;
3. Rails Cookie Handling[http://techbrahmana.blogspot.com/2012/03/rails-cookie-handling-serialization-and.html]. Cookies are stored marshalled/unmarshalled to and from client machines.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== '''Serialization in Ruby:''' ==&lt;br /&gt;
&lt;br /&gt;
Ruby provides serialization capabilities through its module, [http://www.ruby-doc.org/core-2.0.0/Marshal.html Marshal]. There are also other libraries like YAML[http://yaml.org/] and JSON which can be used in Ruby to generate serialized objects for purposes like platform independence and human readable formats.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:SerializationChart.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== '''Types of Serialization''' ==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
=== Converting Ruby Objects in Human Readable Formats ===&lt;br /&gt;
----&lt;br /&gt;
The conversion of Ruby objects into YAML and JSON formats are explained below.&lt;br /&gt;
&lt;br /&gt;
==== Converting Ruby Objects to YAML format ====&lt;br /&gt;
&lt;br /&gt;
YAML 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 binary format. 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. In order to use it in Ruby, the yaml.rb file is required to be loaded which provides methods for converting objects into yaml format and creating .yml files.&lt;br /&gt;
&lt;br /&gt;
=====Serialization using YAML=====&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;nowiki&amp;gt; #Serialization using YAML's to_yaml method&lt;br /&gt;
 &lt;br /&gt;
  require &amp;quot;yaml&amp;quot;&lt;br /&gt;
     class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
	@name = name&lt;br /&gt;
	@age = age&lt;br /&gt;
	@country=country&lt;br /&gt;
     end&lt;br /&gt;
      def to_s&lt;br /&gt;
	&amp;quot;In First:\n#{@name}, #{@age}, #{@country}\n&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
  x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
  puts x&lt;br /&gt;
  puts x.to_yaml&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   In First:&lt;br /&gt;
   Tom, 25, USA&lt;br /&gt;
   --- !ruby/object:First&lt;br /&gt;
   name: Tom&lt;br /&gt;
   age: 25&lt;br /&gt;
   country: USA&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The above code displays the object x, first as a string and then in the yaml format.&lt;br /&gt;
&lt;br /&gt;
====='''Saving YAML data into a file:'''=====&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt; # Serialization using YAML::dump&lt;br /&gt;
&lt;br /&gt;
require 'yaml'&lt;br /&gt;
&lt;br /&gt;
f = File.open( 'first.yml', 'w' )&lt;br /&gt;
YAML.dump( [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;], f )&lt;br /&gt;
f.close&lt;br /&gt;
		&lt;br /&gt;
File.open( 'first.yml' ){ |f|	&lt;br /&gt;
    $arr= YAML.load(f)&lt;br /&gt;
}	&lt;br /&gt;
&lt;br /&gt;
p( $arr )&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;]&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The dump function can be used to serialize the data and save it into a file in YAML format. As shown in the above example the data in YAML format can be de-serialized using the load function.&lt;br /&gt;
&lt;br /&gt;
YAML libraries also provides an option of selecting only those variables of the object that are needed to be serialized. This is done using the to_yaml_properties method as shown in the below example.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=====Custom Serialization using YAML=====&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;nowiki&amp;gt;&lt;br /&gt;
    require &amp;quot;yaml&amp;quot;&lt;br /&gt;
     class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
	@name = name&lt;br /&gt;
	@age = age&lt;br /&gt;
	@country=country&lt;br /&gt;
     end&lt;br /&gt;
     def to_s&lt;br /&gt;
	&amp;quot;In First:\n#{@name}, #{@age}, #{@country}\n&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
      def to_yaml_properties&lt;br /&gt;
	   [&amp;quot;@name&amp;quot;,&amp;quot;@age&amp;quot;]  #@country will not be serialized&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
  x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
  puts x&lt;br /&gt;
  puts x.to_yaml&lt;br /&gt;
  &amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   In First:&lt;br /&gt;
   Tom, 25, USA&lt;br /&gt;
   --- !ruby/object:First&lt;br /&gt;
   name: Tom&lt;br /&gt;
   age: 25&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Converting Ruby Objects to JSON format: ====&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
JSON 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. Ruby 1.8.7 distribution is not bundled with json gem. However, in Ruby 1.9.2, the json gem is bundled with the core Ruby distribution.&lt;br /&gt;
The JSON library can be installed using Ruby Gems[http://rubygems.org/] as shown below:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;# gem install json&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A JSON string for serialization can be created by using the JSON.generate method:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
        require 'json'&lt;br /&gt;
        my_hash = {:Welcome =&amp;gt; &amp;quot;Ruby&amp;quot;}&lt;br /&gt;
        puts JSON.generate(my_hash) =&amp;gt; &amp;quot;{\&amp;quot;WELCOME\&amp;quot;:\&amp;quot;RUBY\&amp;quot;}&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
'''Output:'''&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;{&amp;quot;{\&amp;quot;Welcome\&amp;quot;:\&amp;quot;Ruby\&amp;quot;}&amp;quot;=&amp;gt;&amp;quot;{\&amp;quot;WELCOME\&amp;quot;:\&amp;quot;RUBY\&amp;quot;}&amp;quot;}&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A JSON string received from another program can be parsed by using JSON.parse&lt;br /&gt;
Ruby thus converts String to Hash.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
        require 'json'&lt;br /&gt;
        my_hash = JSON.parse('{&amp;quot;Welcome&amp;quot;: &amp;quot;Ruby&amp;quot;}')&lt;br /&gt;
        puts my_hash[&amp;quot;Welcome&amp;quot;] =&amp;gt; &amp;quot;Ruby&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Converting Ruby Objects to Binary Formats ===&lt;br /&gt;
----&lt;br /&gt;
Binary Serialization is another form of serialization in Ruby which is not in human readable form. 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. Binary Serialization  is done using Marshal which is built into Ruby and the code for it is written in Ruby's Marshal module(marshal.c) and thus no additional files are required in order to use it. The _dump and _load methods defined in marshal are used for serialization. Using marshal module the following types of objects can not be serialized: bindings, procedure objects, singleton objects, instances of IO objects and interfaces.&lt;br /&gt;
&lt;br /&gt;
Since the Binary Serialized data is not in human readable form, the following guidelines need to be followed.&lt;br /&gt;
     1.Use print instead of puts[http://ruby-doc.org/core-2.0.0/ARGF.html#] when serialized objects are written to a file in order to avoid new line characters to be written &lt;br /&gt;
       in the file.&lt;br /&gt;
     &lt;br /&gt;
     2.Use a record separator in order to differentiate between two objects.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====='''Binary Serialization Example:'''=====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    class Animal&lt;br /&gt;
      def initialize  name, age&lt;br /&gt;
      @name = name&lt;br /&gt;
      @age=age&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
   class Cat &amp;lt; Animal&lt;br /&gt;
    def to_s&lt;br /&gt;
     &amp;quot;In Cat C: #{@name} \t #{@age}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
  c = Cat.new(&amp;quot;Kitty Kat&amp;quot;,5)&lt;br /&gt;
  puts &amp;quot;Before Serialization&amp;quot;&lt;br /&gt;
  puts c&lt;br /&gt;
  #puts d&lt;br /&gt;
  serialize_cat= Marshal.dump(c) #dumps the serialized cat object into serialize_cat&lt;br /&gt;
  puts &amp;quot;\nAfter Serialization:\n #{serialize_cat}&amp;quot;&lt;br /&gt;
  deserialize_cat= Marshal::load(serialize_cat) #deserializes the cat object and loads it back into deserialize_cat&lt;br /&gt;
  puts &amp;quot;\nAfter Deserialization\n #{deserialize_cat}&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   Before Serialization&lt;br /&gt;
   In Cat C: Kitty Kat 	 5&lt;br /&gt;
   After Serialization:&lt;br /&gt;
   oCat:&lt;br /&gt;
   @nameI&amp;quot;Kitty Kat:ET:	@agei&lt;br /&gt;
   After Deserialization&lt;br /&gt;
   In Cat C: Kitty Kat 	 5&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====='''Saving Marshal data into a file'''=====&lt;br /&gt;
&lt;br /&gt;
Similar to YAML, Marshal can also be used to dump data into a file. The above example showing the serialization using YAML::dump can be written using marshal as shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    f = File.open( 'first.yml', 'w' )&lt;br /&gt;
    Marshal.dump( [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;], f )&lt;br /&gt;
    f.close	&lt;br /&gt;
    File.open( 'first.yml' ){ |f|	&lt;br /&gt;
    $arr= Marshal.load(f)&lt;br /&gt;
    }	&lt;br /&gt;
    p( $arr )&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;]&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice that in the above example there is no &amp;quot;require&amp;quot; statement as opposed to the earlier example of writing serialized data into files using YAML. This is because unlike YAML, marshal is built-in Ruby and  no external libraries is required in order to use its functionality.&lt;br /&gt;
&lt;br /&gt;
====='''Custom serialization using Marshal'''=====&lt;br /&gt;
&lt;br /&gt;
Marshal can also be used to custom serialize an object i.e. it provides an option to omit the variables of an object that are not required in the serialized data. The following program shows the use of marshal_dump method for achieving custom serialization.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
      class First&lt;br /&gt;
      def initialize(name, age, country)&lt;br /&gt;
         @name = name&lt;br /&gt;
         @age = age&lt;br /&gt;
         @country=country&lt;br /&gt;
      end&lt;br /&gt;
      def to_s&lt;br /&gt;
            &amp;quot;In First: #{@name}, #{@age}, #{@country}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
      def marshal_dump&lt;br /&gt;
            [@name,@age]  #@country will not be serialized&lt;br /&gt;
      end&lt;br /&gt;
      def marshal_load(data)&lt;br /&gt;
       @name=data[0]&lt;br /&gt;
       @age=data[1]&lt;br /&gt;
       @country=&amp;quot;United States of America&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
      end&lt;br /&gt;
 &lt;br /&gt;
      x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
      puts x&lt;br /&gt;
  &lt;br /&gt;
      marshal_data = Marshal.dump( x ) &lt;br /&gt;
      y = Marshal.load( marshal_data ) &lt;br /&gt;
      p( y.to_s )&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    In First: Tom, 25, USA&lt;br /&gt;
    &amp;quot;In First: Tom, 25, United States of America&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== '''Serialization Performance in Ruby''' ==&lt;br /&gt;
The different serialization formats described above differ in the efficiency at which they can serialize and deserialize data and thus while serializing large amount of data their efficiency is taken into account. The .report[http://www.ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html] method in Ruby can be used to evaluate the performance of these serialization patterns. Marshal, as it handles data in binary format, is the most efficient form of serialization in Ruby. The following example compares the performance of marshal, JSON and YAML format.[http://www.pauldix.net/2008/08/serializing-dat.html]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   require 'benchmark'&lt;br /&gt;
   require 'rubygems'&lt;br /&gt;
   require 'json'&lt;br /&gt;
   require 'yaml'&lt;br /&gt;
   include Benchmark&lt;br /&gt;
   class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
      @name=name&lt;br /&gt;
      @age=age&lt;br /&gt;
      @country=country&lt;br /&gt;
     end&lt;br /&gt;
   end  &lt;br /&gt;
    x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
    puts &amp;quot;            User     System     Total      Real&amp;quot;&lt;br /&gt;
   benchmark do |t|&lt;br /&gt;
    print &amp;quot;Marshal:&amp;quot;&lt;br /&gt;
    t.report{1000.times do; Marshal.load(Marshal.dump(x));end}&lt;br /&gt;
    print &amp;quot;JSON:   &amp;quot;&lt;br /&gt;
    t.report{1000.times do; JSON.load(JSON.dump(x));end}&lt;br /&gt;
    print &amp;quot;YAML:   &amp;quot;&lt;br /&gt;
    t.report{1000.times do; YAML.load(YAML.dump(x));end}&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
              User     System     Total      Real&lt;br /&gt;
  Marshal:   0.010000   0.000000   0.010000 (  0.015408)&lt;br /&gt;
  JSON:      0.020000   0.010000   0.030000 (  0.022462)&lt;br /&gt;
  YAML:      0.460000   0.010000   0.470000 (  0.472149)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output of the above example shows that, given large chunks of data, marshal is comparatively more efficient format for serialization in Ruby.&lt;br /&gt;
&lt;br /&gt;
== Serialization in other Object-Oriented Languages ==&lt;br /&gt;
&lt;br /&gt;
===Comparison===&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Sl.No !! Ruby !! Java !! .Net Framework !! C++&lt;br /&gt;
|-&lt;br /&gt;
| 1 || Ruby provides a built in module called 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 || Although, there is no built in support for serialization in C++, it can be achieved by using Boost libraries[http://www.boost.org/doc/libs/1_36_0/libs/serialization/example/demo.cpp]&lt;br /&gt;
|-&lt;br /&gt;
| 2 || The built in module of Ruby (Marshal) does not support platform independence, however, it can be achieved by using external libraries like YAML and JSON || Similarly, Java's built in serialization is also not platform independent and in order to use serialization in Java across Ruby platform, jruby library should be used.  || .Net used Remoting technology to make it platform independent. || Serialization using the Boost libraries is not platform independent.&lt;br /&gt;
|-&lt;br /&gt;
| 3 || YAML provides a method [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/syck/rdoc/Object.html (to_yaml_properties)] which can be used to select the variables who's value is need to be serialized. With Marshal, we need to write a method named marshal_dump defining the variables of an object that has to be serialized. || Provides an option for serializing only the required 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 data 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 || Serialization using the Boost libraries is custom and thus the user can specify the part of the objects to be serialized.&lt;br /&gt;
|-&lt;br /&gt;
| 4 || Bindings, procedure objects, singleton objects, instances of IO objects and interfaces can not be serialized. Serializing these objects throws TypeError exceptions. || Thread, OutputStream and its subclasses, and Socket are not serializable in Java.[http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html] || Objects like DataRow are non serializable in .NET. Whether an object is serializable or not can be checked using the [http://msdn.microsoft.com/en-us/library/system.type.isserializable(v=vs.110).aspx?cs-save-lang=1&amp;amp;cs-lang=cpp#code-snippet-1 Type.IsSerializable] || There are no such objects in C++.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===Serialization in Java===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;  &lt;br /&gt;
  import java.io.FileInputStream;&lt;br /&gt;
  import java.io.FileOutputStream;&lt;br /&gt;
  import java.io.IOException;&lt;br /&gt;
  import java.io.ObjectInputStream;&lt;br /&gt;
  import java.io.ObjectOutputStream;&lt;br /&gt;
  import java.io.Serializable;&lt;br /&gt;
           &lt;br /&gt;
 public class First implements Serializable {&lt;br /&gt;
 &lt;br /&gt;
 private static final long serialVersionUID = 1L;&lt;br /&gt;
 public String name;&lt;br /&gt;
 public int age;&lt;br /&gt;
 public String country;&lt;br /&gt;
 &lt;br /&gt;
 private static void serialize(First f, String path) throws IOException {&lt;br /&gt;
  &lt;br /&gt;
       FileOutputStream fos = new FileOutputStream(path);&lt;br /&gt;
       ObjectOutputStream oos = new ObjectOutputStream(fos);&lt;br /&gt;
    &lt;br /&gt;
       // Serializing&lt;br /&gt;
       oos.writeObject(f);&lt;br /&gt;
       oos.close();&lt;br /&gt;
       fos.close();&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
 private static First deserialize(String path) throws IOException,&lt;br /&gt;
  		ClassNotFoundException {&lt;br /&gt;
  	// tom.ser will deserialize in object f&lt;br /&gt;
  	First f = null;&lt;br /&gt;
  	FileInputStream fin = new FileInputStream(path);&lt;br /&gt;
  	ObjectInputStream oin = new ObjectInputStream(fin);&lt;br /&gt;
  	// DeSerializing and Casting to Class First&lt;br /&gt;
  	f = (First) oin.readObject();&lt;br /&gt;
  	oin.close();&lt;br /&gt;
  	fin.close();&lt;br /&gt;
  	return f;&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  public static void main(String[] args) {&lt;br /&gt;
  	try {&lt;br /&gt;
  		// Creating object for Tom&lt;br /&gt;
  		First f1 = new First();&lt;br /&gt;
  		f1.name = &amp;quot;Tom&amp;quot;;&lt;br /&gt;
  		f1.age = 25;&lt;br /&gt;
  		f1.country = &amp;quot;USA&amp;quot;;&lt;br /&gt;
  		First f2 = null;&lt;br /&gt;
  		long start = System.currentTimeMillis();&lt;br /&gt;
  		for (int i = 0; i &amp;lt; 1000; i++){&lt;br /&gt;
  		// serializing&lt;br /&gt;
  		serialize(f1, &amp;quot;/path/to/files/&amp;quot; + i + &amp;quot;.ser&amp;quot;);&lt;br /&gt;
  		// deserializing&lt;br /&gt;
  		f2 = deserialize(&amp;quot;/path/to/files&amp;quot; + i + &amp;quot;.ser&amp;quot;);&lt;br /&gt;
  		}&lt;br /&gt;
  		long end = System.currentTimeMillis();&lt;br /&gt;
  		long totalDuration = end - start;&lt;br /&gt;
  		System.out.println(&amp;quot;Serialization/Deserialization for 1000 Iterations duration: &amp;quot; + totalDuration + &amp;quot; Milliseconds&amp;quot;);&lt;br /&gt;
  		// Printing deserialized object&lt;br /&gt;
  		System.out.println(&amp;quot;Deserialized =&amp;gt; Name: &amp;quot; + f2.name + &amp;quot;, Age: &amp;quot; + f2.age + &amp;quot;, Country: &amp;quot; + f2.country);&lt;br /&gt;
  	} catch (IOException ex) {&lt;br /&gt;
  		e.printStackTrace();&lt;br /&gt;
  	} catch (ClassNotFoundException ex) {&lt;br /&gt;
  		e.printStackTrace();&lt;br /&gt;
  	}&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 Serialization/Deserialization for 1000 Iterations duration: 1651335109 Nano Seconds&lt;br /&gt;
 Deserialized =&amp;gt; Name: Tom, Age: 25, Country: USA&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== See Also==&lt;br /&gt;
&lt;br /&gt;
1. [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/json/rdoc/JSON.html Serialization in Ruby JSON]&lt;br /&gt;
&lt;br /&gt;
2. [http://api.rubyonrails.org/classes/ActiveModel/Serialization.html Serialization in Rails]&lt;br /&gt;
&lt;br /&gt;
3. [http://gregmoreno.wordpress.com/2011/01/27/preventing-model-explosion-via-rails-serialization/ Article on Rails Serialization]&lt;br /&gt;
&lt;br /&gt;
4. [http://en.wikipedia.org/wiki/Google_Protocol_Buffers Protocol Buffers]&lt;br /&gt;
&lt;br /&gt;
5. [https://rubygems.org/gems/ruby-protocol-buffers Ruby Protocol Buffers Gem]&lt;br /&gt;
&lt;br /&gt;
6. [http://en.wikipedia.org/wiki/Apache_Avro Avro]&lt;br /&gt;
&lt;br /&gt;
7. [http://rubygems.org/gems/avro Ruby Avro Gem]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
1. [http://en.wikipedia.org/wiki/Serialization Serilization in General]&lt;br /&gt;
&lt;br /&gt;
2. [http://en.wikipedia.org/wiki/Marshalling_%28computer_science%29 Marshaling]&lt;br /&gt;
&lt;br /&gt;
3. [http://techbrahmana.blogspot.com/2012/03/rails-cookie-handling-serialization-and.html Rails Cookie Handling]&lt;br /&gt;
&lt;br /&gt;
4. [http://yaml.org/ YAML]&lt;br /&gt;
&lt;br /&gt;
5. [http://www.ruby-doc.org/core-2.1.0/Marshal.html Marshal module]&lt;br /&gt;
&lt;br /&gt;
6. [http://json.org/ JSON]&lt;br /&gt;
&lt;br /&gt;
7. [http://rubygems.org/ Ruby Gems]&lt;br /&gt;
&lt;br /&gt;
8. [http://ruby-doc.org/core-2.0.0/ARGF.html# Ruby ARGF Documentation]&lt;br /&gt;
&lt;br /&gt;
9. [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html Ruby Documentation for Benchmark Gem]&lt;br /&gt;
&lt;br /&gt;
10. [http://www.pauldix.net/2008/08/serializing-dat.html Serialization Performance Comparison]&lt;br /&gt;
&lt;br /&gt;
11. [http://www.boost.org/doc/libs/1_36_0/libs/serialization/example/demo.cpp Boost Libraries]&lt;br /&gt;
&lt;br /&gt;
12. [http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html Java Serialization]&lt;br /&gt;
&lt;br /&gt;
13. [http://www.w3schools.com/json/ JSON Tutorial]&lt;br /&gt;
&lt;br /&gt;
14. [http://www.skorks.com/2010/04/serializing-and-deserializing-objects-with-ruby/ Serializing and De-serializing in Ruby]&lt;br /&gt;
&lt;br /&gt;
15. [http://www.codeproject.com/Articles/33296/Serialization-and-De-serialization Serialization and De-serialization]&lt;br /&gt;
&lt;br /&gt;
16. [http://msdn.microsoft.com/en-us/library/182eeyhh.aspx XML Serialization]&lt;br /&gt;
&lt;br /&gt;
17. [http://www.tutorialspoint.com/java/java_serialization.htm Java Serialization Tutorial]&lt;br /&gt;
&lt;br /&gt;
18. [http://www.sapphiresteel.com/ruby-programming/The-Book-Of-Ruby The Book of Ruby]&lt;br /&gt;
&lt;br /&gt;
19. [http://www.codeproject.com/Articles/20962/Introducing-Serialization-in-NET Serialization in .Net]&lt;br /&gt;
&lt;br /&gt;
20. [http://ruby.about.com/od/advancedruby/ss/Serialization-In-Ruby-Yaml.html Serialization in Ruby YAML]&lt;br /&gt;
&lt;br /&gt;
21. [http://ruby.about.com/od/tasks/a/The-Json-Gem.html Installing JSON gem]&lt;br /&gt;
&lt;br /&gt;
22. [http://www.ruby-doc.org/stdlib-2.0.0/libdoc/json/rdoc/JSON.html Serialization in Ruby JSON]&lt;/div&gt;</summary>
		<author><name>Mmehta2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1d_mm&amp;diff=83427</id>
		<title>CSC/ECE 517 Spring 2014/ch1a 1d mm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1d_mm&amp;diff=83427"/>
		<updated>2014-02-18T03:14:28Z</updated>

		<summary type="html">&lt;p&gt;Mmehta2: /* Serialization in Java */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page discusses Serialization, focusing on Ruby programming language. It describes different methods of Serialization in Ruby. In addition, it also compares serialization in different Object Oriented languages.&lt;br /&gt;
&lt;br /&gt;
== '''Introduction''' ==&lt;br /&gt;
&lt;br /&gt;
==='''Serialization'''===&lt;br /&gt;
&lt;br /&gt;
'''Serialization'''[http://en.wikipedia.org/wiki/Serialization] is a process of converting a data structure or an object into a stream of bytes or string to facilitate storage in memory, file (persistence storage) or transmission over a network. The process of Serialization is also referred to as ''Marshalling''[http://en.wikipedia.org/wiki/Marshalling_%28computer_science%29]. 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.&lt;br /&gt;
&lt;br /&gt;
==='''De-serialization'''===&lt;br /&gt;
&lt;br /&gt;
'''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''.&lt;br /&gt;
&lt;br /&gt;
The process of Serialization/De-serialization is not a replacement for database systems. Database Systems are used for their relational and transactional semantics.&lt;br /&gt;
&lt;br /&gt;
==='''Basic Advantages of Serialization'''===&lt;br /&gt;
&lt;br /&gt;
1. Communication between two or more processes on same machine. Object state can be saved and shared in a persistent or in-memory store.&lt;br /&gt;
&lt;br /&gt;
2. Communication between processes on different machines. Serialization facilitates the transmission of an object through a network.&lt;br /&gt;
&lt;br /&gt;
3. Creating a clone of an object. &lt;br /&gt;
&lt;br /&gt;
4. Cross-platform compatibility. Object can be serialized in a common format that is understood by multiple platforms. For example, JSON[http://www.json.org/] and XML.&lt;br /&gt;
&lt;br /&gt;
==='''Practical Applications for Serialization'''===&lt;br /&gt;
&lt;br /&gt;
1. [http://docs.oracle.com/cd/E13222_01/wls/docs81/webapp/sessions.html#139726 HTTP Session] Replication by sharing session objects across web servers for load balancing and handling failover scenarios.&lt;br /&gt;
&lt;br /&gt;
2. Serialization facilitates communication in [http://en.wikipedia.org/wiki/Java_remote_method_invocation Remote Method Invocation] or [http://en.wikipedia.org/wiki/Remote_procedure_call Remote procedure calls]&lt;br /&gt;
&lt;br /&gt;
3. Rails Cookie Handling[http://techbrahmana.blogspot.com/2012/03/rails-cookie-handling-serialization-and.html]. Cookies are stored marshalled/unmarshalled to and from client machines.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== '''Serialization in Ruby:''' ==&lt;br /&gt;
&lt;br /&gt;
Ruby provides serialization capabilities through its module, [http://www.ruby-doc.org/core-2.0.0/Marshal.html Marshal]. There are also other libraries like YAML[http://yaml.org/] and JSON which can be used in Ruby to generate serialized objects for purposes like platform independence and human readable formats.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:SerializationChart.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== '''Types of Serialization''' ==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
=== Converting Ruby Objects in Human Readable Formats ===&lt;br /&gt;
----&lt;br /&gt;
The conversion of Ruby objects into YAML and JSON formats are explained below.&lt;br /&gt;
&lt;br /&gt;
==== Converting Ruby Objects to YAML format ====&lt;br /&gt;
&lt;br /&gt;
YAML 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 binary format. 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. In order to use it in Ruby, the yaml.rb file is required to be loaded which provides methods for converting objects into yaml format and creating .yml files.&lt;br /&gt;
&lt;br /&gt;
=====Serialization using YAML=====&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;nowiki&amp;gt; #Serialization using YAML's to_yaml method&lt;br /&gt;
 &lt;br /&gt;
  require &amp;quot;yaml&amp;quot;&lt;br /&gt;
     class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
	@name = name&lt;br /&gt;
	@age = age&lt;br /&gt;
	@country=country&lt;br /&gt;
     end&lt;br /&gt;
      def to_s&lt;br /&gt;
	&amp;quot;In First:\n#{@name}, #{@age}, #{@country}\n&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
  x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
  puts x&lt;br /&gt;
  puts x.to_yaml&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   In First:&lt;br /&gt;
   Tom, 25, USA&lt;br /&gt;
   --- !ruby/object:First&lt;br /&gt;
   name: Tom&lt;br /&gt;
   age: 25&lt;br /&gt;
   country: USA&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The above code displays the object x, first as a string and then in the yaml format.&lt;br /&gt;
&lt;br /&gt;
====='''Saving YAML data into a file:'''=====&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt; # Serialization using YAML::dump&lt;br /&gt;
&lt;br /&gt;
require 'yaml'&lt;br /&gt;
&lt;br /&gt;
f = File.open( 'first.yml', 'w' )&lt;br /&gt;
YAML.dump( [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;], f )&lt;br /&gt;
f.close&lt;br /&gt;
		&lt;br /&gt;
File.open( 'first.yml' ){ |f|	&lt;br /&gt;
    $arr= YAML.load(f)&lt;br /&gt;
}	&lt;br /&gt;
&lt;br /&gt;
p( $arr )&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;]&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The dump function can be used to serialize the data and save it into a file in YAML format. As shown in the above example the data in YAML format can be de-serialized using the load function.&lt;br /&gt;
&lt;br /&gt;
YAML libraries also provides an option of selecting only those variables of the object that are needed to be serialized. This is done using the to_yaml_properties method as shown in the below example.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=====Custom Serialization using YAML=====&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;nowiki&amp;gt;&lt;br /&gt;
    require &amp;quot;yaml&amp;quot;&lt;br /&gt;
     class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
	@name = name&lt;br /&gt;
	@age = age&lt;br /&gt;
	@country=country&lt;br /&gt;
     end&lt;br /&gt;
     def to_s&lt;br /&gt;
	&amp;quot;In First:\n#{@name}, #{@age}, #{@country}\n&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
      def to_yaml_properties&lt;br /&gt;
	   [&amp;quot;@name&amp;quot;,&amp;quot;@age&amp;quot;]  #@country will not be serialized&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
  x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
  puts x&lt;br /&gt;
  puts x.to_yaml&lt;br /&gt;
  &amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   In First:&lt;br /&gt;
   Tom, 25, USA&lt;br /&gt;
   --- !ruby/object:First&lt;br /&gt;
   name: Tom&lt;br /&gt;
   age: 25&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Converting Ruby Objects to JSON format: ====&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
JSON 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. Ruby 1.8.7 distribution is not bundled with json gem. However, in Ruby 1.9.2, the json gem is bundled with the core Ruby distribution.&lt;br /&gt;
The JSON library can be installed using Ruby Gems[http://rubygems.org/] as shown below:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;# gem install json&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A JSON string for serialization can be created by using the JSON.generate method:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
        require 'json'&lt;br /&gt;
        my_hash = {:Welcome =&amp;gt; &amp;quot;Ruby&amp;quot;}&lt;br /&gt;
        puts JSON.generate(my_hash) =&amp;gt; &amp;quot;{\&amp;quot;WELCOME\&amp;quot;:\&amp;quot;RUBY\&amp;quot;}&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
'''Output:'''&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;{&amp;quot;{\&amp;quot;Welcome\&amp;quot;:\&amp;quot;Ruby\&amp;quot;}&amp;quot;=&amp;gt;&amp;quot;{\&amp;quot;WELCOME\&amp;quot;:\&amp;quot;RUBY\&amp;quot;}&amp;quot;}&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A JSON string received from another program can be parsed by using JSON.parse&lt;br /&gt;
Ruby thus converts String to Hash.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
        require 'json'&lt;br /&gt;
        my_hash = JSON.parse('{&amp;quot;Welcome&amp;quot;: &amp;quot;Ruby&amp;quot;}')&lt;br /&gt;
        puts my_hash[&amp;quot;Welcome&amp;quot;] =&amp;gt; &amp;quot;Ruby&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Converting Ruby Objects to Binary Formats ===&lt;br /&gt;
----&lt;br /&gt;
Binary Serialization is another form of serialization in Ruby which is not in human readable form. 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. Binary Serialization  is done using Marshal which is built into Ruby and the code for it is written in Ruby's Marshal module(marshal.c) and thus no additional files are required in order to use it. The _dump and _load methods defined in marshal are used for serialization. Using marshal module the following types of objects can not be serialized: bindings, procedure objects, singleton objects, instances of IO objects and interfaces.&lt;br /&gt;
&lt;br /&gt;
Since the Binary Serialized data is not in human readable form, the following guidelines need to be followed.&lt;br /&gt;
     1.Use print instead of puts[http://ruby-doc.org/core-2.0.0/ARGF.html#] when serialized objects are written to a file in order to avoid new line characters to be written &lt;br /&gt;
       in the file.&lt;br /&gt;
     &lt;br /&gt;
     2.Use a record separator in order to differentiate between two objects.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====='''Binary Serialization Example:'''=====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    class Animal&lt;br /&gt;
      def initialize  name, age&lt;br /&gt;
      @name = name&lt;br /&gt;
      @age=age&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
   class Cat &amp;lt; Animal&lt;br /&gt;
    def to_s&lt;br /&gt;
     &amp;quot;In Cat C: #{@name} \t #{@age}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
  c = Cat.new(&amp;quot;Kitty Kat&amp;quot;,5)&lt;br /&gt;
  puts &amp;quot;Before Serialization&amp;quot;&lt;br /&gt;
  puts c&lt;br /&gt;
  #puts d&lt;br /&gt;
  serialize_cat= Marshal.dump(c) #dumps the serialized cat object into serialize_cat&lt;br /&gt;
  puts &amp;quot;\nAfter Serialization:\n #{serialize_cat}&amp;quot;&lt;br /&gt;
  deserialize_cat= Marshal::load(serialize_cat) #deserializes the cat object and loads it back into deserialize_cat&lt;br /&gt;
  puts &amp;quot;\nAfter Deserialization\n #{deserialize_cat}&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   Before Serialization&lt;br /&gt;
   In Cat C: Kitty Kat 	 5&lt;br /&gt;
   After Serialization:&lt;br /&gt;
   oCat:&lt;br /&gt;
   @nameI&amp;quot;Kitty Kat:ET:	@agei&lt;br /&gt;
   After Deserialization&lt;br /&gt;
   In Cat C: Kitty Kat 	 5&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====='''Saving Marshal data into a file'''=====&lt;br /&gt;
&lt;br /&gt;
Similar to YAML, Marshal can also be used to dump data into a file. The above example showing the serialization using YAML::dump can be written using marshal as shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    f = File.open( 'first.yml', 'w' )&lt;br /&gt;
    Marshal.dump( [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;], f )&lt;br /&gt;
    f.close	&lt;br /&gt;
    File.open( 'first.yml' ){ |f|	&lt;br /&gt;
    $arr= Marshal.load(f)&lt;br /&gt;
    }	&lt;br /&gt;
    p( $arr )&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;]&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice that in the above example there is no &amp;quot;require&amp;quot; statement as opposed to the earlier example of writing serialized data into files using YAML. This is because unlike YAML, marshal is built-in Ruby and  no external libraries is required in order to use its functionality.&lt;br /&gt;
&lt;br /&gt;
====='''Custom serialization using Marshal'''=====&lt;br /&gt;
&lt;br /&gt;
Marshal can also be used to custom serialize an object i.e. it provides an option to omit the variables of an object that are not required in the serialized data. The following program shows the use of marshal_dump method for achieving custom serialization.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
      class First&lt;br /&gt;
      def initialize(name, age, country)&lt;br /&gt;
         @name = name&lt;br /&gt;
         @age = age&lt;br /&gt;
         @country=country&lt;br /&gt;
      end&lt;br /&gt;
      def to_s&lt;br /&gt;
            &amp;quot;In First: #{@name}, #{@age}, #{@country}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
      def marshal_dump&lt;br /&gt;
            [@name,@age]  #@country will not be serialized&lt;br /&gt;
      end&lt;br /&gt;
      def marshal_load(data)&lt;br /&gt;
       @name=data[0]&lt;br /&gt;
       @age=data[1]&lt;br /&gt;
       @country=&amp;quot;United States of America&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
      end&lt;br /&gt;
 &lt;br /&gt;
      x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
      puts x&lt;br /&gt;
  &lt;br /&gt;
      marshal_data = Marshal.dump( x ) &lt;br /&gt;
      y = Marshal.load( marshal_data ) &lt;br /&gt;
      p( y.to_s )&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    In First: Tom, 25, USA&lt;br /&gt;
    &amp;quot;In First: Tom, 25, United States of America&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== '''Serialization Performance in Ruby''' ==&lt;br /&gt;
The different serialization formats described above differ in the efficiency at which they can serialize and deserialize data and thus while serializing large amount of data their efficiency is taken into account. The .report[http://www.ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html] method in Ruby can be used to evaluate the performance of these serialization patterns. Marshal, as it handles data in binary format, is the most efficient form of serialization in Ruby. The following example compares the performance of marshal, JSON and YAML format.[http://www.pauldix.net/2008/08/serializing-dat.html]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   require 'benchmark'&lt;br /&gt;
   require 'rubygems'&lt;br /&gt;
   require 'json'&lt;br /&gt;
   require 'yaml'&lt;br /&gt;
   include Benchmark&lt;br /&gt;
   class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
      @name=name&lt;br /&gt;
      @age=age&lt;br /&gt;
      @country=country&lt;br /&gt;
     end&lt;br /&gt;
   end  &lt;br /&gt;
    x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
    puts &amp;quot;            User     System     Total      Real&amp;quot;&lt;br /&gt;
   benchmark do |t|&lt;br /&gt;
    print &amp;quot;Marshal:&amp;quot;&lt;br /&gt;
    t.report{1000.times do; Marshal.load(Marshal.dump(x));end}&lt;br /&gt;
    print &amp;quot;JSON:   &amp;quot;&lt;br /&gt;
    t.report{1000.times do; JSON.load(JSON.dump(x));end}&lt;br /&gt;
    print &amp;quot;YAML:   &amp;quot;&lt;br /&gt;
    t.report{1000.times do; YAML.load(YAML.dump(x));end}&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
              User     System     Total      Real&lt;br /&gt;
  Marshal:   0.010000   0.000000   0.010000 (  0.015408)&lt;br /&gt;
  JSON:      0.020000   0.010000   0.030000 (  0.022462)&lt;br /&gt;
  YAML:      0.460000   0.010000   0.470000 (  0.472149)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output of the above example shows that, given large chunks of data, marshal is comparatively more efficient format for serialization in Ruby.&lt;br /&gt;
&lt;br /&gt;
== Serialization in other Object-Oriented Languages ==&lt;br /&gt;
&lt;br /&gt;
===Comparison===&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Sl.No !! Ruby !! Java !! .Net Framework !! C++&lt;br /&gt;
|-&lt;br /&gt;
| 1 || Ruby provides a built in module called 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 || Although, there is no built in support for serialization in C++, it can be achieved by using Boost libraries[http://www.boost.org/doc/libs/1_36_0/libs/serialization/example/demo.cpp]&lt;br /&gt;
|-&lt;br /&gt;
| 2 || The built in module of Ruby (Marshal) does not support platform independence, however, it can be achieved by using external libraries like YAML and JSON || Similarly, Java's built in serialization is also not platform independent and in order to use serialization in Java across Ruby platform, jruby library should be used.  || .Net used Remoting technology to make it platform independent. || Serialization using the Boost libraries is not platform independent.&lt;br /&gt;
|-&lt;br /&gt;
| 3 || YAML provides a method [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/syck/rdoc/Object.html (to_yaml_properties)] which can be used to select the variables who's value is need to be serialized. With Marshal, we need to write a method named marshal_dump defining the variables of an object that has to be serialized. || Provides an option for serializing only the required 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 data 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 || Serialization using the Boost libraries is custom and thus the user can specify the part of the objects to be serialized.&lt;br /&gt;
|-&lt;br /&gt;
| 4 || Bindings, procedure objects, singleton objects, instances of IO objects and interfaces can not be serialized. Serializing these objects throws TypeError exceptions. || Thread, OutputStream and its subclasses, and Socket are not serializable in Java.[http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html] || Objects like DataRow are non serializable in .NET. Whether an object is serializable or not can be checked using the [http://msdn.microsoft.com/en-us/library/system.type.isserializable(v=vs.110).aspx?cs-save-lang=1&amp;amp;cs-lang=cpp#code-snippet-1 Type.IsSerializable] || There are no such objects in C++.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===Serialization in Java===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;           &lt;br /&gt;
 public class First implements Serializable {&lt;br /&gt;
 &lt;br /&gt;
 private static final long serialVersionUID = 1L;&lt;br /&gt;
 public String name;&lt;br /&gt;
 public int age;&lt;br /&gt;
 public String country;&lt;br /&gt;
 &lt;br /&gt;
 private static void serialize(First f, String path) throws IOException {&lt;br /&gt;
  &lt;br /&gt;
       FileOutputStream fos = new FileOutputStream(path);&lt;br /&gt;
       ObjectOutputStream oos = new ObjectOutputStream(fos);&lt;br /&gt;
    &lt;br /&gt;
       // Serializing&lt;br /&gt;
       oos.writeObject(f);&lt;br /&gt;
       oos.close();&lt;br /&gt;
       fos.close();&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
 private static First deserialize(String path) throws IOException,&lt;br /&gt;
  		ClassNotFoundException {&lt;br /&gt;
  	// tom.ser will deserialize in object f&lt;br /&gt;
  	First f = null;&lt;br /&gt;
  	FileInputStream fin = new FileInputStream(path);&lt;br /&gt;
  	ObjectInputStream oin = new ObjectInputStream(fin);&lt;br /&gt;
  	// DeSerializing and Casting to Class First&lt;br /&gt;
  	f = (First) oin.readObject();&lt;br /&gt;
  	oin.close();&lt;br /&gt;
  	fin.close();&lt;br /&gt;
  	return f;&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  public static void main(String[] args) {&lt;br /&gt;
  	try {&lt;br /&gt;
  		// Creating object for Tom&lt;br /&gt;
  		First f1 = new First();&lt;br /&gt;
  		f1.name = &amp;quot;Tom&amp;quot;;&lt;br /&gt;
  		f1.age = 25;&lt;br /&gt;
  		f1.country = &amp;quot;USA&amp;quot;;&lt;br /&gt;
  		First f2 = null;&lt;br /&gt;
  		long start = System.currentTimeMillis();&lt;br /&gt;
  		for (int i = 0; i &amp;lt; 1000; i++){&lt;br /&gt;
  		// serializing&lt;br /&gt;
  		serialize(f1, &amp;quot;/path/to/files/&amp;quot; + i + &amp;quot;.ser&amp;quot;);&lt;br /&gt;
  		// deserializing&lt;br /&gt;
  		f2 = deserialize(&amp;quot;/path/to/files&amp;quot; + i + &amp;quot;.ser&amp;quot;);&lt;br /&gt;
  		}&lt;br /&gt;
  		long end = System.currentTimeMillis();&lt;br /&gt;
  		long totalDuration = end - start;&lt;br /&gt;
  		System.out.println(&amp;quot;Serialization/Deserialization for 1000 Iterations duration: &amp;quot; + totalDuration + &amp;quot; Milliseconds&amp;quot;);&lt;br /&gt;
  		// Printing deserialized object&lt;br /&gt;
  		System.out.println(&amp;quot;Deserialized =&amp;gt; Name: &amp;quot; + f2.name + &amp;quot;, Age: &amp;quot; + f2.age + &amp;quot;, Country: &amp;quot; + f2.country);&lt;br /&gt;
  	} catch (IOException ex) {&lt;br /&gt;
  		e.printStackTrace();&lt;br /&gt;
  	} catch (ClassNotFoundException ex) {&lt;br /&gt;
  		e.printStackTrace();&lt;br /&gt;
  	}&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 Serialization/Deserialization for 1000 Iterations duration: 1651335109 Nano Seconds&lt;br /&gt;
 Deserialized =&amp;gt; Name: Tom, Age: 25, Country: USA&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== See Also==&lt;br /&gt;
&lt;br /&gt;
1. [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/json/rdoc/JSON.html Serialization in Ruby JSON]&lt;br /&gt;
&lt;br /&gt;
2. [http://api.rubyonrails.org/classes/ActiveModel/Serialization.html Serialization in Rails]&lt;br /&gt;
&lt;br /&gt;
3. [http://gregmoreno.wordpress.com/2011/01/27/preventing-model-explosion-via-rails-serialization/ Article on Rails Serialization]&lt;br /&gt;
&lt;br /&gt;
4. [http://en.wikipedia.org/wiki/Google_Protocol_Buffers Protocol Buffers]&lt;br /&gt;
&lt;br /&gt;
5. [https://rubygems.org/gems/ruby-protocol-buffers Ruby Protocol Buffers Gem]&lt;br /&gt;
&lt;br /&gt;
6. [http://en.wikipedia.org/wiki/Apache_Avro Avro]&lt;br /&gt;
&lt;br /&gt;
7. [http://rubygems.org/gems/avro Ruby Avro Gem]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
1. [http://en.wikipedia.org/wiki/Serialization Serilization in General]&lt;br /&gt;
&lt;br /&gt;
2. [http://en.wikipedia.org/wiki/Marshalling_%28computer_science%29 Marshaling]&lt;br /&gt;
&lt;br /&gt;
3. [http://techbrahmana.blogspot.com/2012/03/rails-cookie-handling-serialization-and.html Rails Cookie Handling]&lt;br /&gt;
&lt;br /&gt;
4. [http://yaml.org/ YAML]&lt;br /&gt;
&lt;br /&gt;
5. [http://www.ruby-doc.org/core-2.1.0/Marshal.html Marshal module]&lt;br /&gt;
&lt;br /&gt;
6. [http://json.org/ JSON]&lt;br /&gt;
&lt;br /&gt;
7. [http://rubygems.org/ Ruby Gems]&lt;br /&gt;
&lt;br /&gt;
8. [http://ruby-doc.org/core-2.0.0/ARGF.html# Ruby ARGF Documentation]&lt;br /&gt;
&lt;br /&gt;
9. [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html Ruby Documentation for Benchmark Gem]&lt;br /&gt;
&lt;br /&gt;
10. [http://www.pauldix.net/2008/08/serializing-dat.html Serialization Performance Comparison]&lt;br /&gt;
&lt;br /&gt;
11. [http://www.boost.org/doc/libs/1_36_0/libs/serialization/example/demo.cpp Boost Libraries]&lt;br /&gt;
&lt;br /&gt;
12. [http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html Java Serialization]&lt;br /&gt;
&lt;br /&gt;
13. [http://www.w3schools.com/json/ JSON Tutorial]&lt;br /&gt;
&lt;br /&gt;
14. [http://www.skorks.com/2010/04/serializing-and-deserializing-objects-with-ruby/ Serializing and De-serializing in Ruby]&lt;br /&gt;
&lt;br /&gt;
15. [http://www.codeproject.com/Articles/33296/Serialization-and-De-serialization Serialization and De-serialization]&lt;br /&gt;
&lt;br /&gt;
16. [http://msdn.microsoft.com/en-us/library/182eeyhh.aspx XML Serialization]&lt;br /&gt;
&lt;br /&gt;
17. [http://www.tutorialspoint.com/java/java_serialization.htm Java Serialization Tutorial]&lt;br /&gt;
&lt;br /&gt;
18. [http://www.sapphiresteel.com/ruby-programming/The-Book-Of-Ruby The Book of Ruby]&lt;br /&gt;
&lt;br /&gt;
19. [http://www.codeproject.com/Articles/20962/Introducing-Serialization-in-NET Serialization in .Net]&lt;br /&gt;
&lt;br /&gt;
20. [http://ruby.about.com/od/advancedruby/ss/Serialization-In-Ruby-Yaml.html Serialization in Ruby YAML]&lt;br /&gt;
&lt;br /&gt;
21. [http://ruby.about.com/od/tasks/a/The-Json-Gem.html Installing JSON gem]&lt;br /&gt;
&lt;br /&gt;
22. [http://www.ruby-doc.org/stdlib-2.0.0/libdoc/json/rdoc/JSON.html Serialization in Ruby JSON]&lt;/div&gt;</summary>
		<author><name>Mmehta2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1d_mm&amp;diff=83426</id>
		<title>CSC/ECE 517 Spring 2014/ch1a 1d mm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1d_mm&amp;diff=83426"/>
		<updated>2014-02-18T03:13:27Z</updated>

		<summary type="html">&lt;p&gt;Mmehta2: /* Serialization in Java */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page discusses Serialization, focusing on Ruby programming language. It describes different methods of Serialization in Ruby. In addition, it also compares serialization in different Object Oriented languages.&lt;br /&gt;
&lt;br /&gt;
== '''Introduction''' ==&lt;br /&gt;
&lt;br /&gt;
==='''Serialization'''===&lt;br /&gt;
&lt;br /&gt;
'''Serialization'''[http://en.wikipedia.org/wiki/Serialization] is a process of converting a data structure or an object into a stream of bytes or string to facilitate storage in memory, file (persistence storage) or transmission over a network. The process of Serialization is also referred to as ''Marshalling''[http://en.wikipedia.org/wiki/Marshalling_%28computer_science%29]. 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.&lt;br /&gt;
&lt;br /&gt;
==='''De-serialization'''===&lt;br /&gt;
&lt;br /&gt;
'''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''.&lt;br /&gt;
&lt;br /&gt;
The process of Serialization/De-serialization is not a replacement for database systems. Database Systems are used for their relational and transactional semantics.&lt;br /&gt;
&lt;br /&gt;
==='''Basic Advantages of Serialization'''===&lt;br /&gt;
&lt;br /&gt;
1. Communication between two or more processes on same machine. Object state can be saved and shared in a persistent or in-memory store.&lt;br /&gt;
&lt;br /&gt;
2. Communication between processes on different machines. Serialization facilitates the transmission of an object through a network.&lt;br /&gt;
&lt;br /&gt;
3. Creating a clone of an object. &lt;br /&gt;
&lt;br /&gt;
4. Cross-platform compatibility. Object can be serialized in a common format that is understood by multiple platforms. For example, JSON[http://www.json.org/] and XML.&lt;br /&gt;
&lt;br /&gt;
==='''Practical Applications for Serialization'''===&lt;br /&gt;
&lt;br /&gt;
1. [http://docs.oracle.com/cd/E13222_01/wls/docs81/webapp/sessions.html#139726 HTTP Session] Replication by sharing session objects across web servers for load balancing and handling failover scenarios.&lt;br /&gt;
&lt;br /&gt;
2. Serialization facilitates communication in [http://en.wikipedia.org/wiki/Java_remote_method_invocation Remote Method Invocation] or [http://en.wikipedia.org/wiki/Remote_procedure_call Remote procedure calls]&lt;br /&gt;
&lt;br /&gt;
3. Rails Cookie Handling[http://techbrahmana.blogspot.com/2012/03/rails-cookie-handling-serialization-and.html]. Cookies are stored marshalled/unmarshalled to and from client machines.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== '''Serialization in Ruby:''' ==&lt;br /&gt;
&lt;br /&gt;
Ruby provides serialization capabilities through its module, [http://www.ruby-doc.org/core-2.0.0/Marshal.html Marshal]. There are also other libraries like YAML[http://yaml.org/] and JSON which can be used in Ruby to generate serialized objects for purposes like platform independence and human readable formats.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:SerializationChart.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== '''Types of Serialization''' ==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
=== Converting Ruby Objects in Human Readable Formats ===&lt;br /&gt;
----&lt;br /&gt;
The conversion of Ruby objects into YAML and JSON formats are explained below.&lt;br /&gt;
&lt;br /&gt;
==== Converting Ruby Objects to YAML format ====&lt;br /&gt;
&lt;br /&gt;
YAML 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 binary format. 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. In order to use it in Ruby, the yaml.rb file is required to be loaded which provides methods for converting objects into yaml format and creating .yml files.&lt;br /&gt;
&lt;br /&gt;
=====Serialization using YAML=====&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;nowiki&amp;gt; #Serialization using YAML's to_yaml method&lt;br /&gt;
 &lt;br /&gt;
  require &amp;quot;yaml&amp;quot;&lt;br /&gt;
     class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
	@name = name&lt;br /&gt;
	@age = age&lt;br /&gt;
	@country=country&lt;br /&gt;
     end&lt;br /&gt;
      def to_s&lt;br /&gt;
	&amp;quot;In First:\n#{@name}, #{@age}, #{@country}\n&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
  x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
  puts x&lt;br /&gt;
  puts x.to_yaml&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   In First:&lt;br /&gt;
   Tom, 25, USA&lt;br /&gt;
   --- !ruby/object:First&lt;br /&gt;
   name: Tom&lt;br /&gt;
   age: 25&lt;br /&gt;
   country: USA&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The above code displays the object x, first as a string and then in the yaml format.&lt;br /&gt;
&lt;br /&gt;
====='''Saving YAML data into a file:'''=====&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt; # Serialization using YAML::dump&lt;br /&gt;
&lt;br /&gt;
require 'yaml'&lt;br /&gt;
&lt;br /&gt;
f = File.open( 'first.yml', 'w' )&lt;br /&gt;
YAML.dump( [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;], f )&lt;br /&gt;
f.close&lt;br /&gt;
		&lt;br /&gt;
File.open( 'first.yml' ){ |f|	&lt;br /&gt;
    $arr= YAML.load(f)&lt;br /&gt;
}	&lt;br /&gt;
&lt;br /&gt;
p( $arr )&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;]&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The dump function can be used to serialize the data and save it into a file in YAML format. As shown in the above example the data in YAML format can be de-serialized using the load function.&lt;br /&gt;
&lt;br /&gt;
YAML libraries also provides an option of selecting only those variables of the object that are needed to be serialized. This is done using the to_yaml_properties method as shown in the below example.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=====Custom Serialization using YAML=====&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;nowiki&amp;gt;&lt;br /&gt;
    require &amp;quot;yaml&amp;quot;&lt;br /&gt;
     class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
	@name = name&lt;br /&gt;
	@age = age&lt;br /&gt;
	@country=country&lt;br /&gt;
     end&lt;br /&gt;
     def to_s&lt;br /&gt;
	&amp;quot;In First:\n#{@name}, #{@age}, #{@country}\n&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
      def to_yaml_properties&lt;br /&gt;
	   [&amp;quot;@name&amp;quot;,&amp;quot;@age&amp;quot;]  #@country will not be serialized&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
  x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
  puts x&lt;br /&gt;
  puts x.to_yaml&lt;br /&gt;
  &amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   In First:&lt;br /&gt;
   Tom, 25, USA&lt;br /&gt;
   --- !ruby/object:First&lt;br /&gt;
   name: Tom&lt;br /&gt;
   age: 25&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Converting Ruby Objects to JSON format: ====&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
JSON 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. Ruby 1.8.7 distribution is not bundled with json gem. However, in Ruby 1.9.2, the json gem is bundled with the core Ruby distribution.&lt;br /&gt;
The JSON library can be installed using Ruby Gems[http://rubygems.org/] as shown below:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;# gem install json&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A JSON string for serialization can be created by using the JSON.generate method:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
        require 'json'&lt;br /&gt;
        my_hash = {:Welcome =&amp;gt; &amp;quot;Ruby&amp;quot;}&lt;br /&gt;
        puts JSON.generate(my_hash) =&amp;gt; &amp;quot;{\&amp;quot;WELCOME\&amp;quot;:\&amp;quot;RUBY\&amp;quot;}&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
'''Output:'''&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;{&amp;quot;{\&amp;quot;Welcome\&amp;quot;:\&amp;quot;Ruby\&amp;quot;}&amp;quot;=&amp;gt;&amp;quot;{\&amp;quot;WELCOME\&amp;quot;:\&amp;quot;RUBY\&amp;quot;}&amp;quot;}&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A JSON string received from another program can be parsed by using JSON.parse&lt;br /&gt;
Ruby thus converts String to Hash.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
        require 'json'&lt;br /&gt;
        my_hash = JSON.parse('{&amp;quot;Welcome&amp;quot;: &amp;quot;Ruby&amp;quot;}')&lt;br /&gt;
        puts my_hash[&amp;quot;Welcome&amp;quot;] =&amp;gt; &amp;quot;Ruby&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Converting Ruby Objects to Binary Formats ===&lt;br /&gt;
----&lt;br /&gt;
Binary Serialization is another form of serialization in Ruby which is not in human readable form. 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. Binary Serialization  is done using Marshal which is built into Ruby and the code for it is written in Ruby's Marshal module(marshal.c) and thus no additional files are required in order to use it. The _dump and _load methods defined in marshal are used for serialization. Using marshal module the following types of objects can not be serialized: bindings, procedure objects, singleton objects, instances of IO objects and interfaces.&lt;br /&gt;
&lt;br /&gt;
Since the Binary Serialized data is not in human readable form, the following guidelines need to be followed.&lt;br /&gt;
     1.Use print instead of puts[http://ruby-doc.org/core-2.0.0/ARGF.html#] when serialized objects are written to a file in order to avoid new line characters to be written &lt;br /&gt;
       in the file.&lt;br /&gt;
     &lt;br /&gt;
     2.Use a record separator in order to differentiate between two objects.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====='''Binary Serialization Example:'''=====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    class Animal&lt;br /&gt;
      def initialize  name, age&lt;br /&gt;
      @name = name&lt;br /&gt;
      @age=age&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
   class Cat &amp;lt; Animal&lt;br /&gt;
    def to_s&lt;br /&gt;
     &amp;quot;In Cat C: #{@name} \t #{@age}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
  c = Cat.new(&amp;quot;Kitty Kat&amp;quot;,5)&lt;br /&gt;
  puts &amp;quot;Before Serialization&amp;quot;&lt;br /&gt;
  puts c&lt;br /&gt;
  #puts d&lt;br /&gt;
  serialize_cat= Marshal.dump(c) #dumps the serialized cat object into serialize_cat&lt;br /&gt;
  puts &amp;quot;\nAfter Serialization:\n #{serialize_cat}&amp;quot;&lt;br /&gt;
  deserialize_cat= Marshal::load(serialize_cat) #deserializes the cat object and loads it back into deserialize_cat&lt;br /&gt;
  puts &amp;quot;\nAfter Deserialization\n #{deserialize_cat}&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   Before Serialization&lt;br /&gt;
   In Cat C: Kitty Kat 	 5&lt;br /&gt;
   After Serialization:&lt;br /&gt;
   oCat:&lt;br /&gt;
   @nameI&amp;quot;Kitty Kat:ET:	@agei&lt;br /&gt;
   After Deserialization&lt;br /&gt;
   In Cat C: Kitty Kat 	 5&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====='''Saving Marshal data into a file'''=====&lt;br /&gt;
&lt;br /&gt;
Similar to YAML, Marshal can also be used to dump data into a file. The above example showing the serialization using YAML::dump can be written using marshal as shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    f = File.open( 'first.yml', 'w' )&lt;br /&gt;
    Marshal.dump( [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;], f )&lt;br /&gt;
    f.close	&lt;br /&gt;
    File.open( 'first.yml' ){ |f|	&lt;br /&gt;
    $arr= Marshal.load(f)&lt;br /&gt;
    }	&lt;br /&gt;
    p( $arr )&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;]&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice that in the above example there is no &amp;quot;require&amp;quot; statement as opposed to the earlier example of writing serialized data into files using YAML. This is because unlike YAML, marshal is built-in Ruby and  no external libraries is required in order to use its functionality.&lt;br /&gt;
&lt;br /&gt;
====='''Custom serialization using Marshal'''=====&lt;br /&gt;
&lt;br /&gt;
Marshal can also be used to custom serialize an object i.e. it provides an option to omit the variables of an object that are not required in the serialized data. The following program shows the use of marshal_dump method for achieving custom serialization.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
      class First&lt;br /&gt;
      def initialize(name, age, country)&lt;br /&gt;
         @name = name&lt;br /&gt;
         @age = age&lt;br /&gt;
         @country=country&lt;br /&gt;
      end&lt;br /&gt;
      def to_s&lt;br /&gt;
            &amp;quot;In First: #{@name}, #{@age}, #{@country}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
      def marshal_dump&lt;br /&gt;
            [@name,@age]  #@country will not be serialized&lt;br /&gt;
      end&lt;br /&gt;
      def marshal_load(data)&lt;br /&gt;
       @name=data[0]&lt;br /&gt;
       @age=data[1]&lt;br /&gt;
       @country=&amp;quot;United States of America&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
      end&lt;br /&gt;
 &lt;br /&gt;
      x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
      puts x&lt;br /&gt;
  &lt;br /&gt;
      marshal_data = Marshal.dump( x ) &lt;br /&gt;
      y = Marshal.load( marshal_data ) &lt;br /&gt;
      p( y.to_s )&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    In First: Tom, 25, USA&lt;br /&gt;
    &amp;quot;In First: Tom, 25, United States of America&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== '''Serialization Performance in Ruby''' ==&lt;br /&gt;
The different serialization formats described above differ in the efficiency at which they can serialize and deserialize data and thus while serializing large amount of data their efficiency is taken into account. The .report[http://www.ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html] method in Ruby can be used to evaluate the performance of these serialization patterns. Marshal, as it handles data in binary format, is the most efficient form of serialization in Ruby. The following example compares the performance of marshal, JSON and YAML format.[http://www.pauldix.net/2008/08/serializing-dat.html]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   require 'benchmark'&lt;br /&gt;
   require 'rubygems'&lt;br /&gt;
   require 'json'&lt;br /&gt;
   require 'yaml'&lt;br /&gt;
   include Benchmark&lt;br /&gt;
   class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
      @name=name&lt;br /&gt;
      @age=age&lt;br /&gt;
      @country=country&lt;br /&gt;
     end&lt;br /&gt;
   end  &lt;br /&gt;
    x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
    puts &amp;quot;            User     System     Total      Real&amp;quot;&lt;br /&gt;
   benchmark do |t|&lt;br /&gt;
    print &amp;quot;Marshal:&amp;quot;&lt;br /&gt;
    t.report{1000.times do; Marshal.load(Marshal.dump(x));end}&lt;br /&gt;
    print &amp;quot;JSON:   &amp;quot;&lt;br /&gt;
    t.report{1000.times do; JSON.load(JSON.dump(x));end}&lt;br /&gt;
    print &amp;quot;YAML:   &amp;quot;&lt;br /&gt;
    t.report{1000.times do; YAML.load(YAML.dump(x));end}&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
              User     System     Total      Real&lt;br /&gt;
  Marshal:   0.010000   0.000000   0.010000 (  0.015408)&lt;br /&gt;
  JSON:      0.020000   0.010000   0.030000 (  0.022462)&lt;br /&gt;
  YAML:      0.460000   0.010000   0.470000 (  0.472149)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output of the above example shows that, given large chunks of data, marshal is comparatively more efficient format for serialization in Ruby.&lt;br /&gt;
&lt;br /&gt;
== Serialization in other Object-Oriented Languages ==&lt;br /&gt;
&lt;br /&gt;
===Comparison===&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Sl.No !! Ruby !! Java !! .Net Framework !! C++&lt;br /&gt;
|-&lt;br /&gt;
| 1 || Ruby provides a built in module called 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 || Although, there is no built in support for serialization in C++, it can be achieved by using Boost libraries[http://www.boost.org/doc/libs/1_36_0/libs/serialization/example/demo.cpp]&lt;br /&gt;
|-&lt;br /&gt;
| 2 || The built in module of Ruby (Marshal) does not support platform independence, however, it can be achieved by using external libraries like YAML and JSON || Similarly, Java's built in serialization is also not platform independent and in order to use serialization in Java across Ruby platform, jruby library should be used.  || .Net used Remoting technology to make it platform independent. || Serialization using the Boost libraries is not platform independent.&lt;br /&gt;
|-&lt;br /&gt;
| 3 || YAML provides a method [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/syck/rdoc/Object.html (to_yaml_properties)] which can be used to select the variables who's value is need to be serialized. With Marshal, we need to write a method named marshal_dump defining the variables of an object that has to be serialized. || Provides an option for serializing only the required 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 data 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 || Serialization using the Boost libraries is custom and thus the user can specify the part of the objects to be serialized.&lt;br /&gt;
|-&lt;br /&gt;
| 4 || Bindings, procedure objects, singleton objects, instances of IO objects and interfaces can not be serialized. Serializing these objects throws TypeError exceptions. || Thread, OutputStream and its subclasses, and Socket are not serializable in Java.[http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html] || Objects like DataRow are non serializable in .NET. Whether an object is serializable or not can be checked using the [http://msdn.microsoft.com/en-us/library/system.type.isserializable(v=vs.110).aspx?cs-save-lang=1&amp;amp;cs-lang=cpp#code-snippet-1 Type.IsSerializable] || There are no such objects in C++.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===Serialization in Java===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 import java.io.FileInputStream;&lt;br /&gt;
 import java.io.FileOutputStream;&lt;br /&gt;
 import java.io.IOException;&lt;br /&gt;
 import java.io.ObjectInputStream;&lt;br /&gt;
 import java.io.ObjectOutputStream;&lt;br /&gt;
 import java.io.Serializable;&lt;br /&gt;
           &lt;br /&gt;
 public class First implements Serializable {&lt;br /&gt;
 &lt;br /&gt;
 private static final long serialVersionUID = 1L;&lt;br /&gt;
 public String name;&lt;br /&gt;
 public int age;&lt;br /&gt;
 public String country;&lt;br /&gt;
 &lt;br /&gt;
 private static void serialize(First f, String path) throws IOException {&lt;br /&gt;
  &lt;br /&gt;
       FileOutputStream fos = new FileOutputStream(path);&lt;br /&gt;
       ObjectOutputStream oos = new ObjectOutputStream(fos);&lt;br /&gt;
    &lt;br /&gt;
       // Serializing&lt;br /&gt;
       oos.writeObject(f);&lt;br /&gt;
       oos.close();&lt;br /&gt;
       fos.close();&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
 private static First deserialize(String path) throws IOException,&lt;br /&gt;
  		ClassNotFoundException {&lt;br /&gt;
  	// tom.ser will deserialize in object f&lt;br /&gt;
  	First f = null;&lt;br /&gt;
  	FileInputStream fin = new FileInputStream(path);&lt;br /&gt;
  	ObjectInputStream oin = new ObjectInputStream(fin);&lt;br /&gt;
  	// DeSerializing and Casting to Class First&lt;br /&gt;
  	f = (First) oin.readObject();&lt;br /&gt;
  	oin.close();&lt;br /&gt;
  	fin.close();&lt;br /&gt;
  	return f;&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  public static void main(String[] args) {&lt;br /&gt;
  	try {&lt;br /&gt;
  		// Creating object for Tom&lt;br /&gt;
  		First f1 = new First();&lt;br /&gt;
  		f1.name = &amp;quot;Tom&amp;quot;;&lt;br /&gt;
  		f1.age = 25;&lt;br /&gt;
  		f1.country = &amp;quot;USA&amp;quot;;&lt;br /&gt;
  		First f2 = null;&lt;br /&gt;
  		long start = System.currentTimeMillis();&lt;br /&gt;
  		for (int i = 0; i &amp;lt; 1000; i++){&lt;br /&gt;
  		// serializing&lt;br /&gt;
  		serialize(f1, &amp;quot;/path/to/files/&amp;quot; + i + &amp;quot;.ser&amp;quot;);&lt;br /&gt;
  		// deserializing&lt;br /&gt;
  		f2 = deserialize(&amp;quot;/path/to/files&amp;quot; + i + &amp;quot;.ser&amp;quot;);&lt;br /&gt;
  		}&lt;br /&gt;
  		long end = System.currentTimeMillis();&lt;br /&gt;
  		long totalDuration = end - start;&lt;br /&gt;
  		System.out.println(&amp;quot;Serialization/Deserialization for 1000 Iterations duration: &amp;quot; + totalDuration + &amp;quot; Milliseconds&amp;quot;);&lt;br /&gt;
  		// Printing deserialized object&lt;br /&gt;
  		System.out.println(&amp;quot;Deserialized =&amp;gt; Name: &amp;quot; + f2.name + &amp;quot;, Age: &amp;quot; + f2.age + &amp;quot;, Country: &amp;quot; + f2.country);&lt;br /&gt;
  	} catch (IOException e) {&lt;br /&gt;
  		e.printStackTrace();&lt;br /&gt;
  	} catch (ClassNotFoundException e) {&lt;br /&gt;
  		e.printStackTrace();&lt;br /&gt;
  	}&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 Serialization/Deserialization for 1000 Iterations duration: 1651335109 Nano Seconds&lt;br /&gt;
 Deserialized =&amp;gt; Name: Tom, Age: 25, Country: USA&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== See Also==&lt;br /&gt;
&lt;br /&gt;
1. [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/json/rdoc/JSON.html Serialization in Ruby JSON]&lt;br /&gt;
&lt;br /&gt;
2. [http://api.rubyonrails.org/classes/ActiveModel/Serialization.html Serialization in Rails]&lt;br /&gt;
&lt;br /&gt;
3. [http://gregmoreno.wordpress.com/2011/01/27/preventing-model-explosion-via-rails-serialization/ Article on Rails Serialization]&lt;br /&gt;
&lt;br /&gt;
4. [http://en.wikipedia.org/wiki/Google_Protocol_Buffers Protocol Buffers]&lt;br /&gt;
&lt;br /&gt;
5. [https://rubygems.org/gems/ruby-protocol-buffers Ruby Protocol Buffers Gem]&lt;br /&gt;
&lt;br /&gt;
6. [http://en.wikipedia.org/wiki/Apache_Avro Avro]&lt;br /&gt;
&lt;br /&gt;
7. [http://rubygems.org/gems/avro Ruby Avro Gem]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
1. [http://en.wikipedia.org/wiki/Serialization Serilization in General]&lt;br /&gt;
&lt;br /&gt;
2. [http://en.wikipedia.org/wiki/Marshalling_%28computer_science%29 Marshaling]&lt;br /&gt;
&lt;br /&gt;
3. [http://techbrahmana.blogspot.com/2012/03/rails-cookie-handling-serialization-and.html Rails Cookie Handling]&lt;br /&gt;
&lt;br /&gt;
4. [http://yaml.org/ YAML]&lt;br /&gt;
&lt;br /&gt;
5. [http://www.ruby-doc.org/core-2.1.0/Marshal.html Marshal module]&lt;br /&gt;
&lt;br /&gt;
6. [http://json.org/ JSON]&lt;br /&gt;
&lt;br /&gt;
7. [http://rubygems.org/ Ruby Gems]&lt;br /&gt;
&lt;br /&gt;
8. [http://ruby-doc.org/core-2.0.0/ARGF.html# Ruby ARGF Documentation]&lt;br /&gt;
&lt;br /&gt;
9. [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html Ruby Documentation for Benchmark Gem]&lt;br /&gt;
&lt;br /&gt;
10. [http://www.pauldix.net/2008/08/serializing-dat.html Serialization Performance Comparison]&lt;br /&gt;
&lt;br /&gt;
11. [http://www.boost.org/doc/libs/1_36_0/libs/serialization/example/demo.cpp Boost Libraries]&lt;br /&gt;
&lt;br /&gt;
12. [http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html Java Serialization]&lt;br /&gt;
&lt;br /&gt;
13. [http://www.w3schools.com/json/ JSON Tutorial]&lt;br /&gt;
&lt;br /&gt;
14. [http://www.skorks.com/2010/04/serializing-and-deserializing-objects-with-ruby/ Serializing and De-serializing in Ruby]&lt;br /&gt;
&lt;br /&gt;
15. [http://www.codeproject.com/Articles/33296/Serialization-and-De-serialization Serialization and De-serialization]&lt;br /&gt;
&lt;br /&gt;
16. [http://msdn.microsoft.com/en-us/library/182eeyhh.aspx XML Serialization]&lt;br /&gt;
&lt;br /&gt;
17. [http://www.tutorialspoint.com/java/java_serialization.htm Java Serialization Tutorial]&lt;br /&gt;
&lt;br /&gt;
18. [http://www.sapphiresteel.com/ruby-programming/The-Book-Of-Ruby The Book of Ruby]&lt;br /&gt;
&lt;br /&gt;
19. [http://www.codeproject.com/Articles/20962/Introducing-Serialization-in-NET Serialization in .Net]&lt;br /&gt;
&lt;br /&gt;
20. [http://ruby.about.com/od/advancedruby/ss/Serialization-In-Ruby-Yaml.html Serialization in Ruby YAML]&lt;br /&gt;
&lt;br /&gt;
21. [http://ruby.about.com/od/tasks/a/The-Json-Gem.html Installing JSON gem]&lt;br /&gt;
&lt;br /&gt;
22. [http://www.ruby-doc.org/stdlib-2.0.0/libdoc/json/rdoc/JSON.html Serialization in Ruby JSON]&lt;/div&gt;</summary>
		<author><name>Mmehta2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1d_mm&amp;diff=83425</id>
		<title>CSC/ECE 517 Spring 2014/ch1a 1d mm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1d_mm&amp;diff=83425"/>
		<updated>2014-02-18T03:12:52Z</updated>

		<summary type="html">&lt;p&gt;Mmehta2: /* Serialization in Java */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page discusses Serialization, focusing on Ruby programming language. It describes different methods of Serialization in Ruby. In addition, it also compares serialization in different Object Oriented languages.&lt;br /&gt;
&lt;br /&gt;
== '''Introduction''' ==&lt;br /&gt;
&lt;br /&gt;
==='''Serialization'''===&lt;br /&gt;
&lt;br /&gt;
'''Serialization'''[http://en.wikipedia.org/wiki/Serialization] is a process of converting a data structure or an object into a stream of bytes or string to facilitate storage in memory, file (persistence storage) or transmission over a network. The process of Serialization is also referred to as ''Marshalling''[http://en.wikipedia.org/wiki/Marshalling_%28computer_science%29]. 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.&lt;br /&gt;
&lt;br /&gt;
==='''De-serialization'''===&lt;br /&gt;
&lt;br /&gt;
'''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''.&lt;br /&gt;
&lt;br /&gt;
The process of Serialization/De-serialization is not a replacement for database systems. Database Systems are used for their relational and transactional semantics.&lt;br /&gt;
&lt;br /&gt;
==='''Basic Advantages of Serialization'''===&lt;br /&gt;
&lt;br /&gt;
1. Communication between two or more processes on same machine. Object state can be saved and shared in a persistent or in-memory store.&lt;br /&gt;
&lt;br /&gt;
2. Communication between processes on different machines. Serialization facilitates the transmission of an object through a network.&lt;br /&gt;
&lt;br /&gt;
3. Creating a clone of an object. &lt;br /&gt;
&lt;br /&gt;
4. Cross-platform compatibility. Object can be serialized in a common format that is understood by multiple platforms. For example, JSON[http://www.json.org/] and XML.&lt;br /&gt;
&lt;br /&gt;
==='''Practical Applications for Serialization'''===&lt;br /&gt;
&lt;br /&gt;
1. [http://docs.oracle.com/cd/E13222_01/wls/docs81/webapp/sessions.html#139726 HTTP Session] Replication by sharing session objects across web servers for load balancing and handling failover scenarios.&lt;br /&gt;
&lt;br /&gt;
2. Serialization facilitates communication in [http://en.wikipedia.org/wiki/Java_remote_method_invocation Remote Method Invocation] or [http://en.wikipedia.org/wiki/Remote_procedure_call Remote procedure calls]&lt;br /&gt;
&lt;br /&gt;
3. Rails Cookie Handling[http://techbrahmana.blogspot.com/2012/03/rails-cookie-handling-serialization-and.html]. Cookies are stored marshalled/unmarshalled to and from client machines.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== '''Serialization in Ruby:''' ==&lt;br /&gt;
&lt;br /&gt;
Ruby provides serialization capabilities through its module, [http://www.ruby-doc.org/core-2.0.0/Marshal.html Marshal]. There are also other libraries like YAML[http://yaml.org/] and JSON which can be used in Ruby to generate serialized objects for purposes like platform independence and human readable formats.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:SerializationChart.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== '''Types of Serialization''' ==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
=== Converting Ruby Objects in Human Readable Formats ===&lt;br /&gt;
----&lt;br /&gt;
The conversion of Ruby objects into YAML and JSON formats are explained below.&lt;br /&gt;
&lt;br /&gt;
==== Converting Ruby Objects to YAML format ====&lt;br /&gt;
&lt;br /&gt;
YAML 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 binary format. 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. In order to use it in Ruby, the yaml.rb file is required to be loaded which provides methods for converting objects into yaml format and creating .yml files.&lt;br /&gt;
&lt;br /&gt;
=====Serialization using YAML=====&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;nowiki&amp;gt; #Serialization using YAML's to_yaml method&lt;br /&gt;
 &lt;br /&gt;
  require &amp;quot;yaml&amp;quot;&lt;br /&gt;
     class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
	@name = name&lt;br /&gt;
	@age = age&lt;br /&gt;
	@country=country&lt;br /&gt;
     end&lt;br /&gt;
      def to_s&lt;br /&gt;
	&amp;quot;In First:\n#{@name}, #{@age}, #{@country}\n&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
  x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
  puts x&lt;br /&gt;
  puts x.to_yaml&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   In First:&lt;br /&gt;
   Tom, 25, USA&lt;br /&gt;
   --- !ruby/object:First&lt;br /&gt;
   name: Tom&lt;br /&gt;
   age: 25&lt;br /&gt;
   country: USA&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The above code displays the object x, first as a string and then in the yaml format.&lt;br /&gt;
&lt;br /&gt;
====='''Saving YAML data into a file:'''=====&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt; # Serialization using YAML::dump&lt;br /&gt;
&lt;br /&gt;
require 'yaml'&lt;br /&gt;
&lt;br /&gt;
f = File.open( 'first.yml', 'w' )&lt;br /&gt;
YAML.dump( [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;], f )&lt;br /&gt;
f.close&lt;br /&gt;
		&lt;br /&gt;
File.open( 'first.yml' ){ |f|	&lt;br /&gt;
    $arr= YAML.load(f)&lt;br /&gt;
}	&lt;br /&gt;
&lt;br /&gt;
p( $arr )&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;]&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The dump function can be used to serialize the data and save it into a file in YAML format. As shown in the above example the data in YAML format can be de-serialized using the load function.&lt;br /&gt;
&lt;br /&gt;
YAML libraries also provides an option of selecting only those variables of the object that are needed to be serialized. This is done using the to_yaml_properties method as shown in the below example.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=====Custom Serialization using YAML=====&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;nowiki&amp;gt;&lt;br /&gt;
    require &amp;quot;yaml&amp;quot;&lt;br /&gt;
     class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
	@name = name&lt;br /&gt;
	@age = age&lt;br /&gt;
	@country=country&lt;br /&gt;
     end&lt;br /&gt;
     def to_s&lt;br /&gt;
	&amp;quot;In First:\n#{@name}, #{@age}, #{@country}\n&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
      def to_yaml_properties&lt;br /&gt;
	   [&amp;quot;@name&amp;quot;,&amp;quot;@age&amp;quot;]  #@country will not be serialized&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
  x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
  puts x&lt;br /&gt;
  puts x.to_yaml&lt;br /&gt;
  &amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   In First:&lt;br /&gt;
   Tom, 25, USA&lt;br /&gt;
   --- !ruby/object:First&lt;br /&gt;
   name: Tom&lt;br /&gt;
   age: 25&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Converting Ruby Objects to JSON format: ====&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
JSON 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. Ruby 1.8.7 distribution is not bundled with json gem. However, in Ruby 1.9.2, the json gem is bundled with the core Ruby distribution.&lt;br /&gt;
The JSON library can be installed using Ruby Gems[http://rubygems.org/] as shown below:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;# gem install json&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A JSON string for serialization can be created by using the JSON.generate method:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
        require 'json'&lt;br /&gt;
        my_hash = {:Welcome =&amp;gt; &amp;quot;Ruby&amp;quot;}&lt;br /&gt;
        puts JSON.generate(my_hash) =&amp;gt; &amp;quot;{\&amp;quot;WELCOME\&amp;quot;:\&amp;quot;RUBY\&amp;quot;}&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
'''Output:'''&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;{&amp;quot;{\&amp;quot;Welcome\&amp;quot;:\&amp;quot;Ruby\&amp;quot;}&amp;quot;=&amp;gt;&amp;quot;{\&amp;quot;WELCOME\&amp;quot;:\&amp;quot;RUBY\&amp;quot;}&amp;quot;}&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A JSON string received from another program can be parsed by using JSON.parse&lt;br /&gt;
Ruby thus converts String to Hash.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
        require 'json'&lt;br /&gt;
        my_hash = JSON.parse('{&amp;quot;Welcome&amp;quot;: &amp;quot;Ruby&amp;quot;}')&lt;br /&gt;
        puts my_hash[&amp;quot;Welcome&amp;quot;] =&amp;gt; &amp;quot;Ruby&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Converting Ruby Objects to Binary Formats ===&lt;br /&gt;
----&lt;br /&gt;
Binary Serialization is another form of serialization in Ruby which is not in human readable form. 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. Binary Serialization  is done using Marshal which is built into Ruby and the code for it is written in Ruby's Marshal module(marshal.c) and thus no additional files are required in order to use it. The _dump and _load methods defined in marshal are used for serialization. Using marshal module the following types of objects can not be serialized: bindings, procedure objects, singleton objects, instances of IO objects and interfaces.&lt;br /&gt;
&lt;br /&gt;
Since the Binary Serialized data is not in human readable form, the following guidelines need to be followed.&lt;br /&gt;
     1.Use print instead of puts[http://ruby-doc.org/core-2.0.0/ARGF.html#] when serialized objects are written to a file in order to avoid new line characters to be written &lt;br /&gt;
       in the file.&lt;br /&gt;
     &lt;br /&gt;
     2.Use a record separator in order to differentiate between two objects.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====='''Binary Serialization Example:'''=====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    class Animal&lt;br /&gt;
      def initialize  name, age&lt;br /&gt;
      @name = name&lt;br /&gt;
      @age=age&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
   class Cat &amp;lt; Animal&lt;br /&gt;
    def to_s&lt;br /&gt;
     &amp;quot;In Cat C: #{@name} \t #{@age}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
  c = Cat.new(&amp;quot;Kitty Kat&amp;quot;,5)&lt;br /&gt;
  puts &amp;quot;Before Serialization&amp;quot;&lt;br /&gt;
  puts c&lt;br /&gt;
  #puts d&lt;br /&gt;
  serialize_cat= Marshal.dump(c) #dumps the serialized cat object into serialize_cat&lt;br /&gt;
  puts &amp;quot;\nAfter Serialization:\n #{serialize_cat}&amp;quot;&lt;br /&gt;
  deserialize_cat= Marshal::load(serialize_cat) #deserializes the cat object and loads it back into deserialize_cat&lt;br /&gt;
  puts &amp;quot;\nAfter Deserialization\n #{deserialize_cat}&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   Before Serialization&lt;br /&gt;
   In Cat C: Kitty Kat 	 5&lt;br /&gt;
   After Serialization:&lt;br /&gt;
   oCat:&lt;br /&gt;
   @nameI&amp;quot;Kitty Kat:ET:	@agei&lt;br /&gt;
   After Deserialization&lt;br /&gt;
   In Cat C: Kitty Kat 	 5&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====='''Saving Marshal data into a file'''=====&lt;br /&gt;
&lt;br /&gt;
Similar to YAML, Marshal can also be used to dump data into a file. The above example showing the serialization using YAML::dump can be written using marshal as shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    f = File.open( 'first.yml', 'w' )&lt;br /&gt;
    Marshal.dump( [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;], f )&lt;br /&gt;
    f.close	&lt;br /&gt;
    File.open( 'first.yml' ){ |f|	&lt;br /&gt;
    $arr= Marshal.load(f)&lt;br /&gt;
    }	&lt;br /&gt;
    p( $arr )&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;]&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice that in the above example there is no &amp;quot;require&amp;quot; statement as opposed to the earlier example of writing serialized data into files using YAML. This is because unlike YAML, marshal is built-in Ruby and  no external libraries is required in order to use its functionality.&lt;br /&gt;
&lt;br /&gt;
====='''Custom serialization using Marshal'''=====&lt;br /&gt;
&lt;br /&gt;
Marshal can also be used to custom serialize an object i.e. it provides an option to omit the variables of an object that are not required in the serialized data. The following program shows the use of marshal_dump method for achieving custom serialization.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
      class First&lt;br /&gt;
      def initialize(name, age, country)&lt;br /&gt;
         @name = name&lt;br /&gt;
         @age = age&lt;br /&gt;
         @country=country&lt;br /&gt;
      end&lt;br /&gt;
      def to_s&lt;br /&gt;
            &amp;quot;In First: #{@name}, #{@age}, #{@country}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
      def marshal_dump&lt;br /&gt;
            [@name,@age]  #@country will not be serialized&lt;br /&gt;
      end&lt;br /&gt;
      def marshal_load(data)&lt;br /&gt;
       @name=data[0]&lt;br /&gt;
       @age=data[1]&lt;br /&gt;
       @country=&amp;quot;United States of America&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
      end&lt;br /&gt;
 &lt;br /&gt;
      x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
      puts x&lt;br /&gt;
  &lt;br /&gt;
      marshal_data = Marshal.dump( x ) &lt;br /&gt;
      y = Marshal.load( marshal_data ) &lt;br /&gt;
      p( y.to_s )&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    In First: Tom, 25, USA&lt;br /&gt;
    &amp;quot;In First: Tom, 25, United States of America&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== '''Serialization Performance in Ruby''' ==&lt;br /&gt;
The different serialization formats described above differ in the efficiency at which they can serialize and deserialize data and thus while serializing large amount of data their efficiency is taken into account. The .report[http://www.ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html] method in Ruby can be used to evaluate the performance of these serialization patterns. Marshal, as it handles data in binary format, is the most efficient form of serialization in Ruby. The following example compares the performance of marshal, JSON and YAML format.[http://www.pauldix.net/2008/08/serializing-dat.html]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   require 'benchmark'&lt;br /&gt;
   require 'rubygems'&lt;br /&gt;
   require 'json'&lt;br /&gt;
   require 'yaml'&lt;br /&gt;
   include Benchmark&lt;br /&gt;
   class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
      @name=name&lt;br /&gt;
      @age=age&lt;br /&gt;
      @country=country&lt;br /&gt;
     end&lt;br /&gt;
   end  &lt;br /&gt;
    x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
    puts &amp;quot;            User     System     Total      Real&amp;quot;&lt;br /&gt;
   benchmark do |t|&lt;br /&gt;
    print &amp;quot;Marshal:&amp;quot;&lt;br /&gt;
    t.report{1000.times do; Marshal.load(Marshal.dump(x));end}&lt;br /&gt;
    print &amp;quot;JSON:   &amp;quot;&lt;br /&gt;
    t.report{1000.times do; JSON.load(JSON.dump(x));end}&lt;br /&gt;
    print &amp;quot;YAML:   &amp;quot;&lt;br /&gt;
    t.report{1000.times do; YAML.load(YAML.dump(x));end}&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
              User     System     Total      Real&lt;br /&gt;
  Marshal:   0.010000   0.000000   0.010000 (  0.015408)&lt;br /&gt;
  JSON:      0.020000   0.010000   0.030000 (  0.022462)&lt;br /&gt;
  YAML:      0.460000   0.010000   0.470000 (  0.472149)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output of the above example shows that, given large chunks of data, marshal is comparatively more efficient format for serialization in Ruby.&lt;br /&gt;
&lt;br /&gt;
== Serialization in other Object-Oriented Languages ==&lt;br /&gt;
&lt;br /&gt;
===Comparison===&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Sl.No !! Ruby !! Java !! .Net Framework !! C++&lt;br /&gt;
|-&lt;br /&gt;
| 1 || Ruby provides a built in module called 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 || Although, there is no built in support for serialization in C++, it can be achieved by using Boost libraries[http://www.boost.org/doc/libs/1_36_0/libs/serialization/example/demo.cpp]&lt;br /&gt;
|-&lt;br /&gt;
| 2 || The built in module of Ruby (Marshal) does not support platform independence, however, it can be achieved by using external libraries like YAML and JSON || Similarly, Java's built in serialization is also not platform independent and in order to use serialization in Java across Ruby platform, jruby library should be used.  || .Net used Remoting technology to make it platform independent. || Serialization using the Boost libraries is not platform independent.&lt;br /&gt;
|-&lt;br /&gt;
| 3 || YAML provides a method [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/syck/rdoc/Object.html (to_yaml_properties)] which can be used to select the variables who's value is need to be serialized. With Marshal, we need to write a method named marshal_dump defining the variables of an object that has to be serialized. || Provides an option for serializing only the required 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 data 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 || Serialization using the Boost libraries is custom and thus the user can specify the part of the objects to be serialized.&lt;br /&gt;
|-&lt;br /&gt;
| 4 || Bindings, procedure objects, singleton objects, instances of IO objects and interfaces can not be serialized. Serializing these objects throws TypeError exceptions. || Thread, OutputStream and its subclasses, and Socket are not serializable in Java.[http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html] || Objects like DataRow are non serializable in .NET. Whether an object is serializable or not can be checked using the [http://msdn.microsoft.com/en-us/library/system.type.isserializable(v=vs.110).aspx?cs-save-lang=1&amp;amp;cs-lang=cpp#code-snippet-1 Type.IsSerializable] || There are no such objects in C++.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===Serialization in Java===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 import java.io.FileInputStream;&lt;br /&gt;
 import java.io.FileOutputStream;&lt;br /&gt;
 import java.io.IOException;&lt;br /&gt;
 import java.io.ObjectInputStream;&lt;br /&gt;
 import java.io.ObjectOutputStream;&lt;br /&gt;
 import java.io.Serializable;&lt;br /&gt;
           &lt;br /&gt;
 public class First implements Serializable {&lt;br /&gt;
 &lt;br /&gt;
 private static final long serialVersionUID = 1L;&lt;br /&gt;
 public String name;&lt;br /&gt;
 public int age;&lt;br /&gt;
 public String country;&lt;br /&gt;
 &lt;br /&gt;
 private static void serialize(First f, String path) throws IOException {&lt;br /&gt;
  &lt;br /&gt;
       FileOutputStream fos = new FileOutputStream(path);&lt;br /&gt;
       ObjectOutputStream oos = new ObjectOutputStream(fos);&lt;br /&gt;
    &lt;br /&gt;
       // Serializing&lt;br /&gt;
       oos.writeObject(f);&lt;br /&gt;
       oos.close();&lt;br /&gt;
       fos.close();&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
 private static First deserialize(String path) throws IOException,&lt;br /&gt;
  		ClassNotFoundException {&lt;br /&gt;
  	// tom.ser will deserialize in object f&lt;br /&gt;
  	First f = null;&lt;br /&gt;
  	FileInputStream fin = new FileInputStream(path);&lt;br /&gt;
  	ObjectInputStream oin = new ObjectInputStream(fin);&lt;br /&gt;
  	// DeSerializing and Casting to Class First&lt;br /&gt;
  	f = (First) oin.readObject();&lt;br /&gt;
  	oin.close();&lt;br /&gt;
  	fin.close();&lt;br /&gt;
  	return f;&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  public static void main(String[] args) {&lt;br /&gt;
  	try {&lt;br /&gt;
  		// Creating object for Tom&lt;br /&gt;
  		First f1 = new First();&lt;br /&gt;
  		f1.name = &amp;quot;Tom&amp;quot;;&lt;br /&gt;
  		f1.age = 25;&lt;br /&gt;
  		f1.country = &amp;quot;USA&amp;quot;;&lt;br /&gt;
  		First f2 = null;&lt;br /&gt;
  		long start = System.currentTimeMillis();&lt;br /&gt;
  		for (int i = 0; i &amp;lt; 1000; i++){&lt;br /&gt;
  		// serializing&lt;br /&gt;
  		serialize(f1, &amp;quot;/path/to/files/&amp;quot; + i + &amp;quot;.ser&amp;quot;);&lt;br /&gt;
  		// deserializing&lt;br /&gt;
  		f2 = deserialize(&amp;quot;/path/to/files&amp;quot; + i + &amp;quot;.ser&amp;quot;);&lt;br /&gt;
  		}&lt;br /&gt;
  		long end = System.currentTimeMillis();&lt;br /&gt;
  		long totalDuration = end - start;&lt;br /&gt;
  		System.out.println(&amp;quot;Serialization/Deserialization for 1000 Iterations duration: &amp;quot; + totalDuration + &amp;quot; Milliseconds&amp;quot;);&lt;br /&gt;
  		// Printing deserialized object&lt;br /&gt;
  		System.out.println(&amp;quot;Deserialized =&amp;gt; Name: &amp;quot; + f2.name + &amp;quot;, Age: &amp;quot; + f2.age + &amp;quot;, Country: &amp;quot; + f2.country);&lt;br /&gt;
  	} catch (IOException e) {&lt;br /&gt;
  		e.printStackTrace();&lt;br /&gt;
  	} catch (ClassNotFoundException e) {&lt;br /&gt;
  		e.printStackTrace();&lt;br /&gt;
  	}&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 Serialization/Deserialization for 1000 Iterations duration: 1651335109 Nano Seconds&lt;br /&gt;
 Deserialized =&amp;gt; Name: Tom, Age: 25, Country: USA&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== See Also==&lt;br /&gt;
&lt;br /&gt;
1. [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/json/rdoc/JSON.html Serialization in Ruby JSON]&lt;br /&gt;
&lt;br /&gt;
2. [http://api.rubyonrails.org/classes/ActiveModel/Serialization.html Serialization in Rails]&lt;br /&gt;
&lt;br /&gt;
3. [http://gregmoreno.wordpress.com/2011/01/27/preventing-model-explosion-via-rails-serialization/ Article on Rails Serialization]&lt;br /&gt;
&lt;br /&gt;
4. [http://en.wikipedia.org/wiki/Google_Protocol_Buffers Protocol Buffers]&lt;br /&gt;
&lt;br /&gt;
5. [https://rubygems.org/gems/ruby-protocol-buffers Ruby Protocol Buffers Gem]&lt;br /&gt;
&lt;br /&gt;
6. [http://en.wikipedia.org/wiki/Apache_Avro Avro]&lt;br /&gt;
&lt;br /&gt;
7. [http://rubygems.org/gems/avro Ruby Avro Gem]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
1. [http://en.wikipedia.org/wiki/Serialization Serilization in General]&lt;br /&gt;
&lt;br /&gt;
2. [http://en.wikipedia.org/wiki/Marshalling_%28computer_science%29 Marshaling]&lt;br /&gt;
&lt;br /&gt;
3. [http://techbrahmana.blogspot.com/2012/03/rails-cookie-handling-serialization-and.html Rails Cookie Handling]&lt;br /&gt;
&lt;br /&gt;
4. [http://yaml.org/ YAML]&lt;br /&gt;
&lt;br /&gt;
5. [http://www.ruby-doc.org/core-2.1.0/Marshal.html Marshal module]&lt;br /&gt;
&lt;br /&gt;
6. [http://json.org/ JSON]&lt;br /&gt;
&lt;br /&gt;
7. [http://rubygems.org/ Ruby Gems]&lt;br /&gt;
&lt;br /&gt;
8. [http://ruby-doc.org/core-2.0.0/ARGF.html# Ruby ARGF Documentation]&lt;br /&gt;
&lt;br /&gt;
9. [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html Ruby Documentation for Benchmark Gem]&lt;br /&gt;
&lt;br /&gt;
10. [http://www.pauldix.net/2008/08/serializing-dat.html Serialization Performance Comparison]&lt;br /&gt;
&lt;br /&gt;
11. [http://www.boost.org/doc/libs/1_36_0/libs/serialization/example/demo.cpp Boost Libraries]&lt;br /&gt;
&lt;br /&gt;
12. [http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html Java Serialization]&lt;br /&gt;
&lt;br /&gt;
13. [http://www.w3schools.com/json/ JSON Tutorial]&lt;br /&gt;
&lt;br /&gt;
14. [http://www.skorks.com/2010/04/serializing-and-deserializing-objects-with-ruby/ Serializing and De-serializing in Ruby]&lt;br /&gt;
&lt;br /&gt;
15. [http://www.codeproject.com/Articles/33296/Serialization-and-De-serialization Serialization and De-serialization]&lt;br /&gt;
&lt;br /&gt;
16. [http://msdn.microsoft.com/en-us/library/182eeyhh.aspx XML Serialization]&lt;br /&gt;
&lt;br /&gt;
17. [http://www.tutorialspoint.com/java/java_serialization.htm Java Serialization Tutorial]&lt;br /&gt;
&lt;br /&gt;
18. [http://www.sapphiresteel.com/ruby-programming/The-Book-Of-Ruby The Book of Ruby]&lt;br /&gt;
&lt;br /&gt;
19. [http://www.codeproject.com/Articles/20962/Introducing-Serialization-in-NET Serialization in .Net]&lt;br /&gt;
&lt;br /&gt;
20. [http://ruby.about.com/od/advancedruby/ss/Serialization-In-Ruby-Yaml.html Serialization in Ruby YAML]&lt;br /&gt;
&lt;br /&gt;
21. [http://ruby.about.com/od/tasks/a/The-Json-Gem.html Installing JSON gem]&lt;br /&gt;
&lt;br /&gt;
22. [http://www.ruby-doc.org/stdlib-2.0.0/libdoc/json/rdoc/JSON.html Serialization in Ruby JSON]&lt;/div&gt;</summary>
		<author><name>Mmehta2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1d_mm&amp;diff=83424</id>
		<title>CSC/ECE 517 Spring 2014/ch1a 1d mm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1d_mm&amp;diff=83424"/>
		<updated>2014-02-18T03:11:55Z</updated>

		<summary type="html">&lt;p&gt;Mmehta2: /* Serialization in Java */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page discusses Serialization, focusing on Ruby programming language. It describes different methods of Serialization in Ruby. In addition, it also compares serialization in different Object Oriented languages.&lt;br /&gt;
&lt;br /&gt;
== '''Introduction''' ==&lt;br /&gt;
&lt;br /&gt;
==='''Serialization'''===&lt;br /&gt;
&lt;br /&gt;
'''Serialization'''[http://en.wikipedia.org/wiki/Serialization] is a process of converting a data structure or an object into a stream of bytes or string to facilitate storage in memory, file (persistence storage) or transmission over a network. The process of Serialization is also referred to as ''Marshalling''[http://en.wikipedia.org/wiki/Marshalling_%28computer_science%29]. 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.&lt;br /&gt;
&lt;br /&gt;
==='''De-serialization'''===&lt;br /&gt;
&lt;br /&gt;
'''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''.&lt;br /&gt;
&lt;br /&gt;
The process of Serialization/De-serialization is not a replacement for database systems. Database Systems are used for their relational and transactional semantics.&lt;br /&gt;
&lt;br /&gt;
==='''Basic Advantages of Serialization'''===&lt;br /&gt;
&lt;br /&gt;
1. Communication between two or more processes on same machine. Object state can be saved and shared in a persistent or in-memory store.&lt;br /&gt;
&lt;br /&gt;
2. Communication between processes on different machines. Serialization facilitates the transmission of an object through a network.&lt;br /&gt;
&lt;br /&gt;
3. Creating a clone of an object. &lt;br /&gt;
&lt;br /&gt;
4. Cross-platform compatibility. Object can be serialized in a common format that is understood by multiple platforms. For example, JSON[http://www.json.org/] and XML.&lt;br /&gt;
&lt;br /&gt;
==='''Practical Applications for Serialization'''===&lt;br /&gt;
&lt;br /&gt;
1. [http://docs.oracle.com/cd/E13222_01/wls/docs81/webapp/sessions.html#139726 HTTP Session] Replication by sharing session objects across web servers for load balancing and handling failover scenarios.&lt;br /&gt;
&lt;br /&gt;
2. Serialization facilitates communication in [http://en.wikipedia.org/wiki/Java_remote_method_invocation Remote Method Invocation] or [http://en.wikipedia.org/wiki/Remote_procedure_call Remote procedure calls]&lt;br /&gt;
&lt;br /&gt;
3. Rails Cookie Handling[http://techbrahmana.blogspot.com/2012/03/rails-cookie-handling-serialization-and.html]. Cookies are stored marshalled/unmarshalled to and from client machines.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== '''Serialization in Ruby:''' ==&lt;br /&gt;
&lt;br /&gt;
Ruby provides serialization capabilities through its module, [http://www.ruby-doc.org/core-2.0.0/Marshal.html Marshal]. There are also other libraries like YAML[http://yaml.org/] and JSON which can be used in Ruby to generate serialized objects for purposes like platform independence and human readable formats.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:SerializationChart.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== '''Types of Serialization''' ==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
=== Converting Ruby Objects in Human Readable Formats ===&lt;br /&gt;
----&lt;br /&gt;
The conversion of Ruby objects into YAML and JSON formats are explained below.&lt;br /&gt;
&lt;br /&gt;
==== Converting Ruby Objects to YAML format ====&lt;br /&gt;
&lt;br /&gt;
YAML 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 binary format. 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. In order to use it in Ruby, the yaml.rb file is required to be loaded which provides methods for converting objects into yaml format and creating .yml files.&lt;br /&gt;
&lt;br /&gt;
=====Serialization using YAML=====&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;nowiki&amp;gt; #Serialization using YAML's to_yaml method&lt;br /&gt;
 &lt;br /&gt;
  require &amp;quot;yaml&amp;quot;&lt;br /&gt;
     class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
	@name = name&lt;br /&gt;
	@age = age&lt;br /&gt;
	@country=country&lt;br /&gt;
     end&lt;br /&gt;
      def to_s&lt;br /&gt;
	&amp;quot;In First:\n#{@name}, #{@age}, #{@country}\n&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
  x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
  puts x&lt;br /&gt;
  puts x.to_yaml&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   In First:&lt;br /&gt;
   Tom, 25, USA&lt;br /&gt;
   --- !ruby/object:First&lt;br /&gt;
   name: Tom&lt;br /&gt;
   age: 25&lt;br /&gt;
   country: USA&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The above code displays the object x, first as a string and then in the yaml format.&lt;br /&gt;
&lt;br /&gt;
====='''Saving YAML data into a file:'''=====&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt; # Serialization using YAML::dump&lt;br /&gt;
&lt;br /&gt;
require 'yaml'&lt;br /&gt;
&lt;br /&gt;
f = File.open( 'first.yml', 'w' )&lt;br /&gt;
YAML.dump( [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;], f )&lt;br /&gt;
f.close&lt;br /&gt;
		&lt;br /&gt;
File.open( 'first.yml' ){ |f|	&lt;br /&gt;
    $arr= YAML.load(f)&lt;br /&gt;
}	&lt;br /&gt;
&lt;br /&gt;
p( $arr )&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;]&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The dump function can be used to serialize the data and save it into a file in YAML format. As shown in the above example the data in YAML format can be de-serialized using the load function.&lt;br /&gt;
&lt;br /&gt;
YAML libraries also provides an option of selecting only those variables of the object that are needed to be serialized. This is done using the to_yaml_properties method as shown in the below example.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=====Custom Serialization using YAML=====&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;nowiki&amp;gt;&lt;br /&gt;
    require &amp;quot;yaml&amp;quot;&lt;br /&gt;
     class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
	@name = name&lt;br /&gt;
	@age = age&lt;br /&gt;
	@country=country&lt;br /&gt;
     end&lt;br /&gt;
     def to_s&lt;br /&gt;
	&amp;quot;In First:\n#{@name}, #{@age}, #{@country}\n&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
      def to_yaml_properties&lt;br /&gt;
	   [&amp;quot;@name&amp;quot;,&amp;quot;@age&amp;quot;]  #@country will not be serialized&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
  x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
  puts x&lt;br /&gt;
  puts x.to_yaml&lt;br /&gt;
  &amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   In First:&lt;br /&gt;
   Tom, 25, USA&lt;br /&gt;
   --- !ruby/object:First&lt;br /&gt;
   name: Tom&lt;br /&gt;
   age: 25&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Converting Ruby Objects to JSON format: ====&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
JSON 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. Ruby 1.8.7 distribution is not bundled with json gem. However, in Ruby 1.9.2, the json gem is bundled with the core Ruby distribution.&lt;br /&gt;
The JSON library can be installed using Ruby Gems[http://rubygems.org/] as shown below:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;# gem install json&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A JSON string for serialization can be created by using the JSON.generate method:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
        require 'json'&lt;br /&gt;
        my_hash = {:Welcome =&amp;gt; &amp;quot;Ruby&amp;quot;}&lt;br /&gt;
        puts JSON.generate(my_hash) =&amp;gt; &amp;quot;{\&amp;quot;WELCOME\&amp;quot;:\&amp;quot;RUBY\&amp;quot;}&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
'''Output:'''&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;{&amp;quot;{\&amp;quot;Welcome\&amp;quot;:\&amp;quot;Ruby\&amp;quot;}&amp;quot;=&amp;gt;&amp;quot;{\&amp;quot;WELCOME\&amp;quot;:\&amp;quot;RUBY\&amp;quot;}&amp;quot;}&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A JSON string received from another program can be parsed by using JSON.parse&lt;br /&gt;
Ruby thus converts String to Hash.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
        require 'json'&lt;br /&gt;
        my_hash = JSON.parse('{&amp;quot;Welcome&amp;quot;: &amp;quot;Ruby&amp;quot;}')&lt;br /&gt;
        puts my_hash[&amp;quot;Welcome&amp;quot;] =&amp;gt; &amp;quot;Ruby&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Converting Ruby Objects to Binary Formats ===&lt;br /&gt;
----&lt;br /&gt;
Binary Serialization is another form of serialization in Ruby which is not in human readable form. 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. Binary Serialization  is done using Marshal which is built into Ruby and the code for it is written in Ruby's Marshal module(marshal.c) and thus no additional files are required in order to use it. The _dump and _load methods defined in marshal are used for serialization. Using marshal module the following types of objects can not be serialized: bindings, procedure objects, singleton objects, instances of IO objects and interfaces.&lt;br /&gt;
&lt;br /&gt;
Since the Binary Serialized data is not in human readable form, the following guidelines need to be followed.&lt;br /&gt;
     1.Use print instead of puts[http://ruby-doc.org/core-2.0.0/ARGF.html#] when serialized objects are written to a file in order to avoid new line characters to be written &lt;br /&gt;
       in the file.&lt;br /&gt;
     &lt;br /&gt;
     2.Use a record separator in order to differentiate between two objects.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====='''Binary Serialization Example:'''=====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    class Animal&lt;br /&gt;
      def initialize  name, age&lt;br /&gt;
      @name = name&lt;br /&gt;
      @age=age&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
   class Cat &amp;lt; Animal&lt;br /&gt;
    def to_s&lt;br /&gt;
     &amp;quot;In Cat C: #{@name} \t #{@age}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
  c = Cat.new(&amp;quot;Kitty Kat&amp;quot;,5)&lt;br /&gt;
  puts &amp;quot;Before Serialization&amp;quot;&lt;br /&gt;
  puts c&lt;br /&gt;
  #puts d&lt;br /&gt;
  serialize_cat= Marshal.dump(c) #dumps the serialized cat object into serialize_cat&lt;br /&gt;
  puts &amp;quot;\nAfter Serialization:\n #{serialize_cat}&amp;quot;&lt;br /&gt;
  deserialize_cat= Marshal::load(serialize_cat) #deserializes the cat object and loads it back into deserialize_cat&lt;br /&gt;
  puts &amp;quot;\nAfter Deserialization\n #{deserialize_cat}&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   Before Serialization&lt;br /&gt;
   In Cat C: Kitty Kat 	 5&lt;br /&gt;
   After Serialization:&lt;br /&gt;
   oCat:&lt;br /&gt;
   @nameI&amp;quot;Kitty Kat:ET:	@agei&lt;br /&gt;
   After Deserialization&lt;br /&gt;
   In Cat C: Kitty Kat 	 5&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====='''Saving Marshal data into a file'''=====&lt;br /&gt;
&lt;br /&gt;
Similar to YAML, Marshal can also be used to dump data into a file. The above example showing the serialization using YAML::dump can be written using marshal as shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    f = File.open( 'first.yml', 'w' )&lt;br /&gt;
    Marshal.dump( [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;], f )&lt;br /&gt;
    f.close	&lt;br /&gt;
    File.open( 'first.yml' ){ |f|	&lt;br /&gt;
    $arr= Marshal.load(f)&lt;br /&gt;
    }	&lt;br /&gt;
    p( $arr )&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;]&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice that in the above example there is no &amp;quot;require&amp;quot; statement as opposed to the earlier example of writing serialized data into files using YAML. This is because unlike YAML, marshal is built-in Ruby and  no external libraries is required in order to use its functionality.&lt;br /&gt;
&lt;br /&gt;
====='''Custom serialization using Marshal'''=====&lt;br /&gt;
&lt;br /&gt;
Marshal can also be used to custom serialize an object i.e. it provides an option to omit the variables of an object that are not required in the serialized data. The following program shows the use of marshal_dump method for achieving custom serialization.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
      class First&lt;br /&gt;
      def initialize(name, age, country)&lt;br /&gt;
         @name = name&lt;br /&gt;
         @age = age&lt;br /&gt;
         @country=country&lt;br /&gt;
      end&lt;br /&gt;
      def to_s&lt;br /&gt;
            &amp;quot;In First: #{@name}, #{@age}, #{@country}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
      def marshal_dump&lt;br /&gt;
            [@name,@age]  #@country will not be serialized&lt;br /&gt;
      end&lt;br /&gt;
      def marshal_load(data)&lt;br /&gt;
       @name=data[0]&lt;br /&gt;
       @age=data[1]&lt;br /&gt;
       @country=&amp;quot;United States of America&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
      end&lt;br /&gt;
 &lt;br /&gt;
      x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
      puts x&lt;br /&gt;
  &lt;br /&gt;
      marshal_data = Marshal.dump( x ) &lt;br /&gt;
      y = Marshal.load( marshal_data ) &lt;br /&gt;
      p( y.to_s )&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    In First: Tom, 25, USA&lt;br /&gt;
    &amp;quot;In First: Tom, 25, United States of America&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== '''Serialization Performance in Ruby''' ==&lt;br /&gt;
The different serialization formats described above differ in the efficiency at which they can serialize and deserialize data and thus while serializing large amount of data their efficiency is taken into account. The .report[http://www.ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html] method in Ruby can be used to evaluate the performance of these serialization patterns. Marshal, as it handles data in binary format, is the most efficient form of serialization in Ruby. The following example compares the performance of marshal, JSON and YAML format.[http://www.pauldix.net/2008/08/serializing-dat.html]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   require 'benchmark'&lt;br /&gt;
   require 'rubygems'&lt;br /&gt;
   require 'json'&lt;br /&gt;
   require 'yaml'&lt;br /&gt;
   include Benchmark&lt;br /&gt;
   class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
      @name=name&lt;br /&gt;
      @age=age&lt;br /&gt;
      @country=country&lt;br /&gt;
     end&lt;br /&gt;
   end  &lt;br /&gt;
    x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
    puts &amp;quot;            User     System     Total      Real&amp;quot;&lt;br /&gt;
   benchmark do |t|&lt;br /&gt;
    print &amp;quot;Marshal:&amp;quot;&lt;br /&gt;
    t.report{1000.times do; Marshal.load(Marshal.dump(x));end}&lt;br /&gt;
    print &amp;quot;JSON:   &amp;quot;&lt;br /&gt;
    t.report{1000.times do; JSON.load(JSON.dump(x));end}&lt;br /&gt;
    print &amp;quot;YAML:   &amp;quot;&lt;br /&gt;
    t.report{1000.times do; YAML.load(YAML.dump(x));end}&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
              User     System     Total      Real&lt;br /&gt;
  Marshal:   0.010000   0.000000   0.010000 (  0.015408)&lt;br /&gt;
  JSON:      0.020000   0.010000   0.030000 (  0.022462)&lt;br /&gt;
  YAML:      0.460000   0.010000   0.470000 (  0.472149)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output of the above example shows that, given large chunks of data, marshal is comparatively more efficient format for serialization in Ruby.&lt;br /&gt;
&lt;br /&gt;
== Serialization in other Object-Oriented Languages ==&lt;br /&gt;
&lt;br /&gt;
===Comparison===&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Sl.No !! Ruby !! Java !! .Net Framework !! C++&lt;br /&gt;
|-&lt;br /&gt;
| 1 || Ruby provides a built in module called 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 || Although, there is no built in support for serialization in C++, it can be achieved by using Boost libraries[http://www.boost.org/doc/libs/1_36_0/libs/serialization/example/demo.cpp]&lt;br /&gt;
|-&lt;br /&gt;
| 2 || The built in module of Ruby (Marshal) does not support platform independence, however, it can be achieved by using external libraries like YAML and JSON || Similarly, Java's built in serialization is also not platform independent and in order to use serialization in Java across Ruby platform, jruby library should be used.  || .Net used Remoting technology to make it platform independent. || Serialization using the Boost libraries is not platform independent.&lt;br /&gt;
|-&lt;br /&gt;
| 3 || YAML provides a method [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/syck/rdoc/Object.html (to_yaml_properties)] which can be used to select the variables who's value is need to be serialized. With Marshal, we need to write a method named marshal_dump defining the variables of an object that has to be serialized. || Provides an option for serializing only the required 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 data 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 || Serialization using the Boost libraries is custom and thus the user can specify the part of the objects to be serialized.&lt;br /&gt;
|-&lt;br /&gt;
| 4 || Bindings, procedure objects, singleton objects, instances of IO objects and interfaces can not be serialized. Serializing these objects throws TypeError exceptions. || Thread, OutputStream and its subclasses, and Socket are not serializable in Java.[http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html] || Objects like DataRow are non serializable in .NET. Whether an object is serializable or not can be checked using the [http://msdn.microsoft.com/en-us/library/system.type.isserializable(v=vs.110).aspx?cs-save-lang=1&amp;amp;cs-lang=cpp#code-snippet-1 Type.IsSerializable] || There are no such objects in C++.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===Serialization in Java===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 import java.io.FileInputStream;&lt;br /&gt;
 import java.io.FileOutputStream;&lt;br /&gt;
 import java.io.IOException;&lt;br /&gt;
 import java.io.ObjectInputStream;&lt;br /&gt;
 import java.io.ObjectOutputStream;&lt;br /&gt;
 import java.io.Serializable;&lt;br /&gt;
           &lt;br /&gt;
 public class First implements Serializable {&lt;br /&gt;
 &lt;br /&gt;
 private static final long serialVersionUID = 1L;&lt;br /&gt;
 public String name;&lt;br /&gt;
 public int age;&lt;br /&gt;
 public String country;&lt;br /&gt;
 &lt;br /&gt;
 private static void serialize(First f, String path) throws IOException {&lt;br /&gt;
  &lt;br /&gt;
       FileOutputStream fos = new FileOutputStream(path);&lt;br /&gt;
       ObjectOutputStream oos = new ObjectOutputStream(fos);&lt;br /&gt;
    &lt;br /&gt;
       // Serializing&lt;br /&gt;
       oos.writeObject(f);&lt;br /&gt;
       oos.close();&lt;br /&gt;
       fos.close();&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
 private static First deserialize(String path) throws IOException,&lt;br /&gt;
  		ClassNotFoundException {&lt;br /&gt;
  	// tom.ser will deserialize in object f&lt;br /&gt;
  	First f = null;&lt;br /&gt;
  	FileInputStream fin = new FileInputStream(path);&lt;br /&gt;
  	ObjectInputStream oin = new ObjectInputStream(fin);&lt;br /&gt;
  	// DeSerializing and Casting to Class First&lt;br /&gt;
  	f = (First) oin.readObject();&lt;br /&gt;
  	oin.close();&lt;br /&gt;
  	fin.close();&lt;br /&gt;
  	return f;&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  public static void main(String[] args) {&lt;br /&gt;
  	try {&lt;br /&gt;
  		// Creating object for Tom&lt;br /&gt;
  		First f1 = new First();&lt;br /&gt;
  		f1.name = &amp;quot;Tom&amp;quot;;&lt;br /&gt;
  		f1.age = 25;&lt;br /&gt;
  		f1.country = &amp;quot;USA&amp;quot;;&lt;br /&gt;
  		First f2 = null;&lt;br /&gt;
  		long start = System.currentTimeMillis();&lt;br /&gt;
  		for (int i = 0; i &amp;lt; 1000; i++){&lt;br /&gt;
  		// serializing&lt;br /&gt;
  		serialize(f1, &amp;quot;/path/to/files/&amp;quot; + i + &amp;quot;.ser&amp;quot;);&lt;br /&gt;
  		// deserializing&lt;br /&gt;
  		f2 = deserialize(&amp;quot;/path/to/files&amp;quot; + i + &amp;quot;.ser&amp;quot;);&lt;br /&gt;
  		}&lt;br /&gt;
  		long end = System.currentTimeMillis();&lt;br /&gt;
  		long totalDuration = end - start;&lt;br /&gt;
  		System.out.println(&amp;quot;Serialization/Deserialization for 1000 Iterations duration: &amp;quot; + totalDuration + &amp;quot; Milliseconds&amp;quot;);&lt;br /&gt;
  		// Printing deserialized object&lt;br /&gt;
  		System.out.println(&amp;quot;Deserialized =&amp;gt; Name: &amp;quot; + f2.name + &amp;quot;, Age: &amp;quot; + f2.age + &amp;quot;, Country: &amp;quot; + f2.country);&lt;br /&gt;
  	} catch (IOException e) {&lt;br /&gt;
  		e.printStackTrace();&lt;br /&gt;
  	} catch (ClassNotFoundException e) {&lt;br /&gt;
  		e.printStackTrace();&lt;br /&gt;
  	}&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== See Also==&lt;br /&gt;
&lt;br /&gt;
1. [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/json/rdoc/JSON.html Serialization in Ruby JSON]&lt;br /&gt;
&lt;br /&gt;
2. [http://api.rubyonrails.org/classes/ActiveModel/Serialization.html Serialization in Rails]&lt;br /&gt;
&lt;br /&gt;
3. [http://gregmoreno.wordpress.com/2011/01/27/preventing-model-explosion-via-rails-serialization/ Article on Rails Serialization]&lt;br /&gt;
&lt;br /&gt;
4. [http://en.wikipedia.org/wiki/Google_Protocol_Buffers Protocol Buffers]&lt;br /&gt;
&lt;br /&gt;
5. [https://rubygems.org/gems/ruby-protocol-buffers Ruby Protocol Buffers Gem]&lt;br /&gt;
&lt;br /&gt;
6. [http://en.wikipedia.org/wiki/Apache_Avro Avro]&lt;br /&gt;
&lt;br /&gt;
7. [http://rubygems.org/gems/avro Ruby Avro Gem]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
1. [http://en.wikipedia.org/wiki/Serialization Serilization in General]&lt;br /&gt;
&lt;br /&gt;
2. [http://en.wikipedia.org/wiki/Marshalling_%28computer_science%29 Marshaling]&lt;br /&gt;
&lt;br /&gt;
3. [http://techbrahmana.blogspot.com/2012/03/rails-cookie-handling-serialization-and.html Rails Cookie Handling]&lt;br /&gt;
&lt;br /&gt;
4. [http://yaml.org/ YAML]&lt;br /&gt;
&lt;br /&gt;
5. [http://www.ruby-doc.org/core-2.1.0/Marshal.html Marshal module]&lt;br /&gt;
&lt;br /&gt;
6. [http://json.org/ JSON]&lt;br /&gt;
&lt;br /&gt;
7. [http://rubygems.org/ Ruby Gems]&lt;br /&gt;
&lt;br /&gt;
8. [http://ruby-doc.org/core-2.0.0/ARGF.html# Ruby ARGF Documentation]&lt;br /&gt;
&lt;br /&gt;
9. [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html Ruby Documentation for Benchmark Gem]&lt;br /&gt;
&lt;br /&gt;
10. [http://www.pauldix.net/2008/08/serializing-dat.html Serialization Performance Comparison]&lt;br /&gt;
&lt;br /&gt;
11. [http://www.boost.org/doc/libs/1_36_0/libs/serialization/example/demo.cpp Boost Libraries]&lt;br /&gt;
&lt;br /&gt;
12. [http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html Java Serialization]&lt;br /&gt;
&lt;br /&gt;
13. [http://www.w3schools.com/json/ JSON Tutorial]&lt;br /&gt;
&lt;br /&gt;
14. [http://www.skorks.com/2010/04/serializing-and-deserializing-objects-with-ruby/ Serializing and De-serializing in Ruby]&lt;br /&gt;
&lt;br /&gt;
15. [http://www.codeproject.com/Articles/33296/Serialization-and-De-serialization Serialization and De-serialization]&lt;br /&gt;
&lt;br /&gt;
16. [http://msdn.microsoft.com/en-us/library/182eeyhh.aspx XML Serialization]&lt;br /&gt;
&lt;br /&gt;
17. [http://www.tutorialspoint.com/java/java_serialization.htm Java Serialization Tutorial]&lt;br /&gt;
&lt;br /&gt;
18. [http://www.sapphiresteel.com/ruby-programming/The-Book-Of-Ruby The Book of Ruby]&lt;br /&gt;
&lt;br /&gt;
19. [http://www.codeproject.com/Articles/20962/Introducing-Serialization-in-NET Serialization in .Net]&lt;br /&gt;
&lt;br /&gt;
20. [http://ruby.about.com/od/advancedruby/ss/Serialization-In-Ruby-Yaml.html Serialization in Ruby YAML]&lt;br /&gt;
&lt;br /&gt;
21. [http://ruby.about.com/od/tasks/a/The-Json-Gem.html Installing JSON gem]&lt;br /&gt;
&lt;br /&gt;
22. [http://www.ruby-doc.org/stdlib-2.0.0/libdoc/json/rdoc/JSON.html Serialization in Ruby JSON]&lt;/div&gt;</summary>
		<author><name>Mmehta2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1d_mm&amp;diff=83423</id>
		<title>CSC/ECE 517 Spring 2014/ch1a 1d mm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1d_mm&amp;diff=83423"/>
		<updated>2014-02-18T03:01:31Z</updated>

		<summary type="html">&lt;p&gt;Mmehta2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page discusses Serialization, focusing on Ruby programming language. It describes different methods of Serialization in Ruby. In addition, it also compares serialization in different Object Oriented languages.&lt;br /&gt;
&lt;br /&gt;
== '''Introduction''' ==&lt;br /&gt;
&lt;br /&gt;
==='''Serialization'''===&lt;br /&gt;
&lt;br /&gt;
'''Serialization'''[http://en.wikipedia.org/wiki/Serialization] is a process of converting a data structure or an object into a stream of bytes or string to facilitate storage in memory, file (persistence storage) or transmission over a network. The process of Serialization is also referred to as ''Marshalling''[http://en.wikipedia.org/wiki/Marshalling_%28computer_science%29]. 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.&lt;br /&gt;
&lt;br /&gt;
==='''De-serialization'''===&lt;br /&gt;
&lt;br /&gt;
'''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''.&lt;br /&gt;
&lt;br /&gt;
The process of Serialization/De-serialization is not a replacement for database systems. Database Systems are used for their relational and transactional semantics.&lt;br /&gt;
&lt;br /&gt;
==='''Basic Advantages of Serialization'''===&lt;br /&gt;
&lt;br /&gt;
1. Communication between two or more processes on same machine. Object state can be saved and shared in a persistent or in-memory store.&lt;br /&gt;
&lt;br /&gt;
2. Communication between processes on different machines. Serialization facilitates the transmission of an object through a network.&lt;br /&gt;
&lt;br /&gt;
3. Creating a clone of an object. &lt;br /&gt;
&lt;br /&gt;
4. Cross-platform compatibility. Object can be serialized in a common format that is understood by multiple platforms. For example, JSON[http://www.json.org/] and XML.&lt;br /&gt;
&lt;br /&gt;
==='''Practical Applications for Serialization'''===&lt;br /&gt;
&lt;br /&gt;
1. [http://docs.oracle.com/cd/E13222_01/wls/docs81/webapp/sessions.html#139726 HTTP Session] Replication by sharing session objects across web servers for load balancing and handling failover scenarios.&lt;br /&gt;
&lt;br /&gt;
2. Serialization facilitates communication in [http://en.wikipedia.org/wiki/Java_remote_method_invocation Remote Method Invocation] or [http://en.wikipedia.org/wiki/Remote_procedure_call Remote procedure calls]&lt;br /&gt;
&lt;br /&gt;
3. Rails Cookie Handling[http://techbrahmana.blogspot.com/2012/03/rails-cookie-handling-serialization-and.html]. Cookies are stored marshalled/unmarshalled to and from client machines.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== '''Serialization in Ruby:''' ==&lt;br /&gt;
&lt;br /&gt;
Ruby provides serialization capabilities through its module, [http://www.ruby-doc.org/core-2.0.0/Marshal.html Marshal]. There are also other libraries like YAML[http://yaml.org/] and JSON which can be used in Ruby to generate serialized objects for purposes like platform independence and human readable formats.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:SerializationChart.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== '''Types of Serialization''' ==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
=== Converting Ruby Objects in Human Readable Formats ===&lt;br /&gt;
----&lt;br /&gt;
The conversion of Ruby objects into YAML and JSON formats are explained below.&lt;br /&gt;
&lt;br /&gt;
==== Converting Ruby Objects to YAML format ====&lt;br /&gt;
&lt;br /&gt;
YAML 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 binary format. 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. In order to use it in Ruby, the yaml.rb file is required to be loaded which provides methods for converting objects into yaml format and creating .yml files.&lt;br /&gt;
&lt;br /&gt;
=====Serialization using YAML=====&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;nowiki&amp;gt; #Serialization using YAML's to_yaml method&lt;br /&gt;
 &lt;br /&gt;
  require &amp;quot;yaml&amp;quot;&lt;br /&gt;
     class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
	@name = name&lt;br /&gt;
	@age = age&lt;br /&gt;
	@country=country&lt;br /&gt;
     end&lt;br /&gt;
      def to_s&lt;br /&gt;
	&amp;quot;In First:\n#{@name}, #{@age}, #{@country}\n&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
  x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
  puts x&lt;br /&gt;
  puts x.to_yaml&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   In First:&lt;br /&gt;
   Tom, 25, USA&lt;br /&gt;
   --- !ruby/object:First&lt;br /&gt;
   name: Tom&lt;br /&gt;
   age: 25&lt;br /&gt;
   country: USA&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The above code displays the object x, first as a string and then in the yaml format.&lt;br /&gt;
&lt;br /&gt;
====='''Saving YAML data into a file:'''=====&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt; # Serialization using YAML::dump&lt;br /&gt;
&lt;br /&gt;
require 'yaml'&lt;br /&gt;
&lt;br /&gt;
f = File.open( 'first.yml', 'w' )&lt;br /&gt;
YAML.dump( [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;], f )&lt;br /&gt;
f.close&lt;br /&gt;
		&lt;br /&gt;
File.open( 'first.yml' ){ |f|	&lt;br /&gt;
    $arr= YAML.load(f)&lt;br /&gt;
}	&lt;br /&gt;
&lt;br /&gt;
p( $arr )&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;]&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The dump function can be used to serialize the data and save it into a file in YAML format. As shown in the above example the data in YAML format can be de-serialized using the load function.&lt;br /&gt;
&lt;br /&gt;
YAML libraries also provides an option of selecting only those variables of the object that are needed to be serialized. This is done using the to_yaml_properties method as shown in the below example.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=====Custom Serialization using YAML=====&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;nowiki&amp;gt;&lt;br /&gt;
    require &amp;quot;yaml&amp;quot;&lt;br /&gt;
     class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
	@name = name&lt;br /&gt;
	@age = age&lt;br /&gt;
	@country=country&lt;br /&gt;
     end&lt;br /&gt;
     def to_s&lt;br /&gt;
	&amp;quot;In First:\n#{@name}, #{@age}, #{@country}\n&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
      def to_yaml_properties&lt;br /&gt;
	   [&amp;quot;@name&amp;quot;,&amp;quot;@age&amp;quot;]  #@country will not be serialized&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
  x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
  puts x&lt;br /&gt;
  puts x.to_yaml&lt;br /&gt;
  &amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   In First:&lt;br /&gt;
   Tom, 25, USA&lt;br /&gt;
   --- !ruby/object:First&lt;br /&gt;
   name: Tom&lt;br /&gt;
   age: 25&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Converting Ruby Objects to JSON format: ====&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
JSON 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. Ruby 1.8.7 distribution is not bundled with json gem. However, in Ruby 1.9.2, the json gem is bundled with the core Ruby distribution.&lt;br /&gt;
The JSON library can be installed using Ruby Gems[http://rubygems.org/] as shown below:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;# gem install json&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A JSON string for serialization can be created by using the JSON.generate method:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
        require 'json'&lt;br /&gt;
        my_hash = {:Welcome =&amp;gt; &amp;quot;Ruby&amp;quot;}&lt;br /&gt;
        puts JSON.generate(my_hash) =&amp;gt; &amp;quot;{\&amp;quot;WELCOME\&amp;quot;:\&amp;quot;RUBY\&amp;quot;}&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
'''Output:'''&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;{&amp;quot;{\&amp;quot;Welcome\&amp;quot;:\&amp;quot;Ruby\&amp;quot;}&amp;quot;=&amp;gt;&amp;quot;{\&amp;quot;WELCOME\&amp;quot;:\&amp;quot;RUBY\&amp;quot;}&amp;quot;}&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A JSON string received from another program can be parsed by using JSON.parse&lt;br /&gt;
Ruby thus converts String to Hash.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
        require 'json'&lt;br /&gt;
        my_hash = JSON.parse('{&amp;quot;Welcome&amp;quot;: &amp;quot;Ruby&amp;quot;}')&lt;br /&gt;
        puts my_hash[&amp;quot;Welcome&amp;quot;] =&amp;gt; &amp;quot;Ruby&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Converting Ruby Objects to Binary Formats ===&lt;br /&gt;
----&lt;br /&gt;
Binary Serialization is another form of serialization in Ruby which is not in human readable form. 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. Binary Serialization  is done using Marshal which is built into Ruby and the code for it is written in Ruby's Marshal module(marshal.c) and thus no additional files are required in order to use it. The _dump and _load methods defined in marshal are used for serialization. Using marshal module the following types of objects can not be serialized: bindings, procedure objects, singleton objects, instances of IO objects and interfaces.&lt;br /&gt;
&lt;br /&gt;
Since the Binary Serialized data is not in human readable form, the following guidelines need to be followed.&lt;br /&gt;
     1.Use print instead of puts[http://ruby-doc.org/core-2.0.0/ARGF.html#] when serialized objects are written to a file in order to avoid new line characters to be written &lt;br /&gt;
       in the file.&lt;br /&gt;
     &lt;br /&gt;
     2.Use a record separator in order to differentiate between two objects.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====='''Binary Serialization Example:'''=====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    class Animal&lt;br /&gt;
      def initialize  name, age&lt;br /&gt;
      @name = name&lt;br /&gt;
      @age=age&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
   class Cat &amp;lt; Animal&lt;br /&gt;
    def to_s&lt;br /&gt;
     &amp;quot;In Cat C: #{@name} \t #{@age}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
  c = Cat.new(&amp;quot;Kitty Kat&amp;quot;,5)&lt;br /&gt;
  puts &amp;quot;Before Serialization&amp;quot;&lt;br /&gt;
  puts c&lt;br /&gt;
  #puts d&lt;br /&gt;
  serialize_cat= Marshal.dump(c) #dumps the serialized cat object into serialize_cat&lt;br /&gt;
  puts &amp;quot;\nAfter Serialization:\n #{serialize_cat}&amp;quot;&lt;br /&gt;
  deserialize_cat= Marshal::load(serialize_cat) #deserializes the cat object and loads it back into deserialize_cat&lt;br /&gt;
  puts &amp;quot;\nAfter Deserialization\n #{deserialize_cat}&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   Before Serialization&lt;br /&gt;
   In Cat C: Kitty Kat 	 5&lt;br /&gt;
   After Serialization:&lt;br /&gt;
   oCat:&lt;br /&gt;
   @nameI&amp;quot;Kitty Kat:ET:	@agei&lt;br /&gt;
   After Deserialization&lt;br /&gt;
   In Cat C: Kitty Kat 	 5&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====='''Saving Marshal data into a file'''=====&lt;br /&gt;
&lt;br /&gt;
Similar to YAML, Marshal can also be used to dump data into a file. The above example showing the serialization using YAML::dump can be written using marshal as shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    f = File.open( 'first.yml', 'w' )&lt;br /&gt;
    Marshal.dump( [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;], f )&lt;br /&gt;
    f.close	&lt;br /&gt;
    File.open( 'first.yml' ){ |f|	&lt;br /&gt;
    $arr= Marshal.load(f)&lt;br /&gt;
    }	&lt;br /&gt;
    p( $arr )&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;]&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice that in the above example there is no &amp;quot;require&amp;quot; statement as opposed to the earlier example of writing serialized data into files using YAML. This is because unlike YAML, marshal is built-in Ruby and  no external libraries is required in order to use its functionality.&lt;br /&gt;
&lt;br /&gt;
====='''Custom serialization using Marshal'''=====&lt;br /&gt;
&lt;br /&gt;
Marshal can also be used to custom serialize an object i.e. it provides an option to omit the variables of an object that are not required in the serialized data. The following program shows the use of marshal_dump method for achieving custom serialization.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
      class First&lt;br /&gt;
      def initialize(name, age, country)&lt;br /&gt;
         @name = name&lt;br /&gt;
         @age = age&lt;br /&gt;
         @country=country&lt;br /&gt;
      end&lt;br /&gt;
      def to_s&lt;br /&gt;
            &amp;quot;In First: #{@name}, #{@age}, #{@country}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
      def marshal_dump&lt;br /&gt;
            [@name,@age]  #@country will not be serialized&lt;br /&gt;
      end&lt;br /&gt;
      def marshal_load(data)&lt;br /&gt;
       @name=data[0]&lt;br /&gt;
       @age=data[1]&lt;br /&gt;
       @country=&amp;quot;United States of America&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
      end&lt;br /&gt;
 &lt;br /&gt;
      x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
      puts x&lt;br /&gt;
  &lt;br /&gt;
      marshal_data = Marshal.dump( x ) &lt;br /&gt;
      y = Marshal.load( marshal_data ) &lt;br /&gt;
      p( y.to_s )&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    In First: Tom, 25, USA&lt;br /&gt;
    &amp;quot;In First: Tom, 25, United States of America&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== '''Serialization Performance in Ruby''' ==&lt;br /&gt;
The different serialization formats described above differ in the efficiency at which they can serialize and deserialize data and thus while serializing large amount of data their efficiency is taken into account. The .report[http://www.ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html] method in Ruby can be used to evaluate the performance of these serialization patterns. Marshal, as it handles data in binary format, is the most efficient form of serialization in Ruby. The following example compares the performance of marshal, JSON and YAML format.[http://www.pauldix.net/2008/08/serializing-dat.html]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   require 'benchmark'&lt;br /&gt;
   require 'rubygems'&lt;br /&gt;
   require 'json'&lt;br /&gt;
   require 'yaml'&lt;br /&gt;
   include Benchmark&lt;br /&gt;
   class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
      @name=name&lt;br /&gt;
      @age=age&lt;br /&gt;
      @country=country&lt;br /&gt;
     end&lt;br /&gt;
   end  &lt;br /&gt;
    x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
    puts &amp;quot;            User     System     Total      Real&amp;quot;&lt;br /&gt;
   benchmark do |t|&lt;br /&gt;
    print &amp;quot;Marshal:&amp;quot;&lt;br /&gt;
    t.report{1000.times do; Marshal.load(Marshal.dump(x));end}&lt;br /&gt;
    print &amp;quot;JSON:   &amp;quot;&lt;br /&gt;
    t.report{1000.times do; JSON.load(JSON.dump(x));end}&lt;br /&gt;
    print &amp;quot;YAML:   &amp;quot;&lt;br /&gt;
    t.report{1000.times do; YAML.load(YAML.dump(x));end}&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
              User     System     Total      Real&lt;br /&gt;
  Marshal:   0.010000   0.000000   0.010000 (  0.015408)&lt;br /&gt;
  JSON:      0.020000   0.010000   0.030000 (  0.022462)&lt;br /&gt;
  YAML:      0.460000   0.010000   0.470000 (  0.472149)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output of the above example shows that, given large chunks of data, marshal is comparatively more efficient format for serialization in Ruby.&lt;br /&gt;
&lt;br /&gt;
== Serialization in other Object-Oriented Languages ==&lt;br /&gt;
&lt;br /&gt;
===Comparison===&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Sl.No !! Ruby !! Java !! .Net Framework !! C++&lt;br /&gt;
|-&lt;br /&gt;
| 1 || Ruby provides a built in module called 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 || Although, there is no built in support for serialization in C++, it can be achieved by using Boost libraries[http://www.boost.org/doc/libs/1_36_0/libs/serialization/example/demo.cpp]&lt;br /&gt;
|-&lt;br /&gt;
| 2 || The built in module of Ruby (Marshal) does not support platform independence, however, it can be achieved by using external libraries like YAML and JSON || Similarly, Java's built in serialization is also not platform independent and in order to use serialization in Java across Ruby platform, jruby library should be used.  || .Net used Remoting technology to make it platform independent. || Serialization using the Boost libraries is not platform independent.&lt;br /&gt;
|-&lt;br /&gt;
| 3 || YAML provides a method [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/syck/rdoc/Object.html (to_yaml_properties)] which can be used to select the variables who's value is need to be serialized. With Marshal, we need to write a method named marshal_dump defining the variables of an object that has to be serialized. || Provides an option for serializing only the required 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 data 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 || Serialization using the Boost libraries is custom and thus the user can specify the part of the objects to be serialized.&lt;br /&gt;
|-&lt;br /&gt;
| 4 || Bindings, procedure objects, singleton objects, instances of IO objects and interfaces can not be serialized. Serializing these objects throws TypeError exceptions. || Thread, OutputStream and its subclasses, and Socket are not serializable in Java.[http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html] || Objects like DataRow are non serializable in .NET. Whether an object is serializable or not can be checked using the [http://msdn.microsoft.com/en-us/library/system.type.isserializable(v=vs.110).aspx?cs-save-lang=1&amp;amp;cs-lang=cpp#code-snippet-1 Type.IsSerializable] || There are no such objects in C++.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===Serialization in Java===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== See Also==&lt;br /&gt;
&lt;br /&gt;
1. [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/json/rdoc/JSON.html Serialization in Ruby JSON]&lt;br /&gt;
&lt;br /&gt;
2. [http://api.rubyonrails.org/classes/ActiveModel/Serialization.html Serialization in Rails]&lt;br /&gt;
&lt;br /&gt;
3. [http://gregmoreno.wordpress.com/2011/01/27/preventing-model-explosion-via-rails-serialization/ Article on Rails Serialization]&lt;br /&gt;
&lt;br /&gt;
4. [http://en.wikipedia.org/wiki/Google_Protocol_Buffers Protocol Buffers]&lt;br /&gt;
&lt;br /&gt;
5. [https://rubygems.org/gems/ruby-protocol-buffers Ruby Protocol Buffers Gem]&lt;br /&gt;
&lt;br /&gt;
6. [http://en.wikipedia.org/wiki/Apache_Avro Avro]&lt;br /&gt;
&lt;br /&gt;
7. [http://rubygems.org/gems/avro Ruby Avro Gem]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
1. [http://en.wikipedia.org/wiki/Serialization Serilization in General]&lt;br /&gt;
&lt;br /&gt;
2. [http://en.wikipedia.org/wiki/Marshalling_%28computer_science%29 Marshaling]&lt;br /&gt;
&lt;br /&gt;
3. [http://techbrahmana.blogspot.com/2012/03/rails-cookie-handling-serialization-and.html Rails Cookie Handling]&lt;br /&gt;
&lt;br /&gt;
4. [http://yaml.org/ YAML]&lt;br /&gt;
&lt;br /&gt;
5. [http://www.ruby-doc.org/core-2.1.0/Marshal.html Marshal module]&lt;br /&gt;
&lt;br /&gt;
6. [http://json.org/ JSON]&lt;br /&gt;
&lt;br /&gt;
7. [http://rubygems.org/ Ruby Gems]&lt;br /&gt;
&lt;br /&gt;
8. [http://ruby-doc.org/core-2.0.0/ARGF.html# Ruby ARGF Documentation]&lt;br /&gt;
&lt;br /&gt;
9. [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html Ruby Documentation for Benchmark Gem]&lt;br /&gt;
&lt;br /&gt;
10. [http://www.pauldix.net/2008/08/serializing-dat.html Serialization Performance Comparison]&lt;br /&gt;
&lt;br /&gt;
11. [http://www.boost.org/doc/libs/1_36_0/libs/serialization/example/demo.cpp Boost Libraries]&lt;br /&gt;
&lt;br /&gt;
12. [http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html Java Serialization]&lt;br /&gt;
&lt;br /&gt;
13. [http://www.w3schools.com/json/ JSON Tutorial]&lt;br /&gt;
&lt;br /&gt;
14. [http://www.skorks.com/2010/04/serializing-and-deserializing-objects-with-ruby/ Serializing and De-serializing in Ruby]&lt;br /&gt;
&lt;br /&gt;
15. [http://www.codeproject.com/Articles/33296/Serialization-and-De-serialization Serialization and De-serialization]&lt;br /&gt;
&lt;br /&gt;
16. [http://msdn.microsoft.com/en-us/library/182eeyhh.aspx XML Serialization]&lt;br /&gt;
&lt;br /&gt;
17. [http://www.tutorialspoint.com/java/java_serialization.htm Java Serialization Tutorial]&lt;br /&gt;
&lt;br /&gt;
18. [http://www.sapphiresteel.com/ruby-programming/The-Book-Of-Ruby The Book of Ruby]&lt;br /&gt;
&lt;br /&gt;
19. [http://www.codeproject.com/Articles/20962/Introducing-Serialization-in-NET Serialization in .Net]&lt;br /&gt;
&lt;br /&gt;
20. [http://ruby.about.com/od/advancedruby/ss/Serialization-In-Ruby-Yaml.html Serialization in Ruby YAML]&lt;br /&gt;
&lt;br /&gt;
21. [http://ruby.about.com/od/tasks/a/The-Json-Gem.html Installing JSON gem]&lt;br /&gt;
&lt;br /&gt;
22. [http://www.ruby-doc.org/stdlib-2.0.0/libdoc/json/rdoc/JSON.html Serialization in Ruby JSON]&lt;/div&gt;</summary>
		<author><name>Mmehta2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1d_mm&amp;diff=83422</id>
		<title>CSC/ECE 517 Spring 2014/ch1a 1d mm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1d_mm&amp;diff=83422"/>
		<updated>2014-02-18T02:53:57Z</updated>

		<summary type="html">&lt;p&gt;Mmehta2: /* De-serialization */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page discusses Serialization, focusing on Ruby programming language. It describes different methods of Serialization in Ruby. In addition, it also compares serialization in different Object Oriented languages.&lt;br /&gt;
&lt;br /&gt;
== '''Introduction''' ==&lt;br /&gt;
&lt;br /&gt;
==='''Serialization'''===&lt;br /&gt;
&lt;br /&gt;
'''Serialization'''[http://en.wikipedia.org/wiki/Serialization] is a process of converting a data structure or an object into a stream of bytes or string to facilitate storage in memory, file (persistence storage) or transmission over a network. The process of Serialization is also referred to as ''Marshalling''[http://en.wikipedia.org/wiki/Marshalling_%28computer_science%29]. 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.&lt;br /&gt;
&lt;br /&gt;
==='''De-serialization'''===&lt;br /&gt;
&lt;br /&gt;
'''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''.&lt;br /&gt;
&lt;br /&gt;
The process of Serialization/De-serialization is not a replacement for database systems. Database Systems are used for their relational and transactional semantics.&lt;br /&gt;
&lt;br /&gt;
==='''Basic Advantages of Serialization'''===&lt;br /&gt;
&lt;br /&gt;
1. Communication between two or more processes on same machine. Object state can be saved and shared in a persistent or in-memory store.&lt;br /&gt;
&lt;br /&gt;
2. Communication between processes on different machines. Serialization facilitates the transmission of an object through a network.&lt;br /&gt;
&lt;br /&gt;
3. Creating a clone of an object. &lt;br /&gt;
&lt;br /&gt;
4. Cross-platform compatibility. Object can be serialized in a common format that is understood by multiple platforms. For example, JSON[http://www.json.org/] and XML.&lt;br /&gt;
&lt;br /&gt;
==='''Practical Applications for Serialization'''===&lt;br /&gt;
&lt;br /&gt;
1. [http://docs.oracle.com/cd/E13222_01/wls/docs81/webapp/sessions.html#139726 HTTP Session] Replication by sharing session objects across web servers for load balancing and handling failover scenarios.&lt;br /&gt;
&lt;br /&gt;
2. Serialization facilitates communication in [http://en.wikipedia.org/wiki/Java_remote_method_invocation Remote Method Invocation] or [http://en.wikipedia.org/wiki/Remote_procedure_call Remote procedure calls]&lt;br /&gt;
&lt;br /&gt;
3. Rails Cookie Handling[http://techbrahmana.blogspot.com/2012/03/rails-cookie-handling-serialization-and.html]. Cookies are stored marshalled/unmarshalled to and from client machines.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== '''Serialization in Ruby:''' ==&lt;br /&gt;
&lt;br /&gt;
Ruby provides serialization capabilities through its module, [http://www.ruby-doc.org/core-2.0.0/Marshal.html Marshal]. There are also other libraries like YAML[http://yaml.org/] and JSON which can be used in Ruby to generate serialized objects for purposes like platform independence and human readable formats.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:SerializationChart.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== '''Types of Serialization''' ==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
=== Converting Ruby Objects in Human Readable Formats ===&lt;br /&gt;
----&lt;br /&gt;
The conversion of Ruby objects into YAML and JSON formats are explained below.&lt;br /&gt;
&lt;br /&gt;
==== Converting Ruby Objects to YAML format ====&lt;br /&gt;
&lt;br /&gt;
YAML 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 binary format. 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. In order to use it in Ruby, the yaml.rb file is required to be loaded which provides methods for converting objects into yaml format and creating .yml files.&lt;br /&gt;
&lt;br /&gt;
=====Serialization using YAML=====&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;nowiki&amp;gt; #Serialization using YAML's to_yaml method&lt;br /&gt;
 &lt;br /&gt;
  require &amp;quot;yaml&amp;quot;&lt;br /&gt;
     class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
	@name = name&lt;br /&gt;
	@age = age&lt;br /&gt;
	@country=country&lt;br /&gt;
     end&lt;br /&gt;
      def to_s&lt;br /&gt;
	&amp;quot;In First:\n#{@name}, #{@age}, #{@country}\n&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
  x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
  puts x&lt;br /&gt;
  puts x.to_yaml&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   In First:&lt;br /&gt;
   Tom, 25, USA&lt;br /&gt;
   --- !ruby/object:First&lt;br /&gt;
   name: Tom&lt;br /&gt;
   age: 25&lt;br /&gt;
   country: USA&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The above code displays the object x, first as a string and then in the yaml format.&lt;br /&gt;
&lt;br /&gt;
====='''Saving YAML data into a file:'''=====&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt; # Serialization using YAML::dump&lt;br /&gt;
&lt;br /&gt;
require 'yaml'&lt;br /&gt;
&lt;br /&gt;
f = File.open( 'first.yml', 'w' )&lt;br /&gt;
YAML.dump( [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;], f )&lt;br /&gt;
f.close&lt;br /&gt;
		&lt;br /&gt;
File.open( 'first.yml' ){ |f|	&lt;br /&gt;
    $arr= YAML.load(f)&lt;br /&gt;
}	&lt;br /&gt;
&lt;br /&gt;
p( $arr )&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;]&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The dump function can be used to serialize the data and save it into a file in YAML format. As shown in the above example the data in YAML format can be de-serialized using the load function.&lt;br /&gt;
&lt;br /&gt;
YAML libraries also provides an option of selecting only those variables of the object that are needed to be serialized. This is done using the to_yaml_properties method as shown in the below example.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=====Custom Serialization using YAML=====&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;nowiki&amp;gt;&lt;br /&gt;
    require &amp;quot;yaml&amp;quot;&lt;br /&gt;
     class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
	@name = name&lt;br /&gt;
	@age = age&lt;br /&gt;
	@country=country&lt;br /&gt;
     end&lt;br /&gt;
     def to_s&lt;br /&gt;
	&amp;quot;In First:\n#{@name}, #{@age}, #{@country}\n&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
      def to_yaml_properties&lt;br /&gt;
	   [&amp;quot;@name&amp;quot;,&amp;quot;@age&amp;quot;]  #@country will not be serialized&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
  x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
  puts x&lt;br /&gt;
  puts x.to_yaml&lt;br /&gt;
  &amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   In First:&lt;br /&gt;
   Tom, 25, USA&lt;br /&gt;
   --- !ruby/object:First&lt;br /&gt;
   name: Tom&lt;br /&gt;
   age: 25&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Converting Ruby Objects to JSON format: ====&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
JSON 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. Ruby 1.8.7 distribution is not bundled with json gem. However, in Ruby 1.9.2, the json gem is bundled with the core Ruby distribution.&lt;br /&gt;
The JSON library can be installed using Ruby Gems[http://rubygems.org/] as shown below:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;# gem install json&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A JSON string for serialization can be created by using the JSON.generate method:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
        require 'json'&lt;br /&gt;
        my_hash = {:Welcome =&amp;gt; &amp;quot;Ruby&amp;quot;}&lt;br /&gt;
        puts JSON.generate(my_hash) =&amp;gt; &amp;quot;{\&amp;quot;WELCOME\&amp;quot;:\&amp;quot;RUBY\&amp;quot;}&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
'''Output:'''&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;{&amp;quot;{\&amp;quot;Welcome\&amp;quot;:\&amp;quot;Ruby\&amp;quot;}&amp;quot;=&amp;gt;&amp;quot;{\&amp;quot;WELCOME\&amp;quot;:\&amp;quot;RUBY\&amp;quot;}&amp;quot;}&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A JSON string received from another program can be parsed by using JSON.parse&lt;br /&gt;
Ruby thus converts String to Hash.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
        require 'json'&lt;br /&gt;
        my_hash = JSON.parse('{&amp;quot;Welcome&amp;quot;: &amp;quot;Ruby&amp;quot;}')&lt;br /&gt;
        puts my_hash[&amp;quot;Welcome&amp;quot;] =&amp;gt; &amp;quot;Ruby&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Converting Ruby Objects to Binary Formats ===&lt;br /&gt;
----&lt;br /&gt;
Binary Serialization is another form of serialization in Ruby which is not in human readable form. 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. Binary Serialization  is done using Marshal which is built into Ruby and the code for it is written in Ruby's Marshal module(marshal.c) and thus no additional files are required in order to use it. The _dump and _load methods defined in marshal are used for serialization. Using marshal module the following types of objects can not be serialized: bindings, procedure objects, singleton objects, instances of IO objects and interfaces.&lt;br /&gt;
&lt;br /&gt;
Since the Binary Serialized data is not in human readable form, the following guidelines need to be followed.&lt;br /&gt;
     1.Use print instead of puts[http://ruby-doc.org/core-2.0.0/ARGF.html#] when serialized objects are written to a file in order to avoid new line characters to be written &lt;br /&gt;
       in the file.&lt;br /&gt;
     &lt;br /&gt;
     2.Use a record separator in order to differentiate between two objects.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====='''Binary Serialization Example:'''=====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    class Animal&lt;br /&gt;
      def initialize  name, age&lt;br /&gt;
      @name = name&lt;br /&gt;
      @age=age&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
   class Cat &amp;lt; Animal&lt;br /&gt;
    def to_s&lt;br /&gt;
     &amp;quot;In Cat C: #{@name} \t #{@age}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
  c = Cat.new(&amp;quot;Kitty Kat&amp;quot;,5)&lt;br /&gt;
  puts &amp;quot;Before Serialization&amp;quot;&lt;br /&gt;
  puts c&lt;br /&gt;
  #puts d&lt;br /&gt;
  serialize_cat= Marshal.dump(c) #dumps the serialized cat object into serialize_cat&lt;br /&gt;
  puts &amp;quot;\nAfter Serialization:\n #{serialize_cat}&amp;quot;&lt;br /&gt;
  deserialize_cat= Marshal::load(serialize_cat) #deserializes the cat object and loads it back into deserialize_cat&lt;br /&gt;
  puts &amp;quot;\nAfter Deserialization\n #{deserialize_cat}&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   Before Serialization&lt;br /&gt;
   In Cat C: Kitty Kat 	 5&lt;br /&gt;
   After Serialization:&lt;br /&gt;
   oCat:&lt;br /&gt;
   @nameI&amp;quot;Kitty Kat:ET:	@agei&lt;br /&gt;
   After Deserialization&lt;br /&gt;
   In Cat C: Kitty Kat 	 5&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====='''Saving Marshal data into a file'''=====&lt;br /&gt;
&lt;br /&gt;
Similar to YAML, Marshal can also be used to dump data into a file. The above example showing the serialization using YAML::dump can be written using marshal as shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    f = File.open( 'first.yml', 'w' )&lt;br /&gt;
    Marshal.dump( [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;], f )&lt;br /&gt;
    f.close	&lt;br /&gt;
    File.open( 'first.yml' ){ |f|	&lt;br /&gt;
    $arr= Marshal.load(f)&lt;br /&gt;
    }	&lt;br /&gt;
    p( $arr )&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;]&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice that in the above example there is no &amp;quot;require&amp;quot; statement as opposed to the earlier example of writing serialized data into files using YAML. This is because unlike YAML, marshal is built-in Ruby and  no external libraries is required in order to use its functionality.&lt;br /&gt;
&lt;br /&gt;
====='''Custom serialization using Marshal'''=====&lt;br /&gt;
&lt;br /&gt;
Marshal can also be used to custom serialize an object i.e. it provides an option to omit the variables of an object that are not required in the serialized data. The following program shows the use of marshal_dump method for achieving custom serialization.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
      class First&lt;br /&gt;
      def initialize(name, age, country)&lt;br /&gt;
         @name = name&lt;br /&gt;
         @age = age&lt;br /&gt;
         @country=country&lt;br /&gt;
      end&lt;br /&gt;
      def to_s&lt;br /&gt;
            &amp;quot;In First: #{@name}, #{@age}, #{@country}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
      def marshal_dump&lt;br /&gt;
            [@name,@age]  #@country will not be serialized&lt;br /&gt;
      end&lt;br /&gt;
      def marshal_load(data)&lt;br /&gt;
       @name=data[0]&lt;br /&gt;
       @age=data[1]&lt;br /&gt;
       @country=&amp;quot;United States of America&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
      end&lt;br /&gt;
 &lt;br /&gt;
      x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
      puts x&lt;br /&gt;
  &lt;br /&gt;
      marshal_data = Marshal.dump( x ) &lt;br /&gt;
      y = Marshal.load( marshal_data ) &lt;br /&gt;
      p( y.to_s )&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    In First: Tom, 25, USA&lt;br /&gt;
    &amp;quot;In First: Tom, 25, United States of America&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== '''Serialization Performance in Ruby''' ==&lt;br /&gt;
The different serialization formats described above differ in the efficiency at which they can serialize and deserialize data and thus while serializing large amount of data their efficiency is taken into account. The .report[http://www.ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html] method in Ruby can be used to evaluate the performance of these serialization patterns. Marshal, as it handles data in binary format, is the most efficient form of serialization in Ruby. The following example compares the performance of marshal, JSON and YAML format.[http://www.pauldix.net/2008/08/serializing-dat.html]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   require 'benchmark'&lt;br /&gt;
   require 'rubygems'&lt;br /&gt;
   require 'json'&lt;br /&gt;
   require 'yaml'&lt;br /&gt;
   include Benchmark&lt;br /&gt;
   class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
      @name=name&lt;br /&gt;
      @age=age&lt;br /&gt;
      @country=country&lt;br /&gt;
     end&lt;br /&gt;
   end  &lt;br /&gt;
    x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
    puts &amp;quot;            User     System     Total      Real&amp;quot;&lt;br /&gt;
   benchmark do |t|&lt;br /&gt;
    print &amp;quot;Marshal:&amp;quot;&lt;br /&gt;
    t.report{1000.times do; Marshal.load(Marshal.dump(x));end}&lt;br /&gt;
    print &amp;quot;JSON:   &amp;quot;&lt;br /&gt;
    t.report{1000.times do; JSON.load(JSON.dump(x));end}&lt;br /&gt;
    print &amp;quot;YAML:   &amp;quot;&lt;br /&gt;
    t.report{1000.times do; YAML.load(YAML.dump(x));end}&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
              User     System     Total      Real&lt;br /&gt;
  Marshal:   0.010000   0.000000   0.010000 (  0.015408)&lt;br /&gt;
  JSON:      0.020000   0.010000   0.030000 (  0.022462)&lt;br /&gt;
  YAML:      0.460000   0.010000   0.470000 (  0.472149)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output of the above example shows that, given large chunks of data, marshal is comparatively more efficient format for serialization in Ruby.&lt;br /&gt;
&lt;br /&gt;
== Serialization in Object-Oriented Languages: Comparison ==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Sl.No !! Ruby !! Java !! .Net Framework !! C++&lt;br /&gt;
|-&lt;br /&gt;
| 1 || Ruby provides a built in module called 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 || Although, there is no built in support for serialization in C++, it can be achieved by using Boost libraries[http://www.boost.org/doc/libs/1_36_0/libs/serialization/example/demo.cpp]&lt;br /&gt;
|-&lt;br /&gt;
| 2 || The built in module of Ruby (Marshal) does not support platform independence, however, it can be achieved by using external libraries like YAML and JSON || Similarly, Java's built in serialization is also not platform independent and in order to use serialization in Java across Ruby platform, jruby library should be used.  || .Net used Remoting technology to make it platform independent. || Serialization using the Boost libraries is not platform independent.&lt;br /&gt;
|-&lt;br /&gt;
| 3 || YAML provides a method [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/syck/rdoc/Object.html (to_yaml_properties)] which can be used to select the variables who's value is need to be serialized. With Marshal, we need to write a method named marshal_dump defining the variables of an object that has to be serialized. || Provides an option for serializing only the required 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 data 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 || Serialization using the Boost libraries is custom and thus the user can specify the part of the objects to be serialized.&lt;br /&gt;
|-&lt;br /&gt;
| 4 || Bindings, procedure objects, singleton objects, instances of IO objects and interfaces can not be serialized. Serializing these objects throws TypeError exceptions. || Thread, OutputStream and its subclasses, and Socket are not serializable in Java.[http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html] || Objects like DataRow are non serializable in .NET. Whether an object is serializable or not can be checked using the [http://msdn.microsoft.com/en-us/library/system.type.isserializable(v=vs.110).aspx?cs-save-lang=1&amp;amp;cs-lang=cpp#code-snippet-1 Type.IsSerializable] || There are no such objects in C++.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== See Also==&lt;br /&gt;
&lt;br /&gt;
1. [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/json/rdoc/JSON.html Serialization in Ruby JSON]&lt;br /&gt;
&lt;br /&gt;
2. [http://api.rubyonrails.org/classes/ActiveModel/Serialization.html Serialization in Rails]&lt;br /&gt;
&lt;br /&gt;
3. [http://gregmoreno.wordpress.com/2011/01/27/preventing-model-explosion-via-rails-serialization/ Article on Rails Serialization]&lt;br /&gt;
&lt;br /&gt;
4. [http://en.wikipedia.org/wiki/Google_Protocol_Buffers Protocol Buffers]&lt;br /&gt;
&lt;br /&gt;
5. [https://rubygems.org/gems/ruby-protocol-buffers Ruby Protocol Buffers Gem]&lt;br /&gt;
&lt;br /&gt;
6. [http://en.wikipedia.org/wiki/Apache_Avro Avro]&lt;br /&gt;
&lt;br /&gt;
7. [http://rubygems.org/gems/avro Ruby Avro Gem]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
1. [http://en.wikipedia.org/wiki/Serialization Serilization in General]&lt;br /&gt;
&lt;br /&gt;
2. [http://en.wikipedia.org/wiki/Marshalling_%28computer_science%29 Marshaling]&lt;br /&gt;
&lt;br /&gt;
3. [http://techbrahmana.blogspot.com/2012/03/rails-cookie-handling-serialization-and.html Rails Cookie Handling]&lt;br /&gt;
&lt;br /&gt;
4. [http://yaml.org/ YAML]&lt;br /&gt;
&lt;br /&gt;
5. [http://www.ruby-doc.org/core-2.1.0/Marshal.html Marshal module]&lt;br /&gt;
&lt;br /&gt;
6. [http://json.org/ JSON]&lt;br /&gt;
&lt;br /&gt;
7. [http://rubygems.org/ Ruby Gems]&lt;br /&gt;
&lt;br /&gt;
8. [http://ruby-doc.org/core-2.0.0/ARGF.html# Ruby ARGF Documentation]&lt;br /&gt;
&lt;br /&gt;
9. [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html Ruby Documentation for Benchmark Gem]&lt;br /&gt;
&lt;br /&gt;
10. [http://www.pauldix.net/2008/08/serializing-dat.html Serialization Performance Comparison]&lt;br /&gt;
&lt;br /&gt;
11. [http://www.boost.org/doc/libs/1_36_0/libs/serialization/example/demo.cpp Boost Libraries]&lt;br /&gt;
&lt;br /&gt;
12. [http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html Java Serialization]&lt;br /&gt;
&lt;br /&gt;
13. [http://www.w3schools.com/json/ JSON Tutorial]&lt;br /&gt;
&lt;br /&gt;
14. [http://www.skorks.com/2010/04/serializing-and-deserializing-objects-with-ruby/ Serializing and De-serializing in Ruby]&lt;br /&gt;
&lt;br /&gt;
15. [http://www.codeproject.com/Articles/33296/Serialization-and-De-serialization Serialization and De-serialization]&lt;br /&gt;
&lt;br /&gt;
16. [http://msdn.microsoft.com/en-us/library/182eeyhh.aspx XML Serialization]&lt;br /&gt;
&lt;br /&gt;
17. [http://www.tutorialspoint.com/java/java_serialization.htm Java Serialization Tutorial]&lt;br /&gt;
&lt;br /&gt;
18. [http://www.sapphiresteel.com/ruby-programming/The-Book-Of-Ruby The Book of Ruby]&lt;br /&gt;
&lt;br /&gt;
19. [http://www.codeproject.com/Articles/20962/Introducing-Serialization-in-NET Serialization in .Net]&lt;br /&gt;
&lt;br /&gt;
20. [http://ruby.about.com/od/advancedruby/ss/Serialization-In-Ruby-Yaml.html Serialization in Ruby YAML]&lt;br /&gt;
&lt;br /&gt;
21. [http://ruby.about.com/od/tasks/a/The-Json-Gem.html Installing JSON gem]&lt;br /&gt;
&lt;br /&gt;
22. [http://www.ruby-doc.org/stdlib-2.0.0/libdoc/json/rdoc/JSON.html Serialization in Ruby JSON]&lt;/div&gt;</summary>
		<author><name>Mmehta2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1d_mm&amp;diff=83421</id>
		<title>CSC/ECE 517 Spring 2014/ch1a 1d mm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1d_mm&amp;diff=83421"/>
		<updated>2014-02-18T02:52:07Z</updated>

		<summary type="html">&lt;p&gt;Mmehta2: /* Serialization */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page discusses Serialization, focusing on Ruby programming language. It describes different methods of Serialization in Ruby. In addition, it also compares serialization in different Object Oriented languages.&lt;br /&gt;
&lt;br /&gt;
== '''Introduction''' ==&lt;br /&gt;
&lt;br /&gt;
==='''Serialization'''===&lt;br /&gt;
&lt;br /&gt;
'''Serialization'''[http://en.wikipedia.org/wiki/Serialization] is a process of converting a data structure or an object into a stream of bytes or string to facilitate storage in memory, file (persistence storage) or transmission over a network. The process of Serialization is also referred to as ''Marshalling''[http://en.wikipedia.org/wiki/Marshalling_%28computer_science%29]. 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.&lt;br /&gt;
&lt;br /&gt;
==='''De-serialization'''===&lt;br /&gt;
&lt;br /&gt;
'''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''.&lt;br /&gt;
&lt;br /&gt;
==='''Basic Advantages of Serialization'''===&lt;br /&gt;
&lt;br /&gt;
1. Communication between two or more processes on same machine. Object state can be saved and shared in a persistent or in-memory store.&lt;br /&gt;
&lt;br /&gt;
2. Communication between processes on different machines. Serialization facilitates the transmission of an object through a network.&lt;br /&gt;
&lt;br /&gt;
3. Creating a clone of an object. &lt;br /&gt;
&lt;br /&gt;
4. Cross-platform compatibility. Object can be serialized in a common format that is understood by multiple platforms. For example, JSON[http://www.json.org/] and XML.&lt;br /&gt;
&lt;br /&gt;
==='''Practical Applications for Serialization'''===&lt;br /&gt;
&lt;br /&gt;
1. [http://docs.oracle.com/cd/E13222_01/wls/docs81/webapp/sessions.html#139726 HTTP Session] Replication by sharing session objects across web servers for load balancing and handling failover scenarios.&lt;br /&gt;
&lt;br /&gt;
2. Serialization facilitates communication in [http://en.wikipedia.org/wiki/Java_remote_method_invocation Remote Method Invocation] or [http://en.wikipedia.org/wiki/Remote_procedure_call Remote procedure calls]&lt;br /&gt;
&lt;br /&gt;
3. Rails Cookie Handling[http://techbrahmana.blogspot.com/2012/03/rails-cookie-handling-serialization-and.html]. Cookies are stored marshalled/unmarshalled to and from client machines.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== '''Serialization in Ruby:''' ==&lt;br /&gt;
&lt;br /&gt;
Ruby provides serialization capabilities through its module, [http://www.ruby-doc.org/core-2.0.0/Marshal.html Marshal]. There are also other libraries like YAML[http://yaml.org/] and JSON which can be used in Ruby to generate serialized objects for purposes like platform independence and human readable formats.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:SerializationChart.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== '''Types of Serialization''' ==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
=== Converting Ruby Objects in Human Readable Formats ===&lt;br /&gt;
----&lt;br /&gt;
The conversion of Ruby objects into YAML and JSON formats are explained below.&lt;br /&gt;
&lt;br /&gt;
==== Converting Ruby Objects to YAML format ====&lt;br /&gt;
&lt;br /&gt;
YAML 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 binary format. 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. In order to use it in Ruby, the yaml.rb file is required to be loaded which provides methods for converting objects into yaml format and creating .yml files.&lt;br /&gt;
&lt;br /&gt;
=====Serialization using YAML=====&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;nowiki&amp;gt; #Serialization using YAML's to_yaml method&lt;br /&gt;
 &lt;br /&gt;
  require &amp;quot;yaml&amp;quot;&lt;br /&gt;
     class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
	@name = name&lt;br /&gt;
	@age = age&lt;br /&gt;
	@country=country&lt;br /&gt;
     end&lt;br /&gt;
      def to_s&lt;br /&gt;
	&amp;quot;In First:\n#{@name}, #{@age}, #{@country}\n&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
  x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
  puts x&lt;br /&gt;
  puts x.to_yaml&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   In First:&lt;br /&gt;
   Tom, 25, USA&lt;br /&gt;
   --- !ruby/object:First&lt;br /&gt;
   name: Tom&lt;br /&gt;
   age: 25&lt;br /&gt;
   country: USA&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The above code displays the object x, first as a string and then in the yaml format.&lt;br /&gt;
&lt;br /&gt;
====='''Saving YAML data into a file:'''=====&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt; # Serialization using YAML::dump&lt;br /&gt;
&lt;br /&gt;
require 'yaml'&lt;br /&gt;
&lt;br /&gt;
f = File.open( 'first.yml', 'w' )&lt;br /&gt;
YAML.dump( [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;], f )&lt;br /&gt;
f.close&lt;br /&gt;
		&lt;br /&gt;
File.open( 'first.yml' ){ |f|	&lt;br /&gt;
    $arr= YAML.load(f)&lt;br /&gt;
}	&lt;br /&gt;
&lt;br /&gt;
p( $arr )&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;]&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The dump function can be used to serialize the data and save it into a file in YAML format. As shown in the above example the data in YAML format can be de-serialized using the load function.&lt;br /&gt;
&lt;br /&gt;
YAML libraries also provides an option of selecting only those variables of the object that are needed to be serialized. This is done using the to_yaml_properties method as shown in the below example.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=====Custom Serialization using YAML=====&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;nowiki&amp;gt;&lt;br /&gt;
    require &amp;quot;yaml&amp;quot;&lt;br /&gt;
     class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
	@name = name&lt;br /&gt;
	@age = age&lt;br /&gt;
	@country=country&lt;br /&gt;
     end&lt;br /&gt;
     def to_s&lt;br /&gt;
	&amp;quot;In First:\n#{@name}, #{@age}, #{@country}\n&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
      def to_yaml_properties&lt;br /&gt;
	   [&amp;quot;@name&amp;quot;,&amp;quot;@age&amp;quot;]  #@country will not be serialized&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
  x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
  puts x&lt;br /&gt;
  puts x.to_yaml&lt;br /&gt;
  &amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   In First:&lt;br /&gt;
   Tom, 25, USA&lt;br /&gt;
   --- !ruby/object:First&lt;br /&gt;
   name: Tom&lt;br /&gt;
   age: 25&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Converting Ruby Objects to JSON format: ====&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
JSON 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. Ruby 1.8.7 distribution is not bundled with json gem. However, in Ruby 1.9.2, the json gem is bundled with the core Ruby distribution.&lt;br /&gt;
The JSON library can be installed using Ruby Gems[http://rubygems.org/] as shown below:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;# gem install json&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A JSON string for serialization can be created by using the JSON.generate method:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
        require 'json'&lt;br /&gt;
        my_hash = {:Welcome =&amp;gt; &amp;quot;Ruby&amp;quot;}&lt;br /&gt;
        puts JSON.generate(my_hash) =&amp;gt; &amp;quot;{\&amp;quot;WELCOME\&amp;quot;:\&amp;quot;RUBY\&amp;quot;}&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
'''Output:'''&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;{&amp;quot;{\&amp;quot;Welcome\&amp;quot;:\&amp;quot;Ruby\&amp;quot;}&amp;quot;=&amp;gt;&amp;quot;{\&amp;quot;WELCOME\&amp;quot;:\&amp;quot;RUBY\&amp;quot;}&amp;quot;}&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A JSON string received from another program can be parsed by using JSON.parse&lt;br /&gt;
Ruby thus converts String to Hash.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
        require 'json'&lt;br /&gt;
        my_hash = JSON.parse('{&amp;quot;Welcome&amp;quot;: &amp;quot;Ruby&amp;quot;}')&lt;br /&gt;
        puts my_hash[&amp;quot;Welcome&amp;quot;] =&amp;gt; &amp;quot;Ruby&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Converting Ruby Objects to Binary Formats ===&lt;br /&gt;
----&lt;br /&gt;
Binary Serialization is another form of serialization in Ruby which is not in human readable form. 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. Binary Serialization  is done using Marshal which is built into Ruby and the code for it is written in Ruby's Marshal module(marshal.c) and thus no additional files are required in order to use it. The _dump and _load methods defined in marshal are used for serialization. Using marshal module the following types of objects can not be serialized: bindings, procedure objects, singleton objects, instances of IO objects and interfaces.&lt;br /&gt;
&lt;br /&gt;
Since the Binary Serialized data is not in human readable form, the following guidelines need to be followed.&lt;br /&gt;
     1.Use print instead of puts[http://ruby-doc.org/core-2.0.0/ARGF.html#] when serialized objects are written to a file in order to avoid new line characters to be written &lt;br /&gt;
       in the file.&lt;br /&gt;
     &lt;br /&gt;
     2.Use a record separator in order to differentiate between two objects.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====='''Binary Serialization Example:'''=====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    class Animal&lt;br /&gt;
      def initialize  name, age&lt;br /&gt;
      @name = name&lt;br /&gt;
      @age=age&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
   class Cat &amp;lt; Animal&lt;br /&gt;
    def to_s&lt;br /&gt;
     &amp;quot;In Cat C: #{@name} \t #{@age}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
  c = Cat.new(&amp;quot;Kitty Kat&amp;quot;,5)&lt;br /&gt;
  puts &amp;quot;Before Serialization&amp;quot;&lt;br /&gt;
  puts c&lt;br /&gt;
  #puts d&lt;br /&gt;
  serialize_cat= Marshal.dump(c) #dumps the serialized cat object into serialize_cat&lt;br /&gt;
  puts &amp;quot;\nAfter Serialization:\n #{serialize_cat}&amp;quot;&lt;br /&gt;
  deserialize_cat= Marshal::load(serialize_cat) #deserializes the cat object and loads it back into deserialize_cat&lt;br /&gt;
  puts &amp;quot;\nAfter Deserialization\n #{deserialize_cat}&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   Before Serialization&lt;br /&gt;
   In Cat C: Kitty Kat 	 5&lt;br /&gt;
   After Serialization:&lt;br /&gt;
   oCat:&lt;br /&gt;
   @nameI&amp;quot;Kitty Kat:ET:	@agei&lt;br /&gt;
   After Deserialization&lt;br /&gt;
   In Cat C: Kitty Kat 	 5&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====='''Saving Marshal data into a file'''=====&lt;br /&gt;
&lt;br /&gt;
Similar to YAML, Marshal can also be used to dump data into a file. The above example showing the serialization using YAML::dump can be written using marshal as shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    f = File.open( 'first.yml', 'w' )&lt;br /&gt;
    Marshal.dump( [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;], f )&lt;br /&gt;
    f.close	&lt;br /&gt;
    File.open( 'first.yml' ){ |f|	&lt;br /&gt;
    $arr= Marshal.load(f)&lt;br /&gt;
    }	&lt;br /&gt;
    p( $arr )&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;]&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice that in the above example there is no &amp;quot;require&amp;quot; statement as opposed to the earlier example of writing serialized data into files using YAML. This is because unlike YAML, marshal is built-in Ruby and  no external libraries is required in order to use its functionality.&lt;br /&gt;
&lt;br /&gt;
====='''Custom serialization using Marshal'''=====&lt;br /&gt;
&lt;br /&gt;
Marshal can also be used to custom serialize an object i.e. it provides an option to omit the variables of an object that are not required in the serialized data. The following program shows the use of marshal_dump method for achieving custom serialization.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
      class First&lt;br /&gt;
      def initialize(name, age, country)&lt;br /&gt;
         @name = name&lt;br /&gt;
         @age = age&lt;br /&gt;
         @country=country&lt;br /&gt;
      end&lt;br /&gt;
      def to_s&lt;br /&gt;
            &amp;quot;In First: #{@name}, #{@age}, #{@country}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
      def marshal_dump&lt;br /&gt;
            [@name,@age]  #@country will not be serialized&lt;br /&gt;
      end&lt;br /&gt;
      def marshal_load(data)&lt;br /&gt;
       @name=data[0]&lt;br /&gt;
       @age=data[1]&lt;br /&gt;
       @country=&amp;quot;United States of America&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
      end&lt;br /&gt;
 &lt;br /&gt;
      x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
      puts x&lt;br /&gt;
  &lt;br /&gt;
      marshal_data = Marshal.dump( x ) &lt;br /&gt;
      y = Marshal.load( marshal_data ) &lt;br /&gt;
      p( y.to_s )&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    In First: Tom, 25, USA&lt;br /&gt;
    &amp;quot;In First: Tom, 25, United States of America&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== '''Serialization Performance in Ruby''' ==&lt;br /&gt;
The different serialization formats described above differ in the efficiency at which they can serialize and deserialize data and thus while serializing large amount of data their efficiency is taken into account. The .report[http://www.ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html] method in Ruby can be used to evaluate the performance of these serialization patterns. Marshal, as it handles data in binary format, is the most efficient form of serialization in Ruby. The following example compares the performance of marshal, JSON and YAML format.[http://www.pauldix.net/2008/08/serializing-dat.html]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   require 'benchmark'&lt;br /&gt;
   require 'rubygems'&lt;br /&gt;
   require 'json'&lt;br /&gt;
   require 'yaml'&lt;br /&gt;
   include Benchmark&lt;br /&gt;
   class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
      @name=name&lt;br /&gt;
      @age=age&lt;br /&gt;
      @country=country&lt;br /&gt;
     end&lt;br /&gt;
   end  &lt;br /&gt;
    x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
    puts &amp;quot;            User     System     Total      Real&amp;quot;&lt;br /&gt;
   benchmark do |t|&lt;br /&gt;
    print &amp;quot;Marshal:&amp;quot;&lt;br /&gt;
    t.report{1000.times do; Marshal.load(Marshal.dump(x));end}&lt;br /&gt;
    print &amp;quot;JSON:   &amp;quot;&lt;br /&gt;
    t.report{1000.times do; JSON.load(JSON.dump(x));end}&lt;br /&gt;
    print &amp;quot;YAML:   &amp;quot;&lt;br /&gt;
    t.report{1000.times do; YAML.load(YAML.dump(x));end}&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
              User     System     Total      Real&lt;br /&gt;
  Marshal:   0.010000   0.000000   0.010000 (  0.015408)&lt;br /&gt;
  JSON:      0.020000   0.010000   0.030000 (  0.022462)&lt;br /&gt;
  YAML:      0.460000   0.010000   0.470000 (  0.472149)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output of the above example shows that, given large chunks of data, marshal is comparatively more efficient format for serialization in Ruby.&lt;br /&gt;
&lt;br /&gt;
== Serialization in Object-Oriented Languages: Comparison ==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Sl.No !! Ruby !! Java !! .Net Framework !! C++&lt;br /&gt;
|-&lt;br /&gt;
| 1 || Ruby provides a built in module called 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 || Although, there is no built in support for serialization in C++, it can be achieved by using Boost libraries[http://www.boost.org/doc/libs/1_36_0/libs/serialization/example/demo.cpp]&lt;br /&gt;
|-&lt;br /&gt;
| 2 || The built in module of Ruby (Marshal) does not support platform independence, however, it can be achieved by using external libraries like YAML and JSON || Similarly, Java's built in serialization is also not platform independent and in order to use serialization in Java across Ruby platform, jruby library should be used.  || .Net used Remoting technology to make it platform independent. || Serialization using the Boost libraries is not platform independent.&lt;br /&gt;
|-&lt;br /&gt;
| 3 || YAML provides a method [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/syck/rdoc/Object.html (to_yaml_properties)] which can be used to select the variables who's value is need to be serialized. With Marshal, we need to write a method named marshal_dump defining the variables of an object that has to be serialized. || Provides an option for serializing only the required 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 data 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 || Serialization using the Boost libraries is custom and thus the user can specify the part of the objects to be serialized.&lt;br /&gt;
|-&lt;br /&gt;
| 4 || Bindings, procedure objects, singleton objects, instances of IO objects and interfaces can not be serialized. Serializing these objects throws TypeError exceptions. || Thread, OutputStream and its subclasses, and Socket are not serializable in Java.[http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html] || Objects like DataRow are non serializable in .NET. Whether an object is serializable or not can be checked using the [http://msdn.microsoft.com/en-us/library/system.type.isserializable(v=vs.110).aspx?cs-save-lang=1&amp;amp;cs-lang=cpp#code-snippet-1 Type.IsSerializable] || There are no such objects in C++.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== See Also==&lt;br /&gt;
&lt;br /&gt;
1. [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/json/rdoc/JSON.html Serialization in Ruby JSON]&lt;br /&gt;
&lt;br /&gt;
2. [http://api.rubyonrails.org/classes/ActiveModel/Serialization.html Serialization in Rails]&lt;br /&gt;
&lt;br /&gt;
3. [http://gregmoreno.wordpress.com/2011/01/27/preventing-model-explosion-via-rails-serialization/ Article on Rails Serialization]&lt;br /&gt;
&lt;br /&gt;
4. [http://en.wikipedia.org/wiki/Google_Protocol_Buffers Protocol Buffers]&lt;br /&gt;
&lt;br /&gt;
5. [https://rubygems.org/gems/ruby-protocol-buffers Ruby Protocol Buffers Gem]&lt;br /&gt;
&lt;br /&gt;
6. [http://en.wikipedia.org/wiki/Apache_Avro Avro]&lt;br /&gt;
&lt;br /&gt;
7. [http://rubygems.org/gems/avro Ruby Avro Gem]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
1. [http://en.wikipedia.org/wiki/Serialization Serilization in General]&lt;br /&gt;
&lt;br /&gt;
2. [http://en.wikipedia.org/wiki/Marshalling_%28computer_science%29 Marshaling]&lt;br /&gt;
&lt;br /&gt;
3. [http://techbrahmana.blogspot.com/2012/03/rails-cookie-handling-serialization-and.html Rails Cookie Handling]&lt;br /&gt;
&lt;br /&gt;
4. [http://yaml.org/ YAML]&lt;br /&gt;
&lt;br /&gt;
5. [http://www.ruby-doc.org/core-2.1.0/Marshal.html Marshal module]&lt;br /&gt;
&lt;br /&gt;
6. [http://json.org/ JSON]&lt;br /&gt;
&lt;br /&gt;
7. [http://rubygems.org/ Ruby Gems]&lt;br /&gt;
&lt;br /&gt;
8. [http://ruby-doc.org/core-2.0.0/ARGF.html# Ruby ARGF Documentation]&lt;br /&gt;
&lt;br /&gt;
9. [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html Ruby Documentation for Benchmark Gem]&lt;br /&gt;
&lt;br /&gt;
10. [http://www.pauldix.net/2008/08/serializing-dat.html Serialization Performance Comparison]&lt;br /&gt;
&lt;br /&gt;
11. [http://www.boost.org/doc/libs/1_36_0/libs/serialization/example/demo.cpp Boost Libraries]&lt;br /&gt;
&lt;br /&gt;
12. [http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html Java Serialization]&lt;br /&gt;
&lt;br /&gt;
13. [http://www.w3schools.com/json/ JSON Tutorial]&lt;br /&gt;
&lt;br /&gt;
14. [http://www.skorks.com/2010/04/serializing-and-deserializing-objects-with-ruby/ Serializing and De-serializing in Ruby]&lt;br /&gt;
&lt;br /&gt;
15. [http://www.codeproject.com/Articles/33296/Serialization-and-De-serialization Serialization and De-serialization]&lt;br /&gt;
&lt;br /&gt;
16. [http://msdn.microsoft.com/en-us/library/182eeyhh.aspx XML Serialization]&lt;br /&gt;
&lt;br /&gt;
17. [http://www.tutorialspoint.com/java/java_serialization.htm Java Serialization Tutorial]&lt;br /&gt;
&lt;br /&gt;
18. [http://www.sapphiresteel.com/ruby-programming/The-Book-Of-Ruby The Book of Ruby]&lt;br /&gt;
&lt;br /&gt;
19. [http://www.codeproject.com/Articles/20962/Introducing-Serialization-in-NET Serialization in .Net]&lt;br /&gt;
&lt;br /&gt;
20. [http://ruby.about.com/od/advancedruby/ss/Serialization-In-Ruby-Yaml.html Serialization in Ruby YAML]&lt;br /&gt;
&lt;br /&gt;
21. [http://ruby.about.com/od/tasks/a/The-Json-Gem.html Installing JSON gem]&lt;br /&gt;
&lt;br /&gt;
22. [http://www.ruby-doc.org/stdlib-2.0.0/libdoc/json/rdoc/JSON.html Serialization in Ruby JSON]&lt;/div&gt;</summary>
		<author><name>Mmehta2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1d_mm&amp;diff=83420</id>
		<title>CSC/ECE 517 Spring 2014/ch1a 1d mm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1d_mm&amp;diff=83420"/>
		<updated>2014-02-18T02:48:34Z</updated>

		<summary type="html">&lt;p&gt;Mmehta2: /* Examples of serialization using YAML */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page discusses Serialization, focusing on Ruby programming language. It describes different methods of Serialization in Ruby. In addition, it also compares serialization in different Object Oriented languages.&lt;br /&gt;
&lt;br /&gt;
== '''Introduction''' ==&lt;br /&gt;
&lt;br /&gt;
==='''Serialization'''===&lt;br /&gt;
&lt;br /&gt;
'''Serialization'''[http://en.wikipedia.org/wiki/Serialization] is a process of converting a data structure or an object into a stream of bytes or string to facilitate storage in memory, file (persistence storage) or transmission over a network. The process of Serialization is also referred to as ''Marshalling''[http://en.wikipedia.org/wiki/Marshalling_%28computer_science%29]. 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. &lt;br /&gt;
&lt;br /&gt;
==='''De-serialization'''===&lt;br /&gt;
&lt;br /&gt;
'''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''.&lt;br /&gt;
&lt;br /&gt;
==='''Basic Advantages of Serialization'''===&lt;br /&gt;
&lt;br /&gt;
1. Communication between two or more processes on same machine. Object state can be saved and shared in a persistent or in-memory store.&lt;br /&gt;
&lt;br /&gt;
2. Communication between processes on different machines. Serialization facilitates the transmission of an object through a network.&lt;br /&gt;
&lt;br /&gt;
3. Creating a clone of an object. &lt;br /&gt;
&lt;br /&gt;
4. Cross-platform compatibility. Object can be serialized in a common format that is understood by multiple platforms. For example, JSON[http://www.json.org/] and XML.&lt;br /&gt;
&lt;br /&gt;
==='''Practical Applications for Serialization'''===&lt;br /&gt;
&lt;br /&gt;
1. [http://docs.oracle.com/cd/E13222_01/wls/docs81/webapp/sessions.html#139726 HTTP Session] Replication by sharing session objects across web servers for load balancing and handling failover scenarios.&lt;br /&gt;
&lt;br /&gt;
2. Serialization facilitates communication in [http://en.wikipedia.org/wiki/Java_remote_method_invocation Remote Method Invocation] or [http://en.wikipedia.org/wiki/Remote_procedure_call Remote procedure calls]&lt;br /&gt;
&lt;br /&gt;
3. Rails Cookie Handling[http://techbrahmana.blogspot.com/2012/03/rails-cookie-handling-serialization-and.html]. Cookies are stored marshalled/unmarshalled to and from client machines.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== '''Serialization in Ruby:''' ==&lt;br /&gt;
&lt;br /&gt;
Ruby provides serialization capabilities through its module, [http://www.ruby-doc.org/core-2.0.0/Marshal.html Marshal]. There are also other libraries like YAML[http://yaml.org/] and JSON which can be used in Ruby to generate serialized objects for purposes like platform independence and human readable formats.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:SerializationChart.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== '''Types of Serialization''' ==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
=== Converting Ruby Objects in Human Readable Formats ===&lt;br /&gt;
----&lt;br /&gt;
The conversion of Ruby objects into YAML and JSON formats are explained below.&lt;br /&gt;
&lt;br /&gt;
==== Converting Ruby Objects to YAML format ====&lt;br /&gt;
&lt;br /&gt;
YAML 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 binary format. 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. In order to use it in Ruby, the yaml.rb file is required to be loaded which provides methods for converting objects into yaml format and creating .yml files.&lt;br /&gt;
&lt;br /&gt;
=====Serialization using YAML=====&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;nowiki&amp;gt; #Serialization using YAML's to_yaml method&lt;br /&gt;
 &lt;br /&gt;
  require &amp;quot;yaml&amp;quot;&lt;br /&gt;
     class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
	@name = name&lt;br /&gt;
	@age = age&lt;br /&gt;
	@country=country&lt;br /&gt;
     end&lt;br /&gt;
      def to_s&lt;br /&gt;
	&amp;quot;In First:\n#{@name}, #{@age}, #{@country}\n&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
  x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
  puts x&lt;br /&gt;
  puts x.to_yaml&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   In First:&lt;br /&gt;
   Tom, 25, USA&lt;br /&gt;
   --- !ruby/object:First&lt;br /&gt;
   name: Tom&lt;br /&gt;
   age: 25&lt;br /&gt;
   country: USA&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The above code displays the object x, first as a string and then in the yaml format.&lt;br /&gt;
&lt;br /&gt;
====='''Saving YAML data into a file:'''=====&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt; # Serialization using YAML::dump&lt;br /&gt;
&lt;br /&gt;
require 'yaml'&lt;br /&gt;
&lt;br /&gt;
f = File.open( 'first.yml', 'w' )&lt;br /&gt;
YAML.dump( [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;], f )&lt;br /&gt;
f.close&lt;br /&gt;
		&lt;br /&gt;
File.open( 'first.yml' ){ |f|	&lt;br /&gt;
    $arr= YAML.load(f)&lt;br /&gt;
}	&lt;br /&gt;
&lt;br /&gt;
p( $arr )&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;]&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The dump function can be used to serialize the data and save it into a file in YAML format. As shown in the above example the data in YAML format can be de-serialized using the load function.&lt;br /&gt;
&lt;br /&gt;
YAML libraries also provides an option of selecting only those variables of the object that are needed to be serialized. This is done using the to_yaml_properties method as shown in the below example.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=====Custom Serialization using YAML=====&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;nowiki&amp;gt;&lt;br /&gt;
    require &amp;quot;yaml&amp;quot;&lt;br /&gt;
     class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
	@name = name&lt;br /&gt;
	@age = age&lt;br /&gt;
	@country=country&lt;br /&gt;
     end&lt;br /&gt;
     def to_s&lt;br /&gt;
	&amp;quot;In First:\n#{@name}, #{@age}, #{@country}\n&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
      def to_yaml_properties&lt;br /&gt;
	   [&amp;quot;@name&amp;quot;,&amp;quot;@age&amp;quot;]  #@country will not be serialized&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
  x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
  puts x&lt;br /&gt;
  puts x.to_yaml&lt;br /&gt;
  &amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   In First:&lt;br /&gt;
   Tom, 25, USA&lt;br /&gt;
   --- !ruby/object:First&lt;br /&gt;
   name: Tom&lt;br /&gt;
   age: 25&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Converting Ruby Objects to JSON format: ====&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
JSON 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. Ruby 1.8.7 distribution is not bundled with json gem. However, in Ruby 1.9.2, the json gem is bundled with the core Ruby distribution.&lt;br /&gt;
The JSON library can be installed using Ruby Gems[http://rubygems.org/] as shown below:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;# gem install json&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A JSON string for serialization can be created by using the JSON.generate method:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
        require 'json'&lt;br /&gt;
        my_hash = {:Welcome =&amp;gt; &amp;quot;Ruby&amp;quot;}&lt;br /&gt;
        puts JSON.generate(my_hash) =&amp;gt; &amp;quot;{\&amp;quot;WELCOME\&amp;quot;:\&amp;quot;RUBY\&amp;quot;}&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
'''Output:'''&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;{&amp;quot;{\&amp;quot;Welcome\&amp;quot;:\&amp;quot;Ruby\&amp;quot;}&amp;quot;=&amp;gt;&amp;quot;{\&amp;quot;WELCOME\&amp;quot;:\&amp;quot;RUBY\&amp;quot;}&amp;quot;}&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A JSON string received from another program can be parsed by using JSON.parse&lt;br /&gt;
Ruby thus converts String to Hash.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
        require 'json'&lt;br /&gt;
        my_hash = JSON.parse('{&amp;quot;Welcome&amp;quot;: &amp;quot;Ruby&amp;quot;}')&lt;br /&gt;
        puts my_hash[&amp;quot;Welcome&amp;quot;] =&amp;gt; &amp;quot;Ruby&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Converting Ruby Objects to Binary Formats ===&lt;br /&gt;
----&lt;br /&gt;
Binary Serialization is another form of serialization in Ruby which is not in human readable form. 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. Binary Serialization  is done using Marshal which is built into Ruby and the code for it is written in Ruby's Marshal module(marshal.c) and thus no additional files are required in order to use it. The _dump and _load methods defined in marshal are used for serialization. Using marshal module the following types of objects can not be serialized: bindings, procedure objects, singleton objects, instances of IO objects and interfaces.&lt;br /&gt;
&lt;br /&gt;
Since the Binary Serialized data is not in human readable form, the following guidelines need to be followed.&lt;br /&gt;
     1.Use print instead of puts[http://ruby-doc.org/core-2.0.0/ARGF.html#] when serialized objects are written to a file in order to avoid new line characters to be written &lt;br /&gt;
       in the file.&lt;br /&gt;
     &lt;br /&gt;
     2.Use a record separator in order to differentiate between two objects.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====='''Binary Serialization Example:'''=====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    class Animal&lt;br /&gt;
      def initialize  name, age&lt;br /&gt;
      @name = name&lt;br /&gt;
      @age=age&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
   class Cat &amp;lt; Animal&lt;br /&gt;
    def to_s&lt;br /&gt;
     &amp;quot;In Cat C: #{@name} \t #{@age}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
  c = Cat.new(&amp;quot;Kitty Kat&amp;quot;,5)&lt;br /&gt;
  puts &amp;quot;Before Serialization&amp;quot;&lt;br /&gt;
  puts c&lt;br /&gt;
  #puts d&lt;br /&gt;
  serialize_cat= Marshal.dump(c) #dumps the serialized cat object into serialize_cat&lt;br /&gt;
  puts &amp;quot;\nAfter Serialization:\n #{serialize_cat}&amp;quot;&lt;br /&gt;
  deserialize_cat= Marshal::load(serialize_cat) #deserializes the cat object and loads it back into deserialize_cat&lt;br /&gt;
  puts &amp;quot;\nAfter Deserialization\n #{deserialize_cat}&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   Before Serialization&lt;br /&gt;
   In Cat C: Kitty Kat 	 5&lt;br /&gt;
   After Serialization:&lt;br /&gt;
   oCat:&lt;br /&gt;
   @nameI&amp;quot;Kitty Kat:ET:	@agei&lt;br /&gt;
   After Deserialization&lt;br /&gt;
   In Cat C: Kitty Kat 	 5&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====='''Saving Marshal data into a file'''=====&lt;br /&gt;
&lt;br /&gt;
Similar to YAML, Marshal can also be used to dump data into a file. The above example showing the serialization using YAML::dump can be written using marshal as shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    f = File.open( 'first.yml', 'w' )&lt;br /&gt;
    Marshal.dump( [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;], f )&lt;br /&gt;
    f.close	&lt;br /&gt;
    File.open( 'first.yml' ){ |f|	&lt;br /&gt;
    $arr= Marshal.load(f)&lt;br /&gt;
    }	&lt;br /&gt;
    p( $arr )&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;]&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice that in the above example there is no &amp;quot;require&amp;quot; statement as opposed to the earlier example of writing serialized data into files using YAML. This is because unlike YAML, marshal is built-in Ruby and  no external libraries is required in order to use its functionality.&lt;br /&gt;
&lt;br /&gt;
====='''Custom serialization using Marshal'''=====&lt;br /&gt;
&lt;br /&gt;
Marshal can also be used to custom serialize an object i.e. it provides an option to omit the variables of an object that are not required in the serialized data. The following program shows the use of marshal_dump method for achieving custom serialization.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
      class First&lt;br /&gt;
      def initialize(name, age, country)&lt;br /&gt;
         @name = name&lt;br /&gt;
         @age = age&lt;br /&gt;
         @country=country&lt;br /&gt;
      end&lt;br /&gt;
      def to_s&lt;br /&gt;
            &amp;quot;In First: #{@name}, #{@age}, #{@country}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
      def marshal_dump&lt;br /&gt;
            [@name,@age]  #@country will not be serialized&lt;br /&gt;
      end&lt;br /&gt;
      def marshal_load(data)&lt;br /&gt;
       @name=data[0]&lt;br /&gt;
       @age=data[1]&lt;br /&gt;
       @country=&amp;quot;United States of America&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
      end&lt;br /&gt;
 &lt;br /&gt;
      x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
      puts x&lt;br /&gt;
  &lt;br /&gt;
      marshal_data = Marshal.dump( x ) &lt;br /&gt;
      y = Marshal.load( marshal_data ) &lt;br /&gt;
      p( y.to_s )&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    In First: Tom, 25, USA&lt;br /&gt;
    &amp;quot;In First: Tom, 25, United States of America&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== '''Serialization Performance in Ruby''' ==&lt;br /&gt;
The different serialization formats described above differ in the efficiency at which they can serialize and deserialize data and thus while serializing large amount of data their efficiency is taken into account. The .report[http://www.ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html] method in Ruby can be used to evaluate the performance of these serialization patterns. Marshal, as it handles data in binary format, is the most efficient form of serialization in Ruby. The following example compares the performance of marshal, JSON and YAML format.[http://www.pauldix.net/2008/08/serializing-dat.html]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   require 'benchmark'&lt;br /&gt;
   require 'rubygems'&lt;br /&gt;
   require 'json'&lt;br /&gt;
   require 'yaml'&lt;br /&gt;
   include Benchmark&lt;br /&gt;
   class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
      @name=name&lt;br /&gt;
      @age=age&lt;br /&gt;
      @country=country&lt;br /&gt;
     end&lt;br /&gt;
   end  &lt;br /&gt;
    x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
    puts &amp;quot;            User     System     Total      Real&amp;quot;&lt;br /&gt;
   benchmark do |t|&lt;br /&gt;
    print &amp;quot;Marshal:&amp;quot;&lt;br /&gt;
    t.report{1000.times do; Marshal.load(Marshal.dump(x));end}&lt;br /&gt;
    print &amp;quot;JSON:   &amp;quot;&lt;br /&gt;
    t.report{1000.times do; JSON.load(JSON.dump(x));end}&lt;br /&gt;
    print &amp;quot;YAML:   &amp;quot;&lt;br /&gt;
    t.report{1000.times do; YAML.load(YAML.dump(x));end}&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
              User     System     Total      Real&lt;br /&gt;
  Marshal:   0.010000   0.000000   0.010000 (  0.015408)&lt;br /&gt;
  JSON:      0.020000   0.010000   0.030000 (  0.022462)&lt;br /&gt;
  YAML:      0.460000   0.010000   0.470000 (  0.472149)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output of the above example shows that, given large chunks of data, marshal is comparatively more efficient format for serialization in Ruby.&lt;br /&gt;
&lt;br /&gt;
== Serialization in Object-Oriented Languages: Comparison ==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Sl.No !! Ruby !! Java !! .Net Framework !! C++&lt;br /&gt;
|-&lt;br /&gt;
| 1 || Ruby provides a built in module called 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 || Although, there is no built in support for serialization in C++, it can be achieved by using Boost libraries[http://www.boost.org/doc/libs/1_36_0/libs/serialization/example/demo.cpp]&lt;br /&gt;
|-&lt;br /&gt;
| 2 || The built in module of Ruby (Marshal) does not support platform independence, however, it can be achieved by using external libraries like YAML and JSON || Similarly, Java's built in serialization is also not platform independent and in order to use serialization in Java across Ruby platform, jruby library should be used.  || .Net used Remoting technology to make it platform independent. || Serialization using the Boost libraries is not platform independent.&lt;br /&gt;
|-&lt;br /&gt;
| 3 || YAML provides a method [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/syck/rdoc/Object.html (to_yaml_properties)] which can be used to select the variables who's value is need to be serialized. With Marshal, we need to write a method named marshal_dump defining the variables of an object that has to be serialized. || Provides an option for serializing only the required 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 data 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 || Serialization using the Boost libraries is custom and thus the user can specify the part of the objects to be serialized.&lt;br /&gt;
|-&lt;br /&gt;
| 4 || Bindings, procedure objects, singleton objects, instances of IO objects and interfaces can not be serialized. Serializing these objects throws TypeError exceptions. || Thread, OutputStream and its subclasses, and Socket are not serializable in Java.[http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html] || Objects like DataRow are non serializable in .NET. Whether an object is serializable or not can be checked using the [http://msdn.microsoft.com/en-us/library/system.type.isserializable(v=vs.110).aspx?cs-save-lang=1&amp;amp;cs-lang=cpp#code-snippet-1 Type.IsSerializable] || There are no such objects in C++.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== See Also==&lt;br /&gt;
&lt;br /&gt;
1. [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/json/rdoc/JSON.html Serialization in Ruby JSON]&lt;br /&gt;
&lt;br /&gt;
2. [http://api.rubyonrails.org/classes/ActiveModel/Serialization.html Serialization in Rails]&lt;br /&gt;
&lt;br /&gt;
3. [http://gregmoreno.wordpress.com/2011/01/27/preventing-model-explosion-via-rails-serialization/ Article on Rails Serialization]&lt;br /&gt;
&lt;br /&gt;
4. [http://en.wikipedia.org/wiki/Google_Protocol_Buffers Protocol Buffers]&lt;br /&gt;
&lt;br /&gt;
5. [https://rubygems.org/gems/ruby-protocol-buffers Ruby Protocol Buffers Gem]&lt;br /&gt;
&lt;br /&gt;
6. [http://en.wikipedia.org/wiki/Apache_Avro Avro]&lt;br /&gt;
&lt;br /&gt;
7. [http://rubygems.org/gems/avro Ruby Avro Gem]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
1. [http://en.wikipedia.org/wiki/Serialization Serilization in General]&lt;br /&gt;
&lt;br /&gt;
2. [http://en.wikipedia.org/wiki/Marshalling_%28computer_science%29 Marshaling]&lt;br /&gt;
&lt;br /&gt;
3. [http://techbrahmana.blogspot.com/2012/03/rails-cookie-handling-serialization-and.html Rails Cookie Handling]&lt;br /&gt;
&lt;br /&gt;
4. [http://yaml.org/ YAML]&lt;br /&gt;
&lt;br /&gt;
5. [http://www.ruby-doc.org/core-2.1.0/Marshal.html Marshal module]&lt;br /&gt;
&lt;br /&gt;
6. [http://json.org/ JSON]&lt;br /&gt;
&lt;br /&gt;
7. [http://rubygems.org/ Ruby Gems]&lt;br /&gt;
&lt;br /&gt;
8. [http://ruby-doc.org/core-2.0.0/ARGF.html# Ruby ARGF Documentation]&lt;br /&gt;
&lt;br /&gt;
9. [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html Ruby Documentation for Benchmark Gem]&lt;br /&gt;
&lt;br /&gt;
10. [http://www.pauldix.net/2008/08/serializing-dat.html Serialization Performance Comparison]&lt;br /&gt;
&lt;br /&gt;
11. [http://www.boost.org/doc/libs/1_36_0/libs/serialization/example/demo.cpp Boost Libraries]&lt;br /&gt;
&lt;br /&gt;
12. [http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html Java Serialization]&lt;br /&gt;
&lt;br /&gt;
13. [http://www.w3schools.com/json/ JSON Tutorial]&lt;br /&gt;
&lt;br /&gt;
14. [http://www.skorks.com/2010/04/serializing-and-deserializing-objects-with-ruby/ Serializing and De-serializing in Ruby]&lt;br /&gt;
&lt;br /&gt;
15. [http://www.codeproject.com/Articles/33296/Serialization-and-De-serialization Serialization and De-serialization]&lt;br /&gt;
&lt;br /&gt;
16. [http://msdn.microsoft.com/en-us/library/182eeyhh.aspx XML Serialization]&lt;br /&gt;
&lt;br /&gt;
17. [http://www.tutorialspoint.com/java/java_serialization.htm Java Serialization Tutorial]&lt;br /&gt;
&lt;br /&gt;
18. [http://www.sapphiresteel.com/ruby-programming/The-Book-Of-Ruby The Book of Ruby]&lt;br /&gt;
&lt;br /&gt;
19. [http://www.codeproject.com/Articles/20962/Introducing-Serialization-in-NET Serialization in .Net]&lt;br /&gt;
&lt;br /&gt;
20. [http://ruby.about.com/od/advancedruby/ss/Serialization-In-Ruby-Yaml.html Serialization in Ruby YAML]&lt;br /&gt;
&lt;br /&gt;
21. [http://ruby.about.com/od/tasks/a/The-Json-Gem.html Installing JSON gem]&lt;br /&gt;
&lt;br /&gt;
22. [http://www.ruby-doc.org/stdlib-2.0.0/libdoc/json/rdoc/JSON.html Serialization in Ruby JSON]&lt;/div&gt;</summary>
		<author><name>Mmehta2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1d_mm&amp;diff=83419</id>
		<title>CSC/ECE 517 Spring 2014/ch1a 1d mm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1d_mm&amp;diff=83419"/>
		<updated>2014-02-18T02:48:12Z</updated>

		<summary type="html">&lt;p&gt;Mmehta2: /* Examples of serialization using YAML */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page discusses Serialization, focusing on Ruby programming language. It describes different methods of Serialization in Ruby. In addition, it also compares serialization in different Object Oriented languages.&lt;br /&gt;
&lt;br /&gt;
== '''Introduction''' ==&lt;br /&gt;
&lt;br /&gt;
==='''Serialization'''===&lt;br /&gt;
&lt;br /&gt;
'''Serialization'''[http://en.wikipedia.org/wiki/Serialization] is a process of converting a data structure or an object into a stream of bytes or string to facilitate storage in memory, file (persistence storage) or transmission over a network. The process of Serialization is also referred to as ''Marshalling''[http://en.wikipedia.org/wiki/Marshalling_%28computer_science%29]. 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. &lt;br /&gt;
&lt;br /&gt;
==='''De-serialization'''===&lt;br /&gt;
&lt;br /&gt;
'''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''.&lt;br /&gt;
&lt;br /&gt;
==='''Basic Advantages of Serialization'''===&lt;br /&gt;
&lt;br /&gt;
1. Communication between two or more processes on same machine. Object state can be saved and shared in a persistent or in-memory store.&lt;br /&gt;
&lt;br /&gt;
2. Communication between processes on different machines. Serialization facilitates the transmission of an object through a network.&lt;br /&gt;
&lt;br /&gt;
3. Creating a clone of an object. &lt;br /&gt;
&lt;br /&gt;
4. Cross-platform compatibility. Object can be serialized in a common format that is understood by multiple platforms. For example, JSON[http://www.json.org/] and XML.&lt;br /&gt;
&lt;br /&gt;
==='''Practical Applications for Serialization'''===&lt;br /&gt;
&lt;br /&gt;
1. [http://docs.oracle.com/cd/E13222_01/wls/docs81/webapp/sessions.html#139726 HTTP Session] Replication by sharing session objects across web servers for load balancing and handling failover scenarios.&lt;br /&gt;
&lt;br /&gt;
2. Serialization facilitates communication in [http://en.wikipedia.org/wiki/Java_remote_method_invocation Remote Method Invocation] or [http://en.wikipedia.org/wiki/Remote_procedure_call Remote procedure calls]&lt;br /&gt;
&lt;br /&gt;
3. Rails Cookie Handling[http://techbrahmana.blogspot.com/2012/03/rails-cookie-handling-serialization-and.html]. Cookies are stored marshalled/unmarshalled to and from client machines.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== '''Serialization in Ruby:''' ==&lt;br /&gt;
&lt;br /&gt;
Ruby provides serialization capabilities through its module, [http://www.ruby-doc.org/core-2.0.0/Marshal.html Marshal]. There are also other libraries like YAML[http://yaml.org/] and JSON which can be used in Ruby to generate serialized objects for purposes like platform independence and human readable formats.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:SerializationChart.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== '''Types of Serialization''' ==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
=== Converting Ruby Objects in Human Readable Formats ===&lt;br /&gt;
----&lt;br /&gt;
The conversion of Ruby objects into YAML and JSON formats are explained below.&lt;br /&gt;
&lt;br /&gt;
==== Converting Ruby Objects to YAML format ====&lt;br /&gt;
&lt;br /&gt;
YAML 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 binary format. 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. In order to use it in Ruby, the yaml.rb file is required to be loaded which provides methods for converting objects into yaml format and creating .yml files.&lt;br /&gt;
&lt;br /&gt;
=====Examples of serialization using YAML=====&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;nowiki&amp;gt; #Serialization using YAML's to_yaml method&lt;br /&gt;
 &lt;br /&gt;
  require &amp;quot;yaml&amp;quot;&lt;br /&gt;
     class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
	@name = name&lt;br /&gt;
	@age = age&lt;br /&gt;
	@country=country&lt;br /&gt;
     end&lt;br /&gt;
      def to_s&lt;br /&gt;
	&amp;quot;In First:\n#{@name}, #{@age}, #{@country}\n&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
  x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
  puts x&lt;br /&gt;
  puts x.to_yaml&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   In First:&lt;br /&gt;
   Tom, 25, USA&lt;br /&gt;
   --- !ruby/object:First&lt;br /&gt;
   name: Tom&lt;br /&gt;
   age: 25&lt;br /&gt;
   country: USA&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The above code displays the object x, first as a string and then in the yaml format.&lt;br /&gt;
&lt;br /&gt;
====='''Saving YAML data into a file:'''=====&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt; # Serialization using YAML::dump&lt;br /&gt;
&lt;br /&gt;
require 'yaml'&lt;br /&gt;
&lt;br /&gt;
f = File.open( 'first.yml', 'w' )&lt;br /&gt;
YAML.dump( [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;], f )&lt;br /&gt;
f.close&lt;br /&gt;
		&lt;br /&gt;
File.open( 'first.yml' ){ |f|	&lt;br /&gt;
    $arr= YAML.load(f)&lt;br /&gt;
}	&lt;br /&gt;
&lt;br /&gt;
p( $arr )&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;]&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The dump function can be used to serialize the data and save it into a file in YAML format. As shown in the above example the data in YAML format can be de-serialized using the load function.&lt;br /&gt;
&lt;br /&gt;
YAML libraries also provides an option of selecting only those variables of the object that are needed to be serialized. This is done using the to_yaml_properties method as shown in the below example.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=====Custom Serialization using YAML=====&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;nowiki&amp;gt;&lt;br /&gt;
    require &amp;quot;yaml&amp;quot;&lt;br /&gt;
     class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
	@name = name&lt;br /&gt;
	@age = age&lt;br /&gt;
	@country=country&lt;br /&gt;
     end&lt;br /&gt;
     def to_s&lt;br /&gt;
	&amp;quot;In First:\n#{@name}, #{@age}, #{@country}\n&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
      def to_yaml_properties&lt;br /&gt;
	   [&amp;quot;@name&amp;quot;,&amp;quot;@age&amp;quot;]  #@country will not be serialized&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
  x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
  puts x&lt;br /&gt;
  puts x.to_yaml&lt;br /&gt;
  &amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   In First:&lt;br /&gt;
   Tom, 25, USA&lt;br /&gt;
   --- !ruby/object:First&lt;br /&gt;
   name: Tom&lt;br /&gt;
   age: 25&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Converting Ruby Objects to JSON format: ====&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
JSON 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. Ruby 1.8.7 distribution is not bundled with json gem. However, in Ruby 1.9.2, the json gem is bundled with the core Ruby distribution.&lt;br /&gt;
The JSON library can be installed using Ruby Gems[http://rubygems.org/] as shown below:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;# gem install json&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A JSON string for serialization can be created by using the JSON.generate method:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
        require 'json'&lt;br /&gt;
        my_hash = {:Welcome =&amp;gt; &amp;quot;Ruby&amp;quot;}&lt;br /&gt;
        puts JSON.generate(my_hash) =&amp;gt; &amp;quot;{\&amp;quot;WELCOME\&amp;quot;:\&amp;quot;RUBY\&amp;quot;}&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
'''Output:'''&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;{&amp;quot;{\&amp;quot;Welcome\&amp;quot;:\&amp;quot;Ruby\&amp;quot;}&amp;quot;=&amp;gt;&amp;quot;{\&amp;quot;WELCOME\&amp;quot;:\&amp;quot;RUBY\&amp;quot;}&amp;quot;}&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A JSON string received from another program can be parsed by using JSON.parse&lt;br /&gt;
Ruby thus converts String to Hash.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
        require 'json'&lt;br /&gt;
        my_hash = JSON.parse('{&amp;quot;Welcome&amp;quot;: &amp;quot;Ruby&amp;quot;}')&lt;br /&gt;
        puts my_hash[&amp;quot;Welcome&amp;quot;] =&amp;gt; &amp;quot;Ruby&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Converting Ruby Objects to Binary Formats ===&lt;br /&gt;
----&lt;br /&gt;
Binary Serialization is another form of serialization in Ruby which is not in human readable form. 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. Binary Serialization  is done using Marshal which is built into Ruby and the code for it is written in Ruby's Marshal module(marshal.c) and thus no additional files are required in order to use it. The _dump and _load methods defined in marshal are used for serialization. Using marshal module the following types of objects can not be serialized: bindings, procedure objects, singleton objects, instances of IO objects and interfaces.&lt;br /&gt;
&lt;br /&gt;
Since the Binary Serialized data is not in human readable form, the following guidelines need to be followed.&lt;br /&gt;
     1.Use print instead of puts[http://ruby-doc.org/core-2.0.0/ARGF.html#] when serialized objects are written to a file in order to avoid new line characters to be written &lt;br /&gt;
       in the file.&lt;br /&gt;
     &lt;br /&gt;
     2.Use a record separator in order to differentiate between two objects.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====='''Binary Serialization Example:'''=====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    class Animal&lt;br /&gt;
      def initialize  name, age&lt;br /&gt;
      @name = name&lt;br /&gt;
      @age=age&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
   class Cat &amp;lt; Animal&lt;br /&gt;
    def to_s&lt;br /&gt;
     &amp;quot;In Cat C: #{@name} \t #{@age}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
  c = Cat.new(&amp;quot;Kitty Kat&amp;quot;,5)&lt;br /&gt;
  puts &amp;quot;Before Serialization&amp;quot;&lt;br /&gt;
  puts c&lt;br /&gt;
  #puts d&lt;br /&gt;
  serialize_cat= Marshal.dump(c) #dumps the serialized cat object into serialize_cat&lt;br /&gt;
  puts &amp;quot;\nAfter Serialization:\n #{serialize_cat}&amp;quot;&lt;br /&gt;
  deserialize_cat= Marshal::load(serialize_cat) #deserializes the cat object and loads it back into deserialize_cat&lt;br /&gt;
  puts &amp;quot;\nAfter Deserialization\n #{deserialize_cat}&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   Before Serialization&lt;br /&gt;
   In Cat C: Kitty Kat 	 5&lt;br /&gt;
   After Serialization:&lt;br /&gt;
   oCat:&lt;br /&gt;
   @nameI&amp;quot;Kitty Kat:ET:	@agei&lt;br /&gt;
   After Deserialization&lt;br /&gt;
   In Cat C: Kitty Kat 	 5&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====='''Saving Marshal data into a file'''=====&lt;br /&gt;
&lt;br /&gt;
Similar to YAML, Marshal can also be used to dump data into a file. The above example showing the serialization using YAML::dump can be written using marshal as shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    f = File.open( 'first.yml', 'w' )&lt;br /&gt;
    Marshal.dump( [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;], f )&lt;br /&gt;
    f.close	&lt;br /&gt;
    File.open( 'first.yml' ){ |f|	&lt;br /&gt;
    $arr= Marshal.load(f)&lt;br /&gt;
    }	&lt;br /&gt;
    p( $arr )&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;]&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice that in the above example there is no &amp;quot;require&amp;quot; statement as opposed to the earlier example of writing serialized data into files using YAML. This is because unlike YAML, marshal is built-in Ruby and  no external libraries is required in order to use its functionality.&lt;br /&gt;
&lt;br /&gt;
====='''Custom serialization using Marshal'''=====&lt;br /&gt;
&lt;br /&gt;
Marshal can also be used to custom serialize an object i.e. it provides an option to omit the variables of an object that are not required in the serialized data. The following program shows the use of marshal_dump method for achieving custom serialization.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
      class First&lt;br /&gt;
      def initialize(name, age, country)&lt;br /&gt;
         @name = name&lt;br /&gt;
         @age = age&lt;br /&gt;
         @country=country&lt;br /&gt;
      end&lt;br /&gt;
      def to_s&lt;br /&gt;
            &amp;quot;In First: #{@name}, #{@age}, #{@country}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
      def marshal_dump&lt;br /&gt;
            [@name,@age]  #@country will not be serialized&lt;br /&gt;
      end&lt;br /&gt;
      def marshal_load(data)&lt;br /&gt;
       @name=data[0]&lt;br /&gt;
       @age=data[1]&lt;br /&gt;
       @country=&amp;quot;United States of America&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
      end&lt;br /&gt;
 &lt;br /&gt;
      x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
      puts x&lt;br /&gt;
  &lt;br /&gt;
      marshal_data = Marshal.dump( x ) &lt;br /&gt;
      y = Marshal.load( marshal_data ) &lt;br /&gt;
      p( y.to_s )&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    In First: Tom, 25, USA&lt;br /&gt;
    &amp;quot;In First: Tom, 25, United States of America&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== '''Serialization Performance in Ruby''' ==&lt;br /&gt;
The different serialization formats described above differ in the efficiency at which they can serialize and deserialize data and thus while serializing large amount of data their efficiency is taken into account. The .report[http://www.ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html] method in Ruby can be used to evaluate the performance of these serialization patterns. Marshal, as it handles data in binary format, is the most efficient form of serialization in Ruby. The following example compares the performance of marshal, JSON and YAML format.[http://www.pauldix.net/2008/08/serializing-dat.html]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   require 'benchmark'&lt;br /&gt;
   require 'rubygems'&lt;br /&gt;
   require 'json'&lt;br /&gt;
   require 'yaml'&lt;br /&gt;
   include Benchmark&lt;br /&gt;
   class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
      @name=name&lt;br /&gt;
      @age=age&lt;br /&gt;
      @country=country&lt;br /&gt;
     end&lt;br /&gt;
   end  &lt;br /&gt;
    x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
    puts &amp;quot;            User     System     Total      Real&amp;quot;&lt;br /&gt;
   benchmark do |t|&lt;br /&gt;
    print &amp;quot;Marshal:&amp;quot;&lt;br /&gt;
    t.report{1000.times do; Marshal.load(Marshal.dump(x));end}&lt;br /&gt;
    print &amp;quot;JSON:   &amp;quot;&lt;br /&gt;
    t.report{1000.times do; JSON.load(JSON.dump(x));end}&lt;br /&gt;
    print &amp;quot;YAML:   &amp;quot;&lt;br /&gt;
    t.report{1000.times do; YAML.load(YAML.dump(x));end}&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
              User     System     Total      Real&lt;br /&gt;
  Marshal:   0.010000   0.000000   0.010000 (  0.015408)&lt;br /&gt;
  JSON:      0.020000   0.010000   0.030000 (  0.022462)&lt;br /&gt;
  YAML:      0.460000   0.010000   0.470000 (  0.472149)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output of the above example shows that, given large chunks of data, marshal is comparatively more efficient format for serialization in Ruby.&lt;br /&gt;
&lt;br /&gt;
== Serialization in Object-Oriented Languages: Comparison ==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Sl.No !! Ruby !! Java !! .Net Framework !! C++&lt;br /&gt;
|-&lt;br /&gt;
| 1 || Ruby provides a built in module called 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 || Although, there is no built in support for serialization in C++, it can be achieved by using Boost libraries[http://www.boost.org/doc/libs/1_36_0/libs/serialization/example/demo.cpp]&lt;br /&gt;
|-&lt;br /&gt;
| 2 || The built in module of Ruby (Marshal) does not support platform independence, however, it can be achieved by using external libraries like YAML and JSON || Similarly, Java's built in serialization is also not platform independent and in order to use serialization in Java across Ruby platform, jruby library should be used.  || .Net used Remoting technology to make it platform independent. || Serialization using the Boost libraries is not platform independent.&lt;br /&gt;
|-&lt;br /&gt;
| 3 || YAML provides a method [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/syck/rdoc/Object.html (to_yaml_properties)] which can be used to select the variables who's value is need to be serialized. With Marshal, we need to write a method named marshal_dump defining the variables of an object that has to be serialized. || Provides an option for serializing only the required 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 data 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 || Serialization using the Boost libraries is custom and thus the user can specify the part of the objects to be serialized.&lt;br /&gt;
|-&lt;br /&gt;
| 4 || Bindings, procedure objects, singleton objects, instances of IO objects and interfaces can not be serialized. Serializing these objects throws TypeError exceptions. || Thread, OutputStream and its subclasses, and Socket are not serializable in Java.[http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html] || Objects like DataRow are non serializable in .NET. Whether an object is serializable or not can be checked using the [http://msdn.microsoft.com/en-us/library/system.type.isserializable(v=vs.110).aspx?cs-save-lang=1&amp;amp;cs-lang=cpp#code-snippet-1 Type.IsSerializable] || There are no such objects in C++.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== See Also==&lt;br /&gt;
&lt;br /&gt;
1. [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/json/rdoc/JSON.html Serialization in Ruby JSON]&lt;br /&gt;
&lt;br /&gt;
2. [http://api.rubyonrails.org/classes/ActiveModel/Serialization.html Serialization in Rails]&lt;br /&gt;
&lt;br /&gt;
3. [http://gregmoreno.wordpress.com/2011/01/27/preventing-model-explosion-via-rails-serialization/ Article on Rails Serialization]&lt;br /&gt;
&lt;br /&gt;
4. [http://en.wikipedia.org/wiki/Google_Protocol_Buffers Protocol Buffers]&lt;br /&gt;
&lt;br /&gt;
5. [https://rubygems.org/gems/ruby-protocol-buffers Ruby Protocol Buffers Gem]&lt;br /&gt;
&lt;br /&gt;
6. [http://en.wikipedia.org/wiki/Apache_Avro Avro]&lt;br /&gt;
&lt;br /&gt;
7. [http://rubygems.org/gems/avro Ruby Avro Gem]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
1. [http://en.wikipedia.org/wiki/Serialization Serilization in General]&lt;br /&gt;
&lt;br /&gt;
2. [http://en.wikipedia.org/wiki/Marshalling_%28computer_science%29 Marshaling]&lt;br /&gt;
&lt;br /&gt;
3. [http://techbrahmana.blogspot.com/2012/03/rails-cookie-handling-serialization-and.html Rails Cookie Handling]&lt;br /&gt;
&lt;br /&gt;
4. [http://yaml.org/ YAML]&lt;br /&gt;
&lt;br /&gt;
5. [http://www.ruby-doc.org/core-2.1.0/Marshal.html Marshal module]&lt;br /&gt;
&lt;br /&gt;
6. [http://json.org/ JSON]&lt;br /&gt;
&lt;br /&gt;
7. [http://rubygems.org/ Ruby Gems]&lt;br /&gt;
&lt;br /&gt;
8. [http://ruby-doc.org/core-2.0.0/ARGF.html# Ruby ARGF Documentation]&lt;br /&gt;
&lt;br /&gt;
9. [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html Ruby Documentation for Benchmark Gem]&lt;br /&gt;
&lt;br /&gt;
10. [http://www.pauldix.net/2008/08/serializing-dat.html Serialization Performance Comparison]&lt;br /&gt;
&lt;br /&gt;
11. [http://www.boost.org/doc/libs/1_36_0/libs/serialization/example/demo.cpp Boost Libraries]&lt;br /&gt;
&lt;br /&gt;
12. [http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html Java Serialization]&lt;br /&gt;
&lt;br /&gt;
13. [http://www.w3schools.com/json/ JSON Tutorial]&lt;br /&gt;
&lt;br /&gt;
14. [http://www.skorks.com/2010/04/serializing-and-deserializing-objects-with-ruby/ Serializing and De-serializing in Ruby]&lt;br /&gt;
&lt;br /&gt;
15. [http://www.codeproject.com/Articles/33296/Serialization-and-De-serialization Serialization and De-serialization]&lt;br /&gt;
&lt;br /&gt;
16. [http://msdn.microsoft.com/en-us/library/182eeyhh.aspx XML Serialization]&lt;br /&gt;
&lt;br /&gt;
17. [http://www.tutorialspoint.com/java/java_serialization.htm Java Serialization Tutorial]&lt;br /&gt;
&lt;br /&gt;
18. [http://www.sapphiresteel.com/ruby-programming/The-Book-Of-Ruby The Book of Ruby]&lt;br /&gt;
&lt;br /&gt;
19. [http://www.codeproject.com/Articles/20962/Introducing-Serialization-in-NET Serialization in .Net]&lt;br /&gt;
&lt;br /&gt;
20. [http://ruby.about.com/od/advancedruby/ss/Serialization-In-Ruby-Yaml.html Serialization in Ruby YAML]&lt;br /&gt;
&lt;br /&gt;
21. [http://ruby.about.com/od/tasks/a/The-Json-Gem.html Installing JSON gem]&lt;br /&gt;
&lt;br /&gt;
22. [http://www.ruby-doc.org/stdlib-2.0.0/libdoc/json/rdoc/JSON.html Serialization in Ruby JSON]&lt;/div&gt;</summary>
		<author><name>Mmehta2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1d_mm&amp;diff=83418</id>
		<title>CSC/ECE 517 Spring 2014/ch1a 1d mm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1d_mm&amp;diff=83418"/>
		<updated>2014-02-18T02:47:38Z</updated>

		<summary type="html">&lt;p&gt;Mmehta2: /* Examples of serialization using YAML: */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page discusses Serialization, focusing on Ruby programming language. It describes different methods of Serialization in Ruby. In addition, it also compares serialization in different Object Oriented languages.&lt;br /&gt;
&lt;br /&gt;
== '''Introduction''' ==&lt;br /&gt;
&lt;br /&gt;
==='''Serialization'''===&lt;br /&gt;
&lt;br /&gt;
'''Serialization'''[http://en.wikipedia.org/wiki/Serialization] is a process of converting a data structure or an object into a stream of bytes or string to facilitate storage in memory, file (persistence storage) or transmission over a network. The process of Serialization is also referred to as ''Marshalling''[http://en.wikipedia.org/wiki/Marshalling_%28computer_science%29]. 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. &lt;br /&gt;
&lt;br /&gt;
==='''De-serialization'''===&lt;br /&gt;
&lt;br /&gt;
'''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''.&lt;br /&gt;
&lt;br /&gt;
==='''Basic Advantages of Serialization'''===&lt;br /&gt;
&lt;br /&gt;
1. Communication between two or more processes on same machine. Object state can be saved and shared in a persistent or in-memory store.&lt;br /&gt;
&lt;br /&gt;
2. Communication between processes on different machines. Serialization facilitates the transmission of an object through a network.&lt;br /&gt;
&lt;br /&gt;
3. Creating a clone of an object. &lt;br /&gt;
&lt;br /&gt;
4. Cross-platform compatibility. Object can be serialized in a common format that is understood by multiple platforms. For example, JSON[http://www.json.org/] and XML.&lt;br /&gt;
&lt;br /&gt;
==='''Practical Applications for Serialization'''===&lt;br /&gt;
&lt;br /&gt;
1. [http://docs.oracle.com/cd/E13222_01/wls/docs81/webapp/sessions.html#139726 HTTP Session] Replication by sharing session objects across web servers for load balancing and handling failover scenarios.&lt;br /&gt;
&lt;br /&gt;
2. Serialization facilitates communication in [http://en.wikipedia.org/wiki/Java_remote_method_invocation Remote Method Invocation] or [http://en.wikipedia.org/wiki/Remote_procedure_call Remote procedure calls]&lt;br /&gt;
&lt;br /&gt;
3. Rails Cookie Handling[http://techbrahmana.blogspot.com/2012/03/rails-cookie-handling-serialization-and.html]. Cookies are stored marshalled/unmarshalled to and from client machines.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== '''Serialization in Ruby:''' ==&lt;br /&gt;
&lt;br /&gt;
Ruby provides serialization capabilities through its module, [http://www.ruby-doc.org/core-2.0.0/Marshal.html Marshal]. There are also other libraries like YAML[http://yaml.org/] and JSON which can be used in Ruby to generate serialized objects for purposes like platform independence and human readable formats.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:SerializationChart.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== '''Types of Serialization''' ==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
=== Converting Ruby Objects in Human Readable Formats ===&lt;br /&gt;
----&lt;br /&gt;
The conversion of Ruby objects into YAML and JSON formats are explained below.&lt;br /&gt;
&lt;br /&gt;
==== Converting Ruby Objects to YAML format ====&lt;br /&gt;
&lt;br /&gt;
YAML 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 binary format. 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. In order to use it in Ruby, the yaml.rb file is required to be loaded which provides methods for converting objects into yaml format and creating .yml files.&lt;br /&gt;
&lt;br /&gt;
====='''Examples of serialization using YAML'''=====&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;nowiki&amp;gt; #Serialization using YAML's to_yaml method&lt;br /&gt;
 &lt;br /&gt;
  require &amp;quot;yaml&amp;quot;&lt;br /&gt;
     class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
	@name = name&lt;br /&gt;
	@age = age&lt;br /&gt;
	@country=country&lt;br /&gt;
     end&lt;br /&gt;
      def to_s&lt;br /&gt;
	&amp;quot;In First:\n#{@name}, #{@age}, #{@country}\n&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
  x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
  puts x&lt;br /&gt;
  puts x.to_yaml&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   In First:&lt;br /&gt;
   Tom, 25, USA&lt;br /&gt;
   --- !ruby/object:First&lt;br /&gt;
   name: Tom&lt;br /&gt;
   age: 25&lt;br /&gt;
   country: USA&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The above code displays the object x, first as a string and then in the yaml format.&lt;br /&gt;
&lt;br /&gt;
====='''Saving YAML data into a file:'''=====&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt; # Serialization using YAML::dump&lt;br /&gt;
&lt;br /&gt;
require 'yaml'&lt;br /&gt;
&lt;br /&gt;
f = File.open( 'first.yml', 'w' )&lt;br /&gt;
YAML.dump( [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;], f )&lt;br /&gt;
f.close&lt;br /&gt;
		&lt;br /&gt;
File.open( 'first.yml' ){ |f|	&lt;br /&gt;
    $arr= YAML.load(f)&lt;br /&gt;
}	&lt;br /&gt;
&lt;br /&gt;
p( $arr )&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;]&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The dump function can be used to serialize the data and save it into a file in YAML format. As shown in the above example the data in YAML format can be de-serialized using the load function.&lt;br /&gt;
&lt;br /&gt;
YAML libraries also provides an option of selecting only those variables of the object that are needed to be serialized. This is done using the to_yaml_properties method as shown in the below example.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=====Custom Serialization using YAML=====&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;nowiki&amp;gt;&lt;br /&gt;
    require &amp;quot;yaml&amp;quot;&lt;br /&gt;
     class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
	@name = name&lt;br /&gt;
	@age = age&lt;br /&gt;
	@country=country&lt;br /&gt;
     end&lt;br /&gt;
     def to_s&lt;br /&gt;
	&amp;quot;In First:\n#{@name}, #{@age}, #{@country}\n&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
      def to_yaml_properties&lt;br /&gt;
	   [&amp;quot;@name&amp;quot;,&amp;quot;@age&amp;quot;]  #@country will not be serialized&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
  x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
  puts x&lt;br /&gt;
  puts x.to_yaml&lt;br /&gt;
  &amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   In First:&lt;br /&gt;
   Tom, 25, USA&lt;br /&gt;
   --- !ruby/object:First&lt;br /&gt;
   name: Tom&lt;br /&gt;
   age: 25&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Converting Ruby Objects to JSON format: ====&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
JSON 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. Ruby 1.8.7 distribution is not bundled with json gem. However, in Ruby 1.9.2, the json gem is bundled with the core Ruby distribution.&lt;br /&gt;
The JSON library can be installed using Ruby Gems[http://rubygems.org/] as shown below:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;# gem install json&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A JSON string for serialization can be created by using the JSON.generate method:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
        require 'json'&lt;br /&gt;
        my_hash = {:Welcome =&amp;gt; &amp;quot;Ruby&amp;quot;}&lt;br /&gt;
        puts JSON.generate(my_hash) =&amp;gt; &amp;quot;{\&amp;quot;WELCOME\&amp;quot;:\&amp;quot;RUBY\&amp;quot;}&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
'''Output:'''&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;{&amp;quot;{\&amp;quot;Welcome\&amp;quot;:\&amp;quot;Ruby\&amp;quot;}&amp;quot;=&amp;gt;&amp;quot;{\&amp;quot;WELCOME\&amp;quot;:\&amp;quot;RUBY\&amp;quot;}&amp;quot;}&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A JSON string received from another program can be parsed by using JSON.parse&lt;br /&gt;
Ruby thus converts String to Hash.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
        require 'json'&lt;br /&gt;
        my_hash = JSON.parse('{&amp;quot;Welcome&amp;quot;: &amp;quot;Ruby&amp;quot;}')&lt;br /&gt;
        puts my_hash[&amp;quot;Welcome&amp;quot;] =&amp;gt; &amp;quot;Ruby&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Converting Ruby Objects to Binary Formats ===&lt;br /&gt;
----&lt;br /&gt;
Binary Serialization is another form of serialization in Ruby which is not in human readable form. 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. Binary Serialization  is done using Marshal which is built into Ruby and the code for it is written in Ruby's Marshal module(marshal.c) and thus no additional files are required in order to use it. The _dump and _load methods defined in marshal are used for serialization. Using marshal module the following types of objects can not be serialized: bindings, procedure objects, singleton objects, instances of IO objects and interfaces.&lt;br /&gt;
&lt;br /&gt;
Since the Binary Serialized data is not in human readable form, the following guidelines need to be followed.&lt;br /&gt;
     1.Use print instead of puts[http://ruby-doc.org/core-2.0.0/ARGF.html#] when serialized objects are written to a file in order to avoid new line characters to be written &lt;br /&gt;
       in the file.&lt;br /&gt;
     &lt;br /&gt;
     2.Use a record separator in order to differentiate between two objects.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====='''Binary Serialization Example:'''=====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    class Animal&lt;br /&gt;
      def initialize  name, age&lt;br /&gt;
      @name = name&lt;br /&gt;
      @age=age&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
   class Cat &amp;lt; Animal&lt;br /&gt;
    def to_s&lt;br /&gt;
     &amp;quot;In Cat C: #{@name} \t #{@age}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
  c = Cat.new(&amp;quot;Kitty Kat&amp;quot;,5)&lt;br /&gt;
  puts &amp;quot;Before Serialization&amp;quot;&lt;br /&gt;
  puts c&lt;br /&gt;
  #puts d&lt;br /&gt;
  serialize_cat= Marshal.dump(c) #dumps the serialized cat object into serialize_cat&lt;br /&gt;
  puts &amp;quot;\nAfter Serialization:\n #{serialize_cat}&amp;quot;&lt;br /&gt;
  deserialize_cat= Marshal::load(serialize_cat) #deserializes the cat object and loads it back into deserialize_cat&lt;br /&gt;
  puts &amp;quot;\nAfter Deserialization\n #{deserialize_cat}&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   Before Serialization&lt;br /&gt;
   In Cat C: Kitty Kat 	 5&lt;br /&gt;
   After Serialization:&lt;br /&gt;
   oCat:&lt;br /&gt;
   @nameI&amp;quot;Kitty Kat:ET:	@agei&lt;br /&gt;
   After Deserialization&lt;br /&gt;
   In Cat C: Kitty Kat 	 5&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====='''Saving Marshal data into a file'''=====&lt;br /&gt;
&lt;br /&gt;
Similar to YAML, Marshal can also be used to dump data into a file. The above example showing the serialization using YAML::dump can be written using marshal as shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    f = File.open( 'first.yml', 'w' )&lt;br /&gt;
    Marshal.dump( [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;], f )&lt;br /&gt;
    f.close	&lt;br /&gt;
    File.open( 'first.yml' ){ |f|	&lt;br /&gt;
    $arr= Marshal.load(f)&lt;br /&gt;
    }	&lt;br /&gt;
    p( $arr )&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;]&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice that in the above example there is no &amp;quot;require&amp;quot; statement as opposed to the earlier example of writing serialized data into files using YAML. This is because unlike YAML, marshal is built-in Ruby and  no external libraries is required in order to use its functionality.&lt;br /&gt;
&lt;br /&gt;
====='''Custom serialization using Marshal'''=====&lt;br /&gt;
&lt;br /&gt;
Marshal can also be used to custom serialize an object i.e. it provides an option to omit the variables of an object that are not required in the serialized data. The following program shows the use of marshal_dump method for achieving custom serialization.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
      class First&lt;br /&gt;
      def initialize(name, age, country)&lt;br /&gt;
         @name = name&lt;br /&gt;
         @age = age&lt;br /&gt;
         @country=country&lt;br /&gt;
      end&lt;br /&gt;
      def to_s&lt;br /&gt;
            &amp;quot;In First: #{@name}, #{@age}, #{@country}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
      def marshal_dump&lt;br /&gt;
            [@name,@age]  #@country will not be serialized&lt;br /&gt;
      end&lt;br /&gt;
      def marshal_load(data)&lt;br /&gt;
       @name=data[0]&lt;br /&gt;
       @age=data[1]&lt;br /&gt;
       @country=&amp;quot;United States of America&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
      end&lt;br /&gt;
 &lt;br /&gt;
      x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
      puts x&lt;br /&gt;
  &lt;br /&gt;
      marshal_data = Marshal.dump( x ) &lt;br /&gt;
      y = Marshal.load( marshal_data ) &lt;br /&gt;
      p( y.to_s )&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    In First: Tom, 25, USA&lt;br /&gt;
    &amp;quot;In First: Tom, 25, United States of America&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== '''Serialization Performance in Ruby''' ==&lt;br /&gt;
The different serialization formats described above differ in the efficiency at which they can serialize and deserialize data and thus while serializing large amount of data their efficiency is taken into account. The .report[http://www.ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html] method in Ruby can be used to evaluate the performance of these serialization patterns. Marshal, as it handles data in binary format, is the most efficient form of serialization in Ruby. The following example compares the performance of marshal, JSON and YAML format.[http://www.pauldix.net/2008/08/serializing-dat.html]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   require 'benchmark'&lt;br /&gt;
   require 'rubygems'&lt;br /&gt;
   require 'json'&lt;br /&gt;
   require 'yaml'&lt;br /&gt;
   include Benchmark&lt;br /&gt;
   class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
      @name=name&lt;br /&gt;
      @age=age&lt;br /&gt;
      @country=country&lt;br /&gt;
     end&lt;br /&gt;
   end  &lt;br /&gt;
    x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
    puts &amp;quot;            User     System     Total      Real&amp;quot;&lt;br /&gt;
   benchmark do |t|&lt;br /&gt;
    print &amp;quot;Marshal:&amp;quot;&lt;br /&gt;
    t.report{1000.times do; Marshal.load(Marshal.dump(x));end}&lt;br /&gt;
    print &amp;quot;JSON:   &amp;quot;&lt;br /&gt;
    t.report{1000.times do; JSON.load(JSON.dump(x));end}&lt;br /&gt;
    print &amp;quot;YAML:   &amp;quot;&lt;br /&gt;
    t.report{1000.times do; YAML.load(YAML.dump(x));end}&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
              User     System     Total      Real&lt;br /&gt;
  Marshal:   0.010000   0.000000   0.010000 (  0.015408)&lt;br /&gt;
  JSON:      0.020000   0.010000   0.030000 (  0.022462)&lt;br /&gt;
  YAML:      0.460000   0.010000   0.470000 (  0.472149)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output of the above example shows that, given large chunks of data, marshal is comparatively more efficient format for serialization in Ruby.&lt;br /&gt;
&lt;br /&gt;
== Serialization in Object-Oriented Languages: Comparison ==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Sl.No !! Ruby !! Java !! .Net Framework !! C++&lt;br /&gt;
|-&lt;br /&gt;
| 1 || Ruby provides a built in module called 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 || Although, there is no built in support for serialization in C++, it can be achieved by using Boost libraries[http://www.boost.org/doc/libs/1_36_0/libs/serialization/example/demo.cpp]&lt;br /&gt;
|-&lt;br /&gt;
| 2 || The built in module of Ruby (Marshal) does not support platform independence, however, it can be achieved by using external libraries like YAML and JSON || Similarly, Java's built in serialization is also not platform independent and in order to use serialization in Java across Ruby platform, jruby library should be used.  || .Net used Remoting technology to make it platform independent. || Serialization using the Boost libraries is not platform independent.&lt;br /&gt;
|-&lt;br /&gt;
| 3 || YAML provides a method [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/syck/rdoc/Object.html (to_yaml_properties)] which can be used to select the variables who's value is need to be serialized. With Marshal, we need to write a method named marshal_dump defining the variables of an object that has to be serialized. || Provides an option for serializing only the required 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 data 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 || Serialization using the Boost libraries is custom and thus the user can specify the part of the objects to be serialized.&lt;br /&gt;
|-&lt;br /&gt;
| 4 || Bindings, procedure objects, singleton objects, instances of IO objects and interfaces can not be serialized. Serializing these objects throws TypeError exceptions. || Thread, OutputStream and its subclasses, and Socket are not serializable in Java.[http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html] || Objects like DataRow are non serializable in .NET. Whether an object is serializable or not can be checked using the [http://msdn.microsoft.com/en-us/library/system.type.isserializable(v=vs.110).aspx?cs-save-lang=1&amp;amp;cs-lang=cpp#code-snippet-1 Type.IsSerializable] || There are no such objects in C++.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== See Also==&lt;br /&gt;
&lt;br /&gt;
1. [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/json/rdoc/JSON.html Serialization in Ruby JSON]&lt;br /&gt;
&lt;br /&gt;
2. [http://api.rubyonrails.org/classes/ActiveModel/Serialization.html Serialization in Rails]&lt;br /&gt;
&lt;br /&gt;
3. [http://gregmoreno.wordpress.com/2011/01/27/preventing-model-explosion-via-rails-serialization/ Article on Rails Serialization]&lt;br /&gt;
&lt;br /&gt;
4. [http://en.wikipedia.org/wiki/Google_Protocol_Buffers Protocol Buffers]&lt;br /&gt;
&lt;br /&gt;
5. [https://rubygems.org/gems/ruby-protocol-buffers Ruby Protocol Buffers Gem]&lt;br /&gt;
&lt;br /&gt;
6. [http://en.wikipedia.org/wiki/Apache_Avro Avro]&lt;br /&gt;
&lt;br /&gt;
7. [http://rubygems.org/gems/avro Ruby Avro Gem]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
1. [http://en.wikipedia.org/wiki/Serialization Serilization in General]&lt;br /&gt;
&lt;br /&gt;
2. [http://en.wikipedia.org/wiki/Marshalling_%28computer_science%29 Marshaling]&lt;br /&gt;
&lt;br /&gt;
3. [http://techbrahmana.blogspot.com/2012/03/rails-cookie-handling-serialization-and.html Rails Cookie Handling]&lt;br /&gt;
&lt;br /&gt;
4. [http://yaml.org/ YAML]&lt;br /&gt;
&lt;br /&gt;
5. [http://www.ruby-doc.org/core-2.1.0/Marshal.html Marshal module]&lt;br /&gt;
&lt;br /&gt;
6. [http://json.org/ JSON]&lt;br /&gt;
&lt;br /&gt;
7. [http://rubygems.org/ Ruby Gems]&lt;br /&gt;
&lt;br /&gt;
8. [http://ruby-doc.org/core-2.0.0/ARGF.html# Ruby ARGF Documentation]&lt;br /&gt;
&lt;br /&gt;
9. [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html Ruby Documentation for Benchmark Gem]&lt;br /&gt;
&lt;br /&gt;
10. [http://www.pauldix.net/2008/08/serializing-dat.html Serialization Performance Comparison]&lt;br /&gt;
&lt;br /&gt;
11. [http://www.boost.org/doc/libs/1_36_0/libs/serialization/example/demo.cpp Boost Libraries]&lt;br /&gt;
&lt;br /&gt;
12. [http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html Java Serialization]&lt;br /&gt;
&lt;br /&gt;
13. [http://www.w3schools.com/json/ JSON Tutorial]&lt;br /&gt;
&lt;br /&gt;
14. [http://www.skorks.com/2010/04/serializing-and-deserializing-objects-with-ruby/ Serializing and De-serializing in Ruby]&lt;br /&gt;
&lt;br /&gt;
15. [http://www.codeproject.com/Articles/33296/Serialization-and-De-serialization Serialization and De-serialization]&lt;br /&gt;
&lt;br /&gt;
16. [http://msdn.microsoft.com/en-us/library/182eeyhh.aspx XML Serialization]&lt;br /&gt;
&lt;br /&gt;
17. [http://www.tutorialspoint.com/java/java_serialization.htm Java Serialization Tutorial]&lt;br /&gt;
&lt;br /&gt;
18. [http://www.sapphiresteel.com/ruby-programming/The-Book-Of-Ruby The Book of Ruby]&lt;br /&gt;
&lt;br /&gt;
19. [http://www.codeproject.com/Articles/20962/Introducing-Serialization-in-NET Serialization in .Net]&lt;br /&gt;
&lt;br /&gt;
20. [http://ruby.about.com/od/advancedruby/ss/Serialization-In-Ruby-Yaml.html Serialization in Ruby YAML]&lt;br /&gt;
&lt;br /&gt;
21. [http://ruby.about.com/od/tasks/a/The-Json-Gem.html Installing JSON gem]&lt;br /&gt;
&lt;br /&gt;
22. [http://www.ruby-doc.org/stdlib-2.0.0/libdoc/json/rdoc/JSON.html Serialization in Ruby JSON]&lt;/div&gt;</summary>
		<author><name>Mmehta2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1d_mm&amp;diff=83417</id>
		<title>CSC/ECE 517 Spring 2014/ch1a 1d mm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1d_mm&amp;diff=83417"/>
		<updated>2014-02-18T02:45:13Z</updated>

		<summary type="html">&lt;p&gt;Mmehta2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page discusses Serialization, focusing on Ruby programming language. It describes different methods of Serialization in Ruby. In addition, it also compares serialization in different Object Oriented languages.&lt;br /&gt;
&lt;br /&gt;
== '''Introduction''' ==&lt;br /&gt;
&lt;br /&gt;
==='''Serialization'''===&lt;br /&gt;
&lt;br /&gt;
'''Serialization'''[http://en.wikipedia.org/wiki/Serialization] is a process of converting a data structure or an object into a stream of bytes or string to facilitate storage in memory, file (persistence storage) or transmission over a network. The process of Serialization is also referred to as ''Marshalling''[http://en.wikipedia.org/wiki/Marshalling_%28computer_science%29]. 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. &lt;br /&gt;
&lt;br /&gt;
==='''De-serialization'''===&lt;br /&gt;
&lt;br /&gt;
'''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''.&lt;br /&gt;
&lt;br /&gt;
==='''Basic Advantages of Serialization'''===&lt;br /&gt;
&lt;br /&gt;
1. Communication between two or more processes on same machine. Object state can be saved and shared in a persistent or in-memory store.&lt;br /&gt;
&lt;br /&gt;
2. Communication between processes on different machines. Serialization facilitates the transmission of an object through a network.&lt;br /&gt;
&lt;br /&gt;
3. Creating a clone of an object. &lt;br /&gt;
&lt;br /&gt;
4. Cross-platform compatibility. Object can be serialized in a common format that is understood by multiple platforms. For example, JSON[http://www.json.org/] and XML.&lt;br /&gt;
&lt;br /&gt;
==='''Practical Applications for Serialization'''===&lt;br /&gt;
&lt;br /&gt;
1. [http://docs.oracle.com/cd/E13222_01/wls/docs81/webapp/sessions.html#139726 HTTP Session] Replication by sharing session objects across web servers for load balancing and handling failover scenarios.&lt;br /&gt;
&lt;br /&gt;
2. Serialization facilitates communication in [http://en.wikipedia.org/wiki/Java_remote_method_invocation Remote Method Invocation] or [http://en.wikipedia.org/wiki/Remote_procedure_call Remote procedure calls]&lt;br /&gt;
&lt;br /&gt;
3. Rails Cookie Handling[http://techbrahmana.blogspot.com/2012/03/rails-cookie-handling-serialization-and.html]. Cookies are stored marshalled/unmarshalled to and from client machines.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== '''Serialization in Ruby:''' ==&lt;br /&gt;
&lt;br /&gt;
Ruby provides serialization capabilities through its module, [http://www.ruby-doc.org/core-2.0.0/Marshal.html Marshal]. There are also other libraries like YAML[http://yaml.org/] and JSON which can be used in Ruby to generate serialized objects for purposes like platform independence and human readable formats.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:SerializationChart.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== '''Types of Serialization''' ==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
=== Converting Ruby Objects in Human Readable Formats ===&lt;br /&gt;
----&lt;br /&gt;
The conversion of Ruby objects into YAML and JSON formats are explained below.&lt;br /&gt;
&lt;br /&gt;
==== Converting Ruby Objects to YAML format ====&lt;br /&gt;
&lt;br /&gt;
YAML 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 binary format. 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. In order to use it in Ruby, the yaml.rb file is required to be loaded which provides methods for converting objects into yaml format and creating .yml files.&lt;br /&gt;
&lt;br /&gt;
====='''Examples of serialization using YAML:'''=====&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;nowiki&amp;gt; #Serialization using YAML's to_yaml method&lt;br /&gt;
 &lt;br /&gt;
  require &amp;quot;yaml&amp;quot;&lt;br /&gt;
     class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
	@name = name&lt;br /&gt;
	@age = age&lt;br /&gt;
	@country=country&lt;br /&gt;
     end&lt;br /&gt;
      def to_s&lt;br /&gt;
	&amp;quot;In First:\n#{@name}, #{@age}, #{@country}\n&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
  x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
  puts x&lt;br /&gt;
  puts x.to_yaml&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   In First:&lt;br /&gt;
   Tom, 25, USA&lt;br /&gt;
   --- !ruby/object:First&lt;br /&gt;
   name: Tom&lt;br /&gt;
   age: 25&lt;br /&gt;
   country: USA&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The above code displays the object x, first as a string and then in the yaml format. &lt;br /&gt;
&lt;br /&gt;
====='''Saving YAML data into a file:'''=====&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt; # Serialization using YAML::dump&lt;br /&gt;
&lt;br /&gt;
require 'yaml'&lt;br /&gt;
&lt;br /&gt;
f = File.open( 'first.yml', 'w' )&lt;br /&gt;
YAML.dump( [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;], f )&lt;br /&gt;
f.close&lt;br /&gt;
		&lt;br /&gt;
File.open( 'first.yml' ){ |f|	&lt;br /&gt;
    $arr= YAML.load(f)&lt;br /&gt;
}	&lt;br /&gt;
&lt;br /&gt;
p( $arr )&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;]&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The dump function can be used to serialize the data and save it into a file in YAML format. As shown in the above example the data in YAML format can be de-serialized using the load function.&lt;br /&gt;
&lt;br /&gt;
YAML libraries also provides an option of selecting only those variables of the object that are needed to be serialized. This is done using the to_yaml_properties method as shown in the below example.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=====Custom Serialization using YAML=====&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;nowiki&amp;gt;&lt;br /&gt;
    require &amp;quot;yaml&amp;quot;&lt;br /&gt;
     class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
	@name = name&lt;br /&gt;
	@age = age&lt;br /&gt;
	@country=country&lt;br /&gt;
     end&lt;br /&gt;
     def to_s&lt;br /&gt;
	&amp;quot;In First:\n#{@name}, #{@age}, #{@country}\n&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
      def to_yaml_properties&lt;br /&gt;
	   [&amp;quot;@name&amp;quot;,&amp;quot;@age&amp;quot;]  #@country will not be serialized&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
  x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
  puts x&lt;br /&gt;
  puts x.to_yaml&lt;br /&gt;
  &amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   In First:&lt;br /&gt;
   Tom, 25, USA&lt;br /&gt;
   --- !ruby/object:First&lt;br /&gt;
   name: Tom&lt;br /&gt;
   age: 25&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Converting Ruby Objects to JSON format: ====&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
JSON 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. Ruby 1.8.7 distribution is not bundled with json gem. However, in Ruby 1.9.2, the json gem is bundled with the core Ruby distribution.&lt;br /&gt;
The JSON library can be installed using Ruby Gems[http://rubygems.org/] as shown below:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;# gem install json&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A JSON string for serialization can be created by using the JSON.generate method:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
        require 'json'&lt;br /&gt;
        my_hash = {:Welcome =&amp;gt; &amp;quot;Ruby&amp;quot;}&lt;br /&gt;
        puts JSON.generate(my_hash) =&amp;gt; &amp;quot;{\&amp;quot;WELCOME\&amp;quot;:\&amp;quot;RUBY\&amp;quot;}&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
'''Output:'''&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;{&amp;quot;{\&amp;quot;Welcome\&amp;quot;:\&amp;quot;Ruby\&amp;quot;}&amp;quot;=&amp;gt;&amp;quot;{\&amp;quot;WELCOME\&amp;quot;:\&amp;quot;RUBY\&amp;quot;}&amp;quot;}&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A JSON string received from another program can be parsed by using JSON.parse&lt;br /&gt;
Ruby thus converts String to Hash.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
        require 'json'&lt;br /&gt;
        my_hash = JSON.parse('{&amp;quot;Welcome&amp;quot;: &amp;quot;Ruby&amp;quot;}')&lt;br /&gt;
        puts my_hash[&amp;quot;Welcome&amp;quot;] =&amp;gt; &amp;quot;Ruby&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Converting Ruby Objects to Binary Formats ===&lt;br /&gt;
----&lt;br /&gt;
Binary Serialization is another form of serialization in Ruby which is not in human readable form. 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. Binary Serialization  is done using Marshal which is built into Ruby and the code for it is written in Ruby's Marshal module(marshal.c) and thus no additional files are required in order to use it. The _dump and _load methods defined in marshal are used for serialization. Using marshal module the following types of objects can not be serialized: bindings, procedure objects, singleton objects, instances of IO objects and interfaces.&lt;br /&gt;
&lt;br /&gt;
Since the Binary Serialized data is not in human readable form, the following guidelines need to be followed.&lt;br /&gt;
     1.Use print instead of puts[http://ruby-doc.org/core-2.0.0/ARGF.html#] when serialized objects are written to a file in order to avoid new line characters to be written &lt;br /&gt;
       in the file.&lt;br /&gt;
     &lt;br /&gt;
     2.Use a record separator in order to differentiate between two objects.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====='''Binary Serialization Example:'''=====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    class Animal&lt;br /&gt;
      def initialize  name, age&lt;br /&gt;
      @name = name&lt;br /&gt;
      @age=age&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
   class Cat &amp;lt; Animal&lt;br /&gt;
    def to_s&lt;br /&gt;
     &amp;quot;In Cat C: #{@name} \t #{@age}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
  c = Cat.new(&amp;quot;Kitty Kat&amp;quot;,5)&lt;br /&gt;
  puts &amp;quot;Before Serialization&amp;quot;&lt;br /&gt;
  puts c&lt;br /&gt;
  #puts d&lt;br /&gt;
  serialize_cat= Marshal.dump(c) #dumps the serialized cat object into serialize_cat&lt;br /&gt;
  puts &amp;quot;\nAfter Serialization:\n #{serialize_cat}&amp;quot;&lt;br /&gt;
  deserialize_cat= Marshal::load(serialize_cat) #deserializes the cat object and loads it back into deserialize_cat&lt;br /&gt;
  puts &amp;quot;\nAfter Deserialization\n #{deserialize_cat}&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   Before Serialization&lt;br /&gt;
   In Cat C: Kitty Kat 	 5&lt;br /&gt;
   After Serialization:&lt;br /&gt;
   oCat:&lt;br /&gt;
   @nameI&amp;quot;Kitty Kat:ET:	@agei&lt;br /&gt;
   After Deserialization&lt;br /&gt;
   In Cat C: Kitty Kat 	 5&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====='''Saving Marshal data into a file'''=====&lt;br /&gt;
&lt;br /&gt;
Similar to YAML, Marshal can also be used to dump data into a file. The above example showing the serialization using YAML::dump can be written using marshal as shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    f = File.open( 'first.yml', 'w' )&lt;br /&gt;
    Marshal.dump( [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;], f )&lt;br /&gt;
    f.close	&lt;br /&gt;
    File.open( 'first.yml' ){ |f|	&lt;br /&gt;
    $arr= Marshal.load(f)&lt;br /&gt;
    }	&lt;br /&gt;
    p( $arr )&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;]&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice that in the above example there is no &amp;quot;require&amp;quot; statement as opposed to the earlier example of writing serialized data into files using YAML. This is because unlike YAML, marshal is built-in Ruby and  no external libraries is required in order to use its functionality.&lt;br /&gt;
&lt;br /&gt;
====='''Custom serialization using Marshal'''=====&lt;br /&gt;
&lt;br /&gt;
Marshal can also be used to custom serialize an object i.e. it provides an option to omit the variables of an object that are not required in the serialized data. The following program shows the use of marshal_dump method for achieving custom serialization.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
      class First&lt;br /&gt;
      def initialize(name, age, country)&lt;br /&gt;
         @name = name&lt;br /&gt;
         @age = age&lt;br /&gt;
         @country=country&lt;br /&gt;
      end&lt;br /&gt;
      def to_s&lt;br /&gt;
            &amp;quot;In First: #{@name}, #{@age}, #{@country}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
      def marshal_dump&lt;br /&gt;
            [@name,@age]  #@country will not be serialized&lt;br /&gt;
      end&lt;br /&gt;
      def marshal_load(data)&lt;br /&gt;
       @name=data[0]&lt;br /&gt;
       @age=data[1]&lt;br /&gt;
       @country=&amp;quot;United States of America&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
      end&lt;br /&gt;
 &lt;br /&gt;
      x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
      puts x&lt;br /&gt;
  &lt;br /&gt;
      marshal_data = Marshal.dump( x ) &lt;br /&gt;
      y = Marshal.load( marshal_data ) &lt;br /&gt;
      p( y.to_s )&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    In First: Tom, 25, USA&lt;br /&gt;
    &amp;quot;In First: Tom, 25, United States of America&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== '''Serialization Performance in Ruby''' ==&lt;br /&gt;
The different serialization formats described above differ in the efficiency at which they can serialize and deserialize data and thus while serializing large amount of data their efficiency is taken into account. The .report[http://www.ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html] method in Ruby can be used to evaluate the performance of these serialization patterns. Marshal, as it handles data in binary format, is the most efficient form of serialization in Ruby. The following example compares the performance of marshal, JSON and YAML format.[http://www.pauldix.net/2008/08/serializing-dat.html]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   require 'benchmark'&lt;br /&gt;
   require 'rubygems'&lt;br /&gt;
   require 'json'&lt;br /&gt;
   require 'yaml'&lt;br /&gt;
   include Benchmark&lt;br /&gt;
   class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
      @name=name&lt;br /&gt;
      @age=age&lt;br /&gt;
      @country=country&lt;br /&gt;
     end&lt;br /&gt;
   end  &lt;br /&gt;
    x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
    puts &amp;quot;            User     System     Total      Real&amp;quot;&lt;br /&gt;
   benchmark do |t|&lt;br /&gt;
    print &amp;quot;Marshal:&amp;quot;&lt;br /&gt;
    t.report{1000.times do; Marshal.load(Marshal.dump(x));end}&lt;br /&gt;
    print &amp;quot;JSON:   &amp;quot;&lt;br /&gt;
    t.report{1000.times do; JSON.load(JSON.dump(x));end}&lt;br /&gt;
    print &amp;quot;YAML:   &amp;quot;&lt;br /&gt;
    t.report{1000.times do; YAML.load(YAML.dump(x));end}&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
              User     System     Total      Real&lt;br /&gt;
  Marshal:   0.010000   0.000000   0.010000 (  0.015408)&lt;br /&gt;
  JSON:      0.020000   0.010000   0.030000 (  0.022462)&lt;br /&gt;
  YAML:      0.460000   0.010000   0.470000 (  0.472149)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output of the above example shows that, given large chunks of data, marshal is comparatively more efficient format for serialization in Ruby.&lt;br /&gt;
&lt;br /&gt;
== Serialization in Object-Oriented Languages: Comparison ==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Sl.No !! Ruby !! Java !! .Net Framework !! C++&lt;br /&gt;
|-&lt;br /&gt;
| 1 || Ruby provides a built in module called 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 || Although, there is no built in support for serialization in C++, it can be achieved by using Boost libraries[http://www.boost.org/doc/libs/1_36_0/libs/serialization/example/demo.cpp]&lt;br /&gt;
|-&lt;br /&gt;
| 2 || The built in module of Ruby (Marshal) does not support platform independence, however, it can be achieved by using external libraries like YAML and JSON || Similarly, Java's built in serialization is also not platform independent and in order to use serialization in Java across Ruby platform, jruby library should be used.  || .Net used Remoting technology to make it platform independent. || Serialization using the Boost libraries is not platform independent.&lt;br /&gt;
|-&lt;br /&gt;
| 3 || YAML provides a method [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/syck/rdoc/Object.html (to_yaml_properties)] which can be used to select the variables who's value is need to be serialized. With Marshal, we need to write a method named marshal_dump defining the variables of an object that has to be serialized. || Provides an option for serializing only the required 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 data 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 || Serialization using the Boost libraries is custom and thus the user can specify the part of the objects to be serialized.&lt;br /&gt;
|-&lt;br /&gt;
| 4 || Bindings, procedure objects, singleton objects, instances of IO objects and interfaces can not be serialized. Serializing these objects throws TypeError exceptions. || Thread, OutputStream and its subclasses, and Socket are not serializable in Java.[http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html] || Objects like DataRow are non serializable in .NET. Whether an object is serializable or not can be checked using the [http://msdn.microsoft.com/en-us/library/system.type.isserializable(v=vs.110).aspx?cs-save-lang=1&amp;amp;cs-lang=cpp#code-snippet-1 Type.IsSerializable] || There are no such objects in C++.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== See Also==&lt;br /&gt;
&lt;br /&gt;
1. [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/json/rdoc/JSON.html Serialization in Ruby JSON]&lt;br /&gt;
&lt;br /&gt;
2. [http://api.rubyonrails.org/classes/ActiveModel/Serialization.html Serialization in Rails]&lt;br /&gt;
&lt;br /&gt;
3. [http://gregmoreno.wordpress.com/2011/01/27/preventing-model-explosion-via-rails-serialization/ Article on Rails Serialization]&lt;br /&gt;
&lt;br /&gt;
4. [http://en.wikipedia.org/wiki/Google_Protocol_Buffers Protocol Buffers]&lt;br /&gt;
&lt;br /&gt;
5. [https://rubygems.org/gems/ruby-protocol-buffers Ruby Protocol Buffers Gem]&lt;br /&gt;
&lt;br /&gt;
6. [http://en.wikipedia.org/wiki/Apache_Avro Avro]&lt;br /&gt;
&lt;br /&gt;
7. [http://rubygems.org/gems/avro Ruby Avro Gem]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
1. [http://en.wikipedia.org/wiki/Serialization Serilization in General]&lt;br /&gt;
&lt;br /&gt;
2. [http://en.wikipedia.org/wiki/Marshalling_%28computer_science%29 Marshaling]&lt;br /&gt;
&lt;br /&gt;
3. [http://techbrahmana.blogspot.com/2012/03/rails-cookie-handling-serialization-and.html Rails Cookie Handling]&lt;br /&gt;
&lt;br /&gt;
4. [http://yaml.org/ YAML]&lt;br /&gt;
&lt;br /&gt;
5. [http://www.ruby-doc.org/core-2.1.0/Marshal.html Marshal module]&lt;br /&gt;
&lt;br /&gt;
6. [http://json.org/ JSON]&lt;br /&gt;
&lt;br /&gt;
7. [http://rubygems.org/ Ruby Gems]&lt;br /&gt;
&lt;br /&gt;
8. [http://ruby-doc.org/core-2.0.0/ARGF.html# Ruby ARGF Documentation]&lt;br /&gt;
&lt;br /&gt;
9. [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html Ruby Documentation for Benchmark Gem]&lt;br /&gt;
&lt;br /&gt;
10. [http://www.pauldix.net/2008/08/serializing-dat.html Serialization Performance Comparison]&lt;br /&gt;
&lt;br /&gt;
11. [http://www.boost.org/doc/libs/1_36_0/libs/serialization/example/demo.cpp Boost Libraries]&lt;br /&gt;
&lt;br /&gt;
12. [http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html Java Serialization]&lt;br /&gt;
&lt;br /&gt;
13. [http://www.w3schools.com/json/ JSON Tutorial]&lt;br /&gt;
&lt;br /&gt;
14. [http://www.skorks.com/2010/04/serializing-and-deserializing-objects-with-ruby/ Serializing and De-serializing in Ruby]&lt;br /&gt;
&lt;br /&gt;
15. [http://www.codeproject.com/Articles/33296/Serialization-and-De-serialization Serialization and De-serialization]&lt;br /&gt;
&lt;br /&gt;
16. [http://msdn.microsoft.com/en-us/library/182eeyhh.aspx XML Serialization]&lt;br /&gt;
&lt;br /&gt;
17. [http://www.tutorialspoint.com/java/java_serialization.htm Java Serialization Tutorial]&lt;br /&gt;
&lt;br /&gt;
18. [http://www.sapphiresteel.com/ruby-programming/The-Book-Of-Ruby The Book of Ruby]&lt;br /&gt;
&lt;br /&gt;
19. [http://www.codeproject.com/Articles/20962/Introducing-Serialization-in-NET Serialization in .Net]&lt;br /&gt;
&lt;br /&gt;
20. [http://ruby.about.com/od/advancedruby/ss/Serialization-In-Ruby-Yaml.html Serialization in Ruby YAML]&lt;br /&gt;
&lt;br /&gt;
21. [http://ruby.about.com/od/tasks/a/The-Json-Gem.html Installing JSON gem]&lt;br /&gt;
&lt;br /&gt;
22. [http://www.ruby-doc.org/stdlib-2.0.0/libdoc/json/rdoc/JSON.html Serialization in Ruby JSON]&lt;/div&gt;</summary>
		<author><name>Mmehta2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1d_mm&amp;diff=83416</id>
		<title>CSC/ECE 517 Spring 2014/ch1a 1d mm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1d_mm&amp;diff=83416"/>
		<updated>2014-02-18T02:44:25Z</updated>

		<summary type="html">&lt;p&gt;Mmehta2: /* Introduction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page discusses Serialization, focusing on Ruby programming language. It describes different methods of Serialization in Ruby. In addition, it also compares serialization in different Object Oriented languages.&lt;br /&gt;
&lt;br /&gt;
== '''Introduction''' ==&lt;br /&gt;
&lt;br /&gt;
==='''Serialization'''===&lt;br /&gt;
&lt;br /&gt;
'''Serialization'''[http://en.wikipedia.org/wiki/Serialization] is a process of converting a data structure or an object into a stream of bytes or string to facilitate storage in memory, file (persistence storage) or transmission over a network. The process of Serialization is also referred to as ''Marshalling''[http://en.wikipedia.org/wiki/Marshalling_%28computer_science%29]. 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. &lt;br /&gt;
&lt;br /&gt;
==='''De-serialization'''===&lt;br /&gt;
&lt;br /&gt;
'''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''.&lt;br /&gt;
&lt;br /&gt;
==='''Basic Advantages of Serialization'''===&lt;br /&gt;
&lt;br /&gt;
1. Communication between two or more processes on same machine. Object state can be saved and shared in a persistent or in-memory store.&lt;br /&gt;
&lt;br /&gt;
2. Communication between processes on different machines. Serialization facilitates the transmission of an object through a network.&lt;br /&gt;
&lt;br /&gt;
3. Creating a clone of an object. &lt;br /&gt;
&lt;br /&gt;
4. Cross-platform compatibility. Object can be serialized in a common format that is understood by multiple platforms. For example, JSON[http://www.json.org/] and XML.&lt;br /&gt;
&lt;br /&gt;
==='''Practical Applications for Serialization'''===&lt;br /&gt;
&lt;br /&gt;
1. [http://docs.oracle.com/cd/E13222_01/wls/docs81/webapp/sessions.html#139726 HTTP Session] Replication by sharing session objects across web servers for load balancing and handling failover scenarios.&lt;br /&gt;
&lt;br /&gt;
2. Serialization facilitates communication in [http://en.wikipedia.org/wiki/Java_remote_method_invocation Remote Method Invocation] or [http://en.wikipedia.org/wiki/Remote_procedure_call Remote procedure calls]&lt;br /&gt;
&lt;br /&gt;
3. Rails Cookie Handling[http://techbrahmana.blogspot.com/2012/03/rails-cookie-handling-serialization-and.html]. Cookies are stored marshalled/unmarshalled to and from client machines.&lt;br /&gt;
&lt;br /&gt;
== '''Serialization in Ruby:''' ==&lt;br /&gt;
&lt;br /&gt;
Ruby provides serialization capabilities through its module, [http://www.ruby-doc.org/core-2.0.0/Marshal.html Marshal]. There are also other libraries like YAML[http://yaml.org/] and JSON which can be used in Ruby to generate serialized objects for purposes like platform independence and human readable formats.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:SerializationChart.jpg]]&lt;br /&gt;
&lt;br /&gt;
== '''Types of Serialization''' ==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
=== Converting Ruby Objects in Human Readable Formats ===&lt;br /&gt;
----&lt;br /&gt;
The conversion of Ruby objects into YAML and JSON formats are explained below.&lt;br /&gt;
&lt;br /&gt;
==== Converting Ruby Objects to YAML format ====&lt;br /&gt;
&lt;br /&gt;
YAML 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 binary format. 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. In order to use it in Ruby, the yaml.rb file is required to be loaded which provides methods for converting objects into yaml format and creating .yml files.&lt;br /&gt;
&lt;br /&gt;
====='''Examples of serialization using YAML:'''=====&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;nowiki&amp;gt; #Serialization using YAML's to_yaml method&lt;br /&gt;
 &lt;br /&gt;
  require &amp;quot;yaml&amp;quot;&lt;br /&gt;
     class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
	@name = name&lt;br /&gt;
	@age = age&lt;br /&gt;
	@country=country&lt;br /&gt;
     end&lt;br /&gt;
      def to_s&lt;br /&gt;
	&amp;quot;In First:\n#{@name}, #{@age}, #{@country}\n&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
  x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
  puts x&lt;br /&gt;
  puts x.to_yaml&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   In First:&lt;br /&gt;
   Tom, 25, USA&lt;br /&gt;
   --- !ruby/object:First&lt;br /&gt;
   name: Tom&lt;br /&gt;
   age: 25&lt;br /&gt;
   country: USA&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The above code displays the object x, first as a string and then in the yaml format. &lt;br /&gt;
&lt;br /&gt;
====='''Saving YAML data into a file:'''=====&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt; # Serialization using YAML::dump&lt;br /&gt;
&lt;br /&gt;
require 'yaml'&lt;br /&gt;
&lt;br /&gt;
f = File.open( 'first.yml', 'w' )&lt;br /&gt;
YAML.dump( [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;], f )&lt;br /&gt;
f.close&lt;br /&gt;
		&lt;br /&gt;
File.open( 'first.yml' ){ |f|	&lt;br /&gt;
    $arr= YAML.load(f)&lt;br /&gt;
}	&lt;br /&gt;
&lt;br /&gt;
p( $arr )&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;]&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The dump function can be used to serialize the data and save it into a file in YAML format. As shown in the above example the data in YAML format can be de-serialized using the load function.&lt;br /&gt;
&lt;br /&gt;
YAML libraries also provides an option of selecting only those variables of the object that are needed to be serialized. This is done using the to_yaml_properties method as shown in the below example.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=====Custom Serialization using YAML=====&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;nowiki&amp;gt;&lt;br /&gt;
    require &amp;quot;yaml&amp;quot;&lt;br /&gt;
     class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
	@name = name&lt;br /&gt;
	@age = age&lt;br /&gt;
	@country=country&lt;br /&gt;
     end&lt;br /&gt;
     def to_s&lt;br /&gt;
	&amp;quot;In First:\n#{@name}, #{@age}, #{@country}\n&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
      def to_yaml_properties&lt;br /&gt;
	   [&amp;quot;@name&amp;quot;,&amp;quot;@age&amp;quot;]  #@country will not be serialized&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
  x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
  puts x&lt;br /&gt;
  puts x.to_yaml&lt;br /&gt;
  &amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   In First:&lt;br /&gt;
   Tom, 25, USA&lt;br /&gt;
   --- !ruby/object:First&lt;br /&gt;
   name: Tom&lt;br /&gt;
   age: 25&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Converting Ruby Objects to JSON format: ====&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
JSON 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. Ruby 1.8.7 distribution is not bundled with json gem. However, in Ruby 1.9.2, the json gem is bundled with the core Ruby distribution.&lt;br /&gt;
The JSON library can be installed using Ruby Gems[http://rubygems.org/] as shown below:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;# gem install json&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A JSON string for serialization can be created by using the JSON.generate method:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
        require 'json'&lt;br /&gt;
        my_hash = {:Welcome =&amp;gt; &amp;quot;Ruby&amp;quot;}&lt;br /&gt;
        puts JSON.generate(my_hash) =&amp;gt; &amp;quot;{\&amp;quot;WELCOME\&amp;quot;:\&amp;quot;RUBY\&amp;quot;}&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
'''Output:'''&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;{&amp;quot;{\&amp;quot;Welcome\&amp;quot;:\&amp;quot;Ruby\&amp;quot;}&amp;quot;=&amp;gt;&amp;quot;{\&amp;quot;WELCOME\&amp;quot;:\&amp;quot;RUBY\&amp;quot;}&amp;quot;}&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A JSON string received from another program can be parsed by using JSON.parse&lt;br /&gt;
Ruby thus converts String to Hash.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
        require 'json'&lt;br /&gt;
        my_hash = JSON.parse('{&amp;quot;Welcome&amp;quot;: &amp;quot;Ruby&amp;quot;}')&lt;br /&gt;
        puts my_hash[&amp;quot;Welcome&amp;quot;] =&amp;gt; &amp;quot;Ruby&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Converting Ruby Objects to Binary Formats ===&lt;br /&gt;
----&lt;br /&gt;
Binary Serialization is another form of serialization in Ruby which is not in human readable form. 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. Binary Serialization  is done using Marshal which is built into Ruby and the code for it is written in Ruby's Marshal module(marshal.c) and thus no additional files are required in order to use it. The _dump and _load methods defined in marshal are used for serialization. Using marshal module the following types of objects can not be serialized: bindings, procedure objects, singleton objects, instances of IO objects and interfaces.&lt;br /&gt;
&lt;br /&gt;
Since the Binary Serialized data is not in human readable form, the following guidelines need to be followed.&lt;br /&gt;
     1.Use print instead of puts[http://ruby-doc.org/core-2.0.0/ARGF.html#] when serialized objects are written to a file in order to avoid new line characters to be written &lt;br /&gt;
       in the file.&lt;br /&gt;
     &lt;br /&gt;
     2.Use a record separator in order to differentiate between two objects.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====='''Binary Serialization Example:'''=====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    class Animal&lt;br /&gt;
      def initialize  name, age&lt;br /&gt;
      @name = name&lt;br /&gt;
      @age=age&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
   class Cat &amp;lt; Animal&lt;br /&gt;
    def to_s&lt;br /&gt;
     &amp;quot;In Cat C: #{@name} \t #{@age}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
  c = Cat.new(&amp;quot;Kitty Kat&amp;quot;,5)&lt;br /&gt;
  puts &amp;quot;Before Serialization&amp;quot;&lt;br /&gt;
  puts c&lt;br /&gt;
  #puts d&lt;br /&gt;
  serialize_cat= Marshal.dump(c) #dumps the serialized cat object into serialize_cat&lt;br /&gt;
  puts &amp;quot;\nAfter Serialization:\n #{serialize_cat}&amp;quot;&lt;br /&gt;
  deserialize_cat= Marshal::load(serialize_cat) #deserializes the cat object and loads it back into deserialize_cat&lt;br /&gt;
  puts &amp;quot;\nAfter Deserialization\n #{deserialize_cat}&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   Before Serialization&lt;br /&gt;
   In Cat C: Kitty Kat 	 5&lt;br /&gt;
   After Serialization:&lt;br /&gt;
   oCat:&lt;br /&gt;
   @nameI&amp;quot;Kitty Kat:ET:	@agei&lt;br /&gt;
   After Deserialization&lt;br /&gt;
   In Cat C: Kitty Kat 	 5&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====='''Saving Marshal data into a file'''=====&lt;br /&gt;
&lt;br /&gt;
Similar to YAML, Marshal can also be used to dump data into a file. The above example showing the serialization using YAML::dump can be written using marshal as shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    f = File.open( 'first.yml', 'w' )&lt;br /&gt;
    Marshal.dump( [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;], f )&lt;br /&gt;
    f.close	&lt;br /&gt;
    File.open( 'first.yml' ){ |f|	&lt;br /&gt;
    $arr= Marshal.load(f)&lt;br /&gt;
    }	&lt;br /&gt;
    p( $arr )&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;]&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice that in the above example there is no &amp;quot;require&amp;quot; statement as opposed to the earlier example of writing serialized data into files using YAML. This is because unlike YAML, marshal is built-in Ruby and  no external libraries is required in order to use its functionality.&lt;br /&gt;
&lt;br /&gt;
====='''Custom serialization using Marshal'''=====&lt;br /&gt;
&lt;br /&gt;
Marshal can also be used to custom serialize an object i.e. it provides an option to omit the variables of an object that are not required in the serialized data. The following program shows the use of marshal_dump method for achieving custom serialization.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
      class First&lt;br /&gt;
      def initialize(name, age, country)&lt;br /&gt;
         @name = name&lt;br /&gt;
         @age = age&lt;br /&gt;
         @country=country&lt;br /&gt;
      end&lt;br /&gt;
      def to_s&lt;br /&gt;
            &amp;quot;In First: #{@name}, #{@age}, #{@country}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
      def marshal_dump&lt;br /&gt;
            [@name,@age]  #@country will not be serialized&lt;br /&gt;
      end&lt;br /&gt;
      def marshal_load(data)&lt;br /&gt;
       @name=data[0]&lt;br /&gt;
       @age=data[1]&lt;br /&gt;
       @country=&amp;quot;United States of America&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
      end&lt;br /&gt;
 &lt;br /&gt;
      x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
      puts x&lt;br /&gt;
  &lt;br /&gt;
      marshal_data = Marshal.dump( x ) &lt;br /&gt;
      y = Marshal.load( marshal_data ) &lt;br /&gt;
      p( y.to_s )&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    In First: Tom, 25, USA&lt;br /&gt;
    &amp;quot;In First: Tom, 25, United States of America&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== '''Serialization Performance in Ruby''' ==&lt;br /&gt;
The different serialization formats described above differ in the efficiency at which they can serialize and deserialize data and thus while serializing large amount of data their efficiency is taken into account. The .report[http://www.ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html] method in Ruby can be used to evaluate the performance of these serialization patterns. Marshal, as it handles data in binary format, is the most efficient form of serialization in Ruby. The following example compares the performance of marshal, JSON and YAML format.[http://www.pauldix.net/2008/08/serializing-dat.html]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   require 'benchmark'&lt;br /&gt;
   require 'rubygems'&lt;br /&gt;
   require 'json'&lt;br /&gt;
   require 'yaml'&lt;br /&gt;
   include Benchmark&lt;br /&gt;
   class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
      @name=name&lt;br /&gt;
      @age=age&lt;br /&gt;
      @country=country&lt;br /&gt;
     end&lt;br /&gt;
   end  &lt;br /&gt;
    x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
    puts &amp;quot;            User     System     Total      Real&amp;quot;&lt;br /&gt;
   benchmark do |t|&lt;br /&gt;
    print &amp;quot;Marshal:&amp;quot;&lt;br /&gt;
    t.report{1000.times do; Marshal.load(Marshal.dump(x));end}&lt;br /&gt;
    print &amp;quot;JSON:   &amp;quot;&lt;br /&gt;
    t.report{1000.times do; JSON.load(JSON.dump(x));end}&lt;br /&gt;
    print &amp;quot;YAML:   &amp;quot;&lt;br /&gt;
    t.report{1000.times do; YAML.load(YAML.dump(x));end}&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
              User     System     Total      Real&lt;br /&gt;
  Marshal:   0.010000   0.000000   0.010000 (  0.015408)&lt;br /&gt;
  JSON:      0.020000   0.010000   0.030000 (  0.022462)&lt;br /&gt;
  YAML:      0.460000   0.010000   0.470000 (  0.472149)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output of the above example shows that, given large chunks of data, marshal is comparatively more efficient format for serialization in Ruby.&lt;br /&gt;
&lt;br /&gt;
== Serialization in Object-Oriented Languages: Comparison ==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Sl.No !! Ruby !! Java !! .Net Framework !! C++&lt;br /&gt;
|-&lt;br /&gt;
| 1 || Ruby provides a built in module called 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 || Although, there is no built in support for serialization in C++, it can be achieved by using Boost libraries[http://www.boost.org/doc/libs/1_36_0/libs/serialization/example/demo.cpp]&lt;br /&gt;
|-&lt;br /&gt;
| 2 || The built in module of Ruby (Marshal) does not support platform independence, however, it can be achieved by using external libraries like YAML and JSON || Similarly, Java's built in serialization is also not platform independent and in order to use serialization in Java across Ruby platform, jruby library should be used.  || .Net used Remoting technology to make it platform independent. || Serialization using the Boost libraries is not platform independent.&lt;br /&gt;
|-&lt;br /&gt;
| 3 || YAML provides a method [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/syck/rdoc/Object.html (to_yaml_properties)] which can be used to select the variables who's value is need to be serialized. With Marshal, we need to write a method named marshal_dump defining the variables of an object that has to be serialized. || Provides an option for serializing only the required 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 data 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 || Serialization using the Boost libraries is custom and thus the user can specify the part of the objects to be serialized.&lt;br /&gt;
|-&lt;br /&gt;
| 4 || Bindings, procedure objects, singleton objects, instances of IO objects and interfaces can not be serialized. Serializing these objects throws TypeError exceptions. || Thread, OutputStream and its subclasses, and Socket are not serializable in Java.[http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html] || Objects like DataRow are non serializable in .NET. Whether an object is serializable or not can be checked using the [http://msdn.microsoft.com/en-us/library/system.type.isserializable(v=vs.110).aspx?cs-save-lang=1&amp;amp;cs-lang=cpp#code-snippet-1 Type.IsSerializable] || There are no such objects in C++.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== See Also==&lt;br /&gt;
&lt;br /&gt;
1. [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/json/rdoc/JSON.html Serialization in Ruby JSON]&lt;br /&gt;
&lt;br /&gt;
2. [http://api.rubyonrails.org/classes/ActiveModel/Serialization.html Serialization in Rails]&lt;br /&gt;
&lt;br /&gt;
3. [http://gregmoreno.wordpress.com/2011/01/27/preventing-model-explosion-via-rails-serialization/ Article on Rails Serialization]&lt;br /&gt;
&lt;br /&gt;
4. [http://en.wikipedia.org/wiki/Google_Protocol_Buffers Protocol Buffers]&lt;br /&gt;
&lt;br /&gt;
5. [https://rubygems.org/gems/ruby-protocol-buffers Ruby Protocol Buffers Gem]&lt;br /&gt;
&lt;br /&gt;
6. [http://en.wikipedia.org/wiki/Apache_Avro Avro]&lt;br /&gt;
&lt;br /&gt;
7. [http://rubygems.org/gems/avro Ruby Avro Gem]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
1. [http://en.wikipedia.org/wiki/Serialization Serilization in General]&lt;br /&gt;
&lt;br /&gt;
2. [http://en.wikipedia.org/wiki/Marshalling_%28computer_science%29 Marshaling]&lt;br /&gt;
&lt;br /&gt;
3. [http://techbrahmana.blogspot.com/2012/03/rails-cookie-handling-serialization-and.html Rails Cookie Handling]&lt;br /&gt;
&lt;br /&gt;
4. [http://yaml.org/ YAML]&lt;br /&gt;
&lt;br /&gt;
5. [http://www.ruby-doc.org/core-2.1.0/Marshal.html Marshal module]&lt;br /&gt;
&lt;br /&gt;
6. [http://json.org/ JSON]&lt;br /&gt;
&lt;br /&gt;
7. [http://rubygems.org/ Ruby Gems]&lt;br /&gt;
&lt;br /&gt;
8. [http://ruby-doc.org/core-2.0.0/ARGF.html# Ruby ARGF Documentation]&lt;br /&gt;
&lt;br /&gt;
9. [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html Ruby Documentation for Benchmark Gem]&lt;br /&gt;
&lt;br /&gt;
10. [http://www.pauldix.net/2008/08/serializing-dat.html Serialization Performance Comparison]&lt;br /&gt;
&lt;br /&gt;
11. [http://www.boost.org/doc/libs/1_36_0/libs/serialization/example/demo.cpp Boost Libraries]&lt;br /&gt;
&lt;br /&gt;
12. [http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html Java Serialization]&lt;br /&gt;
&lt;br /&gt;
13. [http://www.w3schools.com/json/ JSON Tutorial]&lt;br /&gt;
&lt;br /&gt;
14. [http://www.skorks.com/2010/04/serializing-and-deserializing-objects-with-ruby/ Serializing and De-serializing in Ruby]&lt;br /&gt;
&lt;br /&gt;
15. [http://www.codeproject.com/Articles/33296/Serialization-and-De-serialization Serialization and De-serialization]&lt;br /&gt;
&lt;br /&gt;
16. [http://msdn.microsoft.com/en-us/library/182eeyhh.aspx XML Serialization]&lt;br /&gt;
&lt;br /&gt;
17. [http://www.tutorialspoint.com/java/java_serialization.htm Java Serialization Tutorial]&lt;br /&gt;
&lt;br /&gt;
18. [http://www.sapphiresteel.com/ruby-programming/The-Book-Of-Ruby The Book of Ruby]&lt;br /&gt;
&lt;br /&gt;
19. [http://www.codeproject.com/Articles/20962/Introducing-Serialization-in-NET Serialization in .Net]&lt;br /&gt;
&lt;br /&gt;
20. [http://ruby.about.com/od/advancedruby/ss/Serialization-In-Ruby-Yaml.html Serialization in Ruby YAML]&lt;br /&gt;
&lt;br /&gt;
21. [http://ruby.about.com/od/tasks/a/The-Json-Gem.html Installing JSON gem]&lt;br /&gt;
&lt;br /&gt;
22. [http://www.ruby-doc.org/stdlib-2.0.0/libdoc/json/rdoc/JSON.html Serialization in Ruby JSON]&lt;/div&gt;</summary>
		<author><name>Mmehta2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1d_mm&amp;diff=83415</id>
		<title>CSC/ECE 517 Spring 2014/ch1a 1d mm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1d_mm&amp;diff=83415"/>
		<updated>2014-02-18T02:44:01Z</updated>

		<summary type="html">&lt;p&gt;Mmehta2: /* Basic Advantages of Serialization */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page discusses Serialization, focusing on Ruby programming language. It describes different methods of Serialization in Ruby. In addition, it also compares serialization in different Object Oriented languages.&lt;br /&gt;
&lt;br /&gt;
== '''Introduction''' ==&lt;br /&gt;
&lt;br /&gt;
==='''Serialization'''===&lt;br /&gt;
&lt;br /&gt;
'''Serialization'''[http://en.wikipedia.org/wiki/Serialization] is a process of converting a data structure or an object into a stream of bytes or string to facilitate storage in memory, file (persistence storage) or transmission over a network. The process of Serialization is also referred to as ''Marshalling''[http://en.wikipedia.org/wiki/Marshalling_%28computer_science%29]. 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. &lt;br /&gt;
&lt;br /&gt;
==='''De-serialization'''===&lt;br /&gt;
&lt;br /&gt;
'''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''.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==='''Basic Advantages of Serialization'''===&lt;br /&gt;
&lt;br /&gt;
1. Communication between two or more processes on same machine. Object state can be saved and shared in a persistent or in-memory store.&lt;br /&gt;
&lt;br /&gt;
2. Communication between processes on different machines. Serialization facilitates the transmission of an object through a network.&lt;br /&gt;
&lt;br /&gt;
3. Creating a clone of an object. &lt;br /&gt;
&lt;br /&gt;
4. Cross-platform compatibility. Object can be serialized in a common format that is understood by multiple platforms. For example, JSON[http://www.json.org/] and XML.&lt;br /&gt;
&lt;br /&gt;
==='''Practical Applications for Serialization'''===&lt;br /&gt;
&lt;br /&gt;
1. [http://docs.oracle.com/cd/E13222_01/wls/docs81/webapp/sessions.html#139726 HTTP Session] Replication by sharing session objects across web servers for load balancing and handling failover scenarios.&lt;br /&gt;
&lt;br /&gt;
2. Serialization facilitates communication in [http://en.wikipedia.org/wiki/Java_remote_method_invocation Remote Method Invocation] or [http://en.wikipedia.org/wiki/Remote_procedure_call Remote procedure calls]&lt;br /&gt;
&lt;br /&gt;
3. Rails Cookie Handling[http://techbrahmana.blogspot.com/2012/03/rails-cookie-handling-serialization-and.html]. Cookies are stored marshalled/unmarshalled to and from client machines.&lt;br /&gt;
&lt;br /&gt;
== '''Serialization in Ruby:''' ==&lt;br /&gt;
&lt;br /&gt;
Ruby provides serialization capabilities through its module, [http://www.ruby-doc.org/core-2.0.0/Marshal.html Marshal]. There are also other libraries like YAML[http://yaml.org/] and JSON which can be used in Ruby to generate serialized objects for purposes like platform independence and human readable formats.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:SerializationChart.jpg]]&lt;br /&gt;
&lt;br /&gt;
== '''Types of Serialization''' ==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
=== Converting Ruby Objects in Human Readable Formats ===&lt;br /&gt;
----&lt;br /&gt;
The conversion of Ruby objects into YAML and JSON formats are explained below.&lt;br /&gt;
&lt;br /&gt;
==== Converting Ruby Objects to YAML format ====&lt;br /&gt;
&lt;br /&gt;
YAML 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 binary format. 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. In order to use it in Ruby, the yaml.rb file is required to be loaded which provides methods for converting objects into yaml format and creating .yml files.&lt;br /&gt;
&lt;br /&gt;
====='''Examples of serialization using YAML:'''=====&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;nowiki&amp;gt; #Serialization using YAML's to_yaml method&lt;br /&gt;
 &lt;br /&gt;
  require &amp;quot;yaml&amp;quot;&lt;br /&gt;
     class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
	@name = name&lt;br /&gt;
	@age = age&lt;br /&gt;
	@country=country&lt;br /&gt;
     end&lt;br /&gt;
      def to_s&lt;br /&gt;
	&amp;quot;In First:\n#{@name}, #{@age}, #{@country}\n&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
  x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
  puts x&lt;br /&gt;
  puts x.to_yaml&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   In First:&lt;br /&gt;
   Tom, 25, USA&lt;br /&gt;
   --- !ruby/object:First&lt;br /&gt;
   name: Tom&lt;br /&gt;
   age: 25&lt;br /&gt;
   country: USA&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The above code displays the object x, first as a string and then in the yaml format. &lt;br /&gt;
&lt;br /&gt;
====='''Saving YAML data into a file:'''=====&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt; # Serialization using YAML::dump&lt;br /&gt;
&lt;br /&gt;
require 'yaml'&lt;br /&gt;
&lt;br /&gt;
f = File.open( 'first.yml', 'w' )&lt;br /&gt;
YAML.dump( [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;], f )&lt;br /&gt;
f.close&lt;br /&gt;
		&lt;br /&gt;
File.open( 'first.yml' ){ |f|	&lt;br /&gt;
    $arr= YAML.load(f)&lt;br /&gt;
}	&lt;br /&gt;
&lt;br /&gt;
p( $arr )&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;]&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The dump function can be used to serialize the data and save it into a file in YAML format. As shown in the above example the data in YAML format can be de-serialized using the load function.&lt;br /&gt;
&lt;br /&gt;
YAML libraries also provides an option of selecting only those variables of the object that are needed to be serialized. This is done using the to_yaml_properties method as shown in the below example.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=====Custom Serialization using YAML=====&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;nowiki&amp;gt;&lt;br /&gt;
    require &amp;quot;yaml&amp;quot;&lt;br /&gt;
     class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
	@name = name&lt;br /&gt;
	@age = age&lt;br /&gt;
	@country=country&lt;br /&gt;
     end&lt;br /&gt;
     def to_s&lt;br /&gt;
	&amp;quot;In First:\n#{@name}, #{@age}, #{@country}\n&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
      def to_yaml_properties&lt;br /&gt;
	   [&amp;quot;@name&amp;quot;,&amp;quot;@age&amp;quot;]  #@country will not be serialized&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
  x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
  puts x&lt;br /&gt;
  puts x.to_yaml&lt;br /&gt;
  &amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   In First:&lt;br /&gt;
   Tom, 25, USA&lt;br /&gt;
   --- !ruby/object:First&lt;br /&gt;
   name: Tom&lt;br /&gt;
   age: 25&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Converting Ruby Objects to JSON format: ====&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
JSON 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. Ruby 1.8.7 distribution is not bundled with json gem. However, in Ruby 1.9.2, the json gem is bundled with the core Ruby distribution.&lt;br /&gt;
The JSON library can be installed using Ruby Gems[http://rubygems.org/] as shown below:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;# gem install json&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A JSON string for serialization can be created by using the JSON.generate method:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
        require 'json'&lt;br /&gt;
        my_hash = {:Welcome =&amp;gt; &amp;quot;Ruby&amp;quot;}&lt;br /&gt;
        puts JSON.generate(my_hash) =&amp;gt; &amp;quot;{\&amp;quot;WELCOME\&amp;quot;:\&amp;quot;RUBY\&amp;quot;}&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
'''Output:'''&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;{&amp;quot;{\&amp;quot;Welcome\&amp;quot;:\&amp;quot;Ruby\&amp;quot;}&amp;quot;=&amp;gt;&amp;quot;{\&amp;quot;WELCOME\&amp;quot;:\&amp;quot;RUBY\&amp;quot;}&amp;quot;}&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A JSON string received from another program can be parsed by using JSON.parse&lt;br /&gt;
Ruby thus converts String to Hash.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
        require 'json'&lt;br /&gt;
        my_hash = JSON.parse('{&amp;quot;Welcome&amp;quot;: &amp;quot;Ruby&amp;quot;}')&lt;br /&gt;
        puts my_hash[&amp;quot;Welcome&amp;quot;] =&amp;gt; &amp;quot;Ruby&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Converting Ruby Objects to Binary Formats ===&lt;br /&gt;
----&lt;br /&gt;
Binary Serialization is another form of serialization in Ruby which is not in human readable form. 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. Binary Serialization  is done using Marshal which is built into Ruby and the code for it is written in Ruby's Marshal module(marshal.c) and thus no additional files are required in order to use it. The _dump and _load methods defined in marshal are used for serialization. Using marshal module the following types of objects can not be serialized: bindings, procedure objects, singleton objects, instances of IO objects and interfaces.&lt;br /&gt;
&lt;br /&gt;
Since the Binary Serialized data is not in human readable form, the following guidelines need to be followed.&lt;br /&gt;
     1.Use print instead of puts[http://ruby-doc.org/core-2.0.0/ARGF.html#] when serialized objects are written to a file in order to avoid new line characters to be written &lt;br /&gt;
       in the file.&lt;br /&gt;
     &lt;br /&gt;
     2.Use a record separator in order to differentiate between two objects.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====='''Binary Serialization Example:'''=====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    class Animal&lt;br /&gt;
      def initialize  name, age&lt;br /&gt;
      @name = name&lt;br /&gt;
      @age=age&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
   class Cat &amp;lt; Animal&lt;br /&gt;
    def to_s&lt;br /&gt;
     &amp;quot;In Cat C: #{@name} \t #{@age}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
  c = Cat.new(&amp;quot;Kitty Kat&amp;quot;,5)&lt;br /&gt;
  puts &amp;quot;Before Serialization&amp;quot;&lt;br /&gt;
  puts c&lt;br /&gt;
  #puts d&lt;br /&gt;
  serialize_cat= Marshal.dump(c) #dumps the serialized cat object into serialize_cat&lt;br /&gt;
  puts &amp;quot;\nAfter Serialization:\n #{serialize_cat}&amp;quot;&lt;br /&gt;
  deserialize_cat= Marshal::load(serialize_cat) #deserializes the cat object and loads it back into deserialize_cat&lt;br /&gt;
  puts &amp;quot;\nAfter Deserialization\n #{deserialize_cat}&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   Before Serialization&lt;br /&gt;
   In Cat C: Kitty Kat 	 5&lt;br /&gt;
   After Serialization:&lt;br /&gt;
   oCat:&lt;br /&gt;
   @nameI&amp;quot;Kitty Kat:ET:	@agei&lt;br /&gt;
   After Deserialization&lt;br /&gt;
   In Cat C: Kitty Kat 	 5&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====='''Saving Marshal data into a file'''=====&lt;br /&gt;
&lt;br /&gt;
Similar to YAML, Marshal can also be used to dump data into a file. The above example showing the serialization using YAML::dump can be written using marshal as shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    f = File.open( 'first.yml', 'w' )&lt;br /&gt;
    Marshal.dump( [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;], f )&lt;br /&gt;
    f.close	&lt;br /&gt;
    File.open( 'first.yml' ){ |f|	&lt;br /&gt;
    $arr= Marshal.load(f)&lt;br /&gt;
    }	&lt;br /&gt;
    p( $arr )&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;]&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice that in the above example there is no &amp;quot;require&amp;quot; statement as opposed to the earlier example of writing serialized data into files using YAML. This is because unlike YAML, marshal is built-in Ruby and  no external libraries is required in order to use its functionality.&lt;br /&gt;
&lt;br /&gt;
====='''Custom serialization using Marshal'''=====&lt;br /&gt;
&lt;br /&gt;
Marshal can also be used to custom serialize an object i.e. it provides an option to omit the variables of an object that are not required in the serialized data. The following program shows the use of marshal_dump method for achieving custom serialization.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
      class First&lt;br /&gt;
      def initialize(name, age, country)&lt;br /&gt;
         @name = name&lt;br /&gt;
         @age = age&lt;br /&gt;
         @country=country&lt;br /&gt;
      end&lt;br /&gt;
      def to_s&lt;br /&gt;
            &amp;quot;In First: #{@name}, #{@age}, #{@country}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
      def marshal_dump&lt;br /&gt;
            [@name,@age]  #@country will not be serialized&lt;br /&gt;
      end&lt;br /&gt;
      def marshal_load(data)&lt;br /&gt;
       @name=data[0]&lt;br /&gt;
       @age=data[1]&lt;br /&gt;
       @country=&amp;quot;United States of America&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
      end&lt;br /&gt;
 &lt;br /&gt;
      x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
      puts x&lt;br /&gt;
  &lt;br /&gt;
      marshal_data = Marshal.dump( x ) &lt;br /&gt;
      y = Marshal.load( marshal_data ) &lt;br /&gt;
      p( y.to_s )&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    In First: Tom, 25, USA&lt;br /&gt;
    &amp;quot;In First: Tom, 25, United States of America&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== '''Serialization Performance in Ruby''' ==&lt;br /&gt;
The different serialization formats described above differ in the efficiency at which they can serialize and deserialize data and thus while serializing large amount of data their efficiency is taken into account. The .report[http://www.ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html] method in Ruby can be used to evaluate the performance of these serialization patterns. Marshal, as it handles data in binary format, is the most efficient form of serialization in Ruby. The following example compares the performance of marshal, JSON and YAML format.[http://www.pauldix.net/2008/08/serializing-dat.html]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   require 'benchmark'&lt;br /&gt;
   require 'rubygems'&lt;br /&gt;
   require 'json'&lt;br /&gt;
   require 'yaml'&lt;br /&gt;
   include Benchmark&lt;br /&gt;
   class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
      @name=name&lt;br /&gt;
      @age=age&lt;br /&gt;
      @country=country&lt;br /&gt;
     end&lt;br /&gt;
   end  &lt;br /&gt;
    x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
    puts &amp;quot;            User     System     Total      Real&amp;quot;&lt;br /&gt;
   benchmark do |t|&lt;br /&gt;
    print &amp;quot;Marshal:&amp;quot;&lt;br /&gt;
    t.report{1000.times do; Marshal.load(Marshal.dump(x));end}&lt;br /&gt;
    print &amp;quot;JSON:   &amp;quot;&lt;br /&gt;
    t.report{1000.times do; JSON.load(JSON.dump(x));end}&lt;br /&gt;
    print &amp;quot;YAML:   &amp;quot;&lt;br /&gt;
    t.report{1000.times do; YAML.load(YAML.dump(x));end}&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
              User     System     Total      Real&lt;br /&gt;
  Marshal:   0.010000   0.000000   0.010000 (  0.015408)&lt;br /&gt;
  JSON:      0.020000   0.010000   0.030000 (  0.022462)&lt;br /&gt;
  YAML:      0.460000   0.010000   0.470000 (  0.472149)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output of the above example shows that, given large chunks of data, marshal is comparatively more efficient format for serialization in Ruby.&lt;br /&gt;
&lt;br /&gt;
== Serialization in Object-Oriented Languages: Comparison ==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Sl.No !! Ruby !! Java !! .Net Framework !! C++&lt;br /&gt;
|-&lt;br /&gt;
| 1 || Ruby provides a built in module called 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 || Although, there is no built in support for serialization in C++, it can be achieved by using Boost libraries[http://www.boost.org/doc/libs/1_36_0/libs/serialization/example/demo.cpp]&lt;br /&gt;
|-&lt;br /&gt;
| 2 || The built in module of Ruby (Marshal) does not support platform independence, however, it can be achieved by using external libraries like YAML and JSON || Similarly, Java's built in serialization is also not platform independent and in order to use serialization in Java across Ruby platform, jruby library should be used.  || .Net used Remoting technology to make it platform independent. || Serialization using the Boost libraries is not platform independent.&lt;br /&gt;
|-&lt;br /&gt;
| 3 || YAML provides a method [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/syck/rdoc/Object.html (to_yaml_properties)] which can be used to select the variables who's value is need to be serialized. With Marshal, we need to write a method named marshal_dump defining the variables of an object that has to be serialized. || Provides an option for serializing only the required 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 data 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 || Serialization using the Boost libraries is custom and thus the user can specify the part of the objects to be serialized.&lt;br /&gt;
|-&lt;br /&gt;
| 4 || Bindings, procedure objects, singleton objects, instances of IO objects and interfaces can not be serialized. Serializing these objects throws TypeError exceptions. || Thread, OutputStream and its subclasses, and Socket are not serializable in Java.[http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html] || Objects like DataRow are non serializable in .NET. Whether an object is serializable or not can be checked using the [http://msdn.microsoft.com/en-us/library/system.type.isserializable(v=vs.110).aspx?cs-save-lang=1&amp;amp;cs-lang=cpp#code-snippet-1 Type.IsSerializable] || There are no such objects in C++.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== See Also==&lt;br /&gt;
&lt;br /&gt;
1. [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/json/rdoc/JSON.html Serialization in Ruby JSON]&lt;br /&gt;
&lt;br /&gt;
2. [http://api.rubyonrails.org/classes/ActiveModel/Serialization.html Serialization in Rails]&lt;br /&gt;
&lt;br /&gt;
3. [http://gregmoreno.wordpress.com/2011/01/27/preventing-model-explosion-via-rails-serialization/ Article on Rails Serialization]&lt;br /&gt;
&lt;br /&gt;
4. [http://en.wikipedia.org/wiki/Google_Protocol_Buffers Protocol Buffers]&lt;br /&gt;
&lt;br /&gt;
5. [https://rubygems.org/gems/ruby-protocol-buffers Ruby Protocol Buffers Gem]&lt;br /&gt;
&lt;br /&gt;
6. [http://en.wikipedia.org/wiki/Apache_Avro Avro]&lt;br /&gt;
&lt;br /&gt;
7. [http://rubygems.org/gems/avro Ruby Avro Gem]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
1. [http://en.wikipedia.org/wiki/Serialization Serilization in General]&lt;br /&gt;
&lt;br /&gt;
2. [http://en.wikipedia.org/wiki/Marshalling_%28computer_science%29 Marshaling]&lt;br /&gt;
&lt;br /&gt;
3. [http://techbrahmana.blogspot.com/2012/03/rails-cookie-handling-serialization-and.html Rails Cookie Handling]&lt;br /&gt;
&lt;br /&gt;
4. [http://yaml.org/ YAML]&lt;br /&gt;
&lt;br /&gt;
5. [http://www.ruby-doc.org/core-2.1.0/Marshal.html Marshal module]&lt;br /&gt;
&lt;br /&gt;
6. [http://json.org/ JSON]&lt;br /&gt;
&lt;br /&gt;
7. [http://rubygems.org/ Ruby Gems]&lt;br /&gt;
&lt;br /&gt;
8. [http://ruby-doc.org/core-2.0.0/ARGF.html# Ruby ARGF Documentation]&lt;br /&gt;
&lt;br /&gt;
9. [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html Ruby Documentation for Benchmark Gem]&lt;br /&gt;
&lt;br /&gt;
10. [http://www.pauldix.net/2008/08/serializing-dat.html Serialization Performance Comparison]&lt;br /&gt;
&lt;br /&gt;
11. [http://www.boost.org/doc/libs/1_36_0/libs/serialization/example/demo.cpp Boost Libraries]&lt;br /&gt;
&lt;br /&gt;
12. [http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html Java Serialization]&lt;br /&gt;
&lt;br /&gt;
13. [http://www.w3schools.com/json/ JSON Tutorial]&lt;br /&gt;
&lt;br /&gt;
14. [http://www.skorks.com/2010/04/serializing-and-deserializing-objects-with-ruby/ Serializing and De-serializing in Ruby]&lt;br /&gt;
&lt;br /&gt;
15. [http://www.codeproject.com/Articles/33296/Serialization-and-De-serialization Serialization and De-serialization]&lt;br /&gt;
&lt;br /&gt;
16. [http://msdn.microsoft.com/en-us/library/182eeyhh.aspx XML Serialization]&lt;br /&gt;
&lt;br /&gt;
17. [http://www.tutorialspoint.com/java/java_serialization.htm Java Serialization Tutorial]&lt;br /&gt;
&lt;br /&gt;
18. [http://www.sapphiresteel.com/ruby-programming/The-Book-Of-Ruby The Book of Ruby]&lt;br /&gt;
&lt;br /&gt;
19. [http://www.codeproject.com/Articles/20962/Introducing-Serialization-in-NET Serialization in .Net]&lt;br /&gt;
&lt;br /&gt;
20. [http://ruby.about.com/od/advancedruby/ss/Serialization-In-Ruby-Yaml.html Serialization in Ruby YAML]&lt;br /&gt;
&lt;br /&gt;
21. [http://ruby.about.com/od/tasks/a/The-Json-Gem.html Installing JSON gem]&lt;br /&gt;
&lt;br /&gt;
22. [http://www.ruby-doc.org/stdlib-2.0.0/libdoc/json/rdoc/JSON.html Serialization in Ruby JSON]&lt;/div&gt;</summary>
		<author><name>Mmehta2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1d_mm&amp;diff=83414</id>
		<title>CSC/ECE 517 Spring 2014/ch1a 1d mm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1d_mm&amp;diff=83414"/>
		<updated>2014-02-18T02:43:45Z</updated>

		<summary type="html">&lt;p&gt;Mmehta2: /* Introduction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page discusses Serialization, focusing on Ruby programming language. It describes different methods of Serialization in Ruby. In addition, it also compares serialization in different Object Oriented languages.&lt;br /&gt;
&lt;br /&gt;
== '''Introduction''' ==&lt;br /&gt;
&lt;br /&gt;
==='''Serialization'''===&lt;br /&gt;
&lt;br /&gt;
'''Serialization'''[http://en.wikipedia.org/wiki/Serialization] is a process of converting a data structure or an object into a stream of bytes or string to facilitate storage in memory, file (persistence storage) or transmission over a network. The process of Serialization is also referred to as ''Marshalling''[http://en.wikipedia.org/wiki/Marshalling_%28computer_science%29]. 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. &lt;br /&gt;
&lt;br /&gt;
==='''De-serialization'''===&lt;br /&gt;
&lt;br /&gt;
'''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''.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==='''Basic Advantages of Serialization'''===&lt;br /&gt;
&lt;br /&gt;
1. Communication between two or more processes on same machine. Object state can be saved and shared in a persistent or in-memory store.&lt;br /&gt;
&lt;br /&gt;
2. Communication between processes on different machines. Serialization facilitates the transmission of an object through a network.&lt;br /&gt;
&lt;br /&gt;
3. Creating a clone of an object. &lt;br /&gt;
&lt;br /&gt;
4. Cross-platform compatibility. Object can be serialized in a common format that is understood by multiple platforms. For example, JSON[http://www.json.org/] and XML.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==='''Practical Applications for Serialization'''===&lt;br /&gt;
&lt;br /&gt;
1. [http://docs.oracle.com/cd/E13222_01/wls/docs81/webapp/sessions.html#139726 HTTP Session] Replication by sharing session objects across web servers for load balancing and handling failover scenarios.&lt;br /&gt;
&lt;br /&gt;
2. Serialization facilitates communication in [http://en.wikipedia.org/wiki/Java_remote_method_invocation Remote Method Invocation] or [http://en.wikipedia.org/wiki/Remote_procedure_call Remote procedure calls]&lt;br /&gt;
&lt;br /&gt;
3. Rails Cookie Handling[http://techbrahmana.blogspot.com/2012/03/rails-cookie-handling-serialization-and.html]. Cookies are stored marshalled/unmarshalled to and from client machines.&lt;br /&gt;
&lt;br /&gt;
== '''Serialization in Ruby:''' ==&lt;br /&gt;
&lt;br /&gt;
Ruby provides serialization capabilities through its module, [http://www.ruby-doc.org/core-2.0.0/Marshal.html Marshal]. There are also other libraries like YAML[http://yaml.org/] and JSON which can be used in Ruby to generate serialized objects for purposes like platform independence and human readable formats.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:SerializationChart.jpg]]&lt;br /&gt;
&lt;br /&gt;
== '''Types of Serialization''' ==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
=== Converting Ruby Objects in Human Readable Formats ===&lt;br /&gt;
----&lt;br /&gt;
The conversion of Ruby objects into YAML and JSON formats are explained below.&lt;br /&gt;
&lt;br /&gt;
==== Converting Ruby Objects to YAML format ====&lt;br /&gt;
&lt;br /&gt;
YAML 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 binary format. 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. In order to use it in Ruby, the yaml.rb file is required to be loaded which provides methods for converting objects into yaml format and creating .yml files.&lt;br /&gt;
&lt;br /&gt;
====='''Examples of serialization using YAML:'''=====&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;nowiki&amp;gt; #Serialization using YAML's to_yaml method&lt;br /&gt;
 &lt;br /&gt;
  require &amp;quot;yaml&amp;quot;&lt;br /&gt;
     class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
	@name = name&lt;br /&gt;
	@age = age&lt;br /&gt;
	@country=country&lt;br /&gt;
     end&lt;br /&gt;
      def to_s&lt;br /&gt;
	&amp;quot;In First:\n#{@name}, #{@age}, #{@country}\n&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
  x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
  puts x&lt;br /&gt;
  puts x.to_yaml&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   In First:&lt;br /&gt;
   Tom, 25, USA&lt;br /&gt;
   --- !ruby/object:First&lt;br /&gt;
   name: Tom&lt;br /&gt;
   age: 25&lt;br /&gt;
   country: USA&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The above code displays the object x, first as a string and then in the yaml format. &lt;br /&gt;
&lt;br /&gt;
====='''Saving YAML data into a file:'''=====&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt; # Serialization using YAML::dump&lt;br /&gt;
&lt;br /&gt;
require 'yaml'&lt;br /&gt;
&lt;br /&gt;
f = File.open( 'first.yml', 'w' )&lt;br /&gt;
YAML.dump( [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;], f )&lt;br /&gt;
f.close&lt;br /&gt;
		&lt;br /&gt;
File.open( 'first.yml' ){ |f|	&lt;br /&gt;
    $arr= YAML.load(f)&lt;br /&gt;
}	&lt;br /&gt;
&lt;br /&gt;
p( $arr )&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;]&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The dump function can be used to serialize the data and save it into a file in YAML format. As shown in the above example the data in YAML format can be de-serialized using the load function.&lt;br /&gt;
&lt;br /&gt;
YAML libraries also provides an option of selecting only those variables of the object that are needed to be serialized. This is done using the to_yaml_properties method as shown in the below example.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=====Custom Serialization using YAML=====&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;nowiki&amp;gt;&lt;br /&gt;
    require &amp;quot;yaml&amp;quot;&lt;br /&gt;
     class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
	@name = name&lt;br /&gt;
	@age = age&lt;br /&gt;
	@country=country&lt;br /&gt;
     end&lt;br /&gt;
     def to_s&lt;br /&gt;
	&amp;quot;In First:\n#{@name}, #{@age}, #{@country}\n&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
      def to_yaml_properties&lt;br /&gt;
	   [&amp;quot;@name&amp;quot;,&amp;quot;@age&amp;quot;]  #@country will not be serialized&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
  x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
  puts x&lt;br /&gt;
  puts x.to_yaml&lt;br /&gt;
  &amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   In First:&lt;br /&gt;
   Tom, 25, USA&lt;br /&gt;
   --- !ruby/object:First&lt;br /&gt;
   name: Tom&lt;br /&gt;
   age: 25&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Converting Ruby Objects to JSON format: ====&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
JSON 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. Ruby 1.8.7 distribution is not bundled with json gem. However, in Ruby 1.9.2, the json gem is bundled with the core Ruby distribution.&lt;br /&gt;
The JSON library can be installed using Ruby Gems[http://rubygems.org/] as shown below:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;# gem install json&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A JSON string for serialization can be created by using the JSON.generate method:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
        require 'json'&lt;br /&gt;
        my_hash = {:Welcome =&amp;gt; &amp;quot;Ruby&amp;quot;}&lt;br /&gt;
        puts JSON.generate(my_hash) =&amp;gt; &amp;quot;{\&amp;quot;WELCOME\&amp;quot;:\&amp;quot;RUBY\&amp;quot;}&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
'''Output:'''&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;{&amp;quot;{\&amp;quot;Welcome\&amp;quot;:\&amp;quot;Ruby\&amp;quot;}&amp;quot;=&amp;gt;&amp;quot;{\&amp;quot;WELCOME\&amp;quot;:\&amp;quot;RUBY\&amp;quot;}&amp;quot;}&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A JSON string received from another program can be parsed by using JSON.parse&lt;br /&gt;
Ruby thus converts String to Hash.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
        require 'json'&lt;br /&gt;
        my_hash = JSON.parse('{&amp;quot;Welcome&amp;quot;: &amp;quot;Ruby&amp;quot;}')&lt;br /&gt;
        puts my_hash[&amp;quot;Welcome&amp;quot;] =&amp;gt; &amp;quot;Ruby&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Converting Ruby Objects to Binary Formats ===&lt;br /&gt;
----&lt;br /&gt;
Binary Serialization is another form of serialization in Ruby which is not in human readable form. 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. Binary Serialization  is done using Marshal which is built into Ruby and the code for it is written in Ruby's Marshal module(marshal.c) and thus no additional files are required in order to use it. The _dump and _load methods defined in marshal are used for serialization. Using marshal module the following types of objects can not be serialized: bindings, procedure objects, singleton objects, instances of IO objects and interfaces.&lt;br /&gt;
&lt;br /&gt;
Since the Binary Serialized data is not in human readable form, the following guidelines need to be followed.&lt;br /&gt;
     1.Use print instead of puts[http://ruby-doc.org/core-2.0.0/ARGF.html#] when serialized objects are written to a file in order to avoid new line characters to be written &lt;br /&gt;
       in the file.&lt;br /&gt;
     &lt;br /&gt;
     2.Use a record separator in order to differentiate between two objects.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====='''Binary Serialization Example:'''=====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    class Animal&lt;br /&gt;
      def initialize  name, age&lt;br /&gt;
      @name = name&lt;br /&gt;
      @age=age&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
   class Cat &amp;lt; Animal&lt;br /&gt;
    def to_s&lt;br /&gt;
     &amp;quot;In Cat C: #{@name} \t #{@age}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
  c = Cat.new(&amp;quot;Kitty Kat&amp;quot;,5)&lt;br /&gt;
  puts &amp;quot;Before Serialization&amp;quot;&lt;br /&gt;
  puts c&lt;br /&gt;
  #puts d&lt;br /&gt;
  serialize_cat= Marshal.dump(c) #dumps the serialized cat object into serialize_cat&lt;br /&gt;
  puts &amp;quot;\nAfter Serialization:\n #{serialize_cat}&amp;quot;&lt;br /&gt;
  deserialize_cat= Marshal::load(serialize_cat) #deserializes the cat object and loads it back into deserialize_cat&lt;br /&gt;
  puts &amp;quot;\nAfter Deserialization\n #{deserialize_cat}&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   Before Serialization&lt;br /&gt;
   In Cat C: Kitty Kat 	 5&lt;br /&gt;
   After Serialization:&lt;br /&gt;
   oCat:&lt;br /&gt;
   @nameI&amp;quot;Kitty Kat:ET:	@agei&lt;br /&gt;
   After Deserialization&lt;br /&gt;
   In Cat C: Kitty Kat 	 5&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====='''Saving Marshal data into a file'''=====&lt;br /&gt;
&lt;br /&gt;
Similar to YAML, Marshal can also be used to dump data into a file. The above example showing the serialization using YAML::dump can be written using marshal as shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    f = File.open( 'first.yml', 'w' )&lt;br /&gt;
    Marshal.dump( [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;], f )&lt;br /&gt;
    f.close	&lt;br /&gt;
    File.open( 'first.yml' ){ |f|	&lt;br /&gt;
    $arr= Marshal.load(f)&lt;br /&gt;
    }	&lt;br /&gt;
    p( $arr )&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;]&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice that in the above example there is no &amp;quot;require&amp;quot; statement as opposed to the earlier example of writing serialized data into files using YAML. This is because unlike YAML, marshal is built-in Ruby and  no external libraries is required in order to use its functionality.&lt;br /&gt;
&lt;br /&gt;
====='''Custom serialization using Marshal'''=====&lt;br /&gt;
&lt;br /&gt;
Marshal can also be used to custom serialize an object i.e. it provides an option to omit the variables of an object that are not required in the serialized data. The following program shows the use of marshal_dump method for achieving custom serialization.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
      class First&lt;br /&gt;
      def initialize(name, age, country)&lt;br /&gt;
         @name = name&lt;br /&gt;
         @age = age&lt;br /&gt;
         @country=country&lt;br /&gt;
      end&lt;br /&gt;
      def to_s&lt;br /&gt;
            &amp;quot;In First: #{@name}, #{@age}, #{@country}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
      def marshal_dump&lt;br /&gt;
            [@name,@age]  #@country will not be serialized&lt;br /&gt;
      end&lt;br /&gt;
      def marshal_load(data)&lt;br /&gt;
       @name=data[0]&lt;br /&gt;
       @age=data[1]&lt;br /&gt;
       @country=&amp;quot;United States of America&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
      end&lt;br /&gt;
 &lt;br /&gt;
      x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
      puts x&lt;br /&gt;
  &lt;br /&gt;
      marshal_data = Marshal.dump( x ) &lt;br /&gt;
      y = Marshal.load( marshal_data ) &lt;br /&gt;
      p( y.to_s )&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    In First: Tom, 25, USA&lt;br /&gt;
    &amp;quot;In First: Tom, 25, United States of America&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== '''Serialization Performance in Ruby''' ==&lt;br /&gt;
The different serialization formats described above differ in the efficiency at which they can serialize and deserialize data and thus while serializing large amount of data their efficiency is taken into account. The .report[http://www.ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html] method in Ruby can be used to evaluate the performance of these serialization patterns. Marshal, as it handles data in binary format, is the most efficient form of serialization in Ruby. The following example compares the performance of marshal, JSON and YAML format.[http://www.pauldix.net/2008/08/serializing-dat.html]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   require 'benchmark'&lt;br /&gt;
   require 'rubygems'&lt;br /&gt;
   require 'json'&lt;br /&gt;
   require 'yaml'&lt;br /&gt;
   include Benchmark&lt;br /&gt;
   class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
      @name=name&lt;br /&gt;
      @age=age&lt;br /&gt;
      @country=country&lt;br /&gt;
     end&lt;br /&gt;
   end  &lt;br /&gt;
    x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
    puts &amp;quot;            User     System     Total      Real&amp;quot;&lt;br /&gt;
   benchmark do |t|&lt;br /&gt;
    print &amp;quot;Marshal:&amp;quot;&lt;br /&gt;
    t.report{1000.times do; Marshal.load(Marshal.dump(x));end}&lt;br /&gt;
    print &amp;quot;JSON:   &amp;quot;&lt;br /&gt;
    t.report{1000.times do; JSON.load(JSON.dump(x));end}&lt;br /&gt;
    print &amp;quot;YAML:   &amp;quot;&lt;br /&gt;
    t.report{1000.times do; YAML.load(YAML.dump(x));end}&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
              User     System     Total      Real&lt;br /&gt;
  Marshal:   0.010000   0.000000   0.010000 (  0.015408)&lt;br /&gt;
  JSON:      0.020000   0.010000   0.030000 (  0.022462)&lt;br /&gt;
  YAML:      0.460000   0.010000   0.470000 (  0.472149)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output of the above example shows that, given large chunks of data, marshal is comparatively more efficient format for serialization in Ruby.&lt;br /&gt;
&lt;br /&gt;
== Serialization in Object-Oriented Languages: Comparison ==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Sl.No !! Ruby !! Java !! .Net Framework !! C++&lt;br /&gt;
|-&lt;br /&gt;
| 1 || Ruby provides a built in module called 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 || Although, there is no built in support for serialization in C++, it can be achieved by using Boost libraries[http://www.boost.org/doc/libs/1_36_0/libs/serialization/example/demo.cpp]&lt;br /&gt;
|-&lt;br /&gt;
| 2 || The built in module of Ruby (Marshal) does not support platform independence, however, it can be achieved by using external libraries like YAML and JSON || Similarly, Java's built in serialization is also not platform independent and in order to use serialization in Java across Ruby platform, jruby library should be used.  || .Net used Remoting technology to make it platform independent. || Serialization using the Boost libraries is not platform independent.&lt;br /&gt;
|-&lt;br /&gt;
| 3 || YAML provides a method [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/syck/rdoc/Object.html (to_yaml_properties)] which can be used to select the variables who's value is need to be serialized. With Marshal, we need to write a method named marshal_dump defining the variables of an object that has to be serialized. || Provides an option for serializing only the required 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 data 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 || Serialization using the Boost libraries is custom and thus the user can specify the part of the objects to be serialized.&lt;br /&gt;
|-&lt;br /&gt;
| 4 || Bindings, procedure objects, singleton objects, instances of IO objects and interfaces can not be serialized. Serializing these objects throws TypeError exceptions. || Thread, OutputStream and its subclasses, and Socket are not serializable in Java.[http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html] || Objects like DataRow are non serializable in .NET. Whether an object is serializable or not can be checked using the [http://msdn.microsoft.com/en-us/library/system.type.isserializable(v=vs.110).aspx?cs-save-lang=1&amp;amp;cs-lang=cpp#code-snippet-1 Type.IsSerializable] || There are no such objects in C++.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== See Also==&lt;br /&gt;
&lt;br /&gt;
1. [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/json/rdoc/JSON.html Serialization in Ruby JSON]&lt;br /&gt;
&lt;br /&gt;
2. [http://api.rubyonrails.org/classes/ActiveModel/Serialization.html Serialization in Rails]&lt;br /&gt;
&lt;br /&gt;
3. [http://gregmoreno.wordpress.com/2011/01/27/preventing-model-explosion-via-rails-serialization/ Article on Rails Serialization]&lt;br /&gt;
&lt;br /&gt;
4. [http://en.wikipedia.org/wiki/Google_Protocol_Buffers Protocol Buffers]&lt;br /&gt;
&lt;br /&gt;
5. [https://rubygems.org/gems/ruby-protocol-buffers Ruby Protocol Buffers Gem]&lt;br /&gt;
&lt;br /&gt;
6. [http://en.wikipedia.org/wiki/Apache_Avro Avro]&lt;br /&gt;
&lt;br /&gt;
7. [http://rubygems.org/gems/avro Ruby Avro Gem]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
1. [http://en.wikipedia.org/wiki/Serialization Serilization in General]&lt;br /&gt;
&lt;br /&gt;
2. [http://en.wikipedia.org/wiki/Marshalling_%28computer_science%29 Marshaling]&lt;br /&gt;
&lt;br /&gt;
3. [http://techbrahmana.blogspot.com/2012/03/rails-cookie-handling-serialization-and.html Rails Cookie Handling]&lt;br /&gt;
&lt;br /&gt;
4. [http://yaml.org/ YAML]&lt;br /&gt;
&lt;br /&gt;
5. [http://www.ruby-doc.org/core-2.1.0/Marshal.html Marshal module]&lt;br /&gt;
&lt;br /&gt;
6. [http://json.org/ JSON]&lt;br /&gt;
&lt;br /&gt;
7. [http://rubygems.org/ Ruby Gems]&lt;br /&gt;
&lt;br /&gt;
8. [http://ruby-doc.org/core-2.0.0/ARGF.html# Ruby ARGF Documentation]&lt;br /&gt;
&lt;br /&gt;
9. [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html Ruby Documentation for Benchmark Gem]&lt;br /&gt;
&lt;br /&gt;
10. [http://www.pauldix.net/2008/08/serializing-dat.html Serialization Performance Comparison]&lt;br /&gt;
&lt;br /&gt;
11. [http://www.boost.org/doc/libs/1_36_0/libs/serialization/example/demo.cpp Boost Libraries]&lt;br /&gt;
&lt;br /&gt;
12. [http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html Java Serialization]&lt;br /&gt;
&lt;br /&gt;
13. [http://www.w3schools.com/json/ JSON Tutorial]&lt;br /&gt;
&lt;br /&gt;
14. [http://www.skorks.com/2010/04/serializing-and-deserializing-objects-with-ruby/ Serializing and De-serializing in Ruby]&lt;br /&gt;
&lt;br /&gt;
15. [http://www.codeproject.com/Articles/33296/Serialization-and-De-serialization Serialization and De-serialization]&lt;br /&gt;
&lt;br /&gt;
16. [http://msdn.microsoft.com/en-us/library/182eeyhh.aspx XML Serialization]&lt;br /&gt;
&lt;br /&gt;
17. [http://www.tutorialspoint.com/java/java_serialization.htm Java Serialization Tutorial]&lt;br /&gt;
&lt;br /&gt;
18. [http://www.sapphiresteel.com/ruby-programming/The-Book-Of-Ruby The Book of Ruby]&lt;br /&gt;
&lt;br /&gt;
19. [http://www.codeproject.com/Articles/20962/Introducing-Serialization-in-NET Serialization in .Net]&lt;br /&gt;
&lt;br /&gt;
20. [http://ruby.about.com/od/advancedruby/ss/Serialization-In-Ruby-Yaml.html Serialization in Ruby YAML]&lt;br /&gt;
&lt;br /&gt;
21. [http://ruby.about.com/od/tasks/a/The-Json-Gem.html Installing JSON gem]&lt;br /&gt;
&lt;br /&gt;
22. [http://www.ruby-doc.org/stdlib-2.0.0/libdoc/json/rdoc/JSON.html Serialization in Ruby JSON]&lt;/div&gt;</summary>
		<author><name>Mmehta2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1d_mm&amp;diff=83413</id>
		<title>CSC/ECE 517 Spring 2014/ch1a 1d mm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1d_mm&amp;diff=83413"/>
		<updated>2014-02-18T02:42:51Z</updated>

		<summary type="html">&lt;p&gt;Mmehta2: /* Converting Ruby Objects to Binary Formats */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page discusses Serialization, focusing on Ruby programming language. It describes different methods of Serialization in Ruby. In addition, it also compares serialization in different Object Oriented languages.&lt;br /&gt;
&lt;br /&gt;
== '''Introduction''' ==&lt;br /&gt;
&lt;br /&gt;
==='''Serialization'''===&lt;br /&gt;
&lt;br /&gt;
'''Serialization'''[http://en.wikipedia.org/wiki/Serialization] is a process of converting a data structure or an object into a stream of bytes or string to facilitate storage in memory, file (persistence storage) or transmission over a network. The process of Serialization is also referred to as ''Marshalling''[http://en.wikipedia.org/wiki/Marshalling_%28computer_science%29]. 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. &lt;br /&gt;
&lt;br /&gt;
==='''De-serialization'''===&lt;br /&gt;
&lt;br /&gt;
'''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''.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==='''Basic Advantages of Serialization'''===&lt;br /&gt;
&lt;br /&gt;
1. Communication between two or more processes on same machine. Object state can be saved and shared in a persistent or in-memory store.&lt;br /&gt;
&lt;br /&gt;
2. Communication between processes on different machines. Serialization facilitates the transmission of an object through a network.&lt;br /&gt;
&lt;br /&gt;
3. Creating a clone of an object. &lt;br /&gt;
&lt;br /&gt;
4. Cross-platform compatibility. Object can be serialized in a common format that is understood by multiple platforms. For example, JSON[http://www.json.org/] and XML.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==='''Practical Applications for Serialization'''===&lt;br /&gt;
&lt;br /&gt;
1. [http://docs.oracle.com/cd/E13222_01/wls/docs81/webapp/sessions.html#139726 HTTP Session] Replication by sharing session objects across web servers for load balancing and handling failover scenarios.&lt;br /&gt;
&lt;br /&gt;
2. Serialization facilitates communication in [http://en.wikipedia.org/wiki/Java_remote_method_invocation Remote Method Invocation] or [http://en.wikipedia.org/wiki/Remote_procedure_call Remote procedure calls]&lt;br /&gt;
&lt;br /&gt;
3. Rails Cookie Handling[http://techbrahmana.blogspot.com/2012/03/rails-cookie-handling-serialization-and.html]. Cookies are stored marshalled/unmarshalled to and from client machines.&lt;br /&gt;
&lt;br /&gt;
== '''Serialization in Ruby:''' ==&lt;br /&gt;
&lt;br /&gt;
Ruby provides serialization capabilities through its module, [http://www.ruby-doc.org/core-2.0.0/Marshal.html Marshal]. There are also other libraries like YAML[http://yaml.org/] and JSON which can be used in Ruby to generate serialized objects for purposes like platform independence and human readable formats.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:SerializationChart.jpg]]&lt;br /&gt;
&lt;br /&gt;
== '''Types of Serialization''' ==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
=== Converting Ruby Objects in Human Readable Formats ===&lt;br /&gt;
----&lt;br /&gt;
The conversion of Ruby objects into YAML and JSON formats are explained below.&lt;br /&gt;
&lt;br /&gt;
==== Converting Ruby Objects to YAML format ====&lt;br /&gt;
&lt;br /&gt;
YAML 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 binary format. 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. In order to use it in Ruby, the yaml.rb file is required to be loaded which provides methods for converting objects into yaml format and creating .yml files.&lt;br /&gt;
&lt;br /&gt;
====='''Examples of serialization using YAML:'''=====&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;nowiki&amp;gt; #Serialization using YAML's to_yaml method&lt;br /&gt;
 &lt;br /&gt;
  require &amp;quot;yaml&amp;quot;&lt;br /&gt;
     class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
	@name = name&lt;br /&gt;
	@age = age&lt;br /&gt;
	@country=country&lt;br /&gt;
     end&lt;br /&gt;
      def to_s&lt;br /&gt;
	&amp;quot;In First:\n#{@name}, #{@age}, #{@country}\n&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
  x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
  puts x&lt;br /&gt;
  puts x.to_yaml&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   In First:&lt;br /&gt;
   Tom, 25, USA&lt;br /&gt;
   --- !ruby/object:First&lt;br /&gt;
   name: Tom&lt;br /&gt;
   age: 25&lt;br /&gt;
   country: USA&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The above code displays the object x, first as a string and then in the yaml format. &lt;br /&gt;
&lt;br /&gt;
====='''Saving YAML data into a file:'''=====&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt; # Serialization using YAML::dump&lt;br /&gt;
&lt;br /&gt;
require 'yaml'&lt;br /&gt;
&lt;br /&gt;
f = File.open( 'first.yml', 'w' )&lt;br /&gt;
YAML.dump( [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;], f )&lt;br /&gt;
f.close&lt;br /&gt;
		&lt;br /&gt;
File.open( 'first.yml' ){ |f|	&lt;br /&gt;
    $arr= YAML.load(f)&lt;br /&gt;
}	&lt;br /&gt;
&lt;br /&gt;
p( $arr )&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;]&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The dump function can be used to serialize the data and save it into a file in YAML format. As shown in the above example the data in YAML format can be de-serialized using the load function.&lt;br /&gt;
&lt;br /&gt;
YAML libraries also provides an option of selecting only those variables of the object that are needed to be serialized. This is done using the to_yaml_properties method as shown in the below example.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=====Custom Serialization using YAML=====&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;nowiki&amp;gt;&lt;br /&gt;
    require &amp;quot;yaml&amp;quot;&lt;br /&gt;
     class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
	@name = name&lt;br /&gt;
	@age = age&lt;br /&gt;
	@country=country&lt;br /&gt;
     end&lt;br /&gt;
     def to_s&lt;br /&gt;
	&amp;quot;In First:\n#{@name}, #{@age}, #{@country}\n&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
      def to_yaml_properties&lt;br /&gt;
	   [&amp;quot;@name&amp;quot;,&amp;quot;@age&amp;quot;]  #@country will not be serialized&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
  x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
  puts x&lt;br /&gt;
  puts x.to_yaml&lt;br /&gt;
  &amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   In First:&lt;br /&gt;
   Tom, 25, USA&lt;br /&gt;
   --- !ruby/object:First&lt;br /&gt;
   name: Tom&lt;br /&gt;
   age: 25&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Converting Ruby Objects to JSON format: ====&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
JSON 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. Ruby 1.8.7 distribution is not bundled with json gem. However, in Ruby 1.9.2, the json gem is bundled with the core Ruby distribution.&lt;br /&gt;
The JSON library can be installed using Ruby Gems[http://rubygems.org/] as shown below:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;# gem install json&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A JSON string for serialization can be created by using the JSON.generate method:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
        require 'json'&lt;br /&gt;
        my_hash = {:Welcome =&amp;gt; &amp;quot;Ruby&amp;quot;}&lt;br /&gt;
        puts JSON.generate(my_hash) =&amp;gt; &amp;quot;{\&amp;quot;WELCOME\&amp;quot;:\&amp;quot;RUBY\&amp;quot;}&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
'''Output:'''&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;{&amp;quot;{\&amp;quot;Welcome\&amp;quot;:\&amp;quot;Ruby\&amp;quot;}&amp;quot;=&amp;gt;&amp;quot;{\&amp;quot;WELCOME\&amp;quot;:\&amp;quot;RUBY\&amp;quot;}&amp;quot;}&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A JSON string received from another program can be parsed by using JSON.parse&lt;br /&gt;
Ruby thus converts String to Hash.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
        require 'json'&lt;br /&gt;
        my_hash = JSON.parse('{&amp;quot;Welcome&amp;quot;: &amp;quot;Ruby&amp;quot;}')&lt;br /&gt;
        puts my_hash[&amp;quot;Welcome&amp;quot;] =&amp;gt; &amp;quot;Ruby&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Converting Ruby Objects to Binary Formats ===&lt;br /&gt;
----&lt;br /&gt;
Binary Serialization is another form of serialization in Ruby which is not in human readable form. 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. Binary Serialization  is done using Marshal which is built into Ruby and the code for it is written in Ruby's Marshal module(marshal.c) and thus no additional files are required in order to use it. The _dump and _load methods defined in marshal are used for serialization. Using marshal module the following types of objects can not be serialized: bindings, procedure objects, singleton objects, instances of IO objects and interfaces.&lt;br /&gt;
&lt;br /&gt;
Since the Binary Serialized data is not in human readable form, the following guidelines need to be followed.&lt;br /&gt;
     1.Use print instead of puts[http://ruby-doc.org/core-2.0.0/ARGF.html#] when serialized objects are written to a file in order to avoid new line characters to be written &lt;br /&gt;
       in the file.&lt;br /&gt;
     &lt;br /&gt;
     2.Use a record separator in order to differentiate between two objects.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====='''Binary Serialization Example:'''=====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    class Animal&lt;br /&gt;
      def initialize  name, age&lt;br /&gt;
      @name = name&lt;br /&gt;
      @age=age&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
   class Cat &amp;lt; Animal&lt;br /&gt;
    def to_s&lt;br /&gt;
     &amp;quot;In Cat C: #{@name} \t #{@age}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
  c = Cat.new(&amp;quot;Kitty Kat&amp;quot;,5)&lt;br /&gt;
  puts &amp;quot;Before Serialization&amp;quot;&lt;br /&gt;
  puts c&lt;br /&gt;
  #puts d&lt;br /&gt;
  serialize_cat= Marshal.dump(c) #dumps the serialized cat object into serialize_cat&lt;br /&gt;
  puts &amp;quot;\nAfter Serialization:\n #{serialize_cat}&amp;quot;&lt;br /&gt;
  deserialize_cat= Marshal::load(serialize_cat) #deserializes the cat object and loads it back into deserialize_cat&lt;br /&gt;
  puts &amp;quot;\nAfter Deserialization\n #{deserialize_cat}&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   Before Serialization&lt;br /&gt;
   In Cat C: Kitty Kat 	 5&lt;br /&gt;
   After Serialization:&lt;br /&gt;
   oCat:&lt;br /&gt;
   @nameI&amp;quot;Kitty Kat:ET:	@agei&lt;br /&gt;
   After Deserialization&lt;br /&gt;
   In Cat C: Kitty Kat 	 5&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====='''Saving Marshal data into a file'''=====&lt;br /&gt;
&lt;br /&gt;
Similar to YAML, Marshal can also be used to dump data into a file. The above example showing the serialization using YAML::dump can be written using marshal as shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    f = File.open( 'first.yml', 'w' )&lt;br /&gt;
    Marshal.dump( [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;], f )&lt;br /&gt;
    f.close	&lt;br /&gt;
    File.open( 'first.yml' ){ |f|	&lt;br /&gt;
    $arr= Marshal.load(f)&lt;br /&gt;
    }	&lt;br /&gt;
    p( $arr )&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;]&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice that in the above example there is no &amp;quot;require&amp;quot; statement as opposed to the earlier example of writing serialized data into files using YAML. This is because unlike YAML, marshal is built-in Ruby and  no external libraries is required in order to use its functionality.&lt;br /&gt;
&lt;br /&gt;
====='''Custom serialization using Marshal'''=====&lt;br /&gt;
&lt;br /&gt;
Marshal can also be used to custom serialize an object i.e. it provides an option to omit the variables of an object that are not required in the serialized data. The following program shows the use of marshal_dump method for achieving custom serialization.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
      class First&lt;br /&gt;
      def initialize(name, age, country)&lt;br /&gt;
         @name = name&lt;br /&gt;
         @age = age&lt;br /&gt;
         @country=country&lt;br /&gt;
      end&lt;br /&gt;
      def to_s&lt;br /&gt;
            &amp;quot;In First: #{@name}, #{@age}, #{@country}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
      def marshal_dump&lt;br /&gt;
            [@name,@age]  #@country will not be serialized&lt;br /&gt;
      end&lt;br /&gt;
      def marshal_load(data)&lt;br /&gt;
       @name=data[0]&lt;br /&gt;
       @age=data[1]&lt;br /&gt;
       @country=&amp;quot;United States of America&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
      end&lt;br /&gt;
 &lt;br /&gt;
      x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
      puts x&lt;br /&gt;
  &lt;br /&gt;
      marshal_data = Marshal.dump( x ) &lt;br /&gt;
      y = Marshal.load( marshal_data ) &lt;br /&gt;
      p( y.to_s )&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    In First: Tom, 25, USA&lt;br /&gt;
    &amp;quot;In First: Tom, 25, United States of America&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== '''Serialization Performance in Ruby''' ==&lt;br /&gt;
The different serialization formats described above differ in the efficiency at which they can serialize and deserialize data and thus while serializing large amount of data their efficiency is taken into account. The .report[http://www.ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html] method in Ruby can be used to evaluate the performance of these serialization patterns. Marshal, as it handles data in binary format, is the most efficient form of serialization in Ruby. The following example compares the performance of marshal, JSON and YAML format.[http://www.pauldix.net/2008/08/serializing-dat.html]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   require 'benchmark'&lt;br /&gt;
   require 'rubygems'&lt;br /&gt;
   require 'json'&lt;br /&gt;
   require 'yaml'&lt;br /&gt;
   include Benchmark&lt;br /&gt;
   class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
      @name=name&lt;br /&gt;
      @age=age&lt;br /&gt;
      @country=country&lt;br /&gt;
     end&lt;br /&gt;
   end  &lt;br /&gt;
    x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
    puts &amp;quot;            User     System     Total      Real&amp;quot;&lt;br /&gt;
   benchmark do |t|&lt;br /&gt;
    print &amp;quot;Marshal:&amp;quot;&lt;br /&gt;
    t.report{1000.times do; Marshal.load(Marshal.dump(x));end}&lt;br /&gt;
    print &amp;quot;JSON:   &amp;quot;&lt;br /&gt;
    t.report{1000.times do; JSON.load(JSON.dump(x));end}&lt;br /&gt;
    print &amp;quot;YAML:   &amp;quot;&lt;br /&gt;
    t.report{1000.times do; YAML.load(YAML.dump(x));end}&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
              User     System     Total      Real&lt;br /&gt;
  Marshal:   0.010000   0.000000   0.010000 (  0.015408)&lt;br /&gt;
  JSON:      0.020000   0.010000   0.030000 (  0.022462)&lt;br /&gt;
  YAML:      0.460000   0.010000   0.470000 (  0.472149)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output of the above example shows that, given large chunks of data, marshal is comparatively more efficient format for serialization in Ruby.&lt;br /&gt;
&lt;br /&gt;
== Serialization in Object-Oriented Languages: Comparison ==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Sl.No !! Ruby !! Java !! .Net Framework !! C++&lt;br /&gt;
|-&lt;br /&gt;
| 1 || Ruby provides a built in module called 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 || Although, there is no built in support for serialization in C++, it can be achieved by using Boost libraries[http://www.boost.org/doc/libs/1_36_0/libs/serialization/example/demo.cpp]&lt;br /&gt;
|-&lt;br /&gt;
| 2 || The built in module of Ruby (Marshal) does not support platform independence, however, it can be achieved by using external libraries like YAML and JSON || Similarly, Java's built in serialization is also not platform independent and in order to use serialization in Java across Ruby platform, jruby library should be used.  || .Net used Remoting technology to make it platform independent. || Serialization using the Boost libraries is not platform independent.&lt;br /&gt;
|-&lt;br /&gt;
| 3 || YAML provides a method [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/syck/rdoc/Object.html (to_yaml_properties)] which can be used to select the variables who's value is need to be serialized. With Marshal, we need to write a method named marshal_dump defining the variables of an object that has to be serialized. || Provides an option for serializing only the required 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 data 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 || Serialization using the Boost libraries is custom and thus the user can specify the part of the objects to be serialized.&lt;br /&gt;
|-&lt;br /&gt;
| 4 || Bindings, procedure objects, singleton objects, instances of IO objects and interfaces can not be serialized. Serializing these objects throws TypeError exceptions. || Thread, OutputStream and its subclasses, and Socket are not serializable in Java.[http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html] || Objects like DataRow are non serializable in .NET. Whether an object is serializable or not can be checked using the [http://msdn.microsoft.com/en-us/library/system.type.isserializable(v=vs.110).aspx?cs-save-lang=1&amp;amp;cs-lang=cpp#code-snippet-1 Type.IsSerializable] || There are no such objects in C++.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== See Also==&lt;br /&gt;
&lt;br /&gt;
1. [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/json/rdoc/JSON.html Serialization in Ruby JSON]&lt;br /&gt;
&lt;br /&gt;
2. [http://api.rubyonrails.org/classes/ActiveModel/Serialization.html Serialization in Rails]&lt;br /&gt;
&lt;br /&gt;
3. [http://gregmoreno.wordpress.com/2011/01/27/preventing-model-explosion-via-rails-serialization/ Article on Rails Serialization]&lt;br /&gt;
&lt;br /&gt;
4. [http://en.wikipedia.org/wiki/Google_Protocol_Buffers Protocol Buffers]&lt;br /&gt;
&lt;br /&gt;
5. [https://rubygems.org/gems/ruby-protocol-buffers Ruby Protocol Buffers Gem]&lt;br /&gt;
&lt;br /&gt;
6. [http://en.wikipedia.org/wiki/Apache_Avro Avro]&lt;br /&gt;
&lt;br /&gt;
7. [http://rubygems.org/gems/avro Ruby Avro Gem]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
1. [http://en.wikipedia.org/wiki/Serialization Serilization in General]&lt;br /&gt;
&lt;br /&gt;
2. [http://en.wikipedia.org/wiki/Marshalling_%28computer_science%29 Marshaling]&lt;br /&gt;
&lt;br /&gt;
3. [http://techbrahmana.blogspot.com/2012/03/rails-cookie-handling-serialization-and.html Rails Cookie Handling]&lt;br /&gt;
&lt;br /&gt;
4. [http://yaml.org/ YAML]&lt;br /&gt;
&lt;br /&gt;
5. [http://www.ruby-doc.org/core-2.1.0/Marshal.html Marshal module]&lt;br /&gt;
&lt;br /&gt;
6. [http://json.org/ JSON]&lt;br /&gt;
&lt;br /&gt;
7. [http://rubygems.org/ Ruby Gems]&lt;br /&gt;
&lt;br /&gt;
8. [http://ruby-doc.org/core-2.0.0/ARGF.html# Ruby ARGF Documentation]&lt;br /&gt;
&lt;br /&gt;
9. [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html Ruby Documentation for Benchmark Gem]&lt;br /&gt;
&lt;br /&gt;
10. [http://www.pauldix.net/2008/08/serializing-dat.html Serialization Performance Comparison]&lt;br /&gt;
&lt;br /&gt;
11. [http://www.boost.org/doc/libs/1_36_0/libs/serialization/example/demo.cpp Boost Libraries]&lt;br /&gt;
&lt;br /&gt;
12. [http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html Java Serialization]&lt;br /&gt;
&lt;br /&gt;
13. [http://www.w3schools.com/json/ JSON Tutorial]&lt;br /&gt;
&lt;br /&gt;
14. [http://www.skorks.com/2010/04/serializing-and-deserializing-objects-with-ruby/ Serializing and De-serializing in Ruby]&lt;br /&gt;
&lt;br /&gt;
15. [http://www.codeproject.com/Articles/33296/Serialization-and-De-serialization Serialization and De-serialization]&lt;br /&gt;
&lt;br /&gt;
16. [http://msdn.microsoft.com/en-us/library/182eeyhh.aspx XML Serialization]&lt;br /&gt;
&lt;br /&gt;
17. [http://www.tutorialspoint.com/java/java_serialization.htm Java Serialization Tutorial]&lt;br /&gt;
&lt;br /&gt;
18. [http://www.sapphiresteel.com/ruby-programming/The-Book-Of-Ruby The Book of Ruby]&lt;br /&gt;
&lt;br /&gt;
19. [http://www.codeproject.com/Articles/20962/Introducing-Serialization-in-NET Serialization in .Net]&lt;br /&gt;
&lt;br /&gt;
20. [http://ruby.about.com/od/advancedruby/ss/Serialization-In-Ruby-Yaml.html Serialization in Ruby YAML]&lt;br /&gt;
&lt;br /&gt;
21. [http://ruby.about.com/od/tasks/a/The-Json-Gem.html Installing JSON gem]&lt;br /&gt;
&lt;br /&gt;
22. [http://www.ruby-doc.org/stdlib-2.0.0/libdoc/json/rdoc/JSON.html Serialization in Ruby JSON]&lt;/div&gt;</summary>
		<author><name>Mmehta2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1d_mm&amp;diff=83412</id>
		<title>CSC/ECE 517 Spring 2014/ch1a 1d mm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1d_mm&amp;diff=83412"/>
		<updated>2014-02-18T02:42:28Z</updated>

		<summary type="html">&lt;p&gt;Mmehta2: /* Converting Ruby Objects in Human Readable Formats */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page discusses Serialization, focusing on Ruby programming language. It describes different methods of Serialization in Ruby. In addition, it also compares serialization in different Object Oriented languages.&lt;br /&gt;
&lt;br /&gt;
== '''Introduction''' ==&lt;br /&gt;
&lt;br /&gt;
==='''Serialization'''===&lt;br /&gt;
&lt;br /&gt;
'''Serialization'''[http://en.wikipedia.org/wiki/Serialization] is a process of converting a data structure or an object into a stream of bytes or string to facilitate storage in memory, file (persistence storage) or transmission over a network. The process of Serialization is also referred to as ''Marshalling''[http://en.wikipedia.org/wiki/Marshalling_%28computer_science%29]. 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. &lt;br /&gt;
&lt;br /&gt;
==='''De-serialization'''===&lt;br /&gt;
&lt;br /&gt;
'''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''.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==='''Basic Advantages of Serialization'''===&lt;br /&gt;
&lt;br /&gt;
1. Communication between two or more processes on same machine. Object state can be saved and shared in a persistent or in-memory store.&lt;br /&gt;
&lt;br /&gt;
2. Communication between processes on different machines. Serialization facilitates the transmission of an object through a network.&lt;br /&gt;
&lt;br /&gt;
3. Creating a clone of an object. &lt;br /&gt;
&lt;br /&gt;
4. Cross-platform compatibility. Object can be serialized in a common format that is understood by multiple platforms. For example, JSON[http://www.json.org/] and XML.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==='''Practical Applications for Serialization'''===&lt;br /&gt;
&lt;br /&gt;
1. [http://docs.oracle.com/cd/E13222_01/wls/docs81/webapp/sessions.html#139726 HTTP Session] Replication by sharing session objects across web servers for load balancing and handling failover scenarios.&lt;br /&gt;
&lt;br /&gt;
2. Serialization facilitates communication in [http://en.wikipedia.org/wiki/Java_remote_method_invocation Remote Method Invocation] or [http://en.wikipedia.org/wiki/Remote_procedure_call Remote procedure calls]&lt;br /&gt;
&lt;br /&gt;
3. Rails Cookie Handling[http://techbrahmana.blogspot.com/2012/03/rails-cookie-handling-serialization-and.html]. Cookies are stored marshalled/unmarshalled to and from client machines.&lt;br /&gt;
&lt;br /&gt;
== '''Serialization in Ruby:''' ==&lt;br /&gt;
&lt;br /&gt;
Ruby provides serialization capabilities through its module, [http://www.ruby-doc.org/core-2.0.0/Marshal.html Marshal]. There are also other libraries like YAML[http://yaml.org/] and JSON which can be used in Ruby to generate serialized objects for purposes like platform independence and human readable formats.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:SerializationChart.jpg]]&lt;br /&gt;
&lt;br /&gt;
== '''Types of Serialization''' ==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
=== Converting Ruby Objects in Human Readable Formats ===&lt;br /&gt;
----&lt;br /&gt;
The conversion of Ruby objects into YAML and JSON formats are explained below.&lt;br /&gt;
&lt;br /&gt;
==== Converting Ruby Objects to YAML format ====&lt;br /&gt;
&lt;br /&gt;
YAML 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 binary format. 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. In order to use it in Ruby, the yaml.rb file is required to be loaded which provides methods for converting objects into yaml format and creating .yml files.&lt;br /&gt;
&lt;br /&gt;
====='''Examples of serialization using YAML:'''=====&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;nowiki&amp;gt; #Serialization using YAML's to_yaml method&lt;br /&gt;
 &lt;br /&gt;
  require &amp;quot;yaml&amp;quot;&lt;br /&gt;
     class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
	@name = name&lt;br /&gt;
	@age = age&lt;br /&gt;
	@country=country&lt;br /&gt;
     end&lt;br /&gt;
      def to_s&lt;br /&gt;
	&amp;quot;In First:\n#{@name}, #{@age}, #{@country}\n&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
  x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
  puts x&lt;br /&gt;
  puts x.to_yaml&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   In First:&lt;br /&gt;
   Tom, 25, USA&lt;br /&gt;
   --- !ruby/object:First&lt;br /&gt;
   name: Tom&lt;br /&gt;
   age: 25&lt;br /&gt;
   country: USA&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The above code displays the object x, first as a string and then in the yaml format. &lt;br /&gt;
&lt;br /&gt;
====='''Saving YAML data into a file:'''=====&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt; # Serialization using YAML::dump&lt;br /&gt;
&lt;br /&gt;
require 'yaml'&lt;br /&gt;
&lt;br /&gt;
f = File.open( 'first.yml', 'w' )&lt;br /&gt;
YAML.dump( [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;], f )&lt;br /&gt;
f.close&lt;br /&gt;
		&lt;br /&gt;
File.open( 'first.yml' ){ |f|	&lt;br /&gt;
    $arr= YAML.load(f)&lt;br /&gt;
}	&lt;br /&gt;
&lt;br /&gt;
p( $arr )&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;]&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The dump function can be used to serialize the data and save it into a file in YAML format. As shown in the above example the data in YAML format can be de-serialized using the load function.&lt;br /&gt;
&lt;br /&gt;
YAML libraries also provides an option of selecting only those variables of the object that are needed to be serialized. This is done using the to_yaml_properties method as shown in the below example.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=====Custom Serialization using YAML=====&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;nowiki&amp;gt;&lt;br /&gt;
    require &amp;quot;yaml&amp;quot;&lt;br /&gt;
     class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
	@name = name&lt;br /&gt;
	@age = age&lt;br /&gt;
	@country=country&lt;br /&gt;
     end&lt;br /&gt;
     def to_s&lt;br /&gt;
	&amp;quot;In First:\n#{@name}, #{@age}, #{@country}\n&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
      def to_yaml_properties&lt;br /&gt;
	   [&amp;quot;@name&amp;quot;,&amp;quot;@age&amp;quot;]  #@country will not be serialized&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
  x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
  puts x&lt;br /&gt;
  puts x.to_yaml&lt;br /&gt;
  &amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   In First:&lt;br /&gt;
   Tom, 25, USA&lt;br /&gt;
   --- !ruby/object:First&lt;br /&gt;
   name: Tom&lt;br /&gt;
   age: 25&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Converting Ruby Objects to JSON format: ====&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
JSON 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. Ruby 1.8.7 distribution is not bundled with json gem. However, in Ruby 1.9.2, the json gem is bundled with the core Ruby distribution.&lt;br /&gt;
The JSON library can be installed using Ruby Gems[http://rubygems.org/] as shown below:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;# gem install json&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A JSON string for serialization can be created by using the JSON.generate method:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
        require 'json'&lt;br /&gt;
        my_hash = {:Welcome =&amp;gt; &amp;quot;Ruby&amp;quot;}&lt;br /&gt;
        puts JSON.generate(my_hash) =&amp;gt; &amp;quot;{\&amp;quot;WELCOME\&amp;quot;:\&amp;quot;RUBY\&amp;quot;}&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
'''Output:'''&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;{&amp;quot;{\&amp;quot;Welcome\&amp;quot;:\&amp;quot;Ruby\&amp;quot;}&amp;quot;=&amp;gt;&amp;quot;{\&amp;quot;WELCOME\&amp;quot;:\&amp;quot;RUBY\&amp;quot;}&amp;quot;}&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A JSON string received from another program can be parsed by using JSON.parse&lt;br /&gt;
Ruby thus converts String to Hash.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
        require 'json'&lt;br /&gt;
        my_hash = JSON.parse('{&amp;quot;Welcome&amp;quot;: &amp;quot;Ruby&amp;quot;}')&lt;br /&gt;
        puts my_hash[&amp;quot;Welcome&amp;quot;] =&amp;gt; &amp;quot;Ruby&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Converting Ruby Objects to Binary Formats ===&lt;br /&gt;
Binary Serialization is another form of serialization in Ruby which is not in human readable form. 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. Binary Serialization  is done using Marshal which is built into Ruby and the code for it is written in Ruby's Marshal module(marshal.c) and thus no additional files are required in order to use it. The _dump and _load methods defined in marshal are used for serialization. Using marshal module the following types of objects can not be serialized: bindings, procedure objects, singleton objects, instances of IO objects and interfaces.&lt;br /&gt;
&lt;br /&gt;
Since the Binary Serialized data is not in human readable form, the following guidelines need to be followed.&lt;br /&gt;
     1.Use print instead of puts[http://ruby-doc.org/core-2.0.0/ARGF.html#] when serialized objects are written to a file in order to avoid new line characters to be written &lt;br /&gt;
       in the file.&lt;br /&gt;
     &lt;br /&gt;
     2.Use a record separator in order to differentiate between two objects.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====='''Binary Serialization Example:'''=====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    class Animal&lt;br /&gt;
      def initialize  name, age&lt;br /&gt;
      @name = name&lt;br /&gt;
      @age=age&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
   class Cat &amp;lt; Animal&lt;br /&gt;
    def to_s&lt;br /&gt;
     &amp;quot;In Cat C: #{@name} \t #{@age}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
  c = Cat.new(&amp;quot;Kitty Kat&amp;quot;,5)&lt;br /&gt;
  puts &amp;quot;Before Serialization&amp;quot;&lt;br /&gt;
  puts c&lt;br /&gt;
  #puts d&lt;br /&gt;
  serialize_cat= Marshal.dump(c) #dumps the serialized cat object into serialize_cat&lt;br /&gt;
  puts &amp;quot;\nAfter Serialization:\n #{serialize_cat}&amp;quot;&lt;br /&gt;
  deserialize_cat= Marshal::load(serialize_cat) #deserializes the cat object and loads it back into deserialize_cat&lt;br /&gt;
  puts &amp;quot;\nAfter Deserialization\n #{deserialize_cat}&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   Before Serialization&lt;br /&gt;
   In Cat C: Kitty Kat 	 5&lt;br /&gt;
   After Serialization:&lt;br /&gt;
   oCat:&lt;br /&gt;
   @nameI&amp;quot;Kitty Kat:ET:	@agei&lt;br /&gt;
   After Deserialization&lt;br /&gt;
   In Cat C: Kitty Kat 	 5&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====='''Saving Marshal data into a file'''=====&lt;br /&gt;
&lt;br /&gt;
Similar to YAML, Marshal can also be used to dump data into a file. The above example showing the serialization using YAML::dump can be written using marshal as shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    f = File.open( 'first.yml', 'w' )&lt;br /&gt;
    Marshal.dump( [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;], f )&lt;br /&gt;
    f.close	&lt;br /&gt;
    File.open( 'first.yml' ){ |f|	&lt;br /&gt;
    $arr= Marshal.load(f)&lt;br /&gt;
    }	&lt;br /&gt;
    p( $arr )&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;]&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice that in the above example there is no &amp;quot;require&amp;quot; statement as opposed to the earlier example of writing serialized data into files using YAML. This is because unlike YAML, marshal is built-in Ruby and  no external libraries is required in order to use its functionality.&lt;br /&gt;
&lt;br /&gt;
====='''Custom serialization using Marshal'''=====&lt;br /&gt;
&lt;br /&gt;
Marshal can also be used to custom serialize an object i.e. it provides an option to omit the variables of an object that are not required in the serialized data. The following program shows the use of marshal_dump method for achieving custom serialization.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
      class First&lt;br /&gt;
      def initialize(name, age, country)&lt;br /&gt;
         @name = name&lt;br /&gt;
         @age = age&lt;br /&gt;
         @country=country&lt;br /&gt;
      end&lt;br /&gt;
      def to_s&lt;br /&gt;
            &amp;quot;In First: #{@name}, #{@age}, #{@country}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
      def marshal_dump&lt;br /&gt;
            [@name,@age]  #@country will not be serialized&lt;br /&gt;
      end&lt;br /&gt;
      def marshal_load(data)&lt;br /&gt;
       @name=data[0]&lt;br /&gt;
       @age=data[1]&lt;br /&gt;
       @country=&amp;quot;United States of America&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
      end&lt;br /&gt;
 &lt;br /&gt;
      x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
      puts x&lt;br /&gt;
  &lt;br /&gt;
      marshal_data = Marshal.dump( x ) &lt;br /&gt;
      y = Marshal.load( marshal_data ) &lt;br /&gt;
      p( y.to_s )&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    In First: Tom, 25, USA&lt;br /&gt;
    &amp;quot;In First: Tom, 25, United States of America&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== '''Serialization Performance in Ruby''' ==&lt;br /&gt;
The different serialization formats described above differ in the efficiency at which they can serialize and deserialize data and thus while serializing large amount of data their efficiency is taken into account. The .report[http://www.ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html] method in Ruby can be used to evaluate the performance of these serialization patterns. Marshal, as it handles data in binary format, is the most efficient form of serialization in Ruby. The following example compares the performance of marshal, JSON and YAML format.[http://www.pauldix.net/2008/08/serializing-dat.html]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   require 'benchmark'&lt;br /&gt;
   require 'rubygems'&lt;br /&gt;
   require 'json'&lt;br /&gt;
   require 'yaml'&lt;br /&gt;
   include Benchmark&lt;br /&gt;
   class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
      @name=name&lt;br /&gt;
      @age=age&lt;br /&gt;
      @country=country&lt;br /&gt;
     end&lt;br /&gt;
   end  &lt;br /&gt;
    x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
    puts &amp;quot;            User     System     Total      Real&amp;quot;&lt;br /&gt;
   benchmark do |t|&lt;br /&gt;
    print &amp;quot;Marshal:&amp;quot;&lt;br /&gt;
    t.report{1000.times do; Marshal.load(Marshal.dump(x));end}&lt;br /&gt;
    print &amp;quot;JSON:   &amp;quot;&lt;br /&gt;
    t.report{1000.times do; JSON.load(JSON.dump(x));end}&lt;br /&gt;
    print &amp;quot;YAML:   &amp;quot;&lt;br /&gt;
    t.report{1000.times do; YAML.load(YAML.dump(x));end}&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
              User     System     Total      Real&lt;br /&gt;
  Marshal:   0.010000   0.000000   0.010000 (  0.015408)&lt;br /&gt;
  JSON:      0.020000   0.010000   0.030000 (  0.022462)&lt;br /&gt;
  YAML:      0.460000   0.010000   0.470000 (  0.472149)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output of the above example shows that, given large chunks of data, marshal is comparatively more efficient format for serialization in Ruby.&lt;br /&gt;
&lt;br /&gt;
== Serialization in Object-Oriented Languages: Comparison ==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Sl.No !! Ruby !! Java !! .Net Framework !! C++&lt;br /&gt;
|-&lt;br /&gt;
| 1 || Ruby provides a built in module called 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 || Although, there is no built in support for serialization in C++, it can be achieved by using Boost libraries[http://www.boost.org/doc/libs/1_36_0/libs/serialization/example/demo.cpp]&lt;br /&gt;
|-&lt;br /&gt;
| 2 || The built in module of Ruby (Marshal) does not support platform independence, however, it can be achieved by using external libraries like YAML and JSON || Similarly, Java's built in serialization is also not platform independent and in order to use serialization in Java across Ruby platform, jruby library should be used.  || .Net used Remoting technology to make it platform independent. || Serialization using the Boost libraries is not platform independent.&lt;br /&gt;
|-&lt;br /&gt;
| 3 || YAML provides a method [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/syck/rdoc/Object.html (to_yaml_properties)] which can be used to select the variables who's value is need to be serialized. With Marshal, we need to write a method named marshal_dump defining the variables of an object that has to be serialized. || Provides an option for serializing only the required 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 data 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 || Serialization using the Boost libraries is custom and thus the user can specify the part of the objects to be serialized.&lt;br /&gt;
|-&lt;br /&gt;
| 4 || Bindings, procedure objects, singleton objects, instances of IO objects and interfaces can not be serialized. Serializing these objects throws TypeError exceptions. || Thread, OutputStream and its subclasses, and Socket are not serializable in Java.[http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html] || Objects like DataRow are non serializable in .NET. Whether an object is serializable or not can be checked using the [http://msdn.microsoft.com/en-us/library/system.type.isserializable(v=vs.110).aspx?cs-save-lang=1&amp;amp;cs-lang=cpp#code-snippet-1 Type.IsSerializable] || There are no such objects in C++.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== See Also==&lt;br /&gt;
&lt;br /&gt;
1. [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/json/rdoc/JSON.html Serialization in Ruby JSON]&lt;br /&gt;
&lt;br /&gt;
2. [http://api.rubyonrails.org/classes/ActiveModel/Serialization.html Serialization in Rails]&lt;br /&gt;
&lt;br /&gt;
3. [http://gregmoreno.wordpress.com/2011/01/27/preventing-model-explosion-via-rails-serialization/ Article on Rails Serialization]&lt;br /&gt;
&lt;br /&gt;
4. [http://en.wikipedia.org/wiki/Google_Protocol_Buffers Protocol Buffers]&lt;br /&gt;
&lt;br /&gt;
5. [https://rubygems.org/gems/ruby-protocol-buffers Ruby Protocol Buffers Gem]&lt;br /&gt;
&lt;br /&gt;
6. [http://en.wikipedia.org/wiki/Apache_Avro Avro]&lt;br /&gt;
&lt;br /&gt;
7. [http://rubygems.org/gems/avro Ruby Avro Gem]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
1. [http://en.wikipedia.org/wiki/Serialization Serilization in General]&lt;br /&gt;
&lt;br /&gt;
2. [http://en.wikipedia.org/wiki/Marshalling_%28computer_science%29 Marshaling]&lt;br /&gt;
&lt;br /&gt;
3. [http://techbrahmana.blogspot.com/2012/03/rails-cookie-handling-serialization-and.html Rails Cookie Handling]&lt;br /&gt;
&lt;br /&gt;
4. [http://yaml.org/ YAML]&lt;br /&gt;
&lt;br /&gt;
5. [http://www.ruby-doc.org/core-2.1.0/Marshal.html Marshal module]&lt;br /&gt;
&lt;br /&gt;
6. [http://json.org/ JSON]&lt;br /&gt;
&lt;br /&gt;
7. [http://rubygems.org/ Ruby Gems]&lt;br /&gt;
&lt;br /&gt;
8. [http://ruby-doc.org/core-2.0.0/ARGF.html# Ruby ARGF Documentation]&lt;br /&gt;
&lt;br /&gt;
9. [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html Ruby Documentation for Benchmark Gem]&lt;br /&gt;
&lt;br /&gt;
10. [http://www.pauldix.net/2008/08/serializing-dat.html Serialization Performance Comparison]&lt;br /&gt;
&lt;br /&gt;
11. [http://www.boost.org/doc/libs/1_36_0/libs/serialization/example/demo.cpp Boost Libraries]&lt;br /&gt;
&lt;br /&gt;
12. [http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html Java Serialization]&lt;br /&gt;
&lt;br /&gt;
13. [http://www.w3schools.com/json/ JSON Tutorial]&lt;br /&gt;
&lt;br /&gt;
14. [http://www.skorks.com/2010/04/serializing-and-deserializing-objects-with-ruby/ Serializing and De-serializing in Ruby]&lt;br /&gt;
&lt;br /&gt;
15. [http://www.codeproject.com/Articles/33296/Serialization-and-De-serialization Serialization and De-serialization]&lt;br /&gt;
&lt;br /&gt;
16. [http://msdn.microsoft.com/en-us/library/182eeyhh.aspx XML Serialization]&lt;br /&gt;
&lt;br /&gt;
17. [http://www.tutorialspoint.com/java/java_serialization.htm Java Serialization Tutorial]&lt;br /&gt;
&lt;br /&gt;
18. [http://www.sapphiresteel.com/ruby-programming/The-Book-Of-Ruby The Book of Ruby]&lt;br /&gt;
&lt;br /&gt;
19. [http://www.codeproject.com/Articles/20962/Introducing-Serialization-in-NET Serialization in .Net]&lt;br /&gt;
&lt;br /&gt;
20. [http://ruby.about.com/od/advancedruby/ss/Serialization-In-Ruby-Yaml.html Serialization in Ruby YAML]&lt;br /&gt;
&lt;br /&gt;
21. [http://ruby.about.com/od/tasks/a/The-Json-Gem.html Installing JSON gem]&lt;br /&gt;
&lt;br /&gt;
22. [http://www.ruby-doc.org/stdlib-2.0.0/libdoc/json/rdoc/JSON.html Serialization in Ruby JSON]&lt;/div&gt;</summary>
		<author><name>Mmehta2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1d_mm&amp;diff=83411</id>
		<title>CSC/ECE 517 Spring 2014/ch1a 1d mm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1d_mm&amp;diff=83411"/>
		<updated>2014-02-18T02:41:00Z</updated>

		<summary type="html">&lt;p&gt;Mmehta2: /* Converting Ruby Objects to Binary Formats */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page discusses Serialization, focusing on Ruby programming language. It describes different methods of Serialization in Ruby. In addition, it also compares serialization in different Object Oriented languages.&lt;br /&gt;
&lt;br /&gt;
== '''Introduction''' ==&lt;br /&gt;
&lt;br /&gt;
==='''Serialization'''===&lt;br /&gt;
&lt;br /&gt;
'''Serialization'''[http://en.wikipedia.org/wiki/Serialization] is a process of converting a data structure or an object into a stream of bytes or string to facilitate storage in memory, file (persistence storage) or transmission over a network. The process of Serialization is also referred to as ''Marshalling''[http://en.wikipedia.org/wiki/Marshalling_%28computer_science%29]. 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. &lt;br /&gt;
&lt;br /&gt;
==='''De-serialization'''===&lt;br /&gt;
&lt;br /&gt;
'''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''.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==='''Basic Advantages of Serialization'''===&lt;br /&gt;
&lt;br /&gt;
1. Communication between two or more processes on same machine. Object state can be saved and shared in a persistent or in-memory store.&lt;br /&gt;
&lt;br /&gt;
2. Communication between processes on different machines. Serialization facilitates the transmission of an object through a network.&lt;br /&gt;
&lt;br /&gt;
3. Creating a clone of an object. &lt;br /&gt;
&lt;br /&gt;
4. Cross-platform compatibility. Object can be serialized in a common format that is understood by multiple platforms. For example, JSON[http://www.json.org/] and XML.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==='''Practical Applications for Serialization'''===&lt;br /&gt;
&lt;br /&gt;
1. [http://docs.oracle.com/cd/E13222_01/wls/docs81/webapp/sessions.html#139726 HTTP Session] Replication by sharing session objects across web servers for load balancing and handling failover scenarios.&lt;br /&gt;
&lt;br /&gt;
2. Serialization facilitates communication in [http://en.wikipedia.org/wiki/Java_remote_method_invocation Remote Method Invocation] or [http://en.wikipedia.org/wiki/Remote_procedure_call Remote procedure calls]&lt;br /&gt;
&lt;br /&gt;
3. Rails Cookie Handling[http://techbrahmana.blogspot.com/2012/03/rails-cookie-handling-serialization-and.html]. Cookies are stored marshalled/unmarshalled to and from client machines.&lt;br /&gt;
&lt;br /&gt;
== '''Serialization in Ruby:''' ==&lt;br /&gt;
&lt;br /&gt;
Ruby provides serialization capabilities through its module, [http://www.ruby-doc.org/core-2.0.0/Marshal.html Marshal]. There are also other libraries like YAML[http://yaml.org/] and JSON which can be used in Ruby to generate serialized objects for purposes like platform independence and human readable formats.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:SerializationChart.jpg]]&lt;br /&gt;
&lt;br /&gt;
== '''Types of Serialization''' ==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
=== Converting Ruby Objects in Human Readable Formats ===&lt;br /&gt;
The conversion of Ruby objects into YAML and JSON formats are explained below.&lt;br /&gt;
&lt;br /&gt;
==== Converting Ruby Objects to YAML format ====&lt;br /&gt;
&lt;br /&gt;
YAML 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 binary format. 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. In order to use it in Ruby, the yaml.rb file is required to be loaded which provides methods for converting objects into yaml format and creating .yml files.&lt;br /&gt;
&lt;br /&gt;
====='''Examples of serialization using YAML:'''=====&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;nowiki&amp;gt; #Serialization using YAML's to_yaml method&lt;br /&gt;
 &lt;br /&gt;
  require &amp;quot;yaml&amp;quot;&lt;br /&gt;
     class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
	@name = name&lt;br /&gt;
	@age = age&lt;br /&gt;
	@country=country&lt;br /&gt;
     end&lt;br /&gt;
      def to_s&lt;br /&gt;
	&amp;quot;In First:\n#{@name}, #{@age}, #{@country}\n&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
  x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
  puts x&lt;br /&gt;
  puts x.to_yaml&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   In First:&lt;br /&gt;
   Tom, 25, USA&lt;br /&gt;
   --- !ruby/object:First&lt;br /&gt;
   name: Tom&lt;br /&gt;
   age: 25&lt;br /&gt;
   country: USA&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The above code displays the object x, first as a string and then in the yaml format. &lt;br /&gt;
&lt;br /&gt;
====='''Saving YAML data into a file:'''=====&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt; # Serialization using YAML::dump&lt;br /&gt;
&lt;br /&gt;
require 'yaml'&lt;br /&gt;
&lt;br /&gt;
f = File.open( 'first.yml', 'w' )&lt;br /&gt;
YAML.dump( [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;], f )&lt;br /&gt;
f.close&lt;br /&gt;
		&lt;br /&gt;
File.open( 'first.yml' ){ |f|	&lt;br /&gt;
    $arr= YAML.load(f)&lt;br /&gt;
}	&lt;br /&gt;
&lt;br /&gt;
p( $arr )&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;]&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The dump function can be used to serialize the data and save it into a file in YAML format. As shown in the above example the data in YAML format can be de-serialized using the load function.&lt;br /&gt;
&lt;br /&gt;
YAML libraries also provides an option of selecting only those variables of the object that are needed to be serialized. This is done using the to_yaml_properties method as shown in the below example.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=====Custom Serialization using YAML=====&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;nowiki&amp;gt;&lt;br /&gt;
    require &amp;quot;yaml&amp;quot;&lt;br /&gt;
     class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
	@name = name&lt;br /&gt;
	@age = age&lt;br /&gt;
	@country=country&lt;br /&gt;
     end&lt;br /&gt;
     def to_s&lt;br /&gt;
	&amp;quot;In First:\n#{@name}, #{@age}, #{@country}\n&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
      def to_yaml_properties&lt;br /&gt;
	   [&amp;quot;@name&amp;quot;,&amp;quot;@age&amp;quot;]  #@country will not be serialized&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
  x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
  puts x&lt;br /&gt;
  puts x.to_yaml&lt;br /&gt;
  &amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   In First:&lt;br /&gt;
   Tom, 25, USA&lt;br /&gt;
   --- !ruby/object:First&lt;br /&gt;
   name: Tom&lt;br /&gt;
   age: 25&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Converting Ruby Objects to JSON format: ====&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
JSON 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. Ruby 1.8.7 distribution is not bundled with json gem. However, in Ruby 1.9.2, the json gem is bundled with the core Ruby distribution.&lt;br /&gt;
The JSON library can be installed using Ruby Gems[http://rubygems.org/] as shown below:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;# gem install json&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A JSON string for serialization can be created by using the JSON.generate method:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
        require 'json'&lt;br /&gt;
        my_hash = {:Welcome =&amp;gt; &amp;quot;Ruby&amp;quot;}&lt;br /&gt;
        puts JSON.generate(my_hash) =&amp;gt; &amp;quot;{\&amp;quot;WELCOME\&amp;quot;:\&amp;quot;RUBY\&amp;quot;}&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
'''Output:'''&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;{&amp;quot;{\&amp;quot;Welcome\&amp;quot;:\&amp;quot;Ruby\&amp;quot;}&amp;quot;=&amp;gt;&amp;quot;{\&amp;quot;WELCOME\&amp;quot;:\&amp;quot;RUBY\&amp;quot;}&amp;quot;}&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A JSON string received from another program can be parsed by using JSON.parse&lt;br /&gt;
Ruby thus converts String to Hash.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
        require 'json'&lt;br /&gt;
        my_hash = JSON.parse('{&amp;quot;Welcome&amp;quot;: &amp;quot;Ruby&amp;quot;}')&lt;br /&gt;
        puts my_hash[&amp;quot;Welcome&amp;quot;] =&amp;gt; &amp;quot;Ruby&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Converting Ruby Objects to Binary Formats ===&lt;br /&gt;
Binary Serialization is another form of serialization in Ruby which is not in human readable form. 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. Binary Serialization  is done using Marshal which is built into Ruby and the code for it is written in Ruby's Marshal module(marshal.c) and thus no additional files are required in order to use it. The _dump and _load methods defined in marshal are used for serialization. Using marshal module the following types of objects can not be serialized: bindings, procedure objects, singleton objects, instances of IO objects and interfaces.&lt;br /&gt;
&lt;br /&gt;
Since the Binary Serialized data is not in human readable form, the following guidelines need to be followed.&lt;br /&gt;
     1.Use print instead of puts[http://ruby-doc.org/core-2.0.0/ARGF.html#] when serialized objects are written to a file in order to avoid new line characters to be written &lt;br /&gt;
       in the file.&lt;br /&gt;
     &lt;br /&gt;
     2.Use a record separator in order to differentiate between two objects.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====='''Binary Serialization Example:'''=====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    class Animal&lt;br /&gt;
      def initialize  name, age&lt;br /&gt;
      @name = name&lt;br /&gt;
      @age=age&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
   class Cat &amp;lt; Animal&lt;br /&gt;
    def to_s&lt;br /&gt;
     &amp;quot;In Cat C: #{@name} \t #{@age}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
  c = Cat.new(&amp;quot;Kitty Kat&amp;quot;,5)&lt;br /&gt;
  puts &amp;quot;Before Serialization&amp;quot;&lt;br /&gt;
  puts c&lt;br /&gt;
  #puts d&lt;br /&gt;
  serialize_cat= Marshal.dump(c) #dumps the serialized cat object into serialize_cat&lt;br /&gt;
  puts &amp;quot;\nAfter Serialization:\n #{serialize_cat}&amp;quot;&lt;br /&gt;
  deserialize_cat= Marshal::load(serialize_cat) #deserializes the cat object and loads it back into deserialize_cat&lt;br /&gt;
  puts &amp;quot;\nAfter Deserialization\n #{deserialize_cat}&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   Before Serialization&lt;br /&gt;
   In Cat C: Kitty Kat 	 5&lt;br /&gt;
   After Serialization:&lt;br /&gt;
   oCat:&lt;br /&gt;
   @nameI&amp;quot;Kitty Kat:ET:	@agei&lt;br /&gt;
   After Deserialization&lt;br /&gt;
   In Cat C: Kitty Kat 	 5&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====='''Saving Marshal data into a file'''=====&lt;br /&gt;
&lt;br /&gt;
Similar to YAML, Marshal can also be used to dump data into a file. The above example showing the serialization using YAML::dump can be written using marshal as shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    f = File.open( 'first.yml', 'w' )&lt;br /&gt;
    Marshal.dump( [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;], f )&lt;br /&gt;
    f.close	&lt;br /&gt;
    File.open( 'first.yml' ){ |f|	&lt;br /&gt;
    $arr= Marshal.load(f)&lt;br /&gt;
    }	&lt;br /&gt;
    p( $arr )&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;]&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice that in the above example there is no &amp;quot;require&amp;quot; statement as opposed to the earlier example of writing serialized data into files using YAML. This is because unlike YAML, marshal is built-in Ruby and  no external libraries is required in order to use its functionality.&lt;br /&gt;
&lt;br /&gt;
====='''Custom serialization using Marshal'''=====&lt;br /&gt;
&lt;br /&gt;
Marshal can also be used to custom serialize an object i.e. it provides an option to omit the variables of an object that are not required in the serialized data. The following program shows the use of marshal_dump method for achieving custom serialization.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
      class First&lt;br /&gt;
      def initialize(name, age, country)&lt;br /&gt;
         @name = name&lt;br /&gt;
         @age = age&lt;br /&gt;
         @country=country&lt;br /&gt;
      end&lt;br /&gt;
      def to_s&lt;br /&gt;
            &amp;quot;In First: #{@name}, #{@age}, #{@country}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
      def marshal_dump&lt;br /&gt;
            [@name,@age]  #@country will not be serialized&lt;br /&gt;
      end&lt;br /&gt;
      def marshal_load(data)&lt;br /&gt;
       @name=data[0]&lt;br /&gt;
       @age=data[1]&lt;br /&gt;
       @country=&amp;quot;United States of America&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
      end&lt;br /&gt;
 &lt;br /&gt;
      x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
      puts x&lt;br /&gt;
  &lt;br /&gt;
      marshal_data = Marshal.dump( x ) &lt;br /&gt;
      y = Marshal.load( marshal_data ) &lt;br /&gt;
      p( y.to_s )&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    In First: Tom, 25, USA&lt;br /&gt;
    &amp;quot;In First: Tom, 25, United States of America&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== '''Serialization Performance in Ruby''' ==&lt;br /&gt;
The different serialization formats described above differ in the efficiency at which they can serialize and deserialize data and thus while serializing large amount of data their efficiency is taken into account. The .report[http://www.ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html] method in Ruby can be used to evaluate the performance of these serialization patterns. Marshal, as it handles data in binary format, is the most efficient form of serialization in Ruby. The following example compares the performance of marshal, JSON and YAML format.[http://www.pauldix.net/2008/08/serializing-dat.html]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   require 'benchmark'&lt;br /&gt;
   require 'rubygems'&lt;br /&gt;
   require 'json'&lt;br /&gt;
   require 'yaml'&lt;br /&gt;
   include Benchmark&lt;br /&gt;
   class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
      @name=name&lt;br /&gt;
      @age=age&lt;br /&gt;
      @country=country&lt;br /&gt;
     end&lt;br /&gt;
   end  &lt;br /&gt;
    x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
    puts &amp;quot;            User     System     Total      Real&amp;quot;&lt;br /&gt;
   benchmark do |t|&lt;br /&gt;
    print &amp;quot;Marshal:&amp;quot;&lt;br /&gt;
    t.report{1000.times do; Marshal.load(Marshal.dump(x));end}&lt;br /&gt;
    print &amp;quot;JSON:   &amp;quot;&lt;br /&gt;
    t.report{1000.times do; JSON.load(JSON.dump(x));end}&lt;br /&gt;
    print &amp;quot;YAML:   &amp;quot;&lt;br /&gt;
    t.report{1000.times do; YAML.load(YAML.dump(x));end}&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
              User     System     Total      Real&lt;br /&gt;
  Marshal:   0.010000   0.000000   0.010000 (  0.015408)&lt;br /&gt;
  JSON:      0.020000   0.010000   0.030000 (  0.022462)&lt;br /&gt;
  YAML:      0.460000   0.010000   0.470000 (  0.472149)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output of the above example shows that, given large chunks of data, marshal is comparatively more efficient format for serialization in Ruby.&lt;br /&gt;
&lt;br /&gt;
== Serialization in Object-Oriented Languages: Comparison ==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Sl.No !! Ruby !! Java !! .Net Framework !! C++&lt;br /&gt;
|-&lt;br /&gt;
| 1 || Ruby provides a built in module called 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 || Although, there is no built in support for serialization in C++, it can be achieved by using Boost libraries[http://www.boost.org/doc/libs/1_36_0/libs/serialization/example/demo.cpp]&lt;br /&gt;
|-&lt;br /&gt;
| 2 || The built in module of Ruby (Marshal) does not support platform independence, however, it can be achieved by using external libraries like YAML and JSON || Similarly, Java's built in serialization is also not platform independent and in order to use serialization in Java across Ruby platform, jruby library should be used.  || .Net used Remoting technology to make it platform independent. || Serialization using the Boost libraries is not platform independent.&lt;br /&gt;
|-&lt;br /&gt;
| 3 || YAML provides a method [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/syck/rdoc/Object.html (to_yaml_properties)] which can be used to select the variables who's value is need to be serialized. With Marshal, we need to write a method named marshal_dump defining the variables of an object that has to be serialized. || Provides an option for serializing only the required 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 data 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 || Serialization using the Boost libraries is custom and thus the user can specify the part of the objects to be serialized.&lt;br /&gt;
|-&lt;br /&gt;
| 4 || Bindings, procedure objects, singleton objects, instances of IO objects and interfaces can not be serialized. Serializing these objects throws TypeError exceptions. || Thread, OutputStream and its subclasses, and Socket are not serializable in Java.[http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html] || Objects like DataRow are non serializable in .NET. Whether an object is serializable or not can be checked using the [http://msdn.microsoft.com/en-us/library/system.type.isserializable(v=vs.110).aspx?cs-save-lang=1&amp;amp;cs-lang=cpp#code-snippet-1 Type.IsSerializable] || There are no such objects in C++.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== See Also==&lt;br /&gt;
&lt;br /&gt;
1. [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/json/rdoc/JSON.html Serialization in Ruby JSON]&lt;br /&gt;
&lt;br /&gt;
2. [http://api.rubyonrails.org/classes/ActiveModel/Serialization.html Serialization in Rails]&lt;br /&gt;
&lt;br /&gt;
3. [http://gregmoreno.wordpress.com/2011/01/27/preventing-model-explosion-via-rails-serialization/ Article on Rails Serialization]&lt;br /&gt;
&lt;br /&gt;
4. [http://en.wikipedia.org/wiki/Google_Protocol_Buffers Protocol Buffers]&lt;br /&gt;
&lt;br /&gt;
5. [https://rubygems.org/gems/ruby-protocol-buffers Ruby Protocol Buffers Gem]&lt;br /&gt;
&lt;br /&gt;
6. [http://en.wikipedia.org/wiki/Apache_Avro Avro]&lt;br /&gt;
&lt;br /&gt;
7. [http://rubygems.org/gems/avro Ruby Avro Gem]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
1. [http://en.wikipedia.org/wiki/Serialization Serilization in General]&lt;br /&gt;
&lt;br /&gt;
2. [http://en.wikipedia.org/wiki/Marshalling_%28computer_science%29 Marshaling]&lt;br /&gt;
&lt;br /&gt;
3. [http://techbrahmana.blogspot.com/2012/03/rails-cookie-handling-serialization-and.html Rails Cookie Handling]&lt;br /&gt;
&lt;br /&gt;
4. [http://yaml.org/ YAML]&lt;br /&gt;
&lt;br /&gt;
5. [http://www.ruby-doc.org/core-2.1.0/Marshal.html Marshal module]&lt;br /&gt;
&lt;br /&gt;
6. [http://json.org/ JSON]&lt;br /&gt;
&lt;br /&gt;
7. [http://rubygems.org/ Ruby Gems]&lt;br /&gt;
&lt;br /&gt;
8. [http://ruby-doc.org/core-2.0.0/ARGF.html# Ruby ARGF Documentation]&lt;br /&gt;
&lt;br /&gt;
9. [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html Ruby Documentation for Benchmark Gem]&lt;br /&gt;
&lt;br /&gt;
10. [http://www.pauldix.net/2008/08/serializing-dat.html Serialization Performance Comparison]&lt;br /&gt;
&lt;br /&gt;
11. [http://www.boost.org/doc/libs/1_36_0/libs/serialization/example/demo.cpp Boost Libraries]&lt;br /&gt;
&lt;br /&gt;
12. [http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html Java Serialization]&lt;br /&gt;
&lt;br /&gt;
13. [http://www.w3schools.com/json/ JSON Tutorial]&lt;br /&gt;
&lt;br /&gt;
14. [http://www.skorks.com/2010/04/serializing-and-deserializing-objects-with-ruby/ Serializing and De-serializing in Ruby]&lt;br /&gt;
&lt;br /&gt;
15. [http://www.codeproject.com/Articles/33296/Serialization-and-De-serialization Serialization and De-serialization]&lt;br /&gt;
&lt;br /&gt;
16. [http://msdn.microsoft.com/en-us/library/182eeyhh.aspx XML Serialization]&lt;br /&gt;
&lt;br /&gt;
17. [http://www.tutorialspoint.com/java/java_serialization.htm Java Serialization Tutorial]&lt;br /&gt;
&lt;br /&gt;
18. [http://www.sapphiresteel.com/ruby-programming/The-Book-Of-Ruby The Book of Ruby]&lt;br /&gt;
&lt;br /&gt;
19. [http://www.codeproject.com/Articles/20962/Introducing-Serialization-in-NET Serialization in .Net]&lt;br /&gt;
&lt;br /&gt;
20. [http://ruby.about.com/od/advancedruby/ss/Serialization-In-Ruby-Yaml.html Serialization in Ruby YAML]&lt;br /&gt;
&lt;br /&gt;
21. [http://ruby.about.com/od/tasks/a/The-Json-Gem.html Installing JSON gem]&lt;br /&gt;
&lt;br /&gt;
22. [http://www.ruby-doc.org/stdlib-2.0.0/libdoc/json/rdoc/JSON.html Serialization in Ruby JSON]&lt;/div&gt;</summary>
		<author><name>Mmehta2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1d_mm&amp;diff=83410</id>
		<title>CSC/ECE 517 Spring 2014/ch1a 1d mm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1d_mm&amp;diff=83410"/>
		<updated>2014-02-18T02:35:00Z</updated>

		<summary type="html">&lt;p&gt;Mmehta2: /* Converting Ruby Objects to YAML format */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page discusses Serialization, focusing on Ruby programming language. It describes different methods of Serialization in Ruby. In addition, it also compares serialization in different Object Oriented languages.&lt;br /&gt;
&lt;br /&gt;
== '''Introduction''' ==&lt;br /&gt;
&lt;br /&gt;
==='''Serialization'''===&lt;br /&gt;
&lt;br /&gt;
'''Serialization'''[http://en.wikipedia.org/wiki/Serialization] is a process of converting a data structure or an object into a stream of bytes or string to facilitate storage in memory, file (persistence storage) or transmission over a network. The process of Serialization is also referred to as ''Marshalling''[http://en.wikipedia.org/wiki/Marshalling_%28computer_science%29]. 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. &lt;br /&gt;
&lt;br /&gt;
==='''De-serialization'''===&lt;br /&gt;
&lt;br /&gt;
'''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''.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==='''Basic Advantages of Serialization'''===&lt;br /&gt;
&lt;br /&gt;
1. Communication between two or more processes on same machine. Object state can be saved and shared in a persistent or in-memory store.&lt;br /&gt;
&lt;br /&gt;
2. Communication between processes on different machines. Serialization facilitates the transmission of an object through a network.&lt;br /&gt;
&lt;br /&gt;
3. Creating a clone of an object. &lt;br /&gt;
&lt;br /&gt;
4. Cross-platform compatibility. Object can be serialized in a common format that is understood by multiple platforms. For example, JSON[http://www.json.org/] and XML.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==='''Practical Applications for Serialization'''===&lt;br /&gt;
&lt;br /&gt;
1. [http://docs.oracle.com/cd/E13222_01/wls/docs81/webapp/sessions.html#139726 HTTP Session] Replication by sharing session objects across web servers for load balancing and handling failover scenarios.&lt;br /&gt;
&lt;br /&gt;
2. Serialization facilitates communication in [http://en.wikipedia.org/wiki/Java_remote_method_invocation Remote Method Invocation] or [http://en.wikipedia.org/wiki/Remote_procedure_call Remote procedure calls]&lt;br /&gt;
&lt;br /&gt;
3. Rails Cookie Handling[http://techbrahmana.blogspot.com/2012/03/rails-cookie-handling-serialization-and.html]. Cookies are stored marshalled/unmarshalled to and from client machines.&lt;br /&gt;
&lt;br /&gt;
== '''Serialization in Ruby:''' ==&lt;br /&gt;
&lt;br /&gt;
Ruby provides serialization capabilities through its module, [http://www.ruby-doc.org/core-2.0.0/Marshal.html Marshal]. There are also other libraries like YAML[http://yaml.org/] and JSON which can be used in Ruby to generate serialized objects for purposes like platform independence and human readable formats.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:SerializationChart.jpg]]&lt;br /&gt;
&lt;br /&gt;
== '''Types of Serialization''' ==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
=== Converting Ruby Objects in Human Readable Formats ===&lt;br /&gt;
The conversion of Ruby objects into YAML and JSON formats are explained below.&lt;br /&gt;
&lt;br /&gt;
==== Converting Ruby Objects to YAML format ====&lt;br /&gt;
&lt;br /&gt;
YAML 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 binary format. 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. In order to use it in Ruby, the yaml.rb file is required to be loaded which provides methods for converting objects into yaml format and creating .yml files.&lt;br /&gt;
&lt;br /&gt;
====='''Examples of serialization using YAML:'''=====&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;nowiki&amp;gt; #Serialization using YAML's to_yaml method&lt;br /&gt;
 &lt;br /&gt;
  require &amp;quot;yaml&amp;quot;&lt;br /&gt;
     class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
	@name = name&lt;br /&gt;
	@age = age&lt;br /&gt;
	@country=country&lt;br /&gt;
     end&lt;br /&gt;
      def to_s&lt;br /&gt;
	&amp;quot;In First:\n#{@name}, #{@age}, #{@country}\n&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
  x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
  puts x&lt;br /&gt;
  puts x.to_yaml&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   In First:&lt;br /&gt;
   Tom, 25, USA&lt;br /&gt;
   --- !ruby/object:First&lt;br /&gt;
   name: Tom&lt;br /&gt;
   age: 25&lt;br /&gt;
   country: USA&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The above code displays the object x, first as a string and then in the yaml format. &lt;br /&gt;
&lt;br /&gt;
====='''Saving YAML data into a file:'''=====&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt; # Serialization using YAML::dump&lt;br /&gt;
&lt;br /&gt;
require 'yaml'&lt;br /&gt;
&lt;br /&gt;
f = File.open( 'first.yml', 'w' )&lt;br /&gt;
YAML.dump( [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;], f )&lt;br /&gt;
f.close&lt;br /&gt;
		&lt;br /&gt;
File.open( 'first.yml' ){ |f|	&lt;br /&gt;
    $arr= YAML.load(f)&lt;br /&gt;
}	&lt;br /&gt;
&lt;br /&gt;
p( $arr )&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;]&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The dump function can be used to serialize the data and save it into a file in YAML format. As shown in the above example the data in YAML format can be de-serialized using the load function.&lt;br /&gt;
&lt;br /&gt;
YAML libraries also provides an option of selecting only those variables of the object that are needed to be serialized. This is done using the to_yaml_properties method as shown in the below example.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=====Custom Serialization using YAML=====&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;nowiki&amp;gt;&lt;br /&gt;
    require &amp;quot;yaml&amp;quot;&lt;br /&gt;
     class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
	@name = name&lt;br /&gt;
	@age = age&lt;br /&gt;
	@country=country&lt;br /&gt;
     end&lt;br /&gt;
     def to_s&lt;br /&gt;
	&amp;quot;In First:\n#{@name}, #{@age}, #{@country}\n&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
      def to_yaml_properties&lt;br /&gt;
	   [&amp;quot;@name&amp;quot;,&amp;quot;@age&amp;quot;]  #@country will not be serialized&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
  x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
  puts x&lt;br /&gt;
  puts x.to_yaml&lt;br /&gt;
  &amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   In First:&lt;br /&gt;
   Tom, 25, USA&lt;br /&gt;
   --- !ruby/object:First&lt;br /&gt;
   name: Tom&lt;br /&gt;
   age: 25&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Converting Ruby Objects to JSON format: ====&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
JSON 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. Ruby 1.8.7 distribution is not bundled with json gem. However, in Ruby 1.9.2, the json gem is bundled with the core Ruby distribution.&lt;br /&gt;
The JSON library can be installed using Ruby Gems[http://rubygems.org/] as shown below:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;# gem install json&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A JSON string for serialization can be created by using the JSON.generate method:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
        require 'json'&lt;br /&gt;
        my_hash = {:Welcome =&amp;gt; &amp;quot;Ruby&amp;quot;}&lt;br /&gt;
        puts JSON.generate(my_hash) =&amp;gt; &amp;quot;{\&amp;quot;WELCOME\&amp;quot;:\&amp;quot;RUBY\&amp;quot;}&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
'''Output:'''&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;{&amp;quot;{\&amp;quot;Welcome\&amp;quot;:\&amp;quot;Ruby\&amp;quot;}&amp;quot;=&amp;gt;&amp;quot;{\&amp;quot;WELCOME\&amp;quot;:\&amp;quot;RUBY\&amp;quot;}&amp;quot;}&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A JSON string received from another program can be parsed by using JSON.parse&lt;br /&gt;
Ruby thus converts String to Hash.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
        require 'json'&lt;br /&gt;
        my_hash = JSON.parse('{&amp;quot;Welcome&amp;quot;: &amp;quot;Ruby&amp;quot;}')&lt;br /&gt;
        puts my_hash[&amp;quot;Welcome&amp;quot;] =&amp;gt; &amp;quot;Ruby&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Converting Ruby Objects to Binary Formats ===&lt;br /&gt;
Binary Serialization is another form of serialization in Ruby which is not in human readable form. 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. Binary Serialization  is done using Marshal which is built into Ruby and the code for it is written in Ruby's Marshal module(marshal.c) and thus no additional files are required in order to use it. The _dump and _load methods defined in marshal are used for serialization. Using marshal module the following types of objects can not be serialized: bindings, procedure objects, singleton objects, instances of IO objects and interfaces.&lt;br /&gt;
&lt;br /&gt;
Since the Binary Serialized data is not in human readable form, the following guidelines need to be followed.&lt;br /&gt;
     1.Use print instead of puts[http://ruby-doc.org/core-2.0.0/ARGF.html#] when serialized objects are written to a file in order to avoid new line characters to be written &lt;br /&gt;
       in the file.&lt;br /&gt;
     &lt;br /&gt;
     2.Use a record separator in order to differentiate between two objects.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Binary Serialization Example:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    class Animal&lt;br /&gt;
      def initialize  name, age&lt;br /&gt;
      @name = name&lt;br /&gt;
      @age=age&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
   class Cat &amp;lt; Animal&lt;br /&gt;
    def to_s&lt;br /&gt;
     &amp;quot;In Cat C: #{@name} \t #{@age}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
  c = Cat.new(&amp;quot;Kitty Kat&amp;quot;,5)&lt;br /&gt;
  puts &amp;quot;Before Serialization&amp;quot;&lt;br /&gt;
  puts c&lt;br /&gt;
  #puts d&lt;br /&gt;
  serialize_cat= Marshal.dump(c) #dumps the serialized cat object into serialize_cat&lt;br /&gt;
  puts &amp;quot;\nAfter Serialization:\n #{serialize_cat}&amp;quot;&lt;br /&gt;
  deserialize_cat= Marshal::load(serialize_cat) #deserializes the cat object and loads it back into deserialize_cat&lt;br /&gt;
  puts &amp;quot;\nAfter Deserialization\n #{deserialize_cat}&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   Before Serialization&lt;br /&gt;
   In Cat C: Kitty Kat 	 5&lt;br /&gt;
   After Serialization:&lt;br /&gt;
   oCat:&lt;br /&gt;
   @nameI&amp;quot;Kitty Kat:ET:	@agei&lt;br /&gt;
   After Deserialization&lt;br /&gt;
   In Cat C: Kitty Kat 	 5&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Similar to YAML, Marshal can also be used to dump data into a file. The above example showing the serialization using YAML::dump can be written using marshal as shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    f = File.open( 'first.yml', 'w' )&lt;br /&gt;
    Marshal.dump( [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;], f )&lt;br /&gt;
    f.close	&lt;br /&gt;
    File.open( 'first.yml' ){ |f|	&lt;br /&gt;
    $arr= Marshal.load(f)&lt;br /&gt;
    }	&lt;br /&gt;
    p( $arr )&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;]&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice that in the above example there is no &amp;quot;require&amp;quot; statement as opposed to the earlier example of writing serialized data into files using YAML. This is because unlike YAML, marshal is built-in Ruby and  no external libraries is required in order to use its functionality.&lt;br /&gt;
&lt;br /&gt;
Marshal can also be used to custom serialize an object i.e. it provides an option to omit the variables of an object that are not required in the serialized data. The following program shows the use of marshal_dump method for achieving custom serialization.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
      class First&lt;br /&gt;
      def initialize(name, age, country)&lt;br /&gt;
         @name = name&lt;br /&gt;
         @age = age&lt;br /&gt;
         @country=country&lt;br /&gt;
      end&lt;br /&gt;
      def to_s&lt;br /&gt;
            &amp;quot;In First: #{@name}, #{@age}, #{@country}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
      def marshal_dump&lt;br /&gt;
            [@name,@age]  #@country will not be serialized&lt;br /&gt;
      end&lt;br /&gt;
      def marshal_load(data)&lt;br /&gt;
       @name=data[0]&lt;br /&gt;
       @age=data[1]&lt;br /&gt;
       @country=&amp;quot;United States of America&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
      end&lt;br /&gt;
 &lt;br /&gt;
      x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
      puts x&lt;br /&gt;
  &lt;br /&gt;
      marshal_data = Marshal.dump( x ) &lt;br /&gt;
      y = Marshal.load( marshal_data ) &lt;br /&gt;
      p( y.to_s )&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    In First: Tom, 25, USA&lt;br /&gt;
    &amp;quot;In First: Tom, 25, United States of America&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== '''Serialization Performance in Ruby''' ==&lt;br /&gt;
The different serialization formats described above differ in the efficiency at which they can serialize and deserialize data and thus while serializing large amount of data their efficiency is taken into account. The .report[http://www.ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html] method in Ruby can be used to evaluate the performance of these serialization patterns. Marshal, as it handles data in binary format, is the most efficient form of serialization in Ruby. The following example compares the performance of marshal, JSON and YAML format.[http://www.pauldix.net/2008/08/serializing-dat.html]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   require 'benchmark'&lt;br /&gt;
   require 'rubygems'&lt;br /&gt;
   require 'json'&lt;br /&gt;
   require 'yaml'&lt;br /&gt;
   include Benchmark&lt;br /&gt;
   class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
      @name=name&lt;br /&gt;
      @age=age&lt;br /&gt;
      @country=country&lt;br /&gt;
     end&lt;br /&gt;
   end  &lt;br /&gt;
    x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
    puts &amp;quot;            User     System     Total      Real&amp;quot;&lt;br /&gt;
   benchmark do |t|&lt;br /&gt;
    print &amp;quot;Marshal:&amp;quot;&lt;br /&gt;
    t.report{1000.times do; Marshal.load(Marshal.dump(x));end}&lt;br /&gt;
    print &amp;quot;JSON:   &amp;quot;&lt;br /&gt;
    t.report{1000.times do; JSON.load(JSON.dump(x));end}&lt;br /&gt;
    print &amp;quot;YAML:   &amp;quot;&lt;br /&gt;
    t.report{1000.times do; YAML.load(YAML.dump(x));end}&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
              User     System     Total      Real&lt;br /&gt;
  Marshal:   0.010000   0.000000   0.010000 (  0.015408)&lt;br /&gt;
  JSON:      0.020000   0.010000   0.030000 (  0.022462)&lt;br /&gt;
  YAML:      0.460000   0.010000   0.470000 (  0.472149)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output of the above example shows that, given large chunks of data, marshal is comparatively more efficient format for serialization in Ruby.&lt;br /&gt;
&lt;br /&gt;
== Serialization in Object-Oriented Languages: Comparison ==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Sl.No !! Ruby !! Java !! .Net Framework !! C++&lt;br /&gt;
|-&lt;br /&gt;
| 1 || Ruby provides a built in module called 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 || Although, there is no built in support for serialization in C++, it can be achieved by using Boost libraries[http://www.boost.org/doc/libs/1_36_0/libs/serialization/example/demo.cpp]&lt;br /&gt;
|-&lt;br /&gt;
| 2 || The built in module of Ruby (Marshal) does not support platform independence, however, it can be achieved by using external libraries like YAML and JSON || Similarly, Java's built in serialization is also not platform independent and in order to use serialization in Java across Ruby platform, jruby library should be used.  || .Net used Remoting technology to make it platform independent. || Serialization using the Boost libraries is not platform independent.&lt;br /&gt;
|-&lt;br /&gt;
| 3 || YAML provides a method [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/syck/rdoc/Object.html (to_yaml_properties)] which can be used to select the variables who's value is need to be serialized. With Marshal, we need to write a method named marshal_dump defining the variables of an object that has to be serialized. || Provides an option for serializing only the required 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 data 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 || Serialization using the Boost libraries is custom and thus the user can specify the part of the objects to be serialized.&lt;br /&gt;
|-&lt;br /&gt;
| 4 || Bindings, procedure objects, singleton objects, instances of IO objects and interfaces can not be serialized. Serializing these objects throws TypeError exceptions. || Thread, OutputStream and its subclasses, and Socket are not serializable in Java.[http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html] || Objects like DataRow are non serializable in .NET. Whether an object is serializable or not can be checked using the [http://msdn.microsoft.com/en-us/library/system.type.isserializable(v=vs.110).aspx?cs-save-lang=1&amp;amp;cs-lang=cpp#code-snippet-1 Type.IsSerializable] || There are no such objects in C++.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== See Also==&lt;br /&gt;
&lt;br /&gt;
1. [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/json/rdoc/JSON.html Serialization in Ruby JSON]&lt;br /&gt;
&lt;br /&gt;
2. [http://api.rubyonrails.org/classes/ActiveModel/Serialization.html Serialization in Rails]&lt;br /&gt;
&lt;br /&gt;
3. [http://gregmoreno.wordpress.com/2011/01/27/preventing-model-explosion-via-rails-serialization/ Article on Rails Serialization]&lt;br /&gt;
&lt;br /&gt;
4. [http://en.wikipedia.org/wiki/Google_Protocol_Buffers Protocol Buffers]&lt;br /&gt;
&lt;br /&gt;
5. [https://rubygems.org/gems/ruby-protocol-buffers Ruby Protocol Buffers Gem]&lt;br /&gt;
&lt;br /&gt;
6. [http://en.wikipedia.org/wiki/Apache_Avro Avro]&lt;br /&gt;
&lt;br /&gt;
7. [http://rubygems.org/gems/avro Ruby Avro Gem]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
1. [http://en.wikipedia.org/wiki/Serialization Serilization in General]&lt;br /&gt;
&lt;br /&gt;
2. [http://en.wikipedia.org/wiki/Marshalling_%28computer_science%29 Marshaling]&lt;br /&gt;
&lt;br /&gt;
3. [http://techbrahmana.blogspot.com/2012/03/rails-cookie-handling-serialization-and.html Rails Cookie Handling]&lt;br /&gt;
&lt;br /&gt;
4. [http://yaml.org/ YAML]&lt;br /&gt;
&lt;br /&gt;
5. [http://www.ruby-doc.org/core-2.1.0/Marshal.html Marshal module]&lt;br /&gt;
&lt;br /&gt;
6. [http://json.org/ JSON]&lt;br /&gt;
&lt;br /&gt;
7. [http://rubygems.org/ Ruby Gems]&lt;br /&gt;
&lt;br /&gt;
8. [http://ruby-doc.org/core-2.0.0/ARGF.html# Ruby ARGF Documentation]&lt;br /&gt;
&lt;br /&gt;
9. [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html Ruby Documentation for Benchmark Gem]&lt;br /&gt;
&lt;br /&gt;
10. [http://www.pauldix.net/2008/08/serializing-dat.html Serialization Performance Comparison]&lt;br /&gt;
&lt;br /&gt;
11. [http://www.boost.org/doc/libs/1_36_0/libs/serialization/example/demo.cpp Boost Libraries]&lt;br /&gt;
&lt;br /&gt;
12. [http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html Java Serialization]&lt;br /&gt;
&lt;br /&gt;
13. [http://www.w3schools.com/json/ JSON Tutorial]&lt;br /&gt;
&lt;br /&gt;
14. [http://www.skorks.com/2010/04/serializing-and-deserializing-objects-with-ruby/ Serializing and De-serializing in Ruby]&lt;br /&gt;
&lt;br /&gt;
15. [http://www.codeproject.com/Articles/33296/Serialization-and-De-serialization Serialization and De-serialization]&lt;br /&gt;
&lt;br /&gt;
16. [http://msdn.microsoft.com/en-us/library/182eeyhh.aspx XML Serialization]&lt;br /&gt;
&lt;br /&gt;
17. [http://www.tutorialspoint.com/java/java_serialization.htm Java Serialization Tutorial]&lt;br /&gt;
&lt;br /&gt;
18. [http://www.sapphiresteel.com/ruby-programming/The-Book-Of-Ruby The Book of Ruby]&lt;br /&gt;
&lt;br /&gt;
19. [http://www.codeproject.com/Articles/20962/Introducing-Serialization-in-NET Serialization in .Net]&lt;br /&gt;
&lt;br /&gt;
20. [http://ruby.about.com/od/advancedruby/ss/Serialization-In-Ruby-Yaml.html Serialization in Ruby YAML]&lt;br /&gt;
&lt;br /&gt;
21. [http://ruby.about.com/od/tasks/a/The-Json-Gem.html Installing JSON gem]&lt;br /&gt;
&lt;br /&gt;
22. [http://www.ruby-doc.org/stdlib-2.0.0/libdoc/json/rdoc/JSON.html Serialization in Ruby JSON]&lt;/div&gt;</summary>
		<author><name>Mmehta2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1d_mm&amp;diff=83409</id>
		<title>CSC/ECE 517 Spring 2014/ch1a 1d mm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1d_mm&amp;diff=83409"/>
		<updated>2014-02-18T02:31:07Z</updated>

		<summary type="html">&lt;p&gt;Mmehta2: /* Serialization in Ruby: */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page discusses Serialization, focusing on Ruby programming language. It describes different methods of Serialization in Ruby. In addition, it also compares serialization in different Object Oriented languages.&lt;br /&gt;
&lt;br /&gt;
== '''Introduction''' ==&lt;br /&gt;
&lt;br /&gt;
==='''Serialization'''===&lt;br /&gt;
&lt;br /&gt;
'''Serialization'''[http://en.wikipedia.org/wiki/Serialization] is a process of converting a data structure or an object into a stream of bytes or string to facilitate storage in memory, file (persistence storage) or transmission over a network. The process of Serialization is also referred to as ''Marshalling''[http://en.wikipedia.org/wiki/Marshalling_%28computer_science%29]. 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. &lt;br /&gt;
&lt;br /&gt;
==='''De-serialization'''===&lt;br /&gt;
&lt;br /&gt;
'''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''.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==='''Basic Advantages of Serialization'''===&lt;br /&gt;
&lt;br /&gt;
1. Communication between two or more processes on same machine. Object state can be saved and shared in a persistent or in-memory store.&lt;br /&gt;
&lt;br /&gt;
2. Communication between processes on different machines. Serialization facilitates the transmission of an object through a network.&lt;br /&gt;
&lt;br /&gt;
3. Creating a clone of an object. &lt;br /&gt;
&lt;br /&gt;
4. Cross-platform compatibility. Object can be serialized in a common format that is understood by multiple platforms. For example, JSON[http://www.json.org/] and XML.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==='''Practical Applications for Serialization'''===&lt;br /&gt;
&lt;br /&gt;
1. [http://docs.oracle.com/cd/E13222_01/wls/docs81/webapp/sessions.html#139726 HTTP Session] Replication by sharing session objects across web servers for load balancing and handling failover scenarios.&lt;br /&gt;
&lt;br /&gt;
2. Serialization facilitates communication in [http://en.wikipedia.org/wiki/Java_remote_method_invocation Remote Method Invocation] or [http://en.wikipedia.org/wiki/Remote_procedure_call Remote procedure calls]&lt;br /&gt;
&lt;br /&gt;
3. Rails Cookie Handling[http://techbrahmana.blogspot.com/2012/03/rails-cookie-handling-serialization-and.html]. Cookies are stored marshalled/unmarshalled to and from client machines.&lt;br /&gt;
&lt;br /&gt;
== '''Serialization in Ruby:''' ==&lt;br /&gt;
&lt;br /&gt;
Ruby provides serialization capabilities through its module, [http://www.ruby-doc.org/core-2.0.0/Marshal.html Marshal]. There are also other libraries like YAML[http://yaml.org/] and JSON which can be used in Ruby to generate serialized objects for purposes like platform independence and human readable formats.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:SerializationChart.jpg]]&lt;br /&gt;
&lt;br /&gt;
== '''Types of Serialization''' ==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
=== Converting Ruby Objects in Human Readable Formats ===&lt;br /&gt;
The conversion of Ruby objects into YAML and JSON formats are explained below.&lt;br /&gt;
&lt;br /&gt;
==== Converting Ruby Objects to YAML format ====&lt;br /&gt;
&lt;br /&gt;
YAML 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 binary format. 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. In order to use it in Ruby, the yaml.rb file is required to be loaded which provides methods for converting objects into yaml format and creating .yml files.&lt;br /&gt;
&lt;br /&gt;
'''Examples of serialization using YAML:'''&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;nowiki&amp;gt; #Serialization using YAML's to_yaml method&lt;br /&gt;
 &lt;br /&gt;
  require &amp;quot;yaml&amp;quot;&lt;br /&gt;
     class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
	@name = name&lt;br /&gt;
	@age = age&lt;br /&gt;
	@country=country&lt;br /&gt;
     end&lt;br /&gt;
      def to_s&lt;br /&gt;
	&amp;quot;In First:\n#{@name}, #{@age}, #{@country}\n&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
  x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
  puts x&lt;br /&gt;
  puts x.to_yaml&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   In First:&lt;br /&gt;
   Tom, 25, USA&lt;br /&gt;
   --- !ruby/object:First&lt;br /&gt;
   name: Tom&lt;br /&gt;
   age: 25&lt;br /&gt;
   country: USA&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The above code displays the object x, first as a string and then in the yaml format. &lt;br /&gt;
&lt;br /&gt;
'''Saving YAML data into a file:'''&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt; # Serialization using YAML::dump&lt;br /&gt;
&lt;br /&gt;
require 'yaml'&lt;br /&gt;
&lt;br /&gt;
f = File.open( 'first.yml', 'w' )&lt;br /&gt;
YAML.dump( [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;], f )&lt;br /&gt;
f.close&lt;br /&gt;
		&lt;br /&gt;
File.open( 'first.yml' ){ |f|	&lt;br /&gt;
    $arr= YAML.load(f)&lt;br /&gt;
}	&lt;br /&gt;
&lt;br /&gt;
p( $arr )&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;]&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The dump function can be used to serialize the data and save it into a file in YAML format. As shown in the above example the data in YAML format can be de-serialized using the load function.&lt;br /&gt;
&lt;br /&gt;
YAML libraries also provides an option of selecting only those variables of the object that are needed to be serialized. This is done using the to_yaml_properties method as shown in the below example.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;nowiki&amp;gt;  #Custom Serialization using YAML&lt;br /&gt;
 &lt;br /&gt;
  require &amp;quot;yaml&amp;quot;&lt;br /&gt;
     class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
	@name = name&lt;br /&gt;
	@age = age&lt;br /&gt;
	@country=country&lt;br /&gt;
     end&lt;br /&gt;
     def to_s&lt;br /&gt;
	&amp;quot;In First:\n#{@name}, #{@age}, #{@country}\n&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
      def to_yaml_properties&lt;br /&gt;
	   [&amp;quot;@name&amp;quot;,&amp;quot;@age&amp;quot;]  #@country will not be serialized&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
  x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
  puts x&lt;br /&gt;
  puts x.to_yaml&lt;br /&gt;
  &amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   In First:&lt;br /&gt;
   Tom, 25, USA&lt;br /&gt;
   --- !ruby/object:First&lt;br /&gt;
   name: Tom&lt;br /&gt;
   age: 25&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Converting Ruby Objects to JSON format: ====&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
JSON 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. Ruby 1.8.7 distribution is not bundled with json gem. However, in Ruby 1.9.2, the json gem is bundled with the core Ruby distribution.&lt;br /&gt;
The JSON library can be installed using Ruby Gems[http://rubygems.org/] as shown below:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;# gem install json&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A JSON string for serialization can be created by using the JSON.generate method:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
        require 'json'&lt;br /&gt;
        my_hash = {:Welcome =&amp;gt; &amp;quot;Ruby&amp;quot;}&lt;br /&gt;
        puts JSON.generate(my_hash) =&amp;gt; &amp;quot;{\&amp;quot;WELCOME\&amp;quot;:\&amp;quot;RUBY\&amp;quot;}&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
'''Output:'''&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;{&amp;quot;{\&amp;quot;Welcome\&amp;quot;:\&amp;quot;Ruby\&amp;quot;}&amp;quot;=&amp;gt;&amp;quot;{\&amp;quot;WELCOME\&amp;quot;:\&amp;quot;RUBY\&amp;quot;}&amp;quot;}&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A JSON string received from another program can be parsed by using JSON.parse&lt;br /&gt;
Ruby thus converts String to Hash.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
        require 'json'&lt;br /&gt;
        my_hash = JSON.parse('{&amp;quot;Welcome&amp;quot;: &amp;quot;Ruby&amp;quot;}')&lt;br /&gt;
        puts my_hash[&amp;quot;Welcome&amp;quot;] =&amp;gt; &amp;quot;Ruby&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Converting Ruby Objects to Binary Formats ===&lt;br /&gt;
Binary Serialization is another form of serialization in Ruby which is not in human readable form. 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. Binary Serialization  is done using Marshal which is built into Ruby and the code for it is written in Ruby's Marshal module(marshal.c) and thus no additional files are required in order to use it. The _dump and _load methods defined in marshal are used for serialization. Using marshal module the following types of objects can not be serialized: bindings, procedure objects, singleton objects, instances of IO objects and interfaces.&lt;br /&gt;
&lt;br /&gt;
Since the Binary Serialized data is not in human readable form, the following guidelines need to be followed.&lt;br /&gt;
     1.Use print instead of puts[http://ruby-doc.org/core-2.0.0/ARGF.html#] when serialized objects are written to a file in order to avoid new line characters to be written &lt;br /&gt;
       in the file.&lt;br /&gt;
     &lt;br /&gt;
     2.Use a record separator in order to differentiate between two objects.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Binary Serialization Example:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    class Animal&lt;br /&gt;
      def initialize  name, age&lt;br /&gt;
      @name = name&lt;br /&gt;
      @age=age&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
   class Cat &amp;lt; Animal&lt;br /&gt;
    def to_s&lt;br /&gt;
     &amp;quot;In Cat C: #{@name} \t #{@age}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
  c = Cat.new(&amp;quot;Kitty Kat&amp;quot;,5)&lt;br /&gt;
  puts &amp;quot;Before Serialization&amp;quot;&lt;br /&gt;
  puts c&lt;br /&gt;
  #puts d&lt;br /&gt;
  serialize_cat= Marshal.dump(c) #dumps the serialized cat object into serialize_cat&lt;br /&gt;
  puts &amp;quot;\nAfter Serialization:\n #{serialize_cat}&amp;quot;&lt;br /&gt;
  deserialize_cat= Marshal::load(serialize_cat) #deserializes the cat object and loads it back into deserialize_cat&lt;br /&gt;
  puts &amp;quot;\nAfter Deserialization\n #{deserialize_cat}&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   Before Serialization&lt;br /&gt;
   In Cat C: Kitty Kat 	 5&lt;br /&gt;
   After Serialization:&lt;br /&gt;
   oCat:&lt;br /&gt;
   @nameI&amp;quot;Kitty Kat:ET:	@agei&lt;br /&gt;
   After Deserialization&lt;br /&gt;
   In Cat C: Kitty Kat 	 5&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Similar to YAML, Marshal can also be used to dump data into a file. The above example showing the serialization using YAML::dump can be written using marshal as shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    f = File.open( 'first.yml', 'w' )&lt;br /&gt;
    Marshal.dump( [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;], f )&lt;br /&gt;
    f.close	&lt;br /&gt;
    File.open( 'first.yml' ){ |f|	&lt;br /&gt;
    $arr= Marshal.load(f)&lt;br /&gt;
    }	&lt;br /&gt;
    p( $arr )&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;]&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice that in the above example there is no &amp;quot;require&amp;quot; statement as opposed to the earlier example of writing serialized data into files using YAML. This is because unlike YAML, marshal is built-in Ruby and  no external libraries is required in order to use its functionality.&lt;br /&gt;
&lt;br /&gt;
Marshal can also be used to custom serialize an object i.e. it provides an option to omit the variables of an object that are not required in the serialized data. The following program shows the use of marshal_dump method for achieving custom serialization.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
      class First&lt;br /&gt;
      def initialize(name, age, country)&lt;br /&gt;
         @name = name&lt;br /&gt;
         @age = age&lt;br /&gt;
         @country=country&lt;br /&gt;
      end&lt;br /&gt;
      def to_s&lt;br /&gt;
            &amp;quot;In First: #{@name}, #{@age}, #{@country}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
      def marshal_dump&lt;br /&gt;
            [@name,@age]  #@country will not be serialized&lt;br /&gt;
      end&lt;br /&gt;
      def marshal_load(data)&lt;br /&gt;
       @name=data[0]&lt;br /&gt;
       @age=data[1]&lt;br /&gt;
       @country=&amp;quot;United States of America&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
      end&lt;br /&gt;
 &lt;br /&gt;
      x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
      puts x&lt;br /&gt;
  &lt;br /&gt;
      marshal_data = Marshal.dump( x ) &lt;br /&gt;
      y = Marshal.load( marshal_data ) &lt;br /&gt;
      p( y.to_s )&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    In First: Tom, 25, USA&lt;br /&gt;
    &amp;quot;In First: Tom, 25, United States of America&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== '''Serialization Performance in Ruby''' ==&lt;br /&gt;
The different serialization formats described above differ in the efficiency at which they can serialize and deserialize data and thus while serializing large amount of data their efficiency is taken into account. The .report[http://www.ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html] method in Ruby can be used to evaluate the performance of these serialization patterns. Marshal, as it handles data in binary format, is the most efficient form of serialization in Ruby. The following example compares the performance of marshal, JSON and YAML format.[http://www.pauldix.net/2008/08/serializing-dat.html]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   require 'benchmark'&lt;br /&gt;
   require 'rubygems'&lt;br /&gt;
   require 'json'&lt;br /&gt;
   require 'yaml'&lt;br /&gt;
   include Benchmark&lt;br /&gt;
   class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
      @name=name&lt;br /&gt;
      @age=age&lt;br /&gt;
      @country=country&lt;br /&gt;
     end&lt;br /&gt;
   end  &lt;br /&gt;
    x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
    puts &amp;quot;            User     System     Total      Real&amp;quot;&lt;br /&gt;
   benchmark do |t|&lt;br /&gt;
    print &amp;quot;Marshal:&amp;quot;&lt;br /&gt;
    t.report{1000.times do; Marshal.load(Marshal.dump(x));end}&lt;br /&gt;
    print &amp;quot;JSON:   &amp;quot;&lt;br /&gt;
    t.report{1000.times do; JSON.load(JSON.dump(x));end}&lt;br /&gt;
    print &amp;quot;YAML:   &amp;quot;&lt;br /&gt;
    t.report{1000.times do; YAML.load(YAML.dump(x));end}&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
              User     System     Total      Real&lt;br /&gt;
  Marshal:   0.010000   0.000000   0.010000 (  0.015408)&lt;br /&gt;
  JSON:      0.020000   0.010000   0.030000 (  0.022462)&lt;br /&gt;
  YAML:      0.460000   0.010000   0.470000 (  0.472149)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output of the above example shows that, given large chunks of data, marshal is comparatively more efficient format for serialization in Ruby.&lt;br /&gt;
&lt;br /&gt;
== Serialization in Object-Oriented Languages: Comparison ==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Sl.No !! Ruby !! Java !! .Net Framework !! C++&lt;br /&gt;
|-&lt;br /&gt;
| 1 || Ruby provides a built in module called 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 || Although, there is no built in support for serialization in C++, it can be achieved by using Boost libraries[http://www.boost.org/doc/libs/1_36_0/libs/serialization/example/demo.cpp]&lt;br /&gt;
|-&lt;br /&gt;
| 2 || The built in module of Ruby (Marshal) does not support platform independence, however, it can be achieved by using external libraries like YAML and JSON || Similarly, Java's built in serialization is also not platform independent and in order to use serialization in Java across Ruby platform, jruby library should be used.  || .Net used Remoting technology to make it platform independent. || Serialization using the Boost libraries is not platform independent.&lt;br /&gt;
|-&lt;br /&gt;
| 3 || YAML provides a method [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/syck/rdoc/Object.html (to_yaml_properties)] which can be used to select the variables who's value is need to be serialized. With Marshal, we need to write a method named marshal_dump defining the variables of an object that has to be serialized. || Provides an option for serializing only the required 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 data 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 || Serialization using the Boost libraries is custom and thus the user can specify the part of the objects to be serialized.&lt;br /&gt;
|-&lt;br /&gt;
| 4 || Bindings, procedure objects, singleton objects, instances of IO objects and interfaces can not be serialized. Serializing these objects throws TypeError exceptions. || Thread, OutputStream and its subclasses, and Socket are not serializable in Java.[http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html] || Objects like DataRow are non serializable in .NET. Whether an object is serializable or not can be checked using the [http://msdn.microsoft.com/en-us/library/system.type.isserializable(v=vs.110).aspx?cs-save-lang=1&amp;amp;cs-lang=cpp#code-snippet-1 Type.IsSerializable] || There are no such objects in C++.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== See Also==&lt;br /&gt;
&lt;br /&gt;
1. [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/json/rdoc/JSON.html Serialization in Ruby JSON]&lt;br /&gt;
&lt;br /&gt;
2. [http://api.rubyonrails.org/classes/ActiveModel/Serialization.html Serialization in Rails]&lt;br /&gt;
&lt;br /&gt;
3. [http://gregmoreno.wordpress.com/2011/01/27/preventing-model-explosion-via-rails-serialization/ Article on Rails Serialization]&lt;br /&gt;
&lt;br /&gt;
4. [http://en.wikipedia.org/wiki/Google_Protocol_Buffers Protocol Buffers]&lt;br /&gt;
&lt;br /&gt;
5. [https://rubygems.org/gems/ruby-protocol-buffers Ruby Protocol Buffers Gem]&lt;br /&gt;
&lt;br /&gt;
6. [http://en.wikipedia.org/wiki/Apache_Avro Avro]&lt;br /&gt;
&lt;br /&gt;
7. [http://rubygems.org/gems/avro Ruby Avro Gem]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
1. [http://en.wikipedia.org/wiki/Serialization Serilization in General]&lt;br /&gt;
&lt;br /&gt;
2. [http://en.wikipedia.org/wiki/Marshalling_%28computer_science%29 Marshaling]&lt;br /&gt;
&lt;br /&gt;
3. [http://techbrahmana.blogspot.com/2012/03/rails-cookie-handling-serialization-and.html Rails Cookie Handling]&lt;br /&gt;
&lt;br /&gt;
4. [http://yaml.org/ YAML]&lt;br /&gt;
&lt;br /&gt;
5. [http://www.ruby-doc.org/core-2.1.0/Marshal.html Marshal module]&lt;br /&gt;
&lt;br /&gt;
6. [http://json.org/ JSON]&lt;br /&gt;
&lt;br /&gt;
7. [http://rubygems.org/ Ruby Gems]&lt;br /&gt;
&lt;br /&gt;
8. [http://ruby-doc.org/core-2.0.0/ARGF.html# Ruby ARGF Documentation]&lt;br /&gt;
&lt;br /&gt;
9. [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html Ruby Documentation for Benchmark Gem]&lt;br /&gt;
&lt;br /&gt;
10. [http://www.pauldix.net/2008/08/serializing-dat.html Serialization Performance Comparison]&lt;br /&gt;
&lt;br /&gt;
11. [http://www.boost.org/doc/libs/1_36_0/libs/serialization/example/demo.cpp Boost Libraries]&lt;br /&gt;
&lt;br /&gt;
12. [http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html Java Serialization]&lt;br /&gt;
&lt;br /&gt;
13. [http://www.w3schools.com/json/ JSON Tutorial]&lt;br /&gt;
&lt;br /&gt;
14. [http://www.skorks.com/2010/04/serializing-and-deserializing-objects-with-ruby/ Serializing and De-serializing in Ruby]&lt;br /&gt;
&lt;br /&gt;
15. [http://www.codeproject.com/Articles/33296/Serialization-and-De-serialization Serialization and De-serialization]&lt;br /&gt;
&lt;br /&gt;
16. [http://msdn.microsoft.com/en-us/library/182eeyhh.aspx XML Serialization]&lt;br /&gt;
&lt;br /&gt;
17. [http://www.tutorialspoint.com/java/java_serialization.htm Java Serialization Tutorial]&lt;br /&gt;
&lt;br /&gt;
18. [http://www.sapphiresteel.com/ruby-programming/The-Book-Of-Ruby The Book of Ruby]&lt;br /&gt;
&lt;br /&gt;
19. [http://www.codeproject.com/Articles/20962/Introducing-Serialization-in-NET Serialization in .Net]&lt;br /&gt;
&lt;br /&gt;
20. [http://ruby.about.com/od/advancedruby/ss/Serialization-In-Ruby-Yaml.html Serialization in Ruby YAML]&lt;br /&gt;
&lt;br /&gt;
21. [http://ruby.about.com/od/tasks/a/The-Json-Gem.html Installing JSON gem]&lt;br /&gt;
&lt;br /&gt;
22. [http://www.ruby-doc.org/stdlib-2.0.0/libdoc/json/rdoc/JSON.html Serialization in Ruby JSON]&lt;/div&gt;</summary>
		<author><name>Mmehta2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1d_mm&amp;diff=83408</id>
		<title>CSC/ECE 517 Spring 2014/ch1a 1d mm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1d_mm&amp;diff=83408"/>
		<updated>2014-02-18T02:30:56Z</updated>

		<summary type="html">&lt;p&gt;Mmehta2: /* Practical Applications for Serialization */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page discusses Serialization, focusing on Ruby programming language. It describes different methods of Serialization in Ruby. In addition, it also compares serialization in different Object Oriented languages.&lt;br /&gt;
&lt;br /&gt;
== '''Introduction''' ==&lt;br /&gt;
&lt;br /&gt;
==='''Serialization'''===&lt;br /&gt;
&lt;br /&gt;
'''Serialization'''[http://en.wikipedia.org/wiki/Serialization] is a process of converting a data structure or an object into a stream of bytes or string to facilitate storage in memory, file (persistence storage) or transmission over a network. The process of Serialization is also referred to as ''Marshalling''[http://en.wikipedia.org/wiki/Marshalling_%28computer_science%29]. 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. &lt;br /&gt;
&lt;br /&gt;
==='''De-serialization'''===&lt;br /&gt;
&lt;br /&gt;
'''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''.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==='''Basic Advantages of Serialization'''===&lt;br /&gt;
&lt;br /&gt;
1. Communication between two or more processes on same machine. Object state can be saved and shared in a persistent or in-memory store.&lt;br /&gt;
&lt;br /&gt;
2. Communication between processes on different machines. Serialization facilitates the transmission of an object through a network.&lt;br /&gt;
&lt;br /&gt;
3. Creating a clone of an object. &lt;br /&gt;
&lt;br /&gt;
4. Cross-platform compatibility. Object can be serialized in a common format that is understood by multiple platforms. For example, JSON[http://www.json.org/] and XML.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==='''Practical Applications for Serialization'''===&lt;br /&gt;
&lt;br /&gt;
1. [http://docs.oracle.com/cd/E13222_01/wls/docs81/webapp/sessions.html#139726 HTTP Session] Replication by sharing session objects across web servers for load balancing and handling failover scenarios.&lt;br /&gt;
&lt;br /&gt;
2. Serialization facilitates communication in [http://en.wikipedia.org/wiki/Java_remote_method_invocation Remote Method Invocation] or [http://en.wikipedia.org/wiki/Remote_procedure_call Remote procedure calls]&lt;br /&gt;
&lt;br /&gt;
3. Rails Cookie Handling[http://techbrahmana.blogspot.com/2012/03/rails-cookie-handling-serialization-and.html]. Cookies are stored marshalled/unmarshalled to and from client machines.&lt;br /&gt;
&lt;br /&gt;
== '''Serialization in Ruby:''' ==&lt;br /&gt;
&lt;br /&gt;
Ruby provides serialization capabilities through its module, [http://www.ruby-doc.org/core-2.0.0/Marshal.html Marshal]. There are also other libraries like YAML[http://yaml.org/] and JSON which can be used in Ruby to generate serialized objects for purposes like platform independence and human readable formats.&lt;br /&gt;
&lt;br /&gt;
== '''Types of Serialization''' ==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
=== Converting Ruby Objects in Human Readable Formats ===&lt;br /&gt;
The conversion of Ruby objects into YAML and JSON formats are explained below.&lt;br /&gt;
&lt;br /&gt;
==== Converting Ruby Objects to YAML format ====&lt;br /&gt;
&lt;br /&gt;
YAML 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 binary format. 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. In order to use it in Ruby, the yaml.rb file is required to be loaded which provides methods for converting objects into yaml format and creating .yml files.&lt;br /&gt;
&lt;br /&gt;
'''Examples of serialization using YAML:'''&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;nowiki&amp;gt; #Serialization using YAML's to_yaml method&lt;br /&gt;
 &lt;br /&gt;
  require &amp;quot;yaml&amp;quot;&lt;br /&gt;
     class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
	@name = name&lt;br /&gt;
	@age = age&lt;br /&gt;
	@country=country&lt;br /&gt;
     end&lt;br /&gt;
      def to_s&lt;br /&gt;
	&amp;quot;In First:\n#{@name}, #{@age}, #{@country}\n&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
  x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
  puts x&lt;br /&gt;
  puts x.to_yaml&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   In First:&lt;br /&gt;
   Tom, 25, USA&lt;br /&gt;
   --- !ruby/object:First&lt;br /&gt;
   name: Tom&lt;br /&gt;
   age: 25&lt;br /&gt;
   country: USA&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The above code displays the object x, first as a string and then in the yaml format. &lt;br /&gt;
&lt;br /&gt;
'''Saving YAML data into a file:'''&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt; # Serialization using YAML::dump&lt;br /&gt;
&lt;br /&gt;
require 'yaml'&lt;br /&gt;
&lt;br /&gt;
f = File.open( 'first.yml', 'w' )&lt;br /&gt;
YAML.dump( [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;], f )&lt;br /&gt;
f.close&lt;br /&gt;
		&lt;br /&gt;
File.open( 'first.yml' ){ |f|	&lt;br /&gt;
    $arr= YAML.load(f)&lt;br /&gt;
}	&lt;br /&gt;
&lt;br /&gt;
p( $arr )&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;]&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The dump function can be used to serialize the data and save it into a file in YAML format. As shown in the above example the data in YAML format can be de-serialized using the load function.&lt;br /&gt;
&lt;br /&gt;
YAML libraries also provides an option of selecting only those variables of the object that are needed to be serialized. This is done using the to_yaml_properties method as shown in the below example.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;nowiki&amp;gt;  #Custom Serialization using YAML&lt;br /&gt;
 &lt;br /&gt;
  require &amp;quot;yaml&amp;quot;&lt;br /&gt;
     class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
	@name = name&lt;br /&gt;
	@age = age&lt;br /&gt;
	@country=country&lt;br /&gt;
     end&lt;br /&gt;
     def to_s&lt;br /&gt;
	&amp;quot;In First:\n#{@name}, #{@age}, #{@country}\n&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
      def to_yaml_properties&lt;br /&gt;
	   [&amp;quot;@name&amp;quot;,&amp;quot;@age&amp;quot;]  #@country will not be serialized&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
  x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
  puts x&lt;br /&gt;
  puts x.to_yaml&lt;br /&gt;
  &amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   In First:&lt;br /&gt;
   Tom, 25, USA&lt;br /&gt;
   --- !ruby/object:First&lt;br /&gt;
   name: Tom&lt;br /&gt;
   age: 25&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Converting Ruby Objects to JSON format: ====&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
JSON 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. Ruby 1.8.7 distribution is not bundled with json gem. However, in Ruby 1.9.2, the json gem is bundled with the core Ruby distribution.&lt;br /&gt;
The JSON library can be installed using Ruby Gems[http://rubygems.org/] as shown below:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;# gem install json&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A JSON string for serialization can be created by using the JSON.generate method:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
        require 'json'&lt;br /&gt;
        my_hash = {:Welcome =&amp;gt; &amp;quot;Ruby&amp;quot;}&lt;br /&gt;
        puts JSON.generate(my_hash) =&amp;gt; &amp;quot;{\&amp;quot;WELCOME\&amp;quot;:\&amp;quot;RUBY\&amp;quot;}&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
'''Output:'''&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;{&amp;quot;{\&amp;quot;Welcome\&amp;quot;:\&amp;quot;Ruby\&amp;quot;}&amp;quot;=&amp;gt;&amp;quot;{\&amp;quot;WELCOME\&amp;quot;:\&amp;quot;RUBY\&amp;quot;}&amp;quot;}&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A JSON string received from another program can be parsed by using JSON.parse&lt;br /&gt;
Ruby thus converts String to Hash.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
        require 'json'&lt;br /&gt;
        my_hash = JSON.parse('{&amp;quot;Welcome&amp;quot;: &amp;quot;Ruby&amp;quot;}')&lt;br /&gt;
        puts my_hash[&amp;quot;Welcome&amp;quot;] =&amp;gt; &amp;quot;Ruby&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Converting Ruby Objects to Binary Formats ===&lt;br /&gt;
Binary Serialization is another form of serialization in Ruby which is not in human readable form. 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. Binary Serialization  is done using Marshal which is built into Ruby and the code for it is written in Ruby's Marshal module(marshal.c) and thus no additional files are required in order to use it. The _dump and _load methods defined in marshal are used for serialization. Using marshal module the following types of objects can not be serialized: bindings, procedure objects, singleton objects, instances of IO objects and interfaces.&lt;br /&gt;
&lt;br /&gt;
Since the Binary Serialized data is not in human readable form, the following guidelines need to be followed.&lt;br /&gt;
     1.Use print instead of puts[http://ruby-doc.org/core-2.0.0/ARGF.html#] when serialized objects are written to a file in order to avoid new line characters to be written &lt;br /&gt;
       in the file.&lt;br /&gt;
     &lt;br /&gt;
     2.Use a record separator in order to differentiate between two objects.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Binary Serialization Example:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    class Animal&lt;br /&gt;
      def initialize  name, age&lt;br /&gt;
      @name = name&lt;br /&gt;
      @age=age&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
   class Cat &amp;lt; Animal&lt;br /&gt;
    def to_s&lt;br /&gt;
     &amp;quot;In Cat C: #{@name} \t #{@age}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
  c = Cat.new(&amp;quot;Kitty Kat&amp;quot;,5)&lt;br /&gt;
  puts &amp;quot;Before Serialization&amp;quot;&lt;br /&gt;
  puts c&lt;br /&gt;
  #puts d&lt;br /&gt;
  serialize_cat= Marshal.dump(c) #dumps the serialized cat object into serialize_cat&lt;br /&gt;
  puts &amp;quot;\nAfter Serialization:\n #{serialize_cat}&amp;quot;&lt;br /&gt;
  deserialize_cat= Marshal::load(serialize_cat) #deserializes the cat object and loads it back into deserialize_cat&lt;br /&gt;
  puts &amp;quot;\nAfter Deserialization\n #{deserialize_cat}&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   Before Serialization&lt;br /&gt;
   In Cat C: Kitty Kat 	 5&lt;br /&gt;
   After Serialization:&lt;br /&gt;
   oCat:&lt;br /&gt;
   @nameI&amp;quot;Kitty Kat:ET:	@agei&lt;br /&gt;
   After Deserialization&lt;br /&gt;
   In Cat C: Kitty Kat 	 5&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Similar to YAML, Marshal can also be used to dump data into a file. The above example showing the serialization using YAML::dump can be written using marshal as shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    f = File.open( 'first.yml', 'w' )&lt;br /&gt;
    Marshal.dump( [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;], f )&lt;br /&gt;
    f.close	&lt;br /&gt;
    File.open( 'first.yml' ){ |f|	&lt;br /&gt;
    $arr= Marshal.load(f)&lt;br /&gt;
    }	&lt;br /&gt;
    p( $arr )&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;]&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice that in the above example there is no &amp;quot;require&amp;quot; statement as opposed to the earlier example of writing serialized data into files using YAML. This is because unlike YAML, marshal is built-in Ruby and  no external libraries is required in order to use its functionality.&lt;br /&gt;
&lt;br /&gt;
Marshal can also be used to custom serialize an object i.e. it provides an option to omit the variables of an object that are not required in the serialized data. The following program shows the use of marshal_dump method for achieving custom serialization.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
      class First&lt;br /&gt;
      def initialize(name, age, country)&lt;br /&gt;
         @name = name&lt;br /&gt;
         @age = age&lt;br /&gt;
         @country=country&lt;br /&gt;
      end&lt;br /&gt;
      def to_s&lt;br /&gt;
            &amp;quot;In First: #{@name}, #{@age}, #{@country}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
      def marshal_dump&lt;br /&gt;
            [@name,@age]  #@country will not be serialized&lt;br /&gt;
      end&lt;br /&gt;
      def marshal_load(data)&lt;br /&gt;
       @name=data[0]&lt;br /&gt;
       @age=data[1]&lt;br /&gt;
       @country=&amp;quot;United States of America&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
      end&lt;br /&gt;
 &lt;br /&gt;
      x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
      puts x&lt;br /&gt;
  &lt;br /&gt;
      marshal_data = Marshal.dump( x ) &lt;br /&gt;
      y = Marshal.load( marshal_data ) &lt;br /&gt;
      p( y.to_s )&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    In First: Tom, 25, USA&lt;br /&gt;
    &amp;quot;In First: Tom, 25, United States of America&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== '''Serialization Performance in Ruby''' ==&lt;br /&gt;
The different serialization formats described above differ in the efficiency at which they can serialize and deserialize data and thus while serializing large amount of data their efficiency is taken into account. The .report[http://www.ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html] method in Ruby can be used to evaluate the performance of these serialization patterns. Marshal, as it handles data in binary format, is the most efficient form of serialization in Ruby. The following example compares the performance of marshal, JSON and YAML format.[http://www.pauldix.net/2008/08/serializing-dat.html]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   require 'benchmark'&lt;br /&gt;
   require 'rubygems'&lt;br /&gt;
   require 'json'&lt;br /&gt;
   require 'yaml'&lt;br /&gt;
   include Benchmark&lt;br /&gt;
   class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
      @name=name&lt;br /&gt;
      @age=age&lt;br /&gt;
      @country=country&lt;br /&gt;
     end&lt;br /&gt;
   end  &lt;br /&gt;
    x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
    puts &amp;quot;            User     System     Total      Real&amp;quot;&lt;br /&gt;
   benchmark do |t|&lt;br /&gt;
    print &amp;quot;Marshal:&amp;quot;&lt;br /&gt;
    t.report{1000.times do; Marshal.load(Marshal.dump(x));end}&lt;br /&gt;
    print &amp;quot;JSON:   &amp;quot;&lt;br /&gt;
    t.report{1000.times do; JSON.load(JSON.dump(x));end}&lt;br /&gt;
    print &amp;quot;YAML:   &amp;quot;&lt;br /&gt;
    t.report{1000.times do; YAML.load(YAML.dump(x));end}&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
              User     System     Total      Real&lt;br /&gt;
  Marshal:   0.010000   0.000000   0.010000 (  0.015408)&lt;br /&gt;
  JSON:      0.020000   0.010000   0.030000 (  0.022462)&lt;br /&gt;
  YAML:      0.460000   0.010000   0.470000 (  0.472149)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output of the above example shows that, given large chunks of data, marshal is comparatively more efficient format for serialization in Ruby.&lt;br /&gt;
&lt;br /&gt;
== Serialization in Object-Oriented Languages: Comparison ==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Sl.No !! Ruby !! Java !! .Net Framework !! C++&lt;br /&gt;
|-&lt;br /&gt;
| 1 || Ruby provides a built in module called 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 || Although, there is no built in support for serialization in C++, it can be achieved by using Boost libraries[http://www.boost.org/doc/libs/1_36_0/libs/serialization/example/demo.cpp]&lt;br /&gt;
|-&lt;br /&gt;
| 2 || The built in module of Ruby (Marshal) does not support platform independence, however, it can be achieved by using external libraries like YAML and JSON || Similarly, Java's built in serialization is also not platform independent and in order to use serialization in Java across Ruby platform, jruby library should be used.  || .Net used Remoting technology to make it platform independent. || Serialization using the Boost libraries is not platform independent.&lt;br /&gt;
|-&lt;br /&gt;
| 3 || YAML provides a method [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/syck/rdoc/Object.html (to_yaml_properties)] which can be used to select the variables who's value is need to be serialized. With Marshal, we need to write a method named marshal_dump defining the variables of an object that has to be serialized. || Provides an option for serializing only the required 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 data 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 || Serialization using the Boost libraries is custom and thus the user can specify the part of the objects to be serialized.&lt;br /&gt;
|-&lt;br /&gt;
| 4 || Bindings, procedure objects, singleton objects, instances of IO objects and interfaces can not be serialized. Serializing these objects throws TypeError exceptions. || Thread, OutputStream and its subclasses, and Socket are not serializable in Java.[http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html] || Objects like DataRow are non serializable in .NET. Whether an object is serializable or not can be checked using the [http://msdn.microsoft.com/en-us/library/system.type.isserializable(v=vs.110).aspx?cs-save-lang=1&amp;amp;cs-lang=cpp#code-snippet-1 Type.IsSerializable] || There are no such objects in C++.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== See Also==&lt;br /&gt;
&lt;br /&gt;
1. [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/json/rdoc/JSON.html Serialization in Ruby JSON]&lt;br /&gt;
&lt;br /&gt;
2. [http://api.rubyonrails.org/classes/ActiveModel/Serialization.html Serialization in Rails]&lt;br /&gt;
&lt;br /&gt;
3. [http://gregmoreno.wordpress.com/2011/01/27/preventing-model-explosion-via-rails-serialization/ Article on Rails Serialization]&lt;br /&gt;
&lt;br /&gt;
4. [http://en.wikipedia.org/wiki/Google_Protocol_Buffers Protocol Buffers]&lt;br /&gt;
&lt;br /&gt;
5. [https://rubygems.org/gems/ruby-protocol-buffers Ruby Protocol Buffers Gem]&lt;br /&gt;
&lt;br /&gt;
6. [http://en.wikipedia.org/wiki/Apache_Avro Avro]&lt;br /&gt;
&lt;br /&gt;
7. [http://rubygems.org/gems/avro Ruby Avro Gem]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
1. [http://en.wikipedia.org/wiki/Serialization Serilization in General]&lt;br /&gt;
&lt;br /&gt;
2. [http://en.wikipedia.org/wiki/Marshalling_%28computer_science%29 Marshaling]&lt;br /&gt;
&lt;br /&gt;
3. [http://techbrahmana.blogspot.com/2012/03/rails-cookie-handling-serialization-and.html Rails Cookie Handling]&lt;br /&gt;
&lt;br /&gt;
4. [http://yaml.org/ YAML]&lt;br /&gt;
&lt;br /&gt;
5. [http://www.ruby-doc.org/core-2.1.0/Marshal.html Marshal module]&lt;br /&gt;
&lt;br /&gt;
6. [http://json.org/ JSON]&lt;br /&gt;
&lt;br /&gt;
7. [http://rubygems.org/ Ruby Gems]&lt;br /&gt;
&lt;br /&gt;
8. [http://ruby-doc.org/core-2.0.0/ARGF.html# Ruby ARGF Documentation]&lt;br /&gt;
&lt;br /&gt;
9. [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html Ruby Documentation for Benchmark Gem]&lt;br /&gt;
&lt;br /&gt;
10. [http://www.pauldix.net/2008/08/serializing-dat.html Serialization Performance Comparison]&lt;br /&gt;
&lt;br /&gt;
11. [http://www.boost.org/doc/libs/1_36_0/libs/serialization/example/demo.cpp Boost Libraries]&lt;br /&gt;
&lt;br /&gt;
12. [http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html Java Serialization]&lt;br /&gt;
&lt;br /&gt;
13. [http://www.w3schools.com/json/ JSON Tutorial]&lt;br /&gt;
&lt;br /&gt;
14. [http://www.skorks.com/2010/04/serializing-and-deserializing-objects-with-ruby/ Serializing and De-serializing in Ruby]&lt;br /&gt;
&lt;br /&gt;
15. [http://www.codeproject.com/Articles/33296/Serialization-and-De-serialization Serialization and De-serialization]&lt;br /&gt;
&lt;br /&gt;
16. [http://msdn.microsoft.com/en-us/library/182eeyhh.aspx XML Serialization]&lt;br /&gt;
&lt;br /&gt;
17. [http://www.tutorialspoint.com/java/java_serialization.htm Java Serialization Tutorial]&lt;br /&gt;
&lt;br /&gt;
18. [http://www.sapphiresteel.com/ruby-programming/The-Book-Of-Ruby The Book of Ruby]&lt;br /&gt;
&lt;br /&gt;
19. [http://www.codeproject.com/Articles/20962/Introducing-Serialization-in-NET Serialization in .Net]&lt;br /&gt;
&lt;br /&gt;
20. [http://ruby.about.com/od/advancedruby/ss/Serialization-In-Ruby-Yaml.html Serialization in Ruby YAML]&lt;br /&gt;
&lt;br /&gt;
21. [http://ruby.about.com/od/tasks/a/The-Json-Gem.html Installing JSON gem]&lt;br /&gt;
&lt;br /&gt;
22. [http://www.ruby-doc.org/stdlib-2.0.0/libdoc/json/rdoc/JSON.html Serialization in Ruby JSON]&lt;/div&gt;</summary>
		<author><name>Mmehta2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1d_mm&amp;diff=83407</id>
		<title>CSC/ECE 517 Spring 2014/ch1a 1d mm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1d_mm&amp;diff=83407"/>
		<updated>2014-02-18T02:30:04Z</updated>

		<summary type="html">&lt;p&gt;Mmehta2: /* Few Practical Applications for Serialization */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page discusses Serialization, focusing on Ruby programming language. It describes different methods of Serialization in Ruby. In addition, it also compares serialization in different Object Oriented languages.&lt;br /&gt;
&lt;br /&gt;
== '''Introduction''' ==&lt;br /&gt;
&lt;br /&gt;
==='''Serialization'''===&lt;br /&gt;
&lt;br /&gt;
'''Serialization'''[http://en.wikipedia.org/wiki/Serialization] is a process of converting a data structure or an object into a stream of bytes or string to facilitate storage in memory, file (persistence storage) or transmission over a network. The process of Serialization is also referred to as ''Marshalling''[http://en.wikipedia.org/wiki/Marshalling_%28computer_science%29]. 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. &lt;br /&gt;
&lt;br /&gt;
==='''De-serialization'''===&lt;br /&gt;
&lt;br /&gt;
'''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''.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==='''Basic Advantages of Serialization'''===&lt;br /&gt;
&lt;br /&gt;
1. Communication between two or more processes on same machine. Object state can be saved and shared in a persistent or in-memory store.&lt;br /&gt;
&lt;br /&gt;
2. Communication between processes on different machines. Serialization facilitates the transmission of an object through a network.&lt;br /&gt;
&lt;br /&gt;
3. Creating a clone of an object. &lt;br /&gt;
&lt;br /&gt;
4. Cross-platform compatibility. Object can be serialized in a common format that is understood by multiple platforms. For example, JSON[http://www.json.org/] and XML.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==='''Practical Applications for Serialization'''===&lt;br /&gt;
&lt;br /&gt;
1. [http://docs.oracle.com/cd/E13222_01/wls/docs81/webapp/sessions.html#139726 HTTP Session] Replication by sharing session objects across web servers for load balancing and handling failover scenarios.&lt;br /&gt;
&lt;br /&gt;
2. Serialization facilitates communication in [http://en.wikipedia.org/wiki/Java_remote_method_invocation Remote Method Invocation] or [http://en.wikipedia.org/wiki/Remote_procedure_call Remote procedure calls]&lt;br /&gt;
&lt;br /&gt;
3. Rails Cookie Handling[http://techbrahmana.blogspot.com/2012/03/rails-cookie-handling-serialization-and.html]. Cookies are stored marshalled/unmarshalled to and from client machines. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:SerializationChart.jpg]]&lt;br /&gt;
&lt;br /&gt;
== '''Serialization in Ruby:''' ==&lt;br /&gt;
&lt;br /&gt;
Ruby provides serialization capabilities through its module, [http://www.ruby-doc.org/core-2.0.0/Marshal.html Marshal]. There are also other libraries like YAML[http://yaml.org/] and JSON which can be used in Ruby to generate serialized objects for purposes like platform independence and human readable formats.&lt;br /&gt;
&lt;br /&gt;
== '''Types of Serialization''' ==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
=== Converting Ruby Objects in Human Readable Formats ===&lt;br /&gt;
The conversion of Ruby objects into YAML and JSON formats are explained below.&lt;br /&gt;
&lt;br /&gt;
==== Converting Ruby Objects to YAML format ====&lt;br /&gt;
&lt;br /&gt;
YAML 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 binary format. 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. In order to use it in Ruby, the yaml.rb file is required to be loaded which provides methods for converting objects into yaml format and creating .yml files.&lt;br /&gt;
&lt;br /&gt;
'''Examples of serialization using YAML:'''&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;nowiki&amp;gt; #Serialization using YAML's to_yaml method&lt;br /&gt;
 &lt;br /&gt;
  require &amp;quot;yaml&amp;quot;&lt;br /&gt;
     class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
	@name = name&lt;br /&gt;
	@age = age&lt;br /&gt;
	@country=country&lt;br /&gt;
     end&lt;br /&gt;
      def to_s&lt;br /&gt;
	&amp;quot;In First:\n#{@name}, #{@age}, #{@country}\n&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
  x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
  puts x&lt;br /&gt;
  puts x.to_yaml&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   In First:&lt;br /&gt;
   Tom, 25, USA&lt;br /&gt;
   --- !ruby/object:First&lt;br /&gt;
   name: Tom&lt;br /&gt;
   age: 25&lt;br /&gt;
   country: USA&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The above code displays the object x, first as a string and then in the yaml format. &lt;br /&gt;
&lt;br /&gt;
'''Saving YAML data into a file:'''&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt; # Serialization using YAML::dump&lt;br /&gt;
&lt;br /&gt;
require 'yaml'&lt;br /&gt;
&lt;br /&gt;
f = File.open( 'first.yml', 'w' )&lt;br /&gt;
YAML.dump( [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;], f )&lt;br /&gt;
f.close&lt;br /&gt;
		&lt;br /&gt;
File.open( 'first.yml' ){ |f|	&lt;br /&gt;
    $arr= YAML.load(f)&lt;br /&gt;
}	&lt;br /&gt;
&lt;br /&gt;
p( $arr )&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;]&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The dump function can be used to serialize the data and save it into a file in YAML format. As shown in the above example the data in YAML format can be de-serialized using the load function.&lt;br /&gt;
&lt;br /&gt;
YAML libraries also provides an option of selecting only those variables of the object that are needed to be serialized. This is done using the to_yaml_properties method as shown in the below example.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;nowiki&amp;gt;  #Custom Serialization using YAML&lt;br /&gt;
 &lt;br /&gt;
  require &amp;quot;yaml&amp;quot;&lt;br /&gt;
     class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
	@name = name&lt;br /&gt;
	@age = age&lt;br /&gt;
	@country=country&lt;br /&gt;
     end&lt;br /&gt;
     def to_s&lt;br /&gt;
	&amp;quot;In First:\n#{@name}, #{@age}, #{@country}\n&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
      def to_yaml_properties&lt;br /&gt;
	   [&amp;quot;@name&amp;quot;,&amp;quot;@age&amp;quot;]  #@country will not be serialized&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
  x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
  puts x&lt;br /&gt;
  puts x.to_yaml&lt;br /&gt;
  &amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   In First:&lt;br /&gt;
   Tom, 25, USA&lt;br /&gt;
   --- !ruby/object:First&lt;br /&gt;
   name: Tom&lt;br /&gt;
   age: 25&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Converting Ruby Objects to JSON format: ====&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
JSON 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. Ruby 1.8.7 distribution is not bundled with json gem. However, in Ruby 1.9.2, the json gem is bundled with the core Ruby distribution.&lt;br /&gt;
The JSON library can be installed using Ruby Gems[http://rubygems.org/] as shown below:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;# gem install json&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A JSON string for serialization can be created by using the JSON.generate method:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
        require 'json'&lt;br /&gt;
        my_hash = {:Welcome =&amp;gt; &amp;quot;Ruby&amp;quot;}&lt;br /&gt;
        puts JSON.generate(my_hash) =&amp;gt; &amp;quot;{\&amp;quot;WELCOME\&amp;quot;:\&amp;quot;RUBY\&amp;quot;}&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
'''Output:'''&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;{&amp;quot;{\&amp;quot;Welcome\&amp;quot;:\&amp;quot;Ruby\&amp;quot;}&amp;quot;=&amp;gt;&amp;quot;{\&amp;quot;WELCOME\&amp;quot;:\&amp;quot;RUBY\&amp;quot;}&amp;quot;}&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A JSON string received from another program can be parsed by using JSON.parse&lt;br /&gt;
Ruby thus converts String to Hash.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
        require 'json'&lt;br /&gt;
        my_hash = JSON.parse('{&amp;quot;Welcome&amp;quot;: &amp;quot;Ruby&amp;quot;}')&lt;br /&gt;
        puts my_hash[&amp;quot;Welcome&amp;quot;] =&amp;gt; &amp;quot;Ruby&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Converting Ruby Objects to Binary Formats ===&lt;br /&gt;
Binary Serialization is another form of serialization in Ruby which is not in human readable form. 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. Binary Serialization  is done using Marshal which is built into Ruby and the code for it is written in Ruby's Marshal module(marshal.c) and thus no additional files are required in order to use it. The _dump and _load methods defined in marshal are used for serialization. Using marshal module the following types of objects can not be serialized: bindings, procedure objects, singleton objects, instances of IO objects and interfaces.&lt;br /&gt;
&lt;br /&gt;
Since the Binary Serialized data is not in human readable form, the following guidelines need to be followed.&lt;br /&gt;
     1.Use print instead of puts[http://ruby-doc.org/core-2.0.0/ARGF.html#] when serialized objects are written to a file in order to avoid new line characters to be written &lt;br /&gt;
       in the file.&lt;br /&gt;
     &lt;br /&gt;
     2.Use a record separator in order to differentiate between two objects.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Binary Serialization Example:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    class Animal&lt;br /&gt;
      def initialize  name, age&lt;br /&gt;
      @name = name&lt;br /&gt;
      @age=age&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
   class Cat &amp;lt; Animal&lt;br /&gt;
    def to_s&lt;br /&gt;
     &amp;quot;In Cat C: #{@name} \t #{@age}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
  c = Cat.new(&amp;quot;Kitty Kat&amp;quot;,5)&lt;br /&gt;
  puts &amp;quot;Before Serialization&amp;quot;&lt;br /&gt;
  puts c&lt;br /&gt;
  #puts d&lt;br /&gt;
  serialize_cat= Marshal.dump(c) #dumps the serialized cat object into serialize_cat&lt;br /&gt;
  puts &amp;quot;\nAfter Serialization:\n #{serialize_cat}&amp;quot;&lt;br /&gt;
  deserialize_cat= Marshal::load(serialize_cat) #deserializes the cat object and loads it back into deserialize_cat&lt;br /&gt;
  puts &amp;quot;\nAfter Deserialization\n #{deserialize_cat}&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   Before Serialization&lt;br /&gt;
   In Cat C: Kitty Kat 	 5&lt;br /&gt;
   After Serialization:&lt;br /&gt;
   oCat:&lt;br /&gt;
   @nameI&amp;quot;Kitty Kat:ET:	@agei&lt;br /&gt;
   After Deserialization&lt;br /&gt;
   In Cat C: Kitty Kat 	 5&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Similar to YAML, Marshal can also be used to dump data into a file. The above example showing the serialization using YAML::dump can be written using marshal as shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    f = File.open( 'first.yml', 'w' )&lt;br /&gt;
    Marshal.dump( [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;], f )&lt;br /&gt;
    f.close	&lt;br /&gt;
    File.open( 'first.yml' ){ |f|	&lt;br /&gt;
    $arr= Marshal.load(f)&lt;br /&gt;
    }	&lt;br /&gt;
    p( $arr )&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;]&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice that in the above example there is no &amp;quot;require&amp;quot; statement as opposed to the earlier example of writing serialized data into files using YAML. This is because unlike YAML, marshal is built-in Ruby and  no external libraries is required in order to use its functionality.&lt;br /&gt;
&lt;br /&gt;
Marshal can also be used to custom serialize an object i.e. it provides an option to omit the variables of an object that are not required in the serialized data. The following program shows the use of marshal_dump method for achieving custom serialization.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
      class First&lt;br /&gt;
      def initialize(name, age, country)&lt;br /&gt;
         @name = name&lt;br /&gt;
         @age = age&lt;br /&gt;
         @country=country&lt;br /&gt;
      end&lt;br /&gt;
      def to_s&lt;br /&gt;
            &amp;quot;In First: #{@name}, #{@age}, #{@country}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
      def marshal_dump&lt;br /&gt;
            [@name,@age]  #@country will not be serialized&lt;br /&gt;
      end&lt;br /&gt;
      def marshal_load(data)&lt;br /&gt;
       @name=data[0]&lt;br /&gt;
       @age=data[1]&lt;br /&gt;
       @country=&amp;quot;United States of America&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
      end&lt;br /&gt;
 &lt;br /&gt;
      x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
      puts x&lt;br /&gt;
  &lt;br /&gt;
      marshal_data = Marshal.dump( x ) &lt;br /&gt;
      y = Marshal.load( marshal_data ) &lt;br /&gt;
      p( y.to_s )&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    In First: Tom, 25, USA&lt;br /&gt;
    &amp;quot;In First: Tom, 25, United States of America&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== '''Serialization Performance in Ruby''' ==&lt;br /&gt;
The different serialization formats described above differ in the efficiency at which they can serialize and deserialize data and thus while serializing large amount of data their efficiency is taken into account. The .report[http://www.ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html] method in Ruby can be used to evaluate the performance of these serialization patterns. Marshal, as it handles data in binary format, is the most efficient form of serialization in Ruby. The following example compares the performance of marshal, JSON and YAML format.[http://www.pauldix.net/2008/08/serializing-dat.html]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   require 'benchmark'&lt;br /&gt;
   require 'rubygems'&lt;br /&gt;
   require 'json'&lt;br /&gt;
   require 'yaml'&lt;br /&gt;
   include Benchmark&lt;br /&gt;
   class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
      @name=name&lt;br /&gt;
      @age=age&lt;br /&gt;
      @country=country&lt;br /&gt;
     end&lt;br /&gt;
   end  &lt;br /&gt;
    x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
    puts &amp;quot;            User     System     Total      Real&amp;quot;&lt;br /&gt;
   benchmark do |t|&lt;br /&gt;
    print &amp;quot;Marshal:&amp;quot;&lt;br /&gt;
    t.report{1000.times do; Marshal.load(Marshal.dump(x));end}&lt;br /&gt;
    print &amp;quot;JSON:   &amp;quot;&lt;br /&gt;
    t.report{1000.times do; JSON.load(JSON.dump(x));end}&lt;br /&gt;
    print &amp;quot;YAML:   &amp;quot;&lt;br /&gt;
    t.report{1000.times do; YAML.load(YAML.dump(x));end}&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
              User     System     Total      Real&lt;br /&gt;
  Marshal:   0.010000   0.000000   0.010000 (  0.015408)&lt;br /&gt;
  JSON:      0.020000   0.010000   0.030000 (  0.022462)&lt;br /&gt;
  YAML:      0.460000   0.010000   0.470000 (  0.472149)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output of the above example shows that, given large chunks of data, marshal is comparatively more efficient format for serialization in Ruby.&lt;br /&gt;
&lt;br /&gt;
== Serialization in Object-Oriented Languages: Comparison ==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Sl.No !! Ruby !! Java !! .Net Framework !! C++&lt;br /&gt;
|-&lt;br /&gt;
| 1 || Ruby provides a built in module called 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 || Although, there is no built in support for serialization in C++, it can be achieved by using Boost libraries[http://www.boost.org/doc/libs/1_36_0/libs/serialization/example/demo.cpp]&lt;br /&gt;
|-&lt;br /&gt;
| 2 || The built in module of Ruby (Marshal) does not support platform independence, however, it can be achieved by using external libraries like YAML and JSON || Similarly, Java's built in serialization is also not platform independent and in order to use serialization in Java across Ruby platform, jruby library should be used.  || .Net used Remoting technology to make it platform independent. || Serialization using the Boost libraries is not platform independent.&lt;br /&gt;
|-&lt;br /&gt;
| 3 || YAML provides a method [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/syck/rdoc/Object.html (to_yaml_properties)] which can be used to select the variables who's value is need to be serialized. With Marshal, we need to write a method named marshal_dump defining the variables of an object that has to be serialized. || Provides an option for serializing only the required 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 data 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 || Serialization using the Boost libraries is custom and thus the user can specify the part of the objects to be serialized.&lt;br /&gt;
|-&lt;br /&gt;
| 4 || Bindings, procedure objects, singleton objects, instances of IO objects and interfaces can not be serialized. Serializing these objects throws TypeError exceptions. || Thread, OutputStream and its subclasses, and Socket are not serializable in Java.[http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html] || Objects like DataRow are non serializable in .NET. Whether an object is serializable or not can be checked using the [http://msdn.microsoft.com/en-us/library/system.type.isserializable(v=vs.110).aspx?cs-save-lang=1&amp;amp;cs-lang=cpp#code-snippet-1 Type.IsSerializable] || There are no such objects in C++.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== See Also==&lt;br /&gt;
&lt;br /&gt;
1. [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/json/rdoc/JSON.html Serialization in Ruby JSON]&lt;br /&gt;
&lt;br /&gt;
2. [http://api.rubyonrails.org/classes/ActiveModel/Serialization.html Serialization in Rails]&lt;br /&gt;
&lt;br /&gt;
3. [http://gregmoreno.wordpress.com/2011/01/27/preventing-model-explosion-via-rails-serialization/ Article on Rails Serialization]&lt;br /&gt;
&lt;br /&gt;
4. [http://en.wikipedia.org/wiki/Google_Protocol_Buffers Protocol Buffers]&lt;br /&gt;
&lt;br /&gt;
5. [https://rubygems.org/gems/ruby-protocol-buffers Ruby Protocol Buffers Gem]&lt;br /&gt;
&lt;br /&gt;
6. [http://en.wikipedia.org/wiki/Apache_Avro Avro]&lt;br /&gt;
&lt;br /&gt;
7. [http://rubygems.org/gems/avro Ruby Avro Gem]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
1. [http://en.wikipedia.org/wiki/Serialization Serilization in General]&lt;br /&gt;
&lt;br /&gt;
2. [http://en.wikipedia.org/wiki/Marshalling_%28computer_science%29 Marshaling]&lt;br /&gt;
&lt;br /&gt;
3. [http://techbrahmana.blogspot.com/2012/03/rails-cookie-handling-serialization-and.html Rails Cookie Handling]&lt;br /&gt;
&lt;br /&gt;
4. [http://yaml.org/ YAML]&lt;br /&gt;
&lt;br /&gt;
5. [http://www.ruby-doc.org/core-2.1.0/Marshal.html Marshal module]&lt;br /&gt;
&lt;br /&gt;
6. [http://json.org/ JSON]&lt;br /&gt;
&lt;br /&gt;
7. [http://rubygems.org/ Ruby Gems]&lt;br /&gt;
&lt;br /&gt;
8. [http://ruby-doc.org/core-2.0.0/ARGF.html# Ruby ARGF Documentation]&lt;br /&gt;
&lt;br /&gt;
9. [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html Ruby Documentation for Benchmark Gem]&lt;br /&gt;
&lt;br /&gt;
10. [http://www.pauldix.net/2008/08/serializing-dat.html Serialization Performance Comparison]&lt;br /&gt;
&lt;br /&gt;
11. [http://www.boost.org/doc/libs/1_36_0/libs/serialization/example/demo.cpp Boost Libraries]&lt;br /&gt;
&lt;br /&gt;
12. [http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html Java Serialization]&lt;br /&gt;
&lt;br /&gt;
13. [http://www.w3schools.com/json/ JSON Tutorial]&lt;br /&gt;
&lt;br /&gt;
14. [http://www.skorks.com/2010/04/serializing-and-deserializing-objects-with-ruby/ Serializing and De-serializing in Ruby]&lt;br /&gt;
&lt;br /&gt;
15. [http://www.codeproject.com/Articles/33296/Serialization-and-De-serialization Serialization and De-serialization]&lt;br /&gt;
&lt;br /&gt;
16. [http://msdn.microsoft.com/en-us/library/182eeyhh.aspx XML Serialization]&lt;br /&gt;
&lt;br /&gt;
17. [http://www.tutorialspoint.com/java/java_serialization.htm Java Serialization Tutorial]&lt;br /&gt;
&lt;br /&gt;
18. [http://www.sapphiresteel.com/ruby-programming/The-Book-Of-Ruby The Book of Ruby]&lt;br /&gt;
&lt;br /&gt;
19. [http://www.codeproject.com/Articles/20962/Introducing-Serialization-in-NET Serialization in .Net]&lt;br /&gt;
&lt;br /&gt;
20. [http://ruby.about.com/od/advancedruby/ss/Serialization-In-Ruby-Yaml.html Serialization in Ruby YAML]&lt;br /&gt;
&lt;br /&gt;
21. [http://ruby.about.com/od/tasks/a/The-Json-Gem.html Installing JSON gem]&lt;br /&gt;
&lt;br /&gt;
22. [http://www.ruby-doc.org/stdlib-2.0.0/libdoc/json/rdoc/JSON.html Serialization in Ruby JSON]&lt;/div&gt;</summary>
		<author><name>Mmehta2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1d_mm&amp;diff=83406</id>
		<title>CSC/ECE 517 Spring 2014/ch1a 1d mm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1d_mm&amp;diff=83406"/>
		<updated>2014-02-18T02:29:44Z</updated>

		<summary type="html">&lt;p&gt;Mmehta2: /* Serialization */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page discusses Serialization, focusing on Ruby programming language. It describes different methods of Serialization in Ruby. In addition, it also compares serialization in different Object Oriented languages.&lt;br /&gt;
&lt;br /&gt;
== '''Introduction''' ==&lt;br /&gt;
&lt;br /&gt;
==='''Serialization'''===&lt;br /&gt;
&lt;br /&gt;
'''Serialization'''[http://en.wikipedia.org/wiki/Serialization] is a process of converting a data structure or an object into a stream of bytes or string to facilitate storage in memory, file (persistence storage) or transmission over a network. The process of Serialization is also referred to as ''Marshalling''[http://en.wikipedia.org/wiki/Marshalling_%28computer_science%29]. 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. &lt;br /&gt;
&lt;br /&gt;
==='''De-serialization'''===&lt;br /&gt;
&lt;br /&gt;
'''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''.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==='''Basic Advantages of Serialization'''===&lt;br /&gt;
&lt;br /&gt;
1. Communication between two or more processes on same machine. Object state can be saved and shared in a persistent or in-memory store.&lt;br /&gt;
&lt;br /&gt;
2. Communication between processes on different machines. Serialization facilitates the transmission of an object through a network.&lt;br /&gt;
&lt;br /&gt;
3. Creating a clone of an object. &lt;br /&gt;
&lt;br /&gt;
4. Cross-platform compatibility. Object can be serialized in a common format that is understood by multiple platforms. For example, JSON[http://www.json.org/] and XML.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==='''Few Practical Applications for Serialization'''===&lt;br /&gt;
&lt;br /&gt;
1. [http://docs.oracle.com/cd/E13222_01/wls/docs81/webapp/sessions.html#139726 HTTP Session] Replication by sharing session objects across web servers for load balancing and handling failover scenarios.&lt;br /&gt;
&lt;br /&gt;
2. Serialization facilitates communication in [http://en.wikipedia.org/wiki/Java_remote_method_invocation Remote Method Invocation] or [http://en.wikipedia.org/wiki/Remote_procedure_call Remote procedure calls]&lt;br /&gt;
&lt;br /&gt;
3. Rails Cookie Handling[http://techbrahmana.blogspot.com/2012/03/rails-cookie-handling-serialization-and.html]. Cookies are stored marshalled/unmarshalled to and from client machines. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:SerializationChart.jpg]]&lt;br /&gt;
&lt;br /&gt;
== '''Serialization in Ruby:''' ==&lt;br /&gt;
&lt;br /&gt;
Ruby provides serialization capabilities through its module, [http://www.ruby-doc.org/core-2.0.0/Marshal.html Marshal]. There are also other libraries like YAML[http://yaml.org/] and JSON which can be used in Ruby to generate serialized objects for purposes like platform independence and human readable formats.&lt;br /&gt;
&lt;br /&gt;
== '''Types of Serialization''' ==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
=== Converting Ruby Objects in Human Readable Formats ===&lt;br /&gt;
The conversion of Ruby objects into YAML and JSON formats are explained below.&lt;br /&gt;
&lt;br /&gt;
==== Converting Ruby Objects to YAML format ====&lt;br /&gt;
&lt;br /&gt;
YAML 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 binary format. 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. In order to use it in Ruby, the yaml.rb file is required to be loaded which provides methods for converting objects into yaml format and creating .yml files.&lt;br /&gt;
&lt;br /&gt;
'''Examples of serialization using YAML:'''&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;nowiki&amp;gt; #Serialization using YAML's to_yaml method&lt;br /&gt;
 &lt;br /&gt;
  require &amp;quot;yaml&amp;quot;&lt;br /&gt;
     class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
	@name = name&lt;br /&gt;
	@age = age&lt;br /&gt;
	@country=country&lt;br /&gt;
     end&lt;br /&gt;
      def to_s&lt;br /&gt;
	&amp;quot;In First:\n#{@name}, #{@age}, #{@country}\n&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
  x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
  puts x&lt;br /&gt;
  puts x.to_yaml&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   In First:&lt;br /&gt;
   Tom, 25, USA&lt;br /&gt;
   --- !ruby/object:First&lt;br /&gt;
   name: Tom&lt;br /&gt;
   age: 25&lt;br /&gt;
   country: USA&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The above code displays the object x, first as a string and then in the yaml format. &lt;br /&gt;
&lt;br /&gt;
'''Saving YAML data into a file:'''&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt; # Serialization using YAML::dump&lt;br /&gt;
&lt;br /&gt;
require 'yaml'&lt;br /&gt;
&lt;br /&gt;
f = File.open( 'first.yml', 'w' )&lt;br /&gt;
YAML.dump( [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;], f )&lt;br /&gt;
f.close&lt;br /&gt;
		&lt;br /&gt;
File.open( 'first.yml' ){ |f|	&lt;br /&gt;
    $arr= YAML.load(f)&lt;br /&gt;
}	&lt;br /&gt;
&lt;br /&gt;
p( $arr )&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;]&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The dump function can be used to serialize the data and save it into a file in YAML format. As shown in the above example the data in YAML format can be de-serialized using the load function.&lt;br /&gt;
&lt;br /&gt;
YAML libraries also provides an option of selecting only those variables of the object that are needed to be serialized. This is done using the to_yaml_properties method as shown in the below example.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;nowiki&amp;gt;  #Custom Serialization using YAML&lt;br /&gt;
 &lt;br /&gt;
  require &amp;quot;yaml&amp;quot;&lt;br /&gt;
     class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
	@name = name&lt;br /&gt;
	@age = age&lt;br /&gt;
	@country=country&lt;br /&gt;
     end&lt;br /&gt;
     def to_s&lt;br /&gt;
	&amp;quot;In First:\n#{@name}, #{@age}, #{@country}\n&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
      def to_yaml_properties&lt;br /&gt;
	   [&amp;quot;@name&amp;quot;,&amp;quot;@age&amp;quot;]  #@country will not be serialized&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
  x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
  puts x&lt;br /&gt;
  puts x.to_yaml&lt;br /&gt;
  &amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   In First:&lt;br /&gt;
   Tom, 25, USA&lt;br /&gt;
   --- !ruby/object:First&lt;br /&gt;
   name: Tom&lt;br /&gt;
   age: 25&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Converting Ruby Objects to JSON format: ====&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
JSON 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. Ruby 1.8.7 distribution is not bundled with json gem. However, in Ruby 1.9.2, the json gem is bundled with the core Ruby distribution.&lt;br /&gt;
The JSON library can be installed using Ruby Gems[http://rubygems.org/] as shown below:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;# gem install json&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A JSON string for serialization can be created by using the JSON.generate method:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
        require 'json'&lt;br /&gt;
        my_hash = {:Welcome =&amp;gt; &amp;quot;Ruby&amp;quot;}&lt;br /&gt;
        puts JSON.generate(my_hash) =&amp;gt; &amp;quot;{\&amp;quot;WELCOME\&amp;quot;:\&amp;quot;RUBY\&amp;quot;}&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
'''Output:'''&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;{&amp;quot;{\&amp;quot;Welcome\&amp;quot;:\&amp;quot;Ruby\&amp;quot;}&amp;quot;=&amp;gt;&amp;quot;{\&amp;quot;WELCOME\&amp;quot;:\&amp;quot;RUBY\&amp;quot;}&amp;quot;}&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A JSON string received from another program can be parsed by using JSON.parse&lt;br /&gt;
Ruby thus converts String to Hash.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
        require 'json'&lt;br /&gt;
        my_hash = JSON.parse('{&amp;quot;Welcome&amp;quot;: &amp;quot;Ruby&amp;quot;}')&lt;br /&gt;
        puts my_hash[&amp;quot;Welcome&amp;quot;] =&amp;gt; &amp;quot;Ruby&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Converting Ruby Objects to Binary Formats ===&lt;br /&gt;
Binary Serialization is another form of serialization in Ruby which is not in human readable form. 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. Binary Serialization  is done using Marshal which is built into Ruby and the code for it is written in Ruby's Marshal module(marshal.c) and thus no additional files are required in order to use it. The _dump and _load methods defined in marshal are used for serialization. Using marshal module the following types of objects can not be serialized: bindings, procedure objects, singleton objects, instances of IO objects and interfaces.&lt;br /&gt;
&lt;br /&gt;
Since the Binary Serialized data is not in human readable form, the following guidelines need to be followed.&lt;br /&gt;
     1.Use print instead of puts[http://ruby-doc.org/core-2.0.0/ARGF.html#] when serialized objects are written to a file in order to avoid new line characters to be written &lt;br /&gt;
       in the file.&lt;br /&gt;
     &lt;br /&gt;
     2.Use a record separator in order to differentiate between two objects.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Binary Serialization Example:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    class Animal&lt;br /&gt;
      def initialize  name, age&lt;br /&gt;
      @name = name&lt;br /&gt;
      @age=age&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
   class Cat &amp;lt; Animal&lt;br /&gt;
    def to_s&lt;br /&gt;
     &amp;quot;In Cat C: #{@name} \t #{@age}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
  c = Cat.new(&amp;quot;Kitty Kat&amp;quot;,5)&lt;br /&gt;
  puts &amp;quot;Before Serialization&amp;quot;&lt;br /&gt;
  puts c&lt;br /&gt;
  #puts d&lt;br /&gt;
  serialize_cat= Marshal.dump(c) #dumps the serialized cat object into serialize_cat&lt;br /&gt;
  puts &amp;quot;\nAfter Serialization:\n #{serialize_cat}&amp;quot;&lt;br /&gt;
  deserialize_cat= Marshal::load(serialize_cat) #deserializes the cat object and loads it back into deserialize_cat&lt;br /&gt;
  puts &amp;quot;\nAfter Deserialization\n #{deserialize_cat}&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   Before Serialization&lt;br /&gt;
   In Cat C: Kitty Kat 	 5&lt;br /&gt;
   After Serialization:&lt;br /&gt;
   oCat:&lt;br /&gt;
   @nameI&amp;quot;Kitty Kat:ET:	@agei&lt;br /&gt;
   After Deserialization&lt;br /&gt;
   In Cat C: Kitty Kat 	 5&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Similar to YAML, Marshal can also be used to dump data into a file. The above example showing the serialization using YAML::dump can be written using marshal as shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    f = File.open( 'first.yml', 'w' )&lt;br /&gt;
    Marshal.dump( [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;], f )&lt;br /&gt;
    f.close	&lt;br /&gt;
    File.open( 'first.yml' ){ |f|	&lt;br /&gt;
    $arr= Marshal.load(f)&lt;br /&gt;
    }	&lt;br /&gt;
    p( $arr )&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;]&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice that in the above example there is no &amp;quot;require&amp;quot; statement as opposed to the earlier example of writing serialized data into files using YAML. This is because unlike YAML, marshal is built-in Ruby and  no external libraries is required in order to use its functionality.&lt;br /&gt;
&lt;br /&gt;
Marshal can also be used to custom serialize an object i.e. it provides an option to omit the variables of an object that are not required in the serialized data. The following program shows the use of marshal_dump method for achieving custom serialization.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
      class First&lt;br /&gt;
      def initialize(name, age, country)&lt;br /&gt;
         @name = name&lt;br /&gt;
         @age = age&lt;br /&gt;
         @country=country&lt;br /&gt;
      end&lt;br /&gt;
      def to_s&lt;br /&gt;
            &amp;quot;In First: #{@name}, #{@age}, #{@country}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
      def marshal_dump&lt;br /&gt;
            [@name,@age]  #@country will not be serialized&lt;br /&gt;
      end&lt;br /&gt;
      def marshal_load(data)&lt;br /&gt;
       @name=data[0]&lt;br /&gt;
       @age=data[1]&lt;br /&gt;
       @country=&amp;quot;United States of America&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
      end&lt;br /&gt;
 &lt;br /&gt;
      x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
      puts x&lt;br /&gt;
  &lt;br /&gt;
      marshal_data = Marshal.dump( x ) &lt;br /&gt;
      y = Marshal.load( marshal_data ) &lt;br /&gt;
      p( y.to_s )&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    In First: Tom, 25, USA&lt;br /&gt;
    &amp;quot;In First: Tom, 25, United States of America&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== '''Serialization Performance in Ruby''' ==&lt;br /&gt;
The different serialization formats described above differ in the efficiency at which they can serialize and deserialize data and thus while serializing large amount of data their efficiency is taken into account. The .report[http://www.ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html] method in Ruby can be used to evaluate the performance of these serialization patterns. Marshal, as it handles data in binary format, is the most efficient form of serialization in Ruby. The following example compares the performance of marshal, JSON and YAML format.[http://www.pauldix.net/2008/08/serializing-dat.html]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   require 'benchmark'&lt;br /&gt;
   require 'rubygems'&lt;br /&gt;
   require 'json'&lt;br /&gt;
   require 'yaml'&lt;br /&gt;
   include Benchmark&lt;br /&gt;
   class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
      @name=name&lt;br /&gt;
      @age=age&lt;br /&gt;
      @country=country&lt;br /&gt;
     end&lt;br /&gt;
   end  &lt;br /&gt;
    x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
    puts &amp;quot;            User     System     Total      Real&amp;quot;&lt;br /&gt;
   benchmark do |t|&lt;br /&gt;
    print &amp;quot;Marshal:&amp;quot;&lt;br /&gt;
    t.report{1000.times do; Marshal.load(Marshal.dump(x));end}&lt;br /&gt;
    print &amp;quot;JSON:   &amp;quot;&lt;br /&gt;
    t.report{1000.times do; JSON.load(JSON.dump(x));end}&lt;br /&gt;
    print &amp;quot;YAML:   &amp;quot;&lt;br /&gt;
    t.report{1000.times do; YAML.load(YAML.dump(x));end}&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
              User     System     Total      Real&lt;br /&gt;
  Marshal:   0.010000   0.000000   0.010000 (  0.015408)&lt;br /&gt;
  JSON:      0.020000   0.010000   0.030000 (  0.022462)&lt;br /&gt;
  YAML:      0.460000   0.010000   0.470000 (  0.472149)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output of the above example shows that, given large chunks of data, marshal is comparatively more efficient format for serialization in Ruby.&lt;br /&gt;
&lt;br /&gt;
== Serialization in Object-Oriented Languages: Comparison ==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Sl.No !! Ruby !! Java !! .Net Framework !! C++&lt;br /&gt;
|-&lt;br /&gt;
| 1 || Ruby provides a built in module called 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 || Although, there is no built in support for serialization in C++, it can be achieved by using Boost libraries[http://www.boost.org/doc/libs/1_36_0/libs/serialization/example/demo.cpp]&lt;br /&gt;
|-&lt;br /&gt;
| 2 || The built in module of Ruby (Marshal) does not support platform independence, however, it can be achieved by using external libraries like YAML and JSON || Similarly, Java's built in serialization is also not platform independent and in order to use serialization in Java across Ruby platform, jruby library should be used.  || .Net used Remoting technology to make it platform independent. || Serialization using the Boost libraries is not platform independent.&lt;br /&gt;
|-&lt;br /&gt;
| 3 || YAML provides a method [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/syck/rdoc/Object.html (to_yaml_properties)] which can be used to select the variables who's value is need to be serialized. With Marshal, we need to write a method named marshal_dump defining the variables of an object that has to be serialized. || Provides an option for serializing only the required 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 data 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 || Serialization using the Boost libraries is custom and thus the user can specify the part of the objects to be serialized.&lt;br /&gt;
|-&lt;br /&gt;
| 4 || Bindings, procedure objects, singleton objects, instances of IO objects and interfaces can not be serialized. Serializing these objects throws TypeError exceptions. || Thread, OutputStream and its subclasses, and Socket are not serializable in Java.[http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html] || Objects like DataRow are non serializable in .NET. Whether an object is serializable or not can be checked using the [http://msdn.microsoft.com/en-us/library/system.type.isserializable(v=vs.110).aspx?cs-save-lang=1&amp;amp;cs-lang=cpp#code-snippet-1 Type.IsSerializable] || There are no such objects in C++.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== See Also==&lt;br /&gt;
&lt;br /&gt;
1. [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/json/rdoc/JSON.html Serialization in Ruby JSON]&lt;br /&gt;
&lt;br /&gt;
2. [http://api.rubyonrails.org/classes/ActiveModel/Serialization.html Serialization in Rails]&lt;br /&gt;
&lt;br /&gt;
3. [http://gregmoreno.wordpress.com/2011/01/27/preventing-model-explosion-via-rails-serialization/ Article on Rails Serialization]&lt;br /&gt;
&lt;br /&gt;
4. [http://en.wikipedia.org/wiki/Google_Protocol_Buffers Protocol Buffers]&lt;br /&gt;
&lt;br /&gt;
5. [https://rubygems.org/gems/ruby-protocol-buffers Ruby Protocol Buffers Gem]&lt;br /&gt;
&lt;br /&gt;
6. [http://en.wikipedia.org/wiki/Apache_Avro Avro]&lt;br /&gt;
&lt;br /&gt;
7. [http://rubygems.org/gems/avro Ruby Avro Gem]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
1. [http://en.wikipedia.org/wiki/Serialization Serilization in General]&lt;br /&gt;
&lt;br /&gt;
2. [http://en.wikipedia.org/wiki/Marshalling_%28computer_science%29 Marshaling]&lt;br /&gt;
&lt;br /&gt;
3. [http://techbrahmana.blogspot.com/2012/03/rails-cookie-handling-serialization-and.html Rails Cookie Handling]&lt;br /&gt;
&lt;br /&gt;
4. [http://yaml.org/ YAML]&lt;br /&gt;
&lt;br /&gt;
5. [http://www.ruby-doc.org/core-2.1.0/Marshal.html Marshal module]&lt;br /&gt;
&lt;br /&gt;
6. [http://json.org/ JSON]&lt;br /&gt;
&lt;br /&gt;
7. [http://rubygems.org/ Ruby Gems]&lt;br /&gt;
&lt;br /&gt;
8. [http://ruby-doc.org/core-2.0.0/ARGF.html# Ruby ARGF Documentation]&lt;br /&gt;
&lt;br /&gt;
9. [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html Ruby Documentation for Benchmark Gem]&lt;br /&gt;
&lt;br /&gt;
10. [http://www.pauldix.net/2008/08/serializing-dat.html Serialization Performance Comparison]&lt;br /&gt;
&lt;br /&gt;
11. [http://www.boost.org/doc/libs/1_36_0/libs/serialization/example/demo.cpp Boost Libraries]&lt;br /&gt;
&lt;br /&gt;
12. [http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html Java Serialization]&lt;br /&gt;
&lt;br /&gt;
13. [http://www.w3schools.com/json/ JSON Tutorial]&lt;br /&gt;
&lt;br /&gt;
14. [http://www.skorks.com/2010/04/serializing-and-deserializing-objects-with-ruby/ Serializing and De-serializing in Ruby]&lt;br /&gt;
&lt;br /&gt;
15. [http://www.codeproject.com/Articles/33296/Serialization-and-De-serialization Serialization and De-serialization]&lt;br /&gt;
&lt;br /&gt;
16. [http://msdn.microsoft.com/en-us/library/182eeyhh.aspx XML Serialization]&lt;br /&gt;
&lt;br /&gt;
17. [http://www.tutorialspoint.com/java/java_serialization.htm Java Serialization Tutorial]&lt;br /&gt;
&lt;br /&gt;
18. [http://www.sapphiresteel.com/ruby-programming/The-Book-Of-Ruby The Book of Ruby]&lt;br /&gt;
&lt;br /&gt;
19. [http://www.codeproject.com/Articles/20962/Introducing-Serialization-in-NET Serialization in .Net]&lt;br /&gt;
&lt;br /&gt;
20. [http://ruby.about.com/od/advancedruby/ss/Serialization-In-Ruby-Yaml.html Serialization in Ruby YAML]&lt;br /&gt;
&lt;br /&gt;
21. [http://ruby.about.com/od/tasks/a/The-Json-Gem.html Installing JSON gem]&lt;br /&gt;
&lt;br /&gt;
22. [http://www.ruby-doc.org/stdlib-2.0.0/libdoc/json/rdoc/JSON.html Serialization in Ruby JSON]&lt;/div&gt;</summary>
		<author><name>Mmehta2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1d_mm&amp;diff=83405</id>
		<title>CSC/ECE 517 Spring 2014/ch1a 1d mm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1d_mm&amp;diff=83405"/>
		<updated>2014-02-18T02:26:08Z</updated>

		<summary type="html">&lt;p&gt;Mmehta2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page discusses Serialization, focusing on Ruby programming language. It describes different methods of Serialization in Ruby. In addition, it also compares serialization in different Object Oriented languages.&lt;br /&gt;
&lt;br /&gt;
== '''Serialization''' ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Serialization'''[http://en.wikipedia.org/wiki/Serialization] is a process of converting a data structure or an object into a stream of bytes or string to facilitate storage in memory, file (persistence storage) or transmission over a network. The process of Serialization is also referred to as ''Marshalling''[http://en.wikipedia.org/wiki/Marshalling_%28computer_science%29]. 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. &lt;br /&gt;
&lt;br /&gt;
'''Basic Advantages of Serialization''':&lt;br /&gt;
&lt;br /&gt;
1. Communication between two or more processes on same machine. Object state can be saved and shared in a persistent or in-memory store.&lt;br /&gt;
&lt;br /&gt;
2. Communication between processes on different machines. Serialization facilitates the transmission of an object through a network.&lt;br /&gt;
&lt;br /&gt;
3. Creating a clone of an object. &lt;br /&gt;
&lt;br /&gt;
4. Cross-platform compatibility. Object can be serialized in a common format that is understood by multiple platforms. For example, JSON[http://www.json.org/] and XML.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''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''.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Few Practical Applications for Serialization'''&lt;br /&gt;
&lt;br /&gt;
1. [http://docs.oracle.com/cd/E13222_01/wls/docs81/webapp/sessions.html#139726 HTTP Session] Replication by sharing session objects across web servers for load balancing and handling failover scenarios.&lt;br /&gt;
&lt;br /&gt;
2. Serialization facilitates communication in [http://en.wikipedia.org/wiki/Java_remote_method_invocation Remote Method Invocation] or [http://en.wikipedia.org/wiki/Remote_procedure_call Remote procedure calls]&lt;br /&gt;
&lt;br /&gt;
3. Rails Cookie Handling[http://techbrahmana.blogspot.com/2012/03/rails-cookie-handling-serialization-and.html]. Cookies are stored marshalled/unmarshalled to and from client machines. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:SerializationChart.jpg]]&lt;br /&gt;
&lt;br /&gt;
== '''Serialization in Ruby:''' ==&lt;br /&gt;
&lt;br /&gt;
Ruby provides serialization capabilities through its module, [http://www.ruby-doc.org/core-2.0.0/Marshal.html Marshal]. There are also other libraries like YAML[http://yaml.org/] and JSON which can be used in Ruby to generate serialized objects for purposes like platform independence and human readable formats.&lt;br /&gt;
&lt;br /&gt;
== '''Types of Serialization''' ==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
=== Converting Ruby Objects in Human Readable Formats ===&lt;br /&gt;
The conversion of Ruby objects into YAML and JSON formats are explained below.&lt;br /&gt;
&lt;br /&gt;
==== Converting Ruby Objects to YAML format ====&lt;br /&gt;
&lt;br /&gt;
YAML 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 binary format. 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. In order to use it in Ruby, the yaml.rb file is required to be loaded which provides methods for converting objects into yaml format and creating .yml files.&lt;br /&gt;
&lt;br /&gt;
'''Examples of serialization using YAML:'''&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;nowiki&amp;gt; #Serialization using YAML's to_yaml method&lt;br /&gt;
 &lt;br /&gt;
  require &amp;quot;yaml&amp;quot;&lt;br /&gt;
     class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
	@name = name&lt;br /&gt;
	@age = age&lt;br /&gt;
	@country=country&lt;br /&gt;
     end&lt;br /&gt;
      def to_s&lt;br /&gt;
	&amp;quot;In First:\n#{@name}, #{@age}, #{@country}\n&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
  x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
  puts x&lt;br /&gt;
  puts x.to_yaml&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   In First:&lt;br /&gt;
   Tom, 25, USA&lt;br /&gt;
   --- !ruby/object:First&lt;br /&gt;
   name: Tom&lt;br /&gt;
   age: 25&lt;br /&gt;
   country: USA&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The above code displays the object x, first as a string and then in the yaml format. &lt;br /&gt;
&lt;br /&gt;
'''Saving YAML data into a file:'''&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt; # Serialization using YAML::dump&lt;br /&gt;
&lt;br /&gt;
require 'yaml'&lt;br /&gt;
&lt;br /&gt;
f = File.open( 'first.yml', 'w' )&lt;br /&gt;
YAML.dump( [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;], f )&lt;br /&gt;
f.close&lt;br /&gt;
		&lt;br /&gt;
File.open( 'first.yml' ){ |f|	&lt;br /&gt;
    $arr= YAML.load(f)&lt;br /&gt;
}	&lt;br /&gt;
&lt;br /&gt;
p( $arr )&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;]&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The dump function can be used to serialize the data and save it into a file in YAML format. As shown in the above example the data in YAML format can be de-serialized using the load function.&lt;br /&gt;
&lt;br /&gt;
YAML libraries also provides an option of selecting only those variables of the object that are needed to be serialized. This is done using the to_yaml_properties method as shown in the below example.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;nowiki&amp;gt;  #Custom Serialization using YAML&lt;br /&gt;
 &lt;br /&gt;
  require &amp;quot;yaml&amp;quot;&lt;br /&gt;
     class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
	@name = name&lt;br /&gt;
	@age = age&lt;br /&gt;
	@country=country&lt;br /&gt;
     end&lt;br /&gt;
     def to_s&lt;br /&gt;
	&amp;quot;In First:\n#{@name}, #{@age}, #{@country}\n&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
      def to_yaml_properties&lt;br /&gt;
	   [&amp;quot;@name&amp;quot;,&amp;quot;@age&amp;quot;]  #@country will not be serialized&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
  x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
  puts x&lt;br /&gt;
  puts x.to_yaml&lt;br /&gt;
  &amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   In First:&lt;br /&gt;
   Tom, 25, USA&lt;br /&gt;
   --- !ruby/object:First&lt;br /&gt;
   name: Tom&lt;br /&gt;
   age: 25&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Converting Ruby Objects to JSON format: ====&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
JSON 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. Ruby 1.8.7 distribution is not bundled with json gem. However, in Ruby 1.9.2, the json gem is bundled with the core Ruby distribution.&lt;br /&gt;
The JSON library can be installed using Ruby Gems[http://rubygems.org/] as shown below:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;# gem install json&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A JSON string for serialization can be created by using the JSON.generate method:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
        require 'json'&lt;br /&gt;
        my_hash = {:Welcome =&amp;gt; &amp;quot;Ruby&amp;quot;}&lt;br /&gt;
        puts JSON.generate(my_hash) =&amp;gt; &amp;quot;{\&amp;quot;WELCOME\&amp;quot;:\&amp;quot;RUBY\&amp;quot;}&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
'''Output:'''&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;{&amp;quot;{\&amp;quot;Welcome\&amp;quot;:\&amp;quot;Ruby\&amp;quot;}&amp;quot;=&amp;gt;&amp;quot;{\&amp;quot;WELCOME\&amp;quot;:\&amp;quot;RUBY\&amp;quot;}&amp;quot;}&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A JSON string received from another program can be parsed by using JSON.parse&lt;br /&gt;
Ruby thus converts String to Hash.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
        require 'json'&lt;br /&gt;
        my_hash = JSON.parse('{&amp;quot;Welcome&amp;quot;: &amp;quot;Ruby&amp;quot;}')&lt;br /&gt;
        puts my_hash[&amp;quot;Welcome&amp;quot;] =&amp;gt; &amp;quot;Ruby&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Converting Ruby Objects to Binary Formats ===&lt;br /&gt;
Binary Serialization is another form of serialization in Ruby which is not in human readable form. 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. Binary Serialization  is done using Marshal which is built into Ruby and the code for it is written in Ruby's Marshal module(marshal.c) and thus no additional files are required in order to use it. The _dump and _load methods defined in marshal are used for serialization. Using marshal module the following types of objects can not be serialized: bindings, procedure objects, singleton objects, instances of IO objects and interfaces.&lt;br /&gt;
&lt;br /&gt;
Since the Binary Serialized data is not in human readable form, the following guidelines need to be followed.&lt;br /&gt;
     1.Use print instead of puts[http://ruby-doc.org/core-2.0.0/ARGF.html#] when serialized objects are written to a file in order to avoid new line characters to be written &lt;br /&gt;
       in the file.&lt;br /&gt;
     &lt;br /&gt;
     2.Use a record separator in order to differentiate between two objects.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Binary Serialization Example:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    class Animal&lt;br /&gt;
      def initialize  name, age&lt;br /&gt;
      @name = name&lt;br /&gt;
      @age=age&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
   class Cat &amp;lt; Animal&lt;br /&gt;
    def to_s&lt;br /&gt;
     &amp;quot;In Cat C: #{@name} \t #{@age}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
  c = Cat.new(&amp;quot;Kitty Kat&amp;quot;,5)&lt;br /&gt;
  puts &amp;quot;Before Serialization&amp;quot;&lt;br /&gt;
  puts c&lt;br /&gt;
  #puts d&lt;br /&gt;
  serialize_cat= Marshal.dump(c) #dumps the serialized cat object into serialize_cat&lt;br /&gt;
  puts &amp;quot;\nAfter Serialization:\n #{serialize_cat}&amp;quot;&lt;br /&gt;
  deserialize_cat= Marshal::load(serialize_cat) #deserializes the cat object and loads it back into deserialize_cat&lt;br /&gt;
  puts &amp;quot;\nAfter Deserialization\n #{deserialize_cat}&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   Before Serialization&lt;br /&gt;
   In Cat C: Kitty Kat 	 5&lt;br /&gt;
   After Serialization:&lt;br /&gt;
   oCat:&lt;br /&gt;
   @nameI&amp;quot;Kitty Kat:ET:	@agei&lt;br /&gt;
   After Deserialization&lt;br /&gt;
   In Cat C: Kitty Kat 	 5&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Similar to YAML, Marshal can also be used to dump data into a file. The above example showing the serialization using YAML::dump can be written using marshal as shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    f = File.open( 'first.yml', 'w' )&lt;br /&gt;
    Marshal.dump( [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;], f )&lt;br /&gt;
    f.close	&lt;br /&gt;
    File.open( 'first.yml' ){ |f|	&lt;br /&gt;
    $arr= Marshal.load(f)&lt;br /&gt;
    }	&lt;br /&gt;
    p( $arr )&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;]&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice that in the above example there is no &amp;quot;require&amp;quot; statement as opposed to the earlier example of writing serialized data into files using YAML. This is because unlike YAML, marshal is built-in Ruby and  no external libraries is required in order to use its functionality.&lt;br /&gt;
&lt;br /&gt;
Marshal can also be used to custom serialize an object i.e. it provides an option to omit the variables of an object that are not required in the serialized data. The following program shows the use of marshal_dump method for achieving custom serialization.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
      class First&lt;br /&gt;
      def initialize(name, age, country)&lt;br /&gt;
         @name = name&lt;br /&gt;
         @age = age&lt;br /&gt;
         @country=country&lt;br /&gt;
      end&lt;br /&gt;
      def to_s&lt;br /&gt;
            &amp;quot;In First: #{@name}, #{@age}, #{@country}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
      def marshal_dump&lt;br /&gt;
            [@name,@age]  #@country will not be serialized&lt;br /&gt;
      end&lt;br /&gt;
      def marshal_load(data)&lt;br /&gt;
       @name=data[0]&lt;br /&gt;
       @age=data[1]&lt;br /&gt;
       @country=&amp;quot;United States of America&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
      end&lt;br /&gt;
 &lt;br /&gt;
      x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
      puts x&lt;br /&gt;
  &lt;br /&gt;
      marshal_data = Marshal.dump( x ) &lt;br /&gt;
      y = Marshal.load( marshal_data ) &lt;br /&gt;
      p( y.to_s )&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    In First: Tom, 25, USA&lt;br /&gt;
    &amp;quot;In First: Tom, 25, United States of America&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== '''Serialization Performance in Ruby''' ==&lt;br /&gt;
The different serialization formats described above differ in the efficiency at which they can serialize and deserialize data and thus while serializing large amount of data their efficiency is taken into account. The .report[http://www.ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html] method in Ruby can be used to evaluate the performance of these serialization patterns. Marshal, as it handles data in binary format, is the most efficient form of serialization in Ruby. The following example compares the performance of marshal, JSON and YAML format.[http://www.pauldix.net/2008/08/serializing-dat.html]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   require 'benchmark'&lt;br /&gt;
   require 'rubygems'&lt;br /&gt;
   require 'json'&lt;br /&gt;
   require 'yaml'&lt;br /&gt;
   include Benchmark&lt;br /&gt;
   class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
      @name=name&lt;br /&gt;
      @age=age&lt;br /&gt;
      @country=country&lt;br /&gt;
     end&lt;br /&gt;
   end  &lt;br /&gt;
    x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
    puts &amp;quot;            User     System     Total      Real&amp;quot;&lt;br /&gt;
   benchmark do |t|&lt;br /&gt;
    print &amp;quot;Marshal:&amp;quot;&lt;br /&gt;
    t.report{1000.times do; Marshal.load(Marshal.dump(x));end}&lt;br /&gt;
    print &amp;quot;JSON:   &amp;quot;&lt;br /&gt;
    t.report{1000.times do; JSON.load(JSON.dump(x));end}&lt;br /&gt;
    print &amp;quot;YAML:   &amp;quot;&lt;br /&gt;
    t.report{1000.times do; YAML.load(YAML.dump(x));end}&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
              User     System     Total      Real&lt;br /&gt;
  Marshal:   0.010000   0.000000   0.010000 (  0.015408)&lt;br /&gt;
  JSON:      0.020000   0.010000   0.030000 (  0.022462)&lt;br /&gt;
  YAML:      0.460000   0.010000   0.470000 (  0.472149)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output of the above example shows that, given large chunks of data, marshal is comparatively more efficient format for serialization in Ruby.&lt;br /&gt;
&lt;br /&gt;
== Serialization in Object-Oriented Languages: Comparison ==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Sl.No !! Ruby !! Java !! .Net Framework !! C++&lt;br /&gt;
|-&lt;br /&gt;
| 1 || Ruby provides a built in module called 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 || Although, there is no built in support for serialization in C++, it can be achieved by using Boost libraries[http://www.boost.org/doc/libs/1_36_0/libs/serialization/example/demo.cpp]&lt;br /&gt;
|-&lt;br /&gt;
| 2 || The built in module of Ruby (Marshal) does not support platform independence, however, it can be achieved by using external libraries like YAML and JSON || Similarly, Java's built in serialization is also not platform independent and in order to use serialization in Java across Ruby platform, jruby library should be used.  || .Net used Remoting technology to make it platform independent. || Serialization using the Boost libraries is not platform independent.&lt;br /&gt;
|-&lt;br /&gt;
| 3 || YAML provides a method [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/syck/rdoc/Object.html (to_yaml_properties)] which can be used to select the variables who's value is need to be serialized. With Marshal, we need to write a method named marshal_dump defining the variables of an object that has to be serialized. || Provides an option for serializing only the required 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 data 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 || Serialization using the Boost libraries is custom and thus the user can specify the part of the objects to be serialized.&lt;br /&gt;
|-&lt;br /&gt;
| 4 || Bindings, procedure objects, singleton objects, instances of IO objects and interfaces can not be serialized. Serializing these objects throws TypeError exceptions. || Thread, OutputStream and its subclasses, and Socket are not serializable in Java.[http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html] || Objects like DataRow are non serializable in .NET. Whether an object is serializable or not can be checked using the [http://msdn.microsoft.com/en-us/library/system.type.isserializable(v=vs.110).aspx?cs-save-lang=1&amp;amp;cs-lang=cpp#code-snippet-1 Type.IsSerializable] || There are no such objects in C++.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== See Also==&lt;br /&gt;
&lt;br /&gt;
1. [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/json/rdoc/JSON.html Serialization in Ruby JSON]&lt;br /&gt;
&lt;br /&gt;
2. [http://api.rubyonrails.org/classes/ActiveModel/Serialization.html Serialization in Rails]&lt;br /&gt;
&lt;br /&gt;
3. [http://gregmoreno.wordpress.com/2011/01/27/preventing-model-explosion-via-rails-serialization/ Article on Rails Serialization]&lt;br /&gt;
&lt;br /&gt;
4. [http://en.wikipedia.org/wiki/Google_Protocol_Buffers Protocol Buffers]&lt;br /&gt;
&lt;br /&gt;
5. [https://rubygems.org/gems/ruby-protocol-buffers Ruby Protocol Buffers Gem]&lt;br /&gt;
&lt;br /&gt;
6. [http://en.wikipedia.org/wiki/Apache_Avro Avro]&lt;br /&gt;
&lt;br /&gt;
7. [http://rubygems.org/gems/avro Ruby Avro Gem]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
1. [http://en.wikipedia.org/wiki/Serialization Serilization in General]&lt;br /&gt;
&lt;br /&gt;
2. [http://en.wikipedia.org/wiki/Marshalling_%28computer_science%29 Marshaling]&lt;br /&gt;
&lt;br /&gt;
3. [http://techbrahmana.blogspot.com/2012/03/rails-cookie-handling-serialization-and.html Rails Cookie Handling]&lt;br /&gt;
&lt;br /&gt;
4. [http://yaml.org/ YAML]&lt;br /&gt;
&lt;br /&gt;
5. [http://www.ruby-doc.org/core-2.1.0/Marshal.html Marshal module]&lt;br /&gt;
&lt;br /&gt;
6. [http://json.org/ JSON]&lt;br /&gt;
&lt;br /&gt;
7. [http://rubygems.org/ Ruby Gems]&lt;br /&gt;
&lt;br /&gt;
8. [http://ruby-doc.org/core-2.0.0/ARGF.html# Ruby ARGF Documentation]&lt;br /&gt;
&lt;br /&gt;
9. [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html Ruby Documentation for Benchmark Gem]&lt;br /&gt;
&lt;br /&gt;
10. [http://www.pauldix.net/2008/08/serializing-dat.html Serialization Performance Comparison]&lt;br /&gt;
&lt;br /&gt;
11. [http://www.boost.org/doc/libs/1_36_0/libs/serialization/example/demo.cpp Boost Libraries]&lt;br /&gt;
&lt;br /&gt;
12. [http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html Java Serialization]&lt;br /&gt;
&lt;br /&gt;
13. [http://www.w3schools.com/json/ JSON Tutorial]&lt;br /&gt;
&lt;br /&gt;
14. [http://www.skorks.com/2010/04/serializing-and-deserializing-objects-with-ruby/ Serializing and De-serializing in Ruby]&lt;br /&gt;
&lt;br /&gt;
15. [http://www.codeproject.com/Articles/33296/Serialization-and-De-serialization Serialization and De-serialization]&lt;br /&gt;
&lt;br /&gt;
16. [http://msdn.microsoft.com/en-us/library/182eeyhh.aspx XML Serialization]&lt;br /&gt;
&lt;br /&gt;
17. [http://www.tutorialspoint.com/java/java_serialization.htm Java Serialization Tutorial]&lt;br /&gt;
&lt;br /&gt;
18. [http://www.sapphiresteel.com/ruby-programming/The-Book-Of-Ruby The Book of Ruby]&lt;br /&gt;
&lt;br /&gt;
19. [http://www.codeproject.com/Articles/20962/Introducing-Serialization-in-NET Serialization in .Net]&lt;br /&gt;
&lt;br /&gt;
20. [http://ruby.about.com/od/advancedruby/ss/Serialization-In-Ruby-Yaml.html Serialization in Ruby YAML]&lt;br /&gt;
&lt;br /&gt;
21. [http://ruby.about.com/od/tasks/a/The-Json-Gem.html Installing JSON gem]&lt;br /&gt;
&lt;br /&gt;
22. [http://www.ruby-doc.org/stdlib-2.0.0/libdoc/json/rdoc/JSON.html Serialization in Ruby JSON]&lt;/div&gt;</summary>
		<author><name>Mmehta2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1d_mm&amp;diff=83404</id>
		<title>CSC/ECE 517 Spring 2014/ch1a 1d mm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1d_mm&amp;diff=83404"/>
		<updated>2014-02-18T02:06:58Z</updated>

		<summary type="html">&lt;p&gt;Mmehta2: /* Serialization in Object-Oriented Languages: Comparison */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== '''Serialization''' ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Serialization'''[http://en.wikipedia.org/wiki/Serialization] is a process of converting a data structure or an object into a stream of bytes or string to facilitate storage in memory, file (persistence storage) or transmission over a network. The process of Serialization is also referred to as ''Marshalling''[http://en.wikipedia.org/wiki/Marshalling_%28computer_science%29]. 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. &lt;br /&gt;
&lt;br /&gt;
'''Basic Advantages of Serialization''':&lt;br /&gt;
&lt;br /&gt;
1. Communication between two or more processes on same machine. Object state can be saved and shared in a persistent or in-memory store.&lt;br /&gt;
&lt;br /&gt;
2. Communication between processes on different machines. Serialization facilitates the transmission of an object through a network.&lt;br /&gt;
&lt;br /&gt;
3. Creating a clone of an object. &lt;br /&gt;
&lt;br /&gt;
4. Cross-platform compatibility. Object can be serialized in a common format that is understood by multiple platforms. For example, JSON[http://www.json.org/] and XML.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''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''.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Few Practical Applications for Serialization'''&lt;br /&gt;
&lt;br /&gt;
1. [http://docs.oracle.com/cd/E13222_01/wls/docs81/webapp/sessions.html#139726 HTTP Session] Replication by sharing session objects across web servers for load balancing and handling failover scenarios.&lt;br /&gt;
&lt;br /&gt;
2. Serialization facilitates communication in [http://en.wikipedia.org/wiki/Java_remote_method_invocation Remote Method Invocation] or [http://en.wikipedia.org/wiki/Remote_procedure_call Remote procedure calls]&lt;br /&gt;
&lt;br /&gt;
3. Rails Cookie Handling[http://techbrahmana.blogspot.com/2012/03/rails-cookie-handling-serialization-and.html]. Cookies are stored marshalled/unmarshalled to and from client machines. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:SerializationChart.jpg]]&lt;br /&gt;
&lt;br /&gt;
== '''Serialization in Ruby:''' ==&lt;br /&gt;
&lt;br /&gt;
Ruby provides serialization capabilities through its module, [http://www.ruby-doc.org/core-2.0.0/Marshal.html Marshal]. There are also other libraries like YAML[http://yaml.org/] and JSON which can be used in Ruby to generate serialized objects for purposes like platform independence and human readable formats.&lt;br /&gt;
&lt;br /&gt;
== '''Types of Serialization''' ==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
=== Converting Ruby Objects in Human Readable Formats ===&lt;br /&gt;
The conversion of Ruby objects into YAML and JSON formats are explained below.&lt;br /&gt;
&lt;br /&gt;
==== Converting Ruby Objects to YAML format ====&lt;br /&gt;
&lt;br /&gt;
YAML 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 binary format. 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. In order to use it in Ruby, the yaml.rb file is required to be loaded which provides methods for converting objects into yaml format and creating .yml files.&lt;br /&gt;
&lt;br /&gt;
'''Examples of serialization using YAML:'''&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;nowiki&amp;gt; #Serialization using YAML's to_yaml method&lt;br /&gt;
 &lt;br /&gt;
  require &amp;quot;yaml&amp;quot;&lt;br /&gt;
     class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
	@name = name&lt;br /&gt;
	@age = age&lt;br /&gt;
	@country=country&lt;br /&gt;
     end&lt;br /&gt;
      def to_s&lt;br /&gt;
	&amp;quot;In First:\n#{@name}, #{@age}, #{@country}\n&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
  x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
  puts x&lt;br /&gt;
  puts x.to_yaml&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   In First:&lt;br /&gt;
   Tom, 25, USA&lt;br /&gt;
   --- !ruby/object:First&lt;br /&gt;
   name: Tom&lt;br /&gt;
   age: 25&lt;br /&gt;
   country: USA&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The above code displays the object x, first as a string and then in the yaml format. &lt;br /&gt;
&lt;br /&gt;
'''Saving YAML data into a file:'''&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt; # Serialization using YAML::dump&lt;br /&gt;
&lt;br /&gt;
require 'yaml'&lt;br /&gt;
&lt;br /&gt;
f = File.open( 'first.yml', 'w' )&lt;br /&gt;
YAML.dump( [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;], f )&lt;br /&gt;
f.close&lt;br /&gt;
		&lt;br /&gt;
File.open( 'first.yml' ){ |f|	&lt;br /&gt;
    $arr= YAML.load(f)&lt;br /&gt;
}	&lt;br /&gt;
&lt;br /&gt;
p( $arr )&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;]&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The dump function can be used to serialize the data and save it into a file in YAML format. As shown in the above example the data in YAML format can be de-serialized using the load function.&lt;br /&gt;
&lt;br /&gt;
YAML libraries also provides an option of selecting only those variables of the object that are needed to be serialized. This is done using the to_yaml_properties method as shown in the below example.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;nowiki&amp;gt;  #Custom Serialization using YAML&lt;br /&gt;
 &lt;br /&gt;
  require &amp;quot;yaml&amp;quot;&lt;br /&gt;
     class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
	@name = name&lt;br /&gt;
	@age = age&lt;br /&gt;
	@country=country&lt;br /&gt;
     end&lt;br /&gt;
     def to_s&lt;br /&gt;
	&amp;quot;In First:\n#{@name}, #{@age}, #{@country}\n&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
      def to_yaml_properties&lt;br /&gt;
	   [&amp;quot;@name&amp;quot;,&amp;quot;@age&amp;quot;]  #@country will not be serialized&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
  x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
  puts x&lt;br /&gt;
  puts x.to_yaml&lt;br /&gt;
  &amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   In First:&lt;br /&gt;
   Tom, 25, USA&lt;br /&gt;
   --- !ruby/object:First&lt;br /&gt;
   name: Tom&lt;br /&gt;
   age: 25&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Converting Ruby Objects to JSON format: ====&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
JSON 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. Ruby 1.8.7 distribution is not bundled with json gem. However, in Ruby 1.9.2, the json gem is bundled with the core Ruby distribution.&lt;br /&gt;
The JSON library can be installed using Ruby Gems[http://rubygems.org/] as shown below:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;# gem install json&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A JSON string for serialization can be created by using the JSON.generate method:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
        require 'json'&lt;br /&gt;
        my_hash = {:Welcome =&amp;gt; &amp;quot;Ruby&amp;quot;}&lt;br /&gt;
        puts JSON.generate(my_hash) =&amp;gt; &amp;quot;{\&amp;quot;WELCOME\&amp;quot;:\&amp;quot;RUBY\&amp;quot;}&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
'''Output:'''&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;{&amp;quot;{\&amp;quot;Welcome\&amp;quot;:\&amp;quot;Ruby\&amp;quot;}&amp;quot;=&amp;gt;&amp;quot;{\&amp;quot;WELCOME\&amp;quot;:\&amp;quot;RUBY\&amp;quot;}&amp;quot;}&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A JSON string received from another program can be parsed by using JSON.parse&lt;br /&gt;
Ruby thus converts String to Hash.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
        require 'json'&lt;br /&gt;
        my_hash = JSON.parse('{&amp;quot;Welcome&amp;quot;: &amp;quot;Ruby&amp;quot;}')&lt;br /&gt;
        puts my_hash[&amp;quot;Welcome&amp;quot;] =&amp;gt; &amp;quot;Ruby&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Converting Ruby Objects to Binary Formats ===&lt;br /&gt;
Binary Serialization is another form of serialization in Ruby which is not in human readable form. 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. Binary Serialization  is done using Marshal which is built into Ruby and the code for it is written in Ruby's Marshal module(marshal.c) and thus no additional files are required in order to use it. The _dump and _load methods defined in marshal are used for serialization. Using marshal module the following types of objects can not be serialized: bindings, procedure objects, singleton objects, instances of IO objects and interfaces.&lt;br /&gt;
&lt;br /&gt;
Since the Binary Serialized data is not in human readable form, the following guidelines need to be followed.&lt;br /&gt;
     1.Use print instead of puts[http://ruby-doc.org/core-2.0.0/ARGF.html#] when serialized objects are written to a file in order to avoid new line characters to be written &lt;br /&gt;
       in the file.&lt;br /&gt;
     &lt;br /&gt;
     2.Use a record separator in order to differentiate between two objects.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Binary Serialization Example:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    class Animal&lt;br /&gt;
      def initialize  name, age&lt;br /&gt;
      @name = name&lt;br /&gt;
      @age=age&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
   class Cat &amp;lt; Animal&lt;br /&gt;
    def to_s&lt;br /&gt;
     &amp;quot;In Cat C: #{@name} \t #{@age}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
  c = Cat.new(&amp;quot;Kitty Kat&amp;quot;,5)&lt;br /&gt;
  puts &amp;quot;Before Serialization&amp;quot;&lt;br /&gt;
  puts c&lt;br /&gt;
  #puts d&lt;br /&gt;
  serialize_cat= Marshal.dump(c) #dumps the serialized cat object into serialize_cat&lt;br /&gt;
  puts &amp;quot;\nAfter Serialization:\n #{serialize_cat}&amp;quot;&lt;br /&gt;
  deserialize_cat= Marshal::load(serialize_cat) #deserializes the cat object and loads it back into deserialize_cat&lt;br /&gt;
  puts &amp;quot;\nAfter Deserialization\n #{deserialize_cat}&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   Before Serialization&lt;br /&gt;
   In Cat C: Kitty Kat 	 5&lt;br /&gt;
   After Serialization:&lt;br /&gt;
   oCat:&lt;br /&gt;
   @nameI&amp;quot;Kitty Kat:ET:	@agei&lt;br /&gt;
   After Deserialization&lt;br /&gt;
   In Cat C: Kitty Kat 	 5&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Similar to YAML, Marshal can also be used to dump data into a file. The above example showing the serialization using YAML::dump can be written using marshal as shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    f = File.open( 'first.yml', 'w' )&lt;br /&gt;
    Marshal.dump( [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;], f )&lt;br /&gt;
    f.close	&lt;br /&gt;
    File.open( 'first.yml' ){ |f|	&lt;br /&gt;
    $arr= Marshal.load(f)&lt;br /&gt;
    }	&lt;br /&gt;
    p( $arr )&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;]&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice that in the above example there is no &amp;quot;require&amp;quot; statement as opposed to the earlier example of writing serialized data into files using YAML. This is because unlike YAML, marshal is built-in Ruby and  no external libraries is required in order to use its functionality.&lt;br /&gt;
&lt;br /&gt;
Marshal can also be used to custom serialize an object i.e. it provides an option to omit the variables of an object that are not required in the serialized data. The following program shows the use of marshal_dump method for achieving custom serialization.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
      class First&lt;br /&gt;
      def initialize(name, age, country)&lt;br /&gt;
         @name = name&lt;br /&gt;
         @age = age&lt;br /&gt;
         @country=country&lt;br /&gt;
      end&lt;br /&gt;
      def to_s&lt;br /&gt;
            &amp;quot;In First: #{@name}, #{@age}, #{@country}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
      def marshal_dump&lt;br /&gt;
            [@name,@age]  #@country will not be serialized&lt;br /&gt;
      end&lt;br /&gt;
      def marshal_load(data)&lt;br /&gt;
       @name=data[0]&lt;br /&gt;
       @age=data[1]&lt;br /&gt;
       @country=&amp;quot;United States of America&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
      end&lt;br /&gt;
 &lt;br /&gt;
      x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
      puts x&lt;br /&gt;
  &lt;br /&gt;
      marshal_data = Marshal.dump( x ) &lt;br /&gt;
      y = Marshal.load( marshal_data ) &lt;br /&gt;
      p( y.to_s )&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    In First: Tom, 25, USA&lt;br /&gt;
    &amp;quot;In First: Tom, 25, United States of America&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== '''Serialization Performance in Ruby''' ==&lt;br /&gt;
The different serialization formats described above differ in the efficiency at which they can serialize and deserialize data and thus while serializing large amount of data their efficiency is taken into account. The .report[http://www.ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html] method in Ruby can be used to evaluate the performance of these serialization patterns. Marshal, as it handles data in binary format, is the most efficient form of serialization in Ruby. The following example compares the performance of marshal, JSON and YAML format.[http://www.pauldix.net/2008/08/serializing-dat.html]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   require 'benchmark'&lt;br /&gt;
   require 'rubygems'&lt;br /&gt;
   require 'json'&lt;br /&gt;
   require 'yaml'&lt;br /&gt;
   include Benchmark&lt;br /&gt;
   class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
      @name=name&lt;br /&gt;
      @age=age&lt;br /&gt;
      @country=country&lt;br /&gt;
     end&lt;br /&gt;
   end  &lt;br /&gt;
    x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
    puts &amp;quot;            User     System     Total      Real&amp;quot;&lt;br /&gt;
   benchmark do |t|&lt;br /&gt;
    print &amp;quot;Marshal:&amp;quot;&lt;br /&gt;
    t.report{1000.times do; Marshal.load(Marshal.dump(x));end}&lt;br /&gt;
    print &amp;quot;JSON:   &amp;quot;&lt;br /&gt;
    t.report{1000.times do; JSON.load(JSON.dump(x));end}&lt;br /&gt;
    print &amp;quot;YAML:   &amp;quot;&lt;br /&gt;
    t.report{1000.times do; YAML.load(YAML.dump(x));end}&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
              User     System     Total      Real&lt;br /&gt;
  Marshal:   0.010000   0.000000   0.010000 (  0.015408)&lt;br /&gt;
  JSON:      0.020000   0.010000   0.030000 (  0.022462)&lt;br /&gt;
  YAML:      0.460000   0.010000   0.470000 (  0.472149)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output of the above example shows that, given large chunks of data, marshal is comparatively more efficient format for serialization in Ruby.&lt;br /&gt;
&lt;br /&gt;
== Serialization in Object-Oriented Languages: Comparison ==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Sl.No !! Ruby !! Java !! .Net Framework !! C++&lt;br /&gt;
|-&lt;br /&gt;
| 1 || Ruby provides a built in module called 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 || Although, there is no built in support for serialization in C++, it can be achieved by using Boost libraries[http://www.boost.org/doc/libs/1_36_0/libs/serialization/example/demo.cpp]&lt;br /&gt;
|-&lt;br /&gt;
| 2 || The built in module of Ruby (Marshal) does not support platform independence, however, it can be achieved by using external libraries like YAML and JSON || Similarly, Java's built in serialization is also not platform independent and in order to use serialization in Java across Ruby platform, jruby library should be used.  || .Net used Remoting technology to make it platform independent. || Serialization using the Boost libraries is not platform independent.&lt;br /&gt;
|-&lt;br /&gt;
| 3 || YAML provides a method [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/syck/rdoc/Object.html (to_yaml_properties)] which can be used to select the variables who's value is need to be serialized. With Marshal, we need to write a method named marshal_dump defining the variables of an object that has to be serialized. || Provides an option for serializing only the required 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 data 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 || Serialization using the Boost libraries is custom and thus the user can specify the part of the objects to be serialized.&lt;br /&gt;
|-&lt;br /&gt;
| 4 || Bindings, procedure objects, singleton objects, instances of IO objects and interfaces can not be serialized. Serializing these objects throws TypeError exceptions. || Thread, OutputStream and its subclasses, and Socket are not serializable in Java.[http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html] || Objects like DataRow are non serializable in .NET. Whether an object is serializable or not can be checked using the [http://msdn.microsoft.com/en-us/library/system.type.isserializable(v=vs.110).aspx?cs-save-lang=1&amp;amp;cs-lang=cpp#code-snippet-1 Type.IsSerializable] || There are no such objects in C++.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== See Also==&lt;br /&gt;
&lt;br /&gt;
1. [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/json/rdoc/JSON.html Serialization in Ruby JSON]&lt;br /&gt;
&lt;br /&gt;
2. [http://api.rubyonrails.org/classes/ActiveModel/Serialization.html Serialization in Rails]&lt;br /&gt;
&lt;br /&gt;
3. [http://gregmoreno.wordpress.com/2011/01/27/preventing-model-explosion-via-rails-serialization/ Article on Rails Serialization]&lt;br /&gt;
&lt;br /&gt;
4. [http://en.wikipedia.org/wiki/Google_Protocol_Buffers Protocol Buffers]&lt;br /&gt;
&lt;br /&gt;
5. [https://rubygems.org/gems/ruby-protocol-buffers Ruby Protocol Buffers Gem]&lt;br /&gt;
&lt;br /&gt;
6. [http://en.wikipedia.org/wiki/Apache_Avro Avro]&lt;br /&gt;
&lt;br /&gt;
7. [http://rubygems.org/gems/avro Ruby Avro Gem]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
1. [http://en.wikipedia.org/wiki/Serialization Serilization in General]&lt;br /&gt;
&lt;br /&gt;
2. [http://en.wikipedia.org/wiki/Marshalling_%28computer_science%29 Marshaling]&lt;br /&gt;
&lt;br /&gt;
3. [http://techbrahmana.blogspot.com/2012/03/rails-cookie-handling-serialization-and.html Rails Cookie Handling]&lt;br /&gt;
&lt;br /&gt;
4. [http://yaml.org/ YAML]&lt;br /&gt;
&lt;br /&gt;
5. [http://www.ruby-doc.org/core-2.1.0/Marshal.html Marshal module]&lt;br /&gt;
&lt;br /&gt;
6. [http://json.org/ JSON]&lt;br /&gt;
&lt;br /&gt;
7. [http://rubygems.org/ Ruby Gems]&lt;br /&gt;
&lt;br /&gt;
8. [http://ruby-doc.org/core-2.0.0/ARGF.html# Ruby ARGF Documentation]&lt;br /&gt;
&lt;br /&gt;
9. [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html Ruby Documentation for Benchmark Gem]&lt;br /&gt;
&lt;br /&gt;
10. [http://www.pauldix.net/2008/08/serializing-dat.html Serialization Performance Comparison]&lt;br /&gt;
&lt;br /&gt;
11. [http://www.boost.org/doc/libs/1_36_0/libs/serialization/example/demo.cpp Boost Libraries]&lt;br /&gt;
&lt;br /&gt;
12. [http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html Java Serialization]&lt;br /&gt;
&lt;br /&gt;
13. [http://www.w3schools.com/json/ JSON Tutorial]&lt;br /&gt;
&lt;br /&gt;
14. [http://www.skorks.com/2010/04/serializing-and-deserializing-objects-with-ruby/ Serializing and De-serializing in Ruby]&lt;br /&gt;
&lt;br /&gt;
15. [http://www.codeproject.com/Articles/33296/Serialization-and-De-serialization Serialization and De-serialization]&lt;br /&gt;
&lt;br /&gt;
16. [http://msdn.microsoft.com/en-us/library/182eeyhh.aspx XML Serialization]&lt;br /&gt;
&lt;br /&gt;
17. [http://www.tutorialspoint.com/java/java_serialization.htm Java Serialization Tutorial]&lt;br /&gt;
&lt;br /&gt;
18. [http://www.sapphiresteel.com/ruby-programming/The-Book-Of-Ruby The Book of Ruby]&lt;br /&gt;
&lt;br /&gt;
19. [http://www.codeproject.com/Articles/20962/Introducing-Serialization-in-NET Serialization in .Net]&lt;br /&gt;
&lt;br /&gt;
20. [http://ruby.about.com/od/advancedruby/ss/Serialization-In-Ruby-Yaml.html Serialization in Ruby YAML]&lt;br /&gt;
&lt;br /&gt;
21. [http://ruby.about.com/od/tasks/a/The-Json-Gem.html Installing JSON gem]&lt;br /&gt;
&lt;br /&gt;
22. [http://www.ruby-doc.org/stdlib-2.0.0/libdoc/json/rdoc/JSON.html Serialization in Ruby JSON]&lt;/div&gt;</summary>
		<author><name>Mmehta2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1d_mm&amp;diff=83403</id>
		<title>CSC/ECE 517 Spring 2014/ch1a 1d mm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1d_mm&amp;diff=83403"/>
		<updated>2014-02-18T02:04:22Z</updated>

		<summary type="html">&lt;p&gt;Mmehta2: /* Serialization */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== '''Serialization''' ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Serialization'''[http://en.wikipedia.org/wiki/Serialization] is a process of converting a data structure or an object into a stream of bytes or string to facilitate storage in memory, file (persistence storage) or transmission over a network. The process of Serialization is also referred to as ''Marshalling''[http://en.wikipedia.org/wiki/Marshalling_%28computer_science%29]. 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. &lt;br /&gt;
&lt;br /&gt;
'''Basic Advantages of Serialization''':&lt;br /&gt;
&lt;br /&gt;
1. Communication between two or more processes on same machine. Object state can be saved and shared in a persistent or in-memory store.&lt;br /&gt;
&lt;br /&gt;
2. Communication between processes on different machines. Serialization facilitates the transmission of an object through a network.&lt;br /&gt;
&lt;br /&gt;
3. Creating a clone of an object. &lt;br /&gt;
&lt;br /&gt;
4. Cross-platform compatibility. Object can be serialized in a common format that is understood by multiple platforms. For example, JSON[http://www.json.org/] and XML.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''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''.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Few Practical Applications for Serialization'''&lt;br /&gt;
&lt;br /&gt;
1. [http://docs.oracle.com/cd/E13222_01/wls/docs81/webapp/sessions.html#139726 HTTP Session] Replication by sharing session objects across web servers for load balancing and handling failover scenarios.&lt;br /&gt;
&lt;br /&gt;
2. Serialization facilitates communication in [http://en.wikipedia.org/wiki/Java_remote_method_invocation Remote Method Invocation] or [http://en.wikipedia.org/wiki/Remote_procedure_call Remote procedure calls]&lt;br /&gt;
&lt;br /&gt;
3. Rails Cookie Handling[http://techbrahmana.blogspot.com/2012/03/rails-cookie-handling-serialization-and.html]. Cookies are stored marshalled/unmarshalled to and from client machines. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:SerializationChart.jpg]]&lt;br /&gt;
&lt;br /&gt;
== '''Serialization in Ruby:''' ==&lt;br /&gt;
&lt;br /&gt;
Ruby provides serialization capabilities through its module, [http://www.ruby-doc.org/core-2.0.0/Marshal.html Marshal]. There are also other libraries like YAML[http://yaml.org/] and JSON which can be used in Ruby to generate serialized objects for purposes like platform independence and human readable formats.&lt;br /&gt;
&lt;br /&gt;
== '''Types of Serialization''' ==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
=== Converting Ruby Objects in Human Readable Formats ===&lt;br /&gt;
The conversion of Ruby objects into YAML and JSON formats are explained below.&lt;br /&gt;
&lt;br /&gt;
==== Converting Ruby Objects to YAML format ====&lt;br /&gt;
&lt;br /&gt;
YAML 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 binary format. 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. In order to use it in Ruby, the yaml.rb file is required to be loaded which provides methods for converting objects into yaml format and creating .yml files.&lt;br /&gt;
&lt;br /&gt;
'''Examples of serialization using YAML:'''&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;nowiki&amp;gt; #Serialization using YAML's to_yaml method&lt;br /&gt;
 &lt;br /&gt;
  require &amp;quot;yaml&amp;quot;&lt;br /&gt;
     class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
	@name = name&lt;br /&gt;
	@age = age&lt;br /&gt;
	@country=country&lt;br /&gt;
     end&lt;br /&gt;
      def to_s&lt;br /&gt;
	&amp;quot;In First:\n#{@name}, #{@age}, #{@country}\n&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
  x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
  puts x&lt;br /&gt;
  puts x.to_yaml&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   In First:&lt;br /&gt;
   Tom, 25, USA&lt;br /&gt;
   --- !ruby/object:First&lt;br /&gt;
   name: Tom&lt;br /&gt;
   age: 25&lt;br /&gt;
   country: USA&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The above code displays the object x, first as a string and then in the yaml format. &lt;br /&gt;
&lt;br /&gt;
'''Saving YAML data into a file:'''&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt; # Serialization using YAML::dump&lt;br /&gt;
&lt;br /&gt;
require 'yaml'&lt;br /&gt;
&lt;br /&gt;
f = File.open( 'first.yml', 'w' )&lt;br /&gt;
YAML.dump( [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;], f )&lt;br /&gt;
f.close&lt;br /&gt;
		&lt;br /&gt;
File.open( 'first.yml' ){ |f|	&lt;br /&gt;
    $arr= YAML.load(f)&lt;br /&gt;
}	&lt;br /&gt;
&lt;br /&gt;
p( $arr )&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;]&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The dump function can be used to serialize the data and save it into a file in YAML format. As shown in the above example the data in YAML format can be de-serialized using the load function.&lt;br /&gt;
&lt;br /&gt;
YAML libraries also provides an option of selecting only those variables of the object that are needed to be serialized. This is done using the to_yaml_properties method as shown in the below example.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;nowiki&amp;gt;  #Custom Serialization using YAML&lt;br /&gt;
 &lt;br /&gt;
  require &amp;quot;yaml&amp;quot;&lt;br /&gt;
     class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
	@name = name&lt;br /&gt;
	@age = age&lt;br /&gt;
	@country=country&lt;br /&gt;
     end&lt;br /&gt;
     def to_s&lt;br /&gt;
	&amp;quot;In First:\n#{@name}, #{@age}, #{@country}\n&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
      def to_yaml_properties&lt;br /&gt;
	   [&amp;quot;@name&amp;quot;,&amp;quot;@age&amp;quot;]  #@country will not be serialized&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
  x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
  puts x&lt;br /&gt;
  puts x.to_yaml&lt;br /&gt;
  &amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   In First:&lt;br /&gt;
   Tom, 25, USA&lt;br /&gt;
   --- !ruby/object:First&lt;br /&gt;
   name: Tom&lt;br /&gt;
   age: 25&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Converting Ruby Objects to JSON format: ====&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
JSON 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. Ruby 1.8.7 distribution is not bundled with json gem. However, in Ruby 1.9.2, the json gem is bundled with the core Ruby distribution.&lt;br /&gt;
The JSON library can be installed using Ruby Gems[http://rubygems.org/] as shown below:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;# gem install json&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A JSON string for serialization can be created by using the JSON.generate method:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
        require 'json'&lt;br /&gt;
        my_hash = {:Welcome =&amp;gt; &amp;quot;Ruby&amp;quot;}&lt;br /&gt;
        puts JSON.generate(my_hash) =&amp;gt; &amp;quot;{\&amp;quot;WELCOME\&amp;quot;:\&amp;quot;RUBY\&amp;quot;}&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
'''Output:'''&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;{&amp;quot;{\&amp;quot;Welcome\&amp;quot;:\&amp;quot;Ruby\&amp;quot;}&amp;quot;=&amp;gt;&amp;quot;{\&amp;quot;WELCOME\&amp;quot;:\&amp;quot;RUBY\&amp;quot;}&amp;quot;}&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A JSON string received from another program can be parsed by using JSON.parse&lt;br /&gt;
Ruby thus converts String to Hash.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
        require 'json'&lt;br /&gt;
        my_hash = JSON.parse('{&amp;quot;Welcome&amp;quot;: &amp;quot;Ruby&amp;quot;}')&lt;br /&gt;
        puts my_hash[&amp;quot;Welcome&amp;quot;] =&amp;gt; &amp;quot;Ruby&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Converting Ruby Objects to Binary Formats ===&lt;br /&gt;
Binary Serialization is another form of serialization in Ruby which is not in human readable form. 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. Binary Serialization  is done using Marshal which is built into Ruby and the code for it is written in Ruby's Marshal module(marshal.c) and thus no additional files are required in order to use it. The _dump and _load methods defined in marshal are used for serialization. Using marshal module the following types of objects can not be serialized: bindings, procedure objects, singleton objects, instances of IO objects and interfaces.&lt;br /&gt;
&lt;br /&gt;
Since the Binary Serialized data is not in human readable form, the following guidelines need to be followed.&lt;br /&gt;
     1.Use print instead of puts[http://ruby-doc.org/core-2.0.0/ARGF.html#] when serialized objects are written to a file in order to avoid new line characters to be written &lt;br /&gt;
       in the file.&lt;br /&gt;
     &lt;br /&gt;
     2.Use a record separator in order to differentiate between two objects.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Binary Serialization Example:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    class Animal&lt;br /&gt;
      def initialize  name, age&lt;br /&gt;
      @name = name&lt;br /&gt;
      @age=age&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
   class Cat &amp;lt; Animal&lt;br /&gt;
    def to_s&lt;br /&gt;
     &amp;quot;In Cat C: #{@name} \t #{@age}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
  c = Cat.new(&amp;quot;Kitty Kat&amp;quot;,5)&lt;br /&gt;
  puts &amp;quot;Before Serialization&amp;quot;&lt;br /&gt;
  puts c&lt;br /&gt;
  #puts d&lt;br /&gt;
  serialize_cat= Marshal.dump(c) #dumps the serialized cat object into serialize_cat&lt;br /&gt;
  puts &amp;quot;\nAfter Serialization:\n #{serialize_cat}&amp;quot;&lt;br /&gt;
  deserialize_cat= Marshal::load(serialize_cat) #deserializes the cat object and loads it back into deserialize_cat&lt;br /&gt;
  puts &amp;quot;\nAfter Deserialization\n #{deserialize_cat}&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   Before Serialization&lt;br /&gt;
   In Cat C: Kitty Kat 	 5&lt;br /&gt;
   After Serialization:&lt;br /&gt;
   oCat:&lt;br /&gt;
   @nameI&amp;quot;Kitty Kat:ET:	@agei&lt;br /&gt;
   After Deserialization&lt;br /&gt;
   In Cat C: Kitty Kat 	 5&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Similar to YAML, Marshal can also be used to dump data into a file. The above example showing the serialization using YAML::dump can be written using marshal as shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    f = File.open( 'first.yml', 'w' )&lt;br /&gt;
    Marshal.dump( [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;], f )&lt;br /&gt;
    f.close	&lt;br /&gt;
    File.open( 'first.yml' ){ |f|	&lt;br /&gt;
    $arr= Marshal.load(f)&lt;br /&gt;
    }	&lt;br /&gt;
    p( $arr )&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;]&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice that in the above example there is no &amp;quot;require&amp;quot; statement as opposed to the earlier example of writing serialized data into files using YAML. This is because unlike YAML, marshal is built-in Ruby and  no external libraries is required in order to use its functionality.&lt;br /&gt;
&lt;br /&gt;
Marshal can also be used to custom serialize an object i.e. it provides an option to omit the variables of an object that are not required in the serialized data. The following program shows the use of marshal_dump method for achieving custom serialization.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
      class First&lt;br /&gt;
      def initialize(name, age, country)&lt;br /&gt;
         @name = name&lt;br /&gt;
         @age = age&lt;br /&gt;
         @country=country&lt;br /&gt;
      end&lt;br /&gt;
      def to_s&lt;br /&gt;
            &amp;quot;In First: #{@name}, #{@age}, #{@country}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
      def marshal_dump&lt;br /&gt;
            [@name,@age]  #@country will not be serialized&lt;br /&gt;
      end&lt;br /&gt;
      def marshal_load(data)&lt;br /&gt;
       @name=data[0]&lt;br /&gt;
       @age=data[1]&lt;br /&gt;
       @country=&amp;quot;United States of America&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
      end&lt;br /&gt;
 &lt;br /&gt;
      x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
      puts x&lt;br /&gt;
  &lt;br /&gt;
      marshal_data = Marshal.dump( x ) &lt;br /&gt;
      y = Marshal.load( marshal_data ) &lt;br /&gt;
      p( y.to_s )&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    In First: Tom, 25, USA&lt;br /&gt;
    &amp;quot;In First: Tom, 25, United States of America&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== '''Serialization Performance in Ruby''' ==&lt;br /&gt;
The different serialization formats described above differ in the efficiency at which they can serialize and deserialize data and thus while serializing large amount of data their efficiency is taken into account. The .report[http://www.ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html] method in Ruby can be used to evaluate the performance of these serialization patterns. Marshal, as it handles data in binary format, is the most efficient form of serialization in Ruby. The following example compares the performance of marshal, JSON and YAML format.[http://www.pauldix.net/2008/08/serializing-dat.html]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   require 'benchmark'&lt;br /&gt;
   require 'rubygems'&lt;br /&gt;
   require 'json'&lt;br /&gt;
   require 'yaml'&lt;br /&gt;
   include Benchmark&lt;br /&gt;
   class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
      @name=name&lt;br /&gt;
      @age=age&lt;br /&gt;
      @country=country&lt;br /&gt;
     end&lt;br /&gt;
   end  &lt;br /&gt;
    x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
    puts &amp;quot;            User     System     Total      Real&amp;quot;&lt;br /&gt;
   benchmark do |t|&lt;br /&gt;
    print &amp;quot;Marshal:&amp;quot;&lt;br /&gt;
    t.report{1000.times do; Marshal.load(Marshal.dump(x));end}&lt;br /&gt;
    print &amp;quot;JSON:   &amp;quot;&lt;br /&gt;
    t.report{1000.times do; JSON.load(JSON.dump(x));end}&lt;br /&gt;
    print &amp;quot;YAML:   &amp;quot;&lt;br /&gt;
    t.report{1000.times do; YAML.load(YAML.dump(x));end}&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
              User     System     Total      Real&lt;br /&gt;
  Marshal:   0.010000   0.000000   0.010000 (  0.015408)&lt;br /&gt;
  JSON:      0.020000   0.010000   0.030000 (  0.022462)&lt;br /&gt;
  YAML:      0.460000   0.010000   0.470000 (  0.472149)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output of the above example shows that, given large chunks of data, marshal is comparatively more efficient format for serialization in Ruby.&lt;br /&gt;
&lt;br /&gt;
== Serialization in Object-Oriented Languages: Comparison ==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Sl.No !! Ruby !! Java !! .Net Framework !! C++&lt;br /&gt;
|-&lt;br /&gt;
| 1 || Ruby provides a built in module called 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 || Although, there is no built in support for serialization in C++, it can be achieved by using Boost libraries[http://www.boost.org/doc/libs/1_36_0/libs/serialization/example/demo.cpp]&lt;br /&gt;
|-&lt;br /&gt;
| 2 || The built in module of Ruby (Marshal) does not support platform independence, however, it can be achieved by using external libraries like [http://yaml.org YAML] and [http://www.w3schools.com/json/ JSON] || Similarly, Java's built in serialization is also not platform independent and in order to use serialization in Java across Ruby platform, jruby library should be used.  || .Net used Remoting technology to make it platform independent. || Serialization using the Boost libraries is not platform independent.&lt;br /&gt;
|-&lt;br /&gt;
| 3 || YAML provides a method [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/syck/rdoc/Object.html (to_yaml_properties)] which can be used to select the variables who's value is need to be serialized. With Marshal, we need to write a method named marshal_dump defining the variables of an object that has to be serialized. || Provides an option for serializing only the required 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 data 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 || Serialization using the Boost libraries is custom and thus the user can specify the part of the objects to be serialized.&lt;br /&gt;
|-&lt;br /&gt;
| 4 || Bindings, procedure objects, singleton objects, instances of IO objects and interfaces can not be serialized. Serializing these objects throws TypeError exceptions. || Thread, OutputStream and its subclasses, and Socket are not serializable in Java.[http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html] || Objects like DataRow are non serializable in .NET. Whether an object is serializable or not can be checked using the [http://msdn.microsoft.com/en-us/library/system.type.isserializable(v=vs.110).aspx?cs-save-lang=1&amp;amp;cs-lang=cpp#code-snippet-1 Type.IsSerializable] || There are no such objects in C++.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== See Also==&lt;br /&gt;
&lt;br /&gt;
1. [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/json/rdoc/JSON.html Serialization in Ruby JSON]&lt;br /&gt;
&lt;br /&gt;
2. [http://api.rubyonrails.org/classes/ActiveModel/Serialization.html Serialization in Rails]&lt;br /&gt;
&lt;br /&gt;
3. [http://gregmoreno.wordpress.com/2011/01/27/preventing-model-explosion-via-rails-serialization/ Article on Rails Serialization]&lt;br /&gt;
&lt;br /&gt;
4. [http://en.wikipedia.org/wiki/Google_Protocol_Buffers Protocol Buffers]&lt;br /&gt;
&lt;br /&gt;
5. [https://rubygems.org/gems/ruby-protocol-buffers Ruby Protocol Buffers Gem]&lt;br /&gt;
&lt;br /&gt;
6. [http://en.wikipedia.org/wiki/Apache_Avro Avro]&lt;br /&gt;
&lt;br /&gt;
7. [http://rubygems.org/gems/avro Ruby Avro Gem]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
1. [http://en.wikipedia.org/wiki/Serialization Serilization in General]&lt;br /&gt;
&lt;br /&gt;
2. [http://en.wikipedia.org/wiki/Marshalling_%28computer_science%29 Marshaling]&lt;br /&gt;
&lt;br /&gt;
3. [http://techbrahmana.blogspot.com/2012/03/rails-cookie-handling-serialization-and.html Rails Cookie Handling]&lt;br /&gt;
&lt;br /&gt;
4. [http://yaml.org/ YAML]&lt;br /&gt;
&lt;br /&gt;
5. [http://www.ruby-doc.org/core-2.1.0/Marshal.html Marshal module]&lt;br /&gt;
&lt;br /&gt;
6. [http://json.org/ JSON]&lt;br /&gt;
&lt;br /&gt;
7. [http://rubygems.org/ Ruby Gems]&lt;br /&gt;
&lt;br /&gt;
8. [http://ruby-doc.org/core-2.0.0/ARGF.html# Ruby ARGF Documentation]&lt;br /&gt;
&lt;br /&gt;
9. [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html Ruby Documentation for Benchmark Gem]&lt;br /&gt;
&lt;br /&gt;
10. [http://www.pauldix.net/2008/08/serializing-dat.html Serialization Performance Comparison]&lt;br /&gt;
&lt;br /&gt;
11. [http://www.boost.org/doc/libs/1_36_0/libs/serialization/example/demo.cpp Boost Libraries]&lt;br /&gt;
&lt;br /&gt;
12. [http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html Java Serialization]&lt;br /&gt;
&lt;br /&gt;
13. [http://www.w3schools.com/json/ JSON Tutorial]&lt;br /&gt;
&lt;br /&gt;
14. [http://www.skorks.com/2010/04/serializing-and-deserializing-objects-with-ruby/ Serializing and De-serializing in Ruby]&lt;br /&gt;
&lt;br /&gt;
15. [http://www.codeproject.com/Articles/33296/Serialization-and-De-serialization Serialization and De-serialization]&lt;br /&gt;
&lt;br /&gt;
16. [http://msdn.microsoft.com/en-us/library/182eeyhh.aspx XML Serialization]&lt;br /&gt;
&lt;br /&gt;
17. [http://www.tutorialspoint.com/java/java_serialization.htm Java Serialization Tutorial]&lt;br /&gt;
&lt;br /&gt;
18. [http://www.sapphiresteel.com/ruby-programming/The-Book-Of-Ruby The Book of Ruby]&lt;br /&gt;
&lt;br /&gt;
19. [http://www.codeproject.com/Articles/20962/Introducing-Serialization-in-NET Serialization in .Net]&lt;br /&gt;
&lt;br /&gt;
20. [http://ruby.about.com/od/advancedruby/ss/Serialization-In-Ruby-Yaml.html Serialization in Ruby YAML]&lt;br /&gt;
&lt;br /&gt;
21. [http://ruby.about.com/od/tasks/a/The-Json-Gem.html Installing JSON gem]&lt;br /&gt;
&lt;br /&gt;
22. [http://www.ruby-doc.org/stdlib-2.0.0/libdoc/json/rdoc/JSON.html Serialization in Ruby JSON]&lt;/div&gt;</summary>
		<author><name>Mmehta2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1d_mm&amp;diff=83402</id>
		<title>CSC/ECE 517 Spring 2014/ch1a 1d mm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1d_mm&amp;diff=83402"/>
		<updated>2014-02-18T02:04:02Z</updated>

		<summary type="html">&lt;p&gt;Mmehta2: /* Converting Ruby Objects to JSON format: */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== '''Serialization''' ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Serialization'''[http://en.wikipedia.org/wiki/Serialization] is a process of converting a data structure or an object into a stream of bytes or string to facilitate storage in memory, file (persistence storage) or transmission over a network. The process of Serialization is also referred to as ''Marshalling''[http://en.wikipedia.org/wiki/Marshalling_%28computer_science%29]. 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. &lt;br /&gt;
&lt;br /&gt;
'''Basic Advantages of Serialization''':&lt;br /&gt;
&lt;br /&gt;
1. Communication between two or more processes on same machine. Object state can be saved and shared in a persistent or in-memory store.&lt;br /&gt;
&lt;br /&gt;
2. Communication between processes on different machines. Serialization facilitates the transmission of an object through a network.&lt;br /&gt;
&lt;br /&gt;
3. Creating a clone of an object. &lt;br /&gt;
&lt;br /&gt;
4. Cross-platform compatibility. Object can be serialized in a common format that is understood by multiple platforms. For example, JSON and XML.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''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''.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Few Practical Applications for Serialization'''&lt;br /&gt;
&lt;br /&gt;
1. [http://docs.oracle.com/cd/E13222_01/wls/docs81/webapp/sessions.html#139726 HTTP Session] Replication by sharing session objects across web servers for load balancing and handling failover scenarios.&lt;br /&gt;
&lt;br /&gt;
2. Serialization facilitates communication in [http://en.wikipedia.org/wiki/Java_remote_method_invocation Remote Method Invocation] or [http://en.wikipedia.org/wiki/Remote_procedure_call Remote procedure calls]&lt;br /&gt;
&lt;br /&gt;
3. Rails Cookie Handling[http://techbrahmana.blogspot.com/2012/03/rails-cookie-handling-serialization-and.html]. Cookies are stored marshalled/unmarshalled to and from client machines. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:SerializationChart.jpg]]&lt;br /&gt;
&lt;br /&gt;
== '''Serialization in Ruby:''' ==&lt;br /&gt;
&lt;br /&gt;
Ruby provides serialization capabilities through its module, [http://www.ruby-doc.org/core-2.0.0/Marshal.html Marshal]. There are also other libraries like YAML[http://yaml.org/] and JSON which can be used in Ruby to generate serialized objects for purposes like platform independence and human readable formats.&lt;br /&gt;
&lt;br /&gt;
== '''Types of Serialization''' ==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
=== Converting Ruby Objects in Human Readable Formats ===&lt;br /&gt;
The conversion of Ruby objects into YAML and JSON formats are explained below.&lt;br /&gt;
&lt;br /&gt;
==== Converting Ruby Objects to YAML format ====&lt;br /&gt;
&lt;br /&gt;
YAML 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 binary format. 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. In order to use it in Ruby, the yaml.rb file is required to be loaded which provides methods for converting objects into yaml format and creating .yml files.&lt;br /&gt;
&lt;br /&gt;
'''Examples of serialization using YAML:'''&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;nowiki&amp;gt; #Serialization using YAML's to_yaml method&lt;br /&gt;
 &lt;br /&gt;
  require &amp;quot;yaml&amp;quot;&lt;br /&gt;
     class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
	@name = name&lt;br /&gt;
	@age = age&lt;br /&gt;
	@country=country&lt;br /&gt;
     end&lt;br /&gt;
      def to_s&lt;br /&gt;
	&amp;quot;In First:\n#{@name}, #{@age}, #{@country}\n&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
  x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
  puts x&lt;br /&gt;
  puts x.to_yaml&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   In First:&lt;br /&gt;
   Tom, 25, USA&lt;br /&gt;
   --- !ruby/object:First&lt;br /&gt;
   name: Tom&lt;br /&gt;
   age: 25&lt;br /&gt;
   country: USA&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The above code displays the object x, first as a string and then in the yaml format. &lt;br /&gt;
&lt;br /&gt;
'''Saving YAML data into a file:'''&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt; # Serialization using YAML::dump&lt;br /&gt;
&lt;br /&gt;
require 'yaml'&lt;br /&gt;
&lt;br /&gt;
f = File.open( 'first.yml', 'w' )&lt;br /&gt;
YAML.dump( [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;], f )&lt;br /&gt;
f.close&lt;br /&gt;
		&lt;br /&gt;
File.open( 'first.yml' ){ |f|	&lt;br /&gt;
    $arr= YAML.load(f)&lt;br /&gt;
}	&lt;br /&gt;
&lt;br /&gt;
p( $arr )&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;]&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The dump function can be used to serialize the data and save it into a file in YAML format. As shown in the above example the data in YAML format can be de-serialized using the load function.&lt;br /&gt;
&lt;br /&gt;
YAML libraries also provides an option of selecting only those variables of the object that are needed to be serialized. This is done using the to_yaml_properties method as shown in the below example.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;nowiki&amp;gt;  #Custom Serialization using YAML&lt;br /&gt;
 &lt;br /&gt;
  require &amp;quot;yaml&amp;quot;&lt;br /&gt;
     class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
	@name = name&lt;br /&gt;
	@age = age&lt;br /&gt;
	@country=country&lt;br /&gt;
     end&lt;br /&gt;
     def to_s&lt;br /&gt;
	&amp;quot;In First:\n#{@name}, #{@age}, #{@country}\n&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
      def to_yaml_properties&lt;br /&gt;
	   [&amp;quot;@name&amp;quot;,&amp;quot;@age&amp;quot;]  #@country will not be serialized&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
  x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
  puts x&lt;br /&gt;
  puts x.to_yaml&lt;br /&gt;
  &amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   In First:&lt;br /&gt;
   Tom, 25, USA&lt;br /&gt;
   --- !ruby/object:First&lt;br /&gt;
   name: Tom&lt;br /&gt;
   age: 25&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Converting Ruby Objects to JSON format: ====&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
JSON 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. Ruby 1.8.7 distribution is not bundled with json gem. However, in Ruby 1.9.2, the json gem is bundled with the core Ruby distribution.&lt;br /&gt;
The JSON library can be installed using Ruby Gems[http://rubygems.org/] as shown below:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;# gem install json&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A JSON string for serialization can be created by using the JSON.generate method:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
        require 'json'&lt;br /&gt;
        my_hash = {:Welcome =&amp;gt; &amp;quot;Ruby&amp;quot;}&lt;br /&gt;
        puts JSON.generate(my_hash) =&amp;gt; &amp;quot;{\&amp;quot;WELCOME\&amp;quot;:\&amp;quot;RUBY\&amp;quot;}&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
'''Output:'''&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;{&amp;quot;{\&amp;quot;Welcome\&amp;quot;:\&amp;quot;Ruby\&amp;quot;}&amp;quot;=&amp;gt;&amp;quot;{\&amp;quot;WELCOME\&amp;quot;:\&amp;quot;RUBY\&amp;quot;}&amp;quot;}&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A JSON string received from another program can be parsed by using JSON.parse&lt;br /&gt;
Ruby thus converts String to Hash.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
        require 'json'&lt;br /&gt;
        my_hash = JSON.parse('{&amp;quot;Welcome&amp;quot;: &amp;quot;Ruby&amp;quot;}')&lt;br /&gt;
        puts my_hash[&amp;quot;Welcome&amp;quot;] =&amp;gt; &amp;quot;Ruby&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Converting Ruby Objects to Binary Formats ===&lt;br /&gt;
Binary Serialization is another form of serialization in Ruby which is not in human readable form. 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. Binary Serialization  is done using Marshal which is built into Ruby and the code for it is written in Ruby's Marshal module(marshal.c) and thus no additional files are required in order to use it. The _dump and _load methods defined in marshal are used for serialization. Using marshal module the following types of objects can not be serialized: bindings, procedure objects, singleton objects, instances of IO objects and interfaces.&lt;br /&gt;
&lt;br /&gt;
Since the Binary Serialized data is not in human readable form, the following guidelines need to be followed.&lt;br /&gt;
     1.Use print instead of puts[http://ruby-doc.org/core-2.0.0/ARGF.html#] when serialized objects are written to a file in order to avoid new line characters to be written &lt;br /&gt;
       in the file.&lt;br /&gt;
     &lt;br /&gt;
     2.Use a record separator in order to differentiate between two objects.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Binary Serialization Example:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    class Animal&lt;br /&gt;
      def initialize  name, age&lt;br /&gt;
      @name = name&lt;br /&gt;
      @age=age&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
   class Cat &amp;lt; Animal&lt;br /&gt;
    def to_s&lt;br /&gt;
     &amp;quot;In Cat C: #{@name} \t #{@age}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
  c = Cat.new(&amp;quot;Kitty Kat&amp;quot;,5)&lt;br /&gt;
  puts &amp;quot;Before Serialization&amp;quot;&lt;br /&gt;
  puts c&lt;br /&gt;
  #puts d&lt;br /&gt;
  serialize_cat= Marshal.dump(c) #dumps the serialized cat object into serialize_cat&lt;br /&gt;
  puts &amp;quot;\nAfter Serialization:\n #{serialize_cat}&amp;quot;&lt;br /&gt;
  deserialize_cat= Marshal::load(serialize_cat) #deserializes the cat object and loads it back into deserialize_cat&lt;br /&gt;
  puts &amp;quot;\nAfter Deserialization\n #{deserialize_cat}&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   Before Serialization&lt;br /&gt;
   In Cat C: Kitty Kat 	 5&lt;br /&gt;
   After Serialization:&lt;br /&gt;
   oCat:&lt;br /&gt;
   @nameI&amp;quot;Kitty Kat:ET:	@agei&lt;br /&gt;
   After Deserialization&lt;br /&gt;
   In Cat C: Kitty Kat 	 5&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Similar to YAML, Marshal can also be used to dump data into a file. The above example showing the serialization using YAML::dump can be written using marshal as shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    f = File.open( 'first.yml', 'w' )&lt;br /&gt;
    Marshal.dump( [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;], f )&lt;br /&gt;
    f.close	&lt;br /&gt;
    File.open( 'first.yml' ){ |f|	&lt;br /&gt;
    $arr= Marshal.load(f)&lt;br /&gt;
    }	&lt;br /&gt;
    p( $arr )&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;]&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice that in the above example there is no &amp;quot;require&amp;quot; statement as opposed to the earlier example of writing serialized data into files using YAML. This is because unlike YAML, marshal is built-in Ruby and  no external libraries is required in order to use its functionality.&lt;br /&gt;
&lt;br /&gt;
Marshal can also be used to custom serialize an object i.e. it provides an option to omit the variables of an object that are not required in the serialized data. The following program shows the use of marshal_dump method for achieving custom serialization.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
      class First&lt;br /&gt;
      def initialize(name, age, country)&lt;br /&gt;
         @name = name&lt;br /&gt;
         @age = age&lt;br /&gt;
         @country=country&lt;br /&gt;
      end&lt;br /&gt;
      def to_s&lt;br /&gt;
            &amp;quot;In First: #{@name}, #{@age}, #{@country}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
      def marshal_dump&lt;br /&gt;
            [@name,@age]  #@country will not be serialized&lt;br /&gt;
      end&lt;br /&gt;
      def marshal_load(data)&lt;br /&gt;
       @name=data[0]&lt;br /&gt;
       @age=data[1]&lt;br /&gt;
       @country=&amp;quot;United States of America&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
      end&lt;br /&gt;
 &lt;br /&gt;
      x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
      puts x&lt;br /&gt;
  &lt;br /&gt;
      marshal_data = Marshal.dump( x ) &lt;br /&gt;
      y = Marshal.load( marshal_data ) &lt;br /&gt;
      p( y.to_s )&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    In First: Tom, 25, USA&lt;br /&gt;
    &amp;quot;In First: Tom, 25, United States of America&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== '''Serialization Performance in Ruby''' ==&lt;br /&gt;
The different serialization formats described above differ in the efficiency at which they can serialize and deserialize data and thus while serializing large amount of data their efficiency is taken into account. The .report[http://www.ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html] method in Ruby can be used to evaluate the performance of these serialization patterns. Marshal, as it handles data in binary format, is the most efficient form of serialization in Ruby. The following example compares the performance of marshal, JSON and YAML format.[http://www.pauldix.net/2008/08/serializing-dat.html]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   require 'benchmark'&lt;br /&gt;
   require 'rubygems'&lt;br /&gt;
   require 'json'&lt;br /&gt;
   require 'yaml'&lt;br /&gt;
   include Benchmark&lt;br /&gt;
   class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
      @name=name&lt;br /&gt;
      @age=age&lt;br /&gt;
      @country=country&lt;br /&gt;
     end&lt;br /&gt;
   end  &lt;br /&gt;
    x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
    puts &amp;quot;            User     System     Total      Real&amp;quot;&lt;br /&gt;
   benchmark do |t|&lt;br /&gt;
    print &amp;quot;Marshal:&amp;quot;&lt;br /&gt;
    t.report{1000.times do; Marshal.load(Marshal.dump(x));end}&lt;br /&gt;
    print &amp;quot;JSON:   &amp;quot;&lt;br /&gt;
    t.report{1000.times do; JSON.load(JSON.dump(x));end}&lt;br /&gt;
    print &amp;quot;YAML:   &amp;quot;&lt;br /&gt;
    t.report{1000.times do; YAML.load(YAML.dump(x));end}&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
              User     System     Total      Real&lt;br /&gt;
  Marshal:   0.010000   0.000000   0.010000 (  0.015408)&lt;br /&gt;
  JSON:      0.020000   0.010000   0.030000 (  0.022462)&lt;br /&gt;
  YAML:      0.460000   0.010000   0.470000 (  0.472149)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output of the above example shows that, given large chunks of data, marshal is comparatively more efficient format for serialization in Ruby.&lt;br /&gt;
&lt;br /&gt;
== Serialization in Object-Oriented Languages: Comparison ==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Sl.No !! Ruby !! Java !! .Net Framework !! C++&lt;br /&gt;
|-&lt;br /&gt;
| 1 || Ruby provides a built in module called 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 || Although, there is no built in support for serialization in C++, it can be achieved by using Boost libraries[http://www.boost.org/doc/libs/1_36_0/libs/serialization/example/demo.cpp]&lt;br /&gt;
|-&lt;br /&gt;
| 2 || The built in module of Ruby (Marshal) does not support platform independence, however, it can be achieved by using external libraries like [http://yaml.org YAML] and [http://www.w3schools.com/json/ JSON] || Similarly, Java's built in serialization is also not platform independent and in order to use serialization in Java across Ruby platform, jruby library should be used.  || .Net used Remoting technology to make it platform independent. || Serialization using the Boost libraries is not platform independent.&lt;br /&gt;
|-&lt;br /&gt;
| 3 || YAML provides a method [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/syck/rdoc/Object.html (to_yaml_properties)] which can be used to select the variables who's value is need to be serialized. With Marshal, we need to write a method named marshal_dump defining the variables of an object that has to be serialized. || Provides an option for serializing only the required 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 data 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 || Serialization using the Boost libraries is custom and thus the user can specify the part of the objects to be serialized.&lt;br /&gt;
|-&lt;br /&gt;
| 4 || Bindings, procedure objects, singleton objects, instances of IO objects and interfaces can not be serialized. Serializing these objects throws TypeError exceptions. || Thread, OutputStream and its subclasses, and Socket are not serializable in Java.[http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html] || Objects like DataRow are non serializable in .NET. Whether an object is serializable or not can be checked using the [http://msdn.microsoft.com/en-us/library/system.type.isserializable(v=vs.110).aspx?cs-save-lang=1&amp;amp;cs-lang=cpp#code-snippet-1 Type.IsSerializable] || There are no such objects in C++.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== See Also==&lt;br /&gt;
&lt;br /&gt;
1. [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/json/rdoc/JSON.html Serialization in Ruby JSON]&lt;br /&gt;
&lt;br /&gt;
2. [http://api.rubyonrails.org/classes/ActiveModel/Serialization.html Serialization in Rails]&lt;br /&gt;
&lt;br /&gt;
3. [http://gregmoreno.wordpress.com/2011/01/27/preventing-model-explosion-via-rails-serialization/ Article on Rails Serialization]&lt;br /&gt;
&lt;br /&gt;
4. [http://en.wikipedia.org/wiki/Google_Protocol_Buffers Protocol Buffers]&lt;br /&gt;
&lt;br /&gt;
5. [https://rubygems.org/gems/ruby-protocol-buffers Ruby Protocol Buffers Gem]&lt;br /&gt;
&lt;br /&gt;
6. [http://en.wikipedia.org/wiki/Apache_Avro Avro]&lt;br /&gt;
&lt;br /&gt;
7. [http://rubygems.org/gems/avro Ruby Avro Gem]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
1. [http://en.wikipedia.org/wiki/Serialization Serilization in General]&lt;br /&gt;
&lt;br /&gt;
2. [http://en.wikipedia.org/wiki/Marshalling_%28computer_science%29 Marshaling]&lt;br /&gt;
&lt;br /&gt;
3. [http://techbrahmana.blogspot.com/2012/03/rails-cookie-handling-serialization-and.html Rails Cookie Handling]&lt;br /&gt;
&lt;br /&gt;
4. [http://yaml.org/ YAML]&lt;br /&gt;
&lt;br /&gt;
5. [http://www.ruby-doc.org/core-2.1.0/Marshal.html Marshal module]&lt;br /&gt;
&lt;br /&gt;
6. [http://json.org/ JSON]&lt;br /&gt;
&lt;br /&gt;
7. [http://rubygems.org/ Ruby Gems]&lt;br /&gt;
&lt;br /&gt;
8. [http://ruby-doc.org/core-2.0.0/ARGF.html# Ruby ARGF Documentation]&lt;br /&gt;
&lt;br /&gt;
9. [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html Ruby Documentation for Benchmark Gem]&lt;br /&gt;
&lt;br /&gt;
10. [http://www.pauldix.net/2008/08/serializing-dat.html Serialization Performance Comparison]&lt;br /&gt;
&lt;br /&gt;
11. [http://www.boost.org/doc/libs/1_36_0/libs/serialization/example/demo.cpp Boost Libraries]&lt;br /&gt;
&lt;br /&gt;
12. [http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html Java Serialization]&lt;br /&gt;
&lt;br /&gt;
13. [http://www.w3schools.com/json/ JSON Tutorial]&lt;br /&gt;
&lt;br /&gt;
14. [http://www.skorks.com/2010/04/serializing-and-deserializing-objects-with-ruby/ Serializing and De-serializing in Ruby]&lt;br /&gt;
&lt;br /&gt;
15. [http://www.codeproject.com/Articles/33296/Serialization-and-De-serialization Serialization and De-serialization]&lt;br /&gt;
&lt;br /&gt;
16. [http://msdn.microsoft.com/en-us/library/182eeyhh.aspx XML Serialization]&lt;br /&gt;
&lt;br /&gt;
17. [http://www.tutorialspoint.com/java/java_serialization.htm Java Serialization Tutorial]&lt;br /&gt;
&lt;br /&gt;
18. [http://www.sapphiresteel.com/ruby-programming/The-Book-Of-Ruby The Book of Ruby]&lt;br /&gt;
&lt;br /&gt;
19. [http://www.codeproject.com/Articles/20962/Introducing-Serialization-in-NET Serialization in .Net]&lt;br /&gt;
&lt;br /&gt;
20. [http://ruby.about.com/od/advancedruby/ss/Serialization-In-Ruby-Yaml.html Serialization in Ruby YAML]&lt;br /&gt;
&lt;br /&gt;
21. [http://ruby.about.com/od/tasks/a/The-Json-Gem.html Installing JSON gem]&lt;br /&gt;
&lt;br /&gt;
22. [http://www.ruby-doc.org/stdlib-2.0.0/libdoc/json/rdoc/JSON.html Serialization in Ruby JSON]&lt;/div&gt;</summary>
		<author><name>Mmehta2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1d_mm&amp;diff=83401</id>
		<title>CSC/ECE 517 Spring 2014/ch1a 1d mm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1d_mm&amp;diff=83401"/>
		<updated>2014-02-18T02:03:28Z</updated>

		<summary type="html">&lt;p&gt;Mmehta2: /* Serialization in Ruby: */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== '''Serialization''' ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Serialization'''[http://en.wikipedia.org/wiki/Serialization] is a process of converting a data structure or an object into a stream of bytes or string to facilitate storage in memory, file (persistence storage) or transmission over a network. The process of Serialization is also referred to as ''Marshalling''[http://en.wikipedia.org/wiki/Marshalling_%28computer_science%29]. 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. &lt;br /&gt;
&lt;br /&gt;
'''Basic Advantages of Serialization''':&lt;br /&gt;
&lt;br /&gt;
1. Communication between two or more processes on same machine. Object state can be saved and shared in a persistent or in-memory store.&lt;br /&gt;
&lt;br /&gt;
2. Communication between processes on different machines. Serialization facilitates the transmission of an object through a network.&lt;br /&gt;
&lt;br /&gt;
3. Creating a clone of an object. &lt;br /&gt;
&lt;br /&gt;
4. Cross-platform compatibility. Object can be serialized in a common format that is understood by multiple platforms. For example, JSON and XML.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''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''.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Few Practical Applications for Serialization'''&lt;br /&gt;
&lt;br /&gt;
1. [http://docs.oracle.com/cd/E13222_01/wls/docs81/webapp/sessions.html#139726 HTTP Session] Replication by sharing session objects across web servers for load balancing and handling failover scenarios.&lt;br /&gt;
&lt;br /&gt;
2. Serialization facilitates communication in [http://en.wikipedia.org/wiki/Java_remote_method_invocation Remote Method Invocation] or [http://en.wikipedia.org/wiki/Remote_procedure_call Remote procedure calls]&lt;br /&gt;
&lt;br /&gt;
3. Rails Cookie Handling[http://techbrahmana.blogspot.com/2012/03/rails-cookie-handling-serialization-and.html]. Cookies are stored marshalled/unmarshalled to and from client machines. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:SerializationChart.jpg]]&lt;br /&gt;
&lt;br /&gt;
== '''Serialization in Ruby:''' ==&lt;br /&gt;
&lt;br /&gt;
Ruby provides serialization capabilities through its module, [http://www.ruby-doc.org/core-2.0.0/Marshal.html Marshal]. There are also other libraries like YAML[http://yaml.org/] and JSON which can be used in Ruby to generate serialized objects for purposes like platform independence and human readable formats.&lt;br /&gt;
&lt;br /&gt;
== '''Types of Serialization''' ==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
=== Converting Ruby Objects in Human Readable Formats ===&lt;br /&gt;
The conversion of Ruby objects into YAML and JSON formats are explained below.&lt;br /&gt;
&lt;br /&gt;
==== Converting Ruby Objects to YAML format ====&lt;br /&gt;
&lt;br /&gt;
YAML 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 binary format. 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. In order to use it in Ruby, the yaml.rb file is required to be loaded which provides methods for converting objects into yaml format and creating .yml files.&lt;br /&gt;
&lt;br /&gt;
'''Examples of serialization using YAML:'''&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;nowiki&amp;gt; #Serialization using YAML's to_yaml method&lt;br /&gt;
 &lt;br /&gt;
  require &amp;quot;yaml&amp;quot;&lt;br /&gt;
     class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
	@name = name&lt;br /&gt;
	@age = age&lt;br /&gt;
	@country=country&lt;br /&gt;
     end&lt;br /&gt;
      def to_s&lt;br /&gt;
	&amp;quot;In First:\n#{@name}, #{@age}, #{@country}\n&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
  x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
  puts x&lt;br /&gt;
  puts x.to_yaml&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   In First:&lt;br /&gt;
   Tom, 25, USA&lt;br /&gt;
   --- !ruby/object:First&lt;br /&gt;
   name: Tom&lt;br /&gt;
   age: 25&lt;br /&gt;
   country: USA&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The above code displays the object x, first as a string and then in the yaml format. &lt;br /&gt;
&lt;br /&gt;
'''Saving YAML data into a file:'''&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt; # Serialization using YAML::dump&lt;br /&gt;
&lt;br /&gt;
require 'yaml'&lt;br /&gt;
&lt;br /&gt;
f = File.open( 'first.yml', 'w' )&lt;br /&gt;
YAML.dump( [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;], f )&lt;br /&gt;
f.close&lt;br /&gt;
		&lt;br /&gt;
File.open( 'first.yml' ){ |f|	&lt;br /&gt;
    $arr= YAML.load(f)&lt;br /&gt;
}	&lt;br /&gt;
&lt;br /&gt;
p( $arr )&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;]&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The dump function can be used to serialize the data and save it into a file in YAML format. As shown in the above example the data in YAML format can be de-serialized using the load function.&lt;br /&gt;
&lt;br /&gt;
YAML libraries also provides an option of selecting only those variables of the object that are needed to be serialized. This is done using the to_yaml_properties method as shown in the below example.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;nowiki&amp;gt;  #Custom Serialization using YAML&lt;br /&gt;
 &lt;br /&gt;
  require &amp;quot;yaml&amp;quot;&lt;br /&gt;
     class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
	@name = name&lt;br /&gt;
	@age = age&lt;br /&gt;
	@country=country&lt;br /&gt;
     end&lt;br /&gt;
     def to_s&lt;br /&gt;
	&amp;quot;In First:\n#{@name}, #{@age}, #{@country}\n&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
      def to_yaml_properties&lt;br /&gt;
	   [&amp;quot;@name&amp;quot;,&amp;quot;@age&amp;quot;]  #@country will not be serialized&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
  x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
  puts x&lt;br /&gt;
  puts x.to_yaml&lt;br /&gt;
  &amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   In First:&lt;br /&gt;
   Tom, 25, USA&lt;br /&gt;
   --- !ruby/object:First&lt;br /&gt;
   name: Tom&lt;br /&gt;
   age: 25&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Converting Ruby Objects to JSON format: ====&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
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. Ruby 1.8.7 distribution is not bundled with json gem. However, in Ruby 1.9.2, the json gem is bundled with the core Ruby distribution.&lt;br /&gt;
The JSON library can be installed using Ruby Gems[http://rubygems.org/] as shown below:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;# gem install json&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A JSON string for serialization can be created by using the JSON.generate method:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
        require 'json'&lt;br /&gt;
        my_hash = {:Welcome =&amp;gt; &amp;quot;Ruby&amp;quot;}&lt;br /&gt;
        puts JSON.generate(my_hash) =&amp;gt; &amp;quot;{\&amp;quot;WELCOME\&amp;quot;:\&amp;quot;RUBY\&amp;quot;}&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
'''Output:'''&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;{&amp;quot;{\&amp;quot;Welcome\&amp;quot;:\&amp;quot;Ruby\&amp;quot;}&amp;quot;=&amp;gt;&amp;quot;{\&amp;quot;WELCOME\&amp;quot;:\&amp;quot;RUBY\&amp;quot;}&amp;quot;}&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A JSON string received from another program can be parsed by using JSON.parse&lt;br /&gt;
Ruby thus converts String to Hash.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
        require 'json'&lt;br /&gt;
        my_hash = JSON.parse('{&amp;quot;Welcome&amp;quot;: &amp;quot;Ruby&amp;quot;}')&lt;br /&gt;
        puts my_hash[&amp;quot;Welcome&amp;quot;] =&amp;gt; &amp;quot;Ruby&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Converting Ruby Objects to Binary Formats ===&lt;br /&gt;
Binary Serialization is another form of serialization in Ruby which is not in human readable form. 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. Binary Serialization  is done using Marshal which is built into Ruby and the code for it is written in Ruby's Marshal module(marshal.c) and thus no additional files are required in order to use it. The _dump and _load methods defined in marshal are used for serialization. Using marshal module the following types of objects can not be serialized: bindings, procedure objects, singleton objects, instances of IO objects and interfaces.&lt;br /&gt;
&lt;br /&gt;
Since the Binary Serialized data is not in human readable form, the following guidelines need to be followed.&lt;br /&gt;
     1.Use print instead of puts[http://ruby-doc.org/core-2.0.0/ARGF.html#] when serialized objects are written to a file in order to avoid new line characters to be written &lt;br /&gt;
       in the file.&lt;br /&gt;
     &lt;br /&gt;
     2.Use a record separator in order to differentiate between two objects.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Binary Serialization Example:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    class Animal&lt;br /&gt;
      def initialize  name, age&lt;br /&gt;
      @name = name&lt;br /&gt;
      @age=age&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
   class Cat &amp;lt; Animal&lt;br /&gt;
    def to_s&lt;br /&gt;
     &amp;quot;In Cat C: #{@name} \t #{@age}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
  c = Cat.new(&amp;quot;Kitty Kat&amp;quot;,5)&lt;br /&gt;
  puts &amp;quot;Before Serialization&amp;quot;&lt;br /&gt;
  puts c&lt;br /&gt;
  #puts d&lt;br /&gt;
  serialize_cat= Marshal.dump(c) #dumps the serialized cat object into serialize_cat&lt;br /&gt;
  puts &amp;quot;\nAfter Serialization:\n #{serialize_cat}&amp;quot;&lt;br /&gt;
  deserialize_cat= Marshal::load(serialize_cat) #deserializes the cat object and loads it back into deserialize_cat&lt;br /&gt;
  puts &amp;quot;\nAfter Deserialization\n #{deserialize_cat}&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   Before Serialization&lt;br /&gt;
   In Cat C: Kitty Kat 	 5&lt;br /&gt;
   After Serialization:&lt;br /&gt;
   oCat:&lt;br /&gt;
   @nameI&amp;quot;Kitty Kat:ET:	@agei&lt;br /&gt;
   After Deserialization&lt;br /&gt;
   In Cat C: Kitty Kat 	 5&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Similar to YAML, Marshal can also be used to dump data into a file. The above example showing the serialization using YAML::dump can be written using marshal as shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    f = File.open( 'first.yml', 'w' )&lt;br /&gt;
    Marshal.dump( [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;], f )&lt;br /&gt;
    f.close	&lt;br /&gt;
    File.open( 'first.yml' ){ |f|	&lt;br /&gt;
    $arr= Marshal.load(f)&lt;br /&gt;
    }	&lt;br /&gt;
    p( $arr )&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;]&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice that in the above example there is no &amp;quot;require&amp;quot; statement as opposed to the earlier example of writing serialized data into files using YAML. This is because unlike YAML, marshal is built-in Ruby and  no external libraries is required in order to use its functionality.&lt;br /&gt;
&lt;br /&gt;
Marshal can also be used to custom serialize an object i.e. it provides an option to omit the variables of an object that are not required in the serialized data. The following program shows the use of marshal_dump method for achieving custom serialization.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
      class First&lt;br /&gt;
      def initialize(name, age, country)&lt;br /&gt;
         @name = name&lt;br /&gt;
         @age = age&lt;br /&gt;
         @country=country&lt;br /&gt;
      end&lt;br /&gt;
      def to_s&lt;br /&gt;
            &amp;quot;In First: #{@name}, #{@age}, #{@country}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
      def marshal_dump&lt;br /&gt;
            [@name,@age]  #@country will not be serialized&lt;br /&gt;
      end&lt;br /&gt;
      def marshal_load(data)&lt;br /&gt;
       @name=data[0]&lt;br /&gt;
       @age=data[1]&lt;br /&gt;
       @country=&amp;quot;United States of America&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
      end&lt;br /&gt;
 &lt;br /&gt;
      x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
      puts x&lt;br /&gt;
  &lt;br /&gt;
      marshal_data = Marshal.dump( x ) &lt;br /&gt;
      y = Marshal.load( marshal_data ) &lt;br /&gt;
      p( y.to_s )&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    In First: Tom, 25, USA&lt;br /&gt;
    &amp;quot;In First: Tom, 25, United States of America&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== '''Serialization Performance in Ruby''' ==&lt;br /&gt;
The different serialization formats described above differ in the efficiency at which they can serialize and deserialize data and thus while serializing large amount of data their efficiency is taken into account. The .report[http://www.ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html] method in Ruby can be used to evaluate the performance of these serialization patterns. Marshal, as it handles data in binary format, is the most efficient form of serialization in Ruby. The following example compares the performance of marshal, JSON and YAML format.[http://www.pauldix.net/2008/08/serializing-dat.html]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   require 'benchmark'&lt;br /&gt;
   require 'rubygems'&lt;br /&gt;
   require 'json'&lt;br /&gt;
   require 'yaml'&lt;br /&gt;
   include Benchmark&lt;br /&gt;
   class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
      @name=name&lt;br /&gt;
      @age=age&lt;br /&gt;
      @country=country&lt;br /&gt;
     end&lt;br /&gt;
   end  &lt;br /&gt;
    x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
    puts &amp;quot;            User     System     Total      Real&amp;quot;&lt;br /&gt;
   benchmark do |t|&lt;br /&gt;
    print &amp;quot;Marshal:&amp;quot;&lt;br /&gt;
    t.report{1000.times do; Marshal.load(Marshal.dump(x));end}&lt;br /&gt;
    print &amp;quot;JSON:   &amp;quot;&lt;br /&gt;
    t.report{1000.times do; JSON.load(JSON.dump(x));end}&lt;br /&gt;
    print &amp;quot;YAML:   &amp;quot;&lt;br /&gt;
    t.report{1000.times do; YAML.load(YAML.dump(x));end}&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
              User     System     Total      Real&lt;br /&gt;
  Marshal:   0.010000   0.000000   0.010000 (  0.015408)&lt;br /&gt;
  JSON:      0.020000   0.010000   0.030000 (  0.022462)&lt;br /&gt;
  YAML:      0.460000   0.010000   0.470000 (  0.472149)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output of the above example shows that, given large chunks of data, marshal is comparatively more efficient format for serialization in Ruby.&lt;br /&gt;
&lt;br /&gt;
== Serialization in Object-Oriented Languages: Comparison ==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Sl.No !! Ruby !! Java !! .Net Framework !! C++&lt;br /&gt;
|-&lt;br /&gt;
| 1 || Ruby provides a built in module called 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 || Although, there is no built in support for serialization in C++, it can be achieved by using Boost libraries[http://www.boost.org/doc/libs/1_36_0/libs/serialization/example/demo.cpp]&lt;br /&gt;
|-&lt;br /&gt;
| 2 || The built in module of Ruby (Marshal) does not support platform independence, however, it can be achieved by using external libraries like [http://yaml.org YAML] and [http://www.w3schools.com/json/ JSON] || Similarly, Java's built in serialization is also not platform independent and in order to use serialization in Java across Ruby platform, jruby library should be used.  || .Net used Remoting technology to make it platform independent. || Serialization using the Boost libraries is not platform independent.&lt;br /&gt;
|-&lt;br /&gt;
| 3 || YAML provides a method [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/syck/rdoc/Object.html (to_yaml_properties)] which can be used to select the variables who's value is need to be serialized. With Marshal, we need to write a method named marshal_dump defining the variables of an object that has to be serialized. || Provides an option for serializing only the required 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 data 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 || Serialization using the Boost libraries is custom and thus the user can specify the part of the objects to be serialized.&lt;br /&gt;
|-&lt;br /&gt;
| 4 || Bindings, procedure objects, singleton objects, instances of IO objects and interfaces can not be serialized. Serializing these objects throws TypeError exceptions. || Thread, OutputStream and its subclasses, and Socket are not serializable in Java.[http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html] || Objects like DataRow are non serializable in .NET. Whether an object is serializable or not can be checked using the [http://msdn.microsoft.com/en-us/library/system.type.isserializable(v=vs.110).aspx?cs-save-lang=1&amp;amp;cs-lang=cpp#code-snippet-1 Type.IsSerializable] || There are no such objects in C++.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== See Also==&lt;br /&gt;
&lt;br /&gt;
1. [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/json/rdoc/JSON.html Serialization in Ruby JSON]&lt;br /&gt;
&lt;br /&gt;
2. [http://api.rubyonrails.org/classes/ActiveModel/Serialization.html Serialization in Rails]&lt;br /&gt;
&lt;br /&gt;
3. [http://gregmoreno.wordpress.com/2011/01/27/preventing-model-explosion-via-rails-serialization/ Article on Rails Serialization]&lt;br /&gt;
&lt;br /&gt;
4. [http://en.wikipedia.org/wiki/Google_Protocol_Buffers Protocol Buffers]&lt;br /&gt;
&lt;br /&gt;
5. [https://rubygems.org/gems/ruby-protocol-buffers Ruby Protocol Buffers Gem]&lt;br /&gt;
&lt;br /&gt;
6. [http://en.wikipedia.org/wiki/Apache_Avro Avro]&lt;br /&gt;
&lt;br /&gt;
7. [http://rubygems.org/gems/avro Ruby Avro Gem]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
1. [http://en.wikipedia.org/wiki/Serialization Serilization in General]&lt;br /&gt;
&lt;br /&gt;
2. [http://en.wikipedia.org/wiki/Marshalling_%28computer_science%29 Marshaling]&lt;br /&gt;
&lt;br /&gt;
3. [http://techbrahmana.blogspot.com/2012/03/rails-cookie-handling-serialization-and.html Rails Cookie Handling]&lt;br /&gt;
&lt;br /&gt;
4. [http://yaml.org/ YAML]&lt;br /&gt;
&lt;br /&gt;
5. [http://www.ruby-doc.org/core-2.1.0/Marshal.html Marshal module]&lt;br /&gt;
&lt;br /&gt;
6. [http://json.org/ JSON]&lt;br /&gt;
&lt;br /&gt;
7. [http://rubygems.org/ Ruby Gems]&lt;br /&gt;
&lt;br /&gt;
8. [http://ruby-doc.org/core-2.0.0/ARGF.html# Ruby ARGF Documentation]&lt;br /&gt;
&lt;br /&gt;
9. [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html Ruby Documentation for Benchmark Gem]&lt;br /&gt;
&lt;br /&gt;
10. [http://www.pauldix.net/2008/08/serializing-dat.html Serialization Performance Comparison]&lt;br /&gt;
&lt;br /&gt;
11. [http://www.boost.org/doc/libs/1_36_0/libs/serialization/example/demo.cpp Boost Libraries]&lt;br /&gt;
&lt;br /&gt;
12. [http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html Java Serialization]&lt;br /&gt;
&lt;br /&gt;
13. [http://www.w3schools.com/json/ JSON Tutorial]&lt;br /&gt;
&lt;br /&gt;
14. [http://www.skorks.com/2010/04/serializing-and-deserializing-objects-with-ruby/ Serializing and De-serializing in Ruby]&lt;br /&gt;
&lt;br /&gt;
15. [http://www.codeproject.com/Articles/33296/Serialization-and-De-serialization Serialization and De-serialization]&lt;br /&gt;
&lt;br /&gt;
16. [http://msdn.microsoft.com/en-us/library/182eeyhh.aspx XML Serialization]&lt;br /&gt;
&lt;br /&gt;
17. [http://www.tutorialspoint.com/java/java_serialization.htm Java Serialization Tutorial]&lt;br /&gt;
&lt;br /&gt;
18. [http://www.sapphiresteel.com/ruby-programming/The-Book-Of-Ruby The Book of Ruby]&lt;br /&gt;
&lt;br /&gt;
19. [http://www.codeproject.com/Articles/20962/Introducing-Serialization-in-NET Serialization in .Net]&lt;br /&gt;
&lt;br /&gt;
20. [http://ruby.about.com/od/advancedruby/ss/Serialization-In-Ruby-Yaml.html Serialization in Ruby YAML]&lt;br /&gt;
&lt;br /&gt;
21. [http://ruby.about.com/od/tasks/a/The-Json-Gem.html Installing JSON gem]&lt;br /&gt;
&lt;br /&gt;
22. [http://www.ruby-doc.org/stdlib-2.0.0/libdoc/json/rdoc/JSON.html Serialization in Ruby JSON]&lt;/div&gt;</summary>
		<author><name>Mmehta2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1d_mm&amp;diff=83400</id>
		<title>CSC/ECE 517 Spring 2014/ch1a 1d mm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1d_mm&amp;diff=83400"/>
		<updated>2014-02-18T02:02:52Z</updated>

		<summary type="html">&lt;p&gt;Mmehta2: /* Converting Ruby Objects to YAML format */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== '''Serialization''' ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Serialization'''[http://en.wikipedia.org/wiki/Serialization] is a process of converting a data structure or an object into a stream of bytes or string to facilitate storage in memory, file (persistence storage) or transmission over a network. The process of Serialization is also referred to as ''Marshalling''[http://en.wikipedia.org/wiki/Marshalling_%28computer_science%29]. 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. &lt;br /&gt;
&lt;br /&gt;
'''Basic Advantages of Serialization''':&lt;br /&gt;
&lt;br /&gt;
1. Communication between two or more processes on same machine. Object state can be saved and shared in a persistent or in-memory store.&lt;br /&gt;
&lt;br /&gt;
2. Communication between processes on different machines. Serialization facilitates the transmission of an object through a network.&lt;br /&gt;
&lt;br /&gt;
3. Creating a clone of an object. &lt;br /&gt;
&lt;br /&gt;
4. Cross-platform compatibility. Object can be serialized in a common format that is understood by multiple platforms. For example, JSON and XML.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''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''.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Few Practical Applications for Serialization'''&lt;br /&gt;
&lt;br /&gt;
1. [http://docs.oracle.com/cd/E13222_01/wls/docs81/webapp/sessions.html#139726 HTTP Session] Replication by sharing session objects across web servers for load balancing and handling failover scenarios.&lt;br /&gt;
&lt;br /&gt;
2. Serialization facilitates communication in [http://en.wikipedia.org/wiki/Java_remote_method_invocation Remote Method Invocation] or [http://en.wikipedia.org/wiki/Remote_procedure_call Remote procedure calls]&lt;br /&gt;
&lt;br /&gt;
3. Rails Cookie Handling[http://techbrahmana.blogspot.com/2012/03/rails-cookie-handling-serialization-and.html]. Cookies are stored marshalled/unmarshalled to and from client machines. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:SerializationChart.jpg]]&lt;br /&gt;
&lt;br /&gt;
== '''Serialization in Ruby:''' ==&lt;br /&gt;
&lt;br /&gt;
Ruby provides serialization capabilities through its module, [http://www.ruby-doc.org/core-2.0.0/Marshal.html Marshal]. There are also other libraries like YAML and JSON which can be used in Ruby to generate serialized objects for purposes like platform independence and human readable formats.&lt;br /&gt;
&lt;br /&gt;
== '''Types of Serialization''' ==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
=== Converting Ruby Objects in Human Readable Formats ===&lt;br /&gt;
The conversion of Ruby objects into YAML and JSON formats are explained below.&lt;br /&gt;
&lt;br /&gt;
==== Converting Ruby Objects to YAML format ====&lt;br /&gt;
&lt;br /&gt;
YAML 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 binary format. 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. In order to use it in Ruby, the yaml.rb file is required to be loaded which provides methods for converting objects into yaml format and creating .yml files.&lt;br /&gt;
&lt;br /&gt;
'''Examples of serialization using YAML:'''&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;nowiki&amp;gt; #Serialization using YAML's to_yaml method&lt;br /&gt;
 &lt;br /&gt;
  require &amp;quot;yaml&amp;quot;&lt;br /&gt;
     class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
	@name = name&lt;br /&gt;
	@age = age&lt;br /&gt;
	@country=country&lt;br /&gt;
     end&lt;br /&gt;
      def to_s&lt;br /&gt;
	&amp;quot;In First:\n#{@name}, #{@age}, #{@country}\n&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
  x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
  puts x&lt;br /&gt;
  puts x.to_yaml&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   In First:&lt;br /&gt;
   Tom, 25, USA&lt;br /&gt;
   --- !ruby/object:First&lt;br /&gt;
   name: Tom&lt;br /&gt;
   age: 25&lt;br /&gt;
   country: USA&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The above code displays the object x, first as a string and then in the yaml format. &lt;br /&gt;
&lt;br /&gt;
'''Saving YAML data into a file:'''&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt; # Serialization using YAML::dump&lt;br /&gt;
&lt;br /&gt;
require 'yaml'&lt;br /&gt;
&lt;br /&gt;
f = File.open( 'first.yml', 'w' )&lt;br /&gt;
YAML.dump( [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;], f )&lt;br /&gt;
f.close&lt;br /&gt;
		&lt;br /&gt;
File.open( 'first.yml' ){ |f|	&lt;br /&gt;
    $arr= YAML.load(f)&lt;br /&gt;
}	&lt;br /&gt;
&lt;br /&gt;
p( $arr )&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;]&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The dump function can be used to serialize the data and save it into a file in YAML format. As shown in the above example the data in YAML format can be de-serialized using the load function.&lt;br /&gt;
&lt;br /&gt;
YAML libraries also provides an option of selecting only those variables of the object that are needed to be serialized. This is done using the to_yaml_properties method as shown in the below example.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;nowiki&amp;gt;  #Custom Serialization using YAML&lt;br /&gt;
 &lt;br /&gt;
  require &amp;quot;yaml&amp;quot;&lt;br /&gt;
     class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
	@name = name&lt;br /&gt;
	@age = age&lt;br /&gt;
	@country=country&lt;br /&gt;
     end&lt;br /&gt;
     def to_s&lt;br /&gt;
	&amp;quot;In First:\n#{@name}, #{@age}, #{@country}\n&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
      def to_yaml_properties&lt;br /&gt;
	   [&amp;quot;@name&amp;quot;,&amp;quot;@age&amp;quot;]  #@country will not be serialized&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
  x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
  puts x&lt;br /&gt;
  puts x.to_yaml&lt;br /&gt;
  &amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   In First:&lt;br /&gt;
   Tom, 25, USA&lt;br /&gt;
   --- !ruby/object:First&lt;br /&gt;
   name: Tom&lt;br /&gt;
   age: 25&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Converting Ruby Objects to JSON format: ====&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
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. Ruby 1.8.7 distribution is not bundled with json gem. However, in Ruby 1.9.2, the json gem is bundled with the core Ruby distribution.&lt;br /&gt;
The JSON library can be installed using Ruby Gems[http://rubygems.org/] as shown below:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;# gem install json&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A JSON string for serialization can be created by using the JSON.generate method:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
        require 'json'&lt;br /&gt;
        my_hash = {:Welcome =&amp;gt; &amp;quot;Ruby&amp;quot;}&lt;br /&gt;
        puts JSON.generate(my_hash) =&amp;gt; &amp;quot;{\&amp;quot;WELCOME\&amp;quot;:\&amp;quot;RUBY\&amp;quot;}&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
'''Output:'''&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;{&amp;quot;{\&amp;quot;Welcome\&amp;quot;:\&amp;quot;Ruby\&amp;quot;}&amp;quot;=&amp;gt;&amp;quot;{\&amp;quot;WELCOME\&amp;quot;:\&amp;quot;RUBY\&amp;quot;}&amp;quot;}&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A JSON string received from another program can be parsed by using JSON.parse&lt;br /&gt;
Ruby thus converts String to Hash.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
        require 'json'&lt;br /&gt;
        my_hash = JSON.parse('{&amp;quot;Welcome&amp;quot;: &amp;quot;Ruby&amp;quot;}')&lt;br /&gt;
        puts my_hash[&amp;quot;Welcome&amp;quot;] =&amp;gt; &amp;quot;Ruby&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Converting Ruby Objects to Binary Formats ===&lt;br /&gt;
Binary Serialization is another form of serialization in Ruby which is not in human readable form. 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. Binary Serialization  is done using Marshal which is built into Ruby and the code for it is written in Ruby's Marshal module(marshal.c) and thus no additional files are required in order to use it. The _dump and _load methods defined in marshal are used for serialization. Using marshal module the following types of objects can not be serialized: bindings, procedure objects, singleton objects, instances of IO objects and interfaces.&lt;br /&gt;
&lt;br /&gt;
Since the Binary Serialized data is not in human readable form, the following guidelines need to be followed.&lt;br /&gt;
     1.Use print instead of puts[http://ruby-doc.org/core-2.0.0/ARGF.html#] when serialized objects are written to a file in order to avoid new line characters to be written &lt;br /&gt;
       in the file.&lt;br /&gt;
     &lt;br /&gt;
     2.Use a record separator in order to differentiate between two objects.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Binary Serialization Example:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    class Animal&lt;br /&gt;
      def initialize  name, age&lt;br /&gt;
      @name = name&lt;br /&gt;
      @age=age&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
   class Cat &amp;lt; Animal&lt;br /&gt;
    def to_s&lt;br /&gt;
     &amp;quot;In Cat C: #{@name} \t #{@age}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
  c = Cat.new(&amp;quot;Kitty Kat&amp;quot;,5)&lt;br /&gt;
  puts &amp;quot;Before Serialization&amp;quot;&lt;br /&gt;
  puts c&lt;br /&gt;
  #puts d&lt;br /&gt;
  serialize_cat= Marshal.dump(c) #dumps the serialized cat object into serialize_cat&lt;br /&gt;
  puts &amp;quot;\nAfter Serialization:\n #{serialize_cat}&amp;quot;&lt;br /&gt;
  deserialize_cat= Marshal::load(serialize_cat) #deserializes the cat object and loads it back into deserialize_cat&lt;br /&gt;
  puts &amp;quot;\nAfter Deserialization\n #{deserialize_cat}&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   Before Serialization&lt;br /&gt;
   In Cat C: Kitty Kat 	 5&lt;br /&gt;
   After Serialization:&lt;br /&gt;
   oCat:&lt;br /&gt;
   @nameI&amp;quot;Kitty Kat:ET:	@agei&lt;br /&gt;
   After Deserialization&lt;br /&gt;
   In Cat C: Kitty Kat 	 5&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Similar to YAML, Marshal can also be used to dump data into a file. The above example showing the serialization using YAML::dump can be written using marshal as shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    f = File.open( 'first.yml', 'w' )&lt;br /&gt;
    Marshal.dump( [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;], f )&lt;br /&gt;
    f.close	&lt;br /&gt;
    File.open( 'first.yml' ){ |f|	&lt;br /&gt;
    $arr= Marshal.load(f)&lt;br /&gt;
    }	&lt;br /&gt;
    p( $arr )&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;]&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice that in the above example there is no &amp;quot;require&amp;quot; statement as opposed to the earlier example of writing serialized data into files using YAML. This is because unlike YAML, marshal is built-in Ruby and  no external libraries is required in order to use its functionality.&lt;br /&gt;
&lt;br /&gt;
Marshal can also be used to custom serialize an object i.e. it provides an option to omit the variables of an object that are not required in the serialized data. The following program shows the use of marshal_dump method for achieving custom serialization.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
      class First&lt;br /&gt;
      def initialize(name, age, country)&lt;br /&gt;
         @name = name&lt;br /&gt;
         @age = age&lt;br /&gt;
         @country=country&lt;br /&gt;
      end&lt;br /&gt;
      def to_s&lt;br /&gt;
            &amp;quot;In First: #{@name}, #{@age}, #{@country}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
      def marshal_dump&lt;br /&gt;
            [@name,@age]  #@country will not be serialized&lt;br /&gt;
      end&lt;br /&gt;
      def marshal_load(data)&lt;br /&gt;
       @name=data[0]&lt;br /&gt;
       @age=data[1]&lt;br /&gt;
       @country=&amp;quot;United States of America&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
      end&lt;br /&gt;
 &lt;br /&gt;
      x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
      puts x&lt;br /&gt;
  &lt;br /&gt;
      marshal_data = Marshal.dump( x ) &lt;br /&gt;
      y = Marshal.load( marshal_data ) &lt;br /&gt;
      p( y.to_s )&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    In First: Tom, 25, USA&lt;br /&gt;
    &amp;quot;In First: Tom, 25, United States of America&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== '''Serialization Performance in Ruby''' ==&lt;br /&gt;
The different serialization formats described above differ in the efficiency at which they can serialize and deserialize data and thus while serializing large amount of data their efficiency is taken into account. The .report[http://www.ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html] method in Ruby can be used to evaluate the performance of these serialization patterns. Marshal, as it handles data in binary format, is the most efficient form of serialization in Ruby. The following example compares the performance of marshal, JSON and YAML format.[http://www.pauldix.net/2008/08/serializing-dat.html]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   require 'benchmark'&lt;br /&gt;
   require 'rubygems'&lt;br /&gt;
   require 'json'&lt;br /&gt;
   require 'yaml'&lt;br /&gt;
   include Benchmark&lt;br /&gt;
   class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
      @name=name&lt;br /&gt;
      @age=age&lt;br /&gt;
      @country=country&lt;br /&gt;
     end&lt;br /&gt;
   end  &lt;br /&gt;
    x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
    puts &amp;quot;            User     System     Total      Real&amp;quot;&lt;br /&gt;
   benchmark do |t|&lt;br /&gt;
    print &amp;quot;Marshal:&amp;quot;&lt;br /&gt;
    t.report{1000.times do; Marshal.load(Marshal.dump(x));end}&lt;br /&gt;
    print &amp;quot;JSON:   &amp;quot;&lt;br /&gt;
    t.report{1000.times do; JSON.load(JSON.dump(x));end}&lt;br /&gt;
    print &amp;quot;YAML:   &amp;quot;&lt;br /&gt;
    t.report{1000.times do; YAML.load(YAML.dump(x));end}&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
              User     System     Total      Real&lt;br /&gt;
  Marshal:   0.010000   0.000000   0.010000 (  0.015408)&lt;br /&gt;
  JSON:      0.020000   0.010000   0.030000 (  0.022462)&lt;br /&gt;
  YAML:      0.460000   0.010000   0.470000 (  0.472149)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output of the above example shows that, given large chunks of data, marshal is comparatively more efficient format for serialization in Ruby.&lt;br /&gt;
&lt;br /&gt;
== Serialization in Object-Oriented Languages: Comparison ==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Sl.No !! Ruby !! Java !! .Net Framework !! C++&lt;br /&gt;
|-&lt;br /&gt;
| 1 || Ruby provides a built in module called 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 || Although, there is no built in support for serialization in C++, it can be achieved by using Boost libraries[http://www.boost.org/doc/libs/1_36_0/libs/serialization/example/demo.cpp]&lt;br /&gt;
|-&lt;br /&gt;
| 2 || The built in module of Ruby (Marshal) does not support platform independence, however, it can be achieved by using external libraries like [http://yaml.org YAML] and [http://www.w3schools.com/json/ JSON] || Similarly, Java's built in serialization is also not platform independent and in order to use serialization in Java across Ruby platform, jruby library should be used.  || .Net used Remoting technology to make it platform independent. || Serialization using the Boost libraries is not platform independent.&lt;br /&gt;
|-&lt;br /&gt;
| 3 || YAML provides a method [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/syck/rdoc/Object.html (to_yaml_properties)] which can be used to select the variables who's value is need to be serialized. With Marshal, we need to write a method named marshal_dump defining the variables of an object that has to be serialized. || Provides an option for serializing only the required 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 data 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 || Serialization using the Boost libraries is custom and thus the user can specify the part of the objects to be serialized.&lt;br /&gt;
|-&lt;br /&gt;
| 4 || Bindings, procedure objects, singleton objects, instances of IO objects and interfaces can not be serialized. Serializing these objects throws TypeError exceptions. || Thread, OutputStream and its subclasses, and Socket are not serializable in Java.[http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html] || Objects like DataRow are non serializable in .NET. Whether an object is serializable or not can be checked using the [http://msdn.microsoft.com/en-us/library/system.type.isserializable(v=vs.110).aspx?cs-save-lang=1&amp;amp;cs-lang=cpp#code-snippet-1 Type.IsSerializable] || There are no such objects in C++.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== See Also==&lt;br /&gt;
&lt;br /&gt;
1. [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/json/rdoc/JSON.html Serialization in Ruby JSON]&lt;br /&gt;
&lt;br /&gt;
2. [http://api.rubyonrails.org/classes/ActiveModel/Serialization.html Serialization in Rails]&lt;br /&gt;
&lt;br /&gt;
3. [http://gregmoreno.wordpress.com/2011/01/27/preventing-model-explosion-via-rails-serialization/ Article on Rails Serialization]&lt;br /&gt;
&lt;br /&gt;
4. [http://en.wikipedia.org/wiki/Google_Protocol_Buffers Protocol Buffers]&lt;br /&gt;
&lt;br /&gt;
5. [https://rubygems.org/gems/ruby-protocol-buffers Ruby Protocol Buffers Gem]&lt;br /&gt;
&lt;br /&gt;
6. [http://en.wikipedia.org/wiki/Apache_Avro Avro]&lt;br /&gt;
&lt;br /&gt;
7. [http://rubygems.org/gems/avro Ruby Avro Gem]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
1. [http://en.wikipedia.org/wiki/Serialization Serilization in General]&lt;br /&gt;
&lt;br /&gt;
2. [http://en.wikipedia.org/wiki/Marshalling_%28computer_science%29 Marshaling]&lt;br /&gt;
&lt;br /&gt;
3. [http://techbrahmana.blogspot.com/2012/03/rails-cookie-handling-serialization-and.html Rails Cookie Handling]&lt;br /&gt;
&lt;br /&gt;
4. [http://yaml.org/ YAML]&lt;br /&gt;
&lt;br /&gt;
5. [http://www.ruby-doc.org/core-2.1.0/Marshal.html Marshal module]&lt;br /&gt;
&lt;br /&gt;
6. [http://json.org/ JSON]&lt;br /&gt;
&lt;br /&gt;
7. [http://rubygems.org/ Ruby Gems]&lt;br /&gt;
&lt;br /&gt;
8. [http://ruby-doc.org/core-2.0.0/ARGF.html# Ruby ARGF Documentation]&lt;br /&gt;
&lt;br /&gt;
9. [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html Ruby Documentation for Benchmark Gem]&lt;br /&gt;
&lt;br /&gt;
10. [http://www.pauldix.net/2008/08/serializing-dat.html Serialization Performance Comparison]&lt;br /&gt;
&lt;br /&gt;
11. [http://www.boost.org/doc/libs/1_36_0/libs/serialization/example/demo.cpp Boost Libraries]&lt;br /&gt;
&lt;br /&gt;
12. [http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html Java Serialization]&lt;br /&gt;
&lt;br /&gt;
13. [http://www.w3schools.com/json/ JSON Tutorial]&lt;br /&gt;
&lt;br /&gt;
14. [http://www.skorks.com/2010/04/serializing-and-deserializing-objects-with-ruby/ Serializing and De-serializing in Ruby]&lt;br /&gt;
&lt;br /&gt;
15. [http://www.codeproject.com/Articles/33296/Serialization-and-De-serialization Serialization and De-serialization]&lt;br /&gt;
&lt;br /&gt;
16. [http://msdn.microsoft.com/en-us/library/182eeyhh.aspx XML Serialization]&lt;br /&gt;
&lt;br /&gt;
17. [http://www.tutorialspoint.com/java/java_serialization.htm Java Serialization Tutorial]&lt;br /&gt;
&lt;br /&gt;
18. [http://www.sapphiresteel.com/ruby-programming/The-Book-Of-Ruby The Book of Ruby]&lt;br /&gt;
&lt;br /&gt;
19. [http://www.codeproject.com/Articles/20962/Introducing-Serialization-in-NET Serialization in .Net]&lt;br /&gt;
&lt;br /&gt;
20. [http://ruby.about.com/od/advancedruby/ss/Serialization-In-Ruby-Yaml.html Serialization in Ruby YAML]&lt;br /&gt;
&lt;br /&gt;
21. [http://ruby.about.com/od/tasks/a/The-Json-Gem.html Installing JSON gem]&lt;br /&gt;
&lt;br /&gt;
22. [http://www.ruby-doc.org/stdlib-2.0.0/libdoc/json/rdoc/JSON.html Serialization in Ruby JSON]&lt;/div&gt;</summary>
		<author><name>Mmehta2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1d_mm&amp;diff=83399</id>
		<title>CSC/ECE 517 Spring 2014/ch1a 1d mm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1d_mm&amp;diff=83399"/>
		<updated>2014-02-18T02:01:25Z</updated>

		<summary type="html">&lt;p&gt;Mmehta2: /* Serialization */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== '''Serialization''' ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Serialization'''[http://en.wikipedia.org/wiki/Serialization] is a process of converting a data structure or an object into a stream of bytes or string to facilitate storage in memory, file (persistence storage) or transmission over a network. The process of Serialization is also referred to as ''Marshalling''[http://en.wikipedia.org/wiki/Marshalling_%28computer_science%29]. 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. &lt;br /&gt;
&lt;br /&gt;
'''Basic Advantages of Serialization''':&lt;br /&gt;
&lt;br /&gt;
1. Communication between two or more processes on same machine. Object state can be saved and shared in a persistent or in-memory store.&lt;br /&gt;
&lt;br /&gt;
2. Communication between processes on different machines. Serialization facilitates the transmission of an object through a network.&lt;br /&gt;
&lt;br /&gt;
3. Creating a clone of an object. &lt;br /&gt;
&lt;br /&gt;
4. Cross-platform compatibility. Object can be serialized in a common format that is understood by multiple platforms. For example, JSON and XML.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''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''.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Few Practical Applications for Serialization'''&lt;br /&gt;
&lt;br /&gt;
1. [http://docs.oracle.com/cd/E13222_01/wls/docs81/webapp/sessions.html#139726 HTTP Session] Replication by sharing session objects across web servers for load balancing and handling failover scenarios.&lt;br /&gt;
&lt;br /&gt;
2. Serialization facilitates communication in [http://en.wikipedia.org/wiki/Java_remote_method_invocation Remote Method Invocation] or [http://en.wikipedia.org/wiki/Remote_procedure_call Remote procedure calls]&lt;br /&gt;
&lt;br /&gt;
3. Rails Cookie Handling[http://techbrahmana.blogspot.com/2012/03/rails-cookie-handling-serialization-and.html]. Cookies are stored marshalled/unmarshalled to and from client machines. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:SerializationChart.jpg]]&lt;br /&gt;
&lt;br /&gt;
== '''Serialization in Ruby:''' ==&lt;br /&gt;
&lt;br /&gt;
Ruby provides serialization capabilities through its module, [http://www.ruby-doc.org/core-2.0.0/Marshal.html Marshal]. There are also other libraries like YAML and JSON which can be used in Ruby to generate serialized objects for purposes like platform independence and human readable formats.&lt;br /&gt;
&lt;br /&gt;
== '''Types of Serialization''' ==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
=== Converting Ruby Objects in Human Readable Formats ===&lt;br /&gt;
The conversion of Ruby objects into YAML and JSON formats are explained below.&lt;br /&gt;
&lt;br /&gt;
==== Converting Ruby Objects to YAML format ====&lt;br /&gt;
&lt;br /&gt;
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 binary format. 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. In order to use it in Ruby, the yaml.rb file is required to be loaded which provides methods for converting objects into yaml format and creating .yml files.&lt;br /&gt;
&lt;br /&gt;
'''Examples of serialization using YAML:'''&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;nowiki&amp;gt; #Serialization using YAML's to_yaml method&lt;br /&gt;
 &lt;br /&gt;
  require &amp;quot;yaml&amp;quot;&lt;br /&gt;
     class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
	@name = name&lt;br /&gt;
	@age = age&lt;br /&gt;
	@country=country&lt;br /&gt;
     end&lt;br /&gt;
      def to_s&lt;br /&gt;
	&amp;quot;In First:\n#{@name}, #{@age}, #{@country}\n&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
  x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
  puts x&lt;br /&gt;
  puts x.to_yaml&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   In First:&lt;br /&gt;
   Tom, 25, USA&lt;br /&gt;
   --- !ruby/object:First&lt;br /&gt;
   name: Tom&lt;br /&gt;
   age: 25&lt;br /&gt;
   country: USA&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The above code displays the object x, first as a string and then in the yaml format. &lt;br /&gt;
&lt;br /&gt;
'''Saving YAML data into a file:'''&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt; # Serialization using YAML::dump&lt;br /&gt;
&lt;br /&gt;
require 'yaml'&lt;br /&gt;
&lt;br /&gt;
f = File.open( 'first.yml', 'w' )&lt;br /&gt;
YAML.dump( [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;], f )&lt;br /&gt;
f.close&lt;br /&gt;
		&lt;br /&gt;
File.open( 'first.yml' ){ |f|	&lt;br /&gt;
    $arr= YAML.load(f)&lt;br /&gt;
}	&lt;br /&gt;
&lt;br /&gt;
p( $arr )&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;]&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The dump function can be used to serialize the data and save it into a file in YAML format. As shown in the above example the data in YAML format can be de-serialized using the load function.&lt;br /&gt;
&lt;br /&gt;
YAML libraries also provides an option of selecting only those variables of the object that are needed to be serialized. This is done using the to_yaml_properties method as shown in the below example.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;nowiki&amp;gt;  #Custom Serialization using YAML&lt;br /&gt;
 &lt;br /&gt;
  require &amp;quot;yaml&amp;quot;&lt;br /&gt;
     class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
	@name = name&lt;br /&gt;
	@age = age&lt;br /&gt;
	@country=country&lt;br /&gt;
     end&lt;br /&gt;
     def to_s&lt;br /&gt;
	&amp;quot;In First:\n#{@name}, #{@age}, #{@country}\n&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
      def to_yaml_properties&lt;br /&gt;
	   [&amp;quot;@name&amp;quot;,&amp;quot;@age&amp;quot;]  #@country will not be serialized&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
  x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
  puts x&lt;br /&gt;
  puts x.to_yaml&lt;br /&gt;
  &amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   In First:&lt;br /&gt;
   Tom, 25, USA&lt;br /&gt;
   --- !ruby/object:First&lt;br /&gt;
   name: Tom&lt;br /&gt;
   age: 25&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Converting Ruby Objects to JSON format: ====&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
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. Ruby 1.8.7 distribution is not bundled with json gem. However, in Ruby 1.9.2, the json gem is bundled with the core Ruby distribution.&lt;br /&gt;
The JSON library can be installed using Ruby Gems[http://rubygems.org/] as shown below:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;# gem install json&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A JSON string for serialization can be created by using the JSON.generate method:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
        require 'json'&lt;br /&gt;
        my_hash = {:Welcome =&amp;gt; &amp;quot;Ruby&amp;quot;}&lt;br /&gt;
        puts JSON.generate(my_hash) =&amp;gt; &amp;quot;{\&amp;quot;WELCOME\&amp;quot;:\&amp;quot;RUBY\&amp;quot;}&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
'''Output:'''&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;{&amp;quot;{\&amp;quot;Welcome\&amp;quot;:\&amp;quot;Ruby\&amp;quot;}&amp;quot;=&amp;gt;&amp;quot;{\&amp;quot;WELCOME\&amp;quot;:\&amp;quot;RUBY\&amp;quot;}&amp;quot;}&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A JSON string received from another program can be parsed by using JSON.parse&lt;br /&gt;
Ruby thus converts String to Hash.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
        require 'json'&lt;br /&gt;
        my_hash = JSON.parse('{&amp;quot;Welcome&amp;quot;: &amp;quot;Ruby&amp;quot;}')&lt;br /&gt;
        puts my_hash[&amp;quot;Welcome&amp;quot;] =&amp;gt; &amp;quot;Ruby&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Converting Ruby Objects to Binary Formats ===&lt;br /&gt;
Binary Serialization is another form of serialization in Ruby which is not in human readable form. 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. Binary Serialization  is done using Marshal which is built into Ruby and the code for it is written in Ruby's Marshal module(marshal.c) and thus no additional files are required in order to use it. The _dump and _load methods defined in marshal are used for serialization. Using marshal module the following types of objects can not be serialized: bindings, procedure objects, singleton objects, instances of IO objects and interfaces.&lt;br /&gt;
&lt;br /&gt;
Since the Binary Serialized data is not in human readable form, the following guidelines need to be followed.&lt;br /&gt;
     1.Use print instead of puts[http://ruby-doc.org/core-2.0.0/ARGF.html#] when serialized objects are written to a file in order to avoid new line characters to be written &lt;br /&gt;
       in the file.&lt;br /&gt;
     &lt;br /&gt;
     2.Use a record separator in order to differentiate between two objects.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Binary Serialization Example:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    class Animal&lt;br /&gt;
      def initialize  name, age&lt;br /&gt;
      @name = name&lt;br /&gt;
      @age=age&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
   class Cat &amp;lt; Animal&lt;br /&gt;
    def to_s&lt;br /&gt;
     &amp;quot;In Cat C: #{@name} \t #{@age}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
  c = Cat.new(&amp;quot;Kitty Kat&amp;quot;,5)&lt;br /&gt;
  puts &amp;quot;Before Serialization&amp;quot;&lt;br /&gt;
  puts c&lt;br /&gt;
  #puts d&lt;br /&gt;
  serialize_cat= Marshal.dump(c) #dumps the serialized cat object into serialize_cat&lt;br /&gt;
  puts &amp;quot;\nAfter Serialization:\n #{serialize_cat}&amp;quot;&lt;br /&gt;
  deserialize_cat= Marshal::load(serialize_cat) #deserializes the cat object and loads it back into deserialize_cat&lt;br /&gt;
  puts &amp;quot;\nAfter Deserialization\n #{deserialize_cat}&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   Before Serialization&lt;br /&gt;
   In Cat C: Kitty Kat 	 5&lt;br /&gt;
   After Serialization:&lt;br /&gt;
   oCat:&lt;br /&gt;
   @nameI&amp;quot;Kitty Kat:ET:	@agei&lt;br /&gt;
   After Deserialization&lt;br /&gt;
   In Cat C: Kitty Kat 	 5&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Similar to YAML, Marshal can also be used to dump data into a file. The above example showing the serialization using YAML::dump can be written using marshal as shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    f = File.open( 'first.yml', 'w' )&lt;br /&gt;
    Marshal.dump( [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;], f )&lt;br /&gt;
    f.close	&lt;br /&gt;
    File.open( 'first.yml' ){ |f|	&lt;br /&gt;
    $arr= Marshal.load(f)&lt;br /&gt;
    }	&lt;br /&gt;
    p( $arr )&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;]&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice that in the above example there is no &amp;quot;require&amp;quot; statement as opposed to the earlier example of writing serialized data into files using YAML. This is because unlike YAML, marshal is built-in Ruby and  no external libraries is required in order to use its functionality.&lt;br /&gt;
&lt;br /&gt;
Marshal can also be used to custom serialize an object i.e. it provides an option to omit the variables of an object that are not required in the serialized data. The following program shows the use of marshal_dump method for achieving custom serialization.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
      class First&lt;br /&gt;
      def initialize(name, age, country)&lt;br /&gt;
         @name = name&lt;br /&gt;
         @age = age&lt;br /&gt;
         @country=country&lt;br /&gt;
      end&lt;br /&gt;
      def to_s&lt;br /&gt;
            &amp;quot;In First: #{@name}, #{@age}, #{@country}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
      def marshal_dump&lt;br /&gt;
            [@name,@age]  #@country will not be serialized&lt;br /&gt;
      end&lt;br /&gt;
      def marshal_load(data)&lt;br /&gt;
       @name=data[0]&lt;br /&gt;
       @age=data[1]&lt;br /&gt;
       @country=&amp;quot;United States of America&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
      end&lt;br /&gt;
 &lt;br /&gt;
      x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
      puts x&lt;br /&gt;
  &lt;br /&gt;
      marshal_data = Marshal.dump( x ) &lt;br /&gt;
      y = Marshal.load( marshal_data ) &lt;br /&gt;
      p( y.to_s )&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    In First: Tom, 25, USA&lt;br /&gt;
    &amp;quot;In First: Tom, 25, United States of America&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== '''Serialization Performance in Ruby''' ==&lt;br /&gt;
The different serialization formats described above differ in the efficiency at which they can serialize and deserialize data and thus while serializing large amount of data their efficiency is taken into account. The .report[http://www.ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html] method in Ruby can be used to evaluate the performance of these serialization patterns. Marshal, as it handles data in binary format, is the most efficient form of serialization in Ruby. The following example compares the performance of marshal, JSON and YAML format.[http://www.pauldix.net/2008/08/serializing-dat.html]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   require 'benchmark'&lt;br /&gt;
   require 'rubygems'&lt;br /&gt;
   require 'json'&lt;br /&gt;
   require 'yaml'&lt;br /&gt;
   include Benchmark&lt;br /&gt;
   class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
      @name=name&lt;br /&gt;
      @age=age&lt;br /&gt;
      @country=country&lt;br /&gt;
     end&lt;br /&gt;
   end  &lt;br /&gt;
    x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
    puts &amp;quot;            User     System     Total      Real&amp;quot;&lt;br /&gt;
   benchmark do |t|&lt;br /&gt;
    print &amp;quot;Marshal:&amp;quot;&lt;br /&gt;
    t.report{1000.times do; Marshal.load(Marshal.dump(x));end}&lt;br /&gt;
    print &amp;quot;JSON:   &amp;quot;&lt;br /&gt;
    t.report{1000.times do; JSON.load(JSON.dump(x));end}&lt;br /&gt;
    print &amp;quot;YAML:   &amp;quot;&lt;br /&gt;
    t.report{1000.times do; YAML.load(YAML.dump(x));end}&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
              User     System     Total      Real&lt;br /&gt;
  Marshal:   0.010000   0.000000   0.010000 (  0.015408)&lt;br /&gt;
  JSON:      0.020000   0.010000   0.030000 (  0.022462)&lt;br /&gt;
  YAML:      0.460000   0.010000   0.470000 (  0.472149)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output of the above example shows that, given large chunks of data, marshal is comparatively more efficient format for serialization in Ruby.&lt;br /&gt;
&lt;br /&gt;
== Serialization in Object-Oriented Languages: Comparison ==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Sl.No !! Ruby !! Java !! .Net Framework !! C++&lt;br /&gt;
|-&lt;br /&gt;
| 1 || Ruby provides a built in module called 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 || Although, there is no built in support for serialization in C++, it can be achieved by using Boost libraries[http://www.boost.org/doc/libs/1_36_0/libs/serialization/example/demo.cpp]&lt;br /&gt;
|-&lt;br /&gt;
| 2 || The built in module of Ruby (Marshal) does not support platform independence, however, it can be achieved by using external libraries like [http://yaml.org YAML] and [http://www.w3schools.com/json/ JSON] || Similarly, Java's built in serialization is also not platform independent and in order to use serialization in Java across Ruby platform, jruby library should be used.  || .Net used Remoting technology to make it platform independent. || Serialization using the Boost libraries is not platform independent.&lt;br /&gt;
|-&lt;br /&gt;
| 3 || YAML provides a method [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/syck/rdoc/Object.html (to_yaml_properties)] which can be used to select the variables who's value is need to be serialized. With Marshal, we need to write a method named marshal_dump defining the variables of an object that has to be serialized. || Provides an option for serializing only the required 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 data 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 || Serialization using the Boost libraries is custom and thus the user can specify the part of the objects to be serialized.&lt;br /&gt;
|-&lt;br /&gt;
| 4 || Bindings, procedure objects, singleton objects, instances of IO objects and interfaces can not be serialized. Serializing these objects throws TypeError exceptions. || Thread, OutputStream and its subclasses, and Socket are not serializable in Java.[http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html] || Objects like DataRow are non serializable in .NET. Whether an object is serializable or not can be checked using the [http://msdn.microsoft.com/en-us/library/system.type.isserializable(v=vs.110).aspx?cs-save-lang=1&amp;amp;cs-lang=cpp#code-snippet-1 Type.IsSerializable] || There are no such objects in C++.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== See Also==&lt;br /&gt;
&lt;br /&gt;
1. [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/json/rdoc/JSON.html Serialization in Ruby JSON]&lt;br /&gt;
&lt;br /&gt;
2. [http://api.rubyonrails.org/classes/ActiveModel/Serialization.html Serialization in Rails]&lt;br /&gt;
&lt;br /&gt;
3. [http://gregmoreno.wordpress.com/2011/01/27/preventing-model-explosion-via-rails-serialization/ Article on Rails Serialization]&lt;br /&gt;
&lt;br /&gt;
4. [http://en.wikipedia.org/wiki/Google_Protocol_Buffers Protocol Buffers]&lt;br /&gt;
&lt;br /&gt;
5. [https://rubygems.org/gems/ruby-protocol-buffers Ruby Protocol Buffers Gem]&lt;br /&gt;
&lt;br /&gt;
6. [http://en.wikipedia.org/wiki/Apache_Avro Avro]&lt;br /&gt;
&lt;br /&gt;
7. [http://rubygems.org/gems/avro Ruby Avro Gem]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
1. [http://en.wikipedia.org/wiki/Serialization Serilization in General]&lt;br /&gt;
&lt;br /&gt;
2. [http://en.wikipedia.org/wiki/Marshalling_%28computer_science%29 Marshaling]&lt;br /&gt;
&lt;br /&gt;
3. [http://techbrahmana.blogspot.com/2012/03/rails-cookie-handling-serialization-and.html Rails Cookie Handling]&lt;br /&gt;
&lt;br /&gt;
4. [http://yaml.org/ YAML]&lt;br /&gt;
&lt;br /&gt;
5. [http://www.ruby-doc.org/core-2.1.0/Marshal.html Marshal module]&lt;br /&gt;
&lt;br /&gt;
6. [http://json.org/ JSON]&lt;br /&gt;
&lt;br /&gt;
7. [http://rubygems.org/ Ruby Gems]&lt;br /&gt;
&lt;br /&gt;
8. [http://ruby-doc.org/core-2.0.0/ARGF.html# Ruby ARGF Documentation]&lt;br /&gt;
&lt;br /&gt;
9. [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html Ruby Documentation for Benchmark Gem]&lt;br /&gt;
&lt;br /&gt;
10. [http://www.pauldix.net/2008/08/serializing-dat.html Serialization Performance Comparison]&lt;br /&gt;
&lt;br /&gt;
11. [http://www.boost.org/doc/libs/1_36_0/libs/serialization/example/demo.cpp Boost Libraries]&lt;br /&gt;
&lt;br /&gt;
12. [http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html Java Serialization]&lt;br /&gt;
&lt;br /&gt;
13. [http://www.w3schools.com/json/ JSON Tutorial]&lt;br /&gt;
&lt;br /&gt;
14. [http://www.skorks.com/2010/04/serializing-and-deserializing-objects-with-ruby/ Serializing and De-serializing in Ruby]&lt;br /&gt;
&lt;br /&gt;
15. [http://www.codeproject.com/Articles/33296/Serialization-and-De-serialization Serialization and De-serialization]&lt;br /&gt;
&lt;br /&gt;
16. [http://msdn.microsoft.com/en-us/library/182eeyhh.aspx XML Serialization]&lt;br /&gt;
&lt;br /&gt;
17. [http://www.tutorialspoint.com/java/java_serialization.htm Java Serialization Tutorial]&lt;br /&gt;
&lt;br /&gt;
18. [http://www.sapphiresteel.com/ruby-programming/The-Book-Of-Ruby The Book of Ruby]&lt;br /&gt;
&lt;br /&gt;
19. [http://www.codeproject.com/Articles/20962/Introducing-Serialization-in-NET Serialization in .Net]&lt;br /&gt;
&lt;br /&gt;
20. [http://ruby.about.com/od/advancedruby/ss/Serialization-In-Ruby-Yaml.html Serialization in Ruby YAML]&lt;br /&gt;
&lt;br /&gt;
21. [http://ruby.about.com/od/tasks/a/The-Json-Gem.html Installing JSON gem]&lt;br /&gt;
&lt;br /&gt;
22. [http://www.ruby-doc.org/stdlib-2.0.0/libdoc/json/rdoc/JSON.html Serialization in Ruby JSON]&lt;/div&gt;</summary>
		<author><name>Mmehta2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1d_mm&amp;diff=83398</id>
		<title>CSC/ECE 517 Spring 2014/ch1a 1d mm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1d_mm&amp;diff=83398"/>
		<updated>2014-02-18T01:59:39Z</updated>

		<summary type="html">&lt;p&gt;Mmehta2: /* Serialization in Object-Oriented Languages: Comparison */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== '''Serialization''' ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Serialization'''[http://en.wikipedia.org/wiki/Serialization] is a process of converting a data structure or an object into a stream of bytes or string to facilitate storage in memory, file (persistence storage) or transmission over a network. The process of Serialization is also referred to as ''Marshalling''[http://en.wikipedia.org/wiki/Marshalling_%28computer_science%29]. 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. &lt;br /&gt;
&lt;br /&gt;
'''Basic Advantages of Serialization''':&lt;br /&gt;
&lt;br /&gt;
1. Communication between two or more processes on same machine. Object state can be saved and shared in a persistent or in-memory store.&lt;br /&gt;
&lt;br /&gt;
2. Communication between processes on different machines. Serialization facilitates the transmission of an object through a network.&lt;br /&gt;
&lt;br /&gt;
3. Creating a clone of an object. &lt;br /&gt;
&lt;br /&gt;
4. Cross-platform compatibility. Object can be serialized in a common format that is understood by multiple platforms. For example, JSON and XML.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''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''.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Few Practical Applications for Serialization'''&lt;br /&gt;
&lt;br /&gt;
1. [http://en.wikipedia.org/wiki/HTTP#HTTP_session HTTP Session] Replication by sharing session objects across web servers for handling failover scenarios&lt;br /&gt;
&lt;br /&gt;
2. Serialization facilitates communication in [http://en.wikipedia.org/wiki/Java_remote_method_invocation Remote Method Invocation] or [http://en.wikipedia.org/wiki/Remote_procedure_call Remote procedure calls]&lt;br /&gt;
&lt;br /&gt;
3. Rails Cookie Handling[http://techbrahmana.blogspot.com/2012/03/rails-cookie-handling-serialization-and.html]. Cookies are stored marshalled/unmarshalled to and from client machines. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:SerializationChart.jpg]]&lt;br /&gt;
&lt;br /&gt;
== '''Serialization in Ruby:''' ==&lt;br /&gt;
&lt;br /&gt;
Ruby provides serialization capabilities through its module, [http://www.ruby-doc.org/core-2.0.0/Marshal.html Marshal]. There are also other libraries like YAML and JSON which can be used in Ruby to generate serialized objects for purposes like platform independence and human readable formats.&lt;br /&gt;
&lt;br /&gt;
== '''Types of Serialization''' ==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
=== Converting Ruby Objects in Human Readable Formats ===&lt;br /&gt;
The conversion of Ruby objects into YAML and JSON formats are explained below.&lt;br /&gt;
&lt;br /&gt;
==== Converting Ruby Objects to YAML format ====&lt;br /&gt;
&lt;br /&gt;
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 binary format. 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. In order to use it in Ruby, the yaml.rb file is required to be loaded which provides methods for converting objects into yaml format and creating .yml files.&lt;br /&gt;
&lt;br /&gt;
'''Examples of serialization using YAML:'''&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;nowiki&amp;gt; #Serialization using YAML's to_yaml method&lt;br /&gt;
 &lt;br /&gt;
  require &amp;quot;yaml&amp;quot;&lt;br /&gt;
     class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
	@name = name&lt;br /&gt;
	@age = age&lt;br /&gt;
	@country=country&lt;br /&gt;
     end&lt;br /&gt;
      def to_s&lt;br /&gt;
	&amp;quot;In First:\n#{@name}, #{@age}, #{@country}\n&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
  x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
  puts x&lt;br /&gt;
  puts x.to_yaml&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   In First:&lt;br /&gt;
   Tom, 25, USA&lt;br /&gt;
   --- !ruby/object:First&lt;br /&gt;
   name: Tom&lt;br /&gt;
   age: 25&lt;br /&gt;
   country: USA&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The above code displays the object x, first as a string and then in the yaml format. &lt;br /&gt;
&lt;br /&gt;
'''Saving YAML data into a file:'''&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt; # Serialization using YAML::dump&lt;br /&gt;
&lt;br /&gt;
require 'yaml'&lt;br /&gt;
&lt;br /&gt;
f = File.open( 'first.yml', 'w' )&lt;br /&gt;
YAML.dump( [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;], f )&lt;br /&gt;
f.close&lt;br /&gt;
		&lt;br /&gt;
File.open( 'first.yml' ){ |f|	&lt;br /&gt;
    $arr= YAML.load(f)&lt;br /&gt;
}	&lt;br /&gt;
&lt;br /&gt;
p( $arr )&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;]&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The dump function can be used to serialize the data and save it into a file in YAML format. As shown in the above example the data in YAML format can be de-serialized using the load function.&lt;br /&gt;
&lt;br /&gt;
YAML libraries also provides an option of selecting only those variables of the object that are needed to be serialized. This is done using the to_yaml_properties method as shown in the below example.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;nowiki&amp;gt;  #Custom Serialization using YAML&lt;br /&gt;
 &lt;br /&gt;
  require &amp;quot;yaml&amp;quot;&lt;br /&gt;
     class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
	@name = name&lt;br /&gt;
	@age = age&lt;br /&gt;
	@country=country&lt;br /&gt;
     end&lt;br /&gt;
     def to_s&lt;br /&gt;
	&amp;quot;In First:\n#{@name}, #{@age}, #{@country}\n&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
      def to_yaml_properties&lt;br /&gt;
	   [&amp;quot;@name&amp;quot;,&amp;quot;@age&amp;quot;]  #@country will not be serialized&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
  x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
  puts x&lt;br /&gt;
  puts x.to_yaml&lt;br /&gt;
  &amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   In First:&lt;br /&gt;
   Tom, 25, USA&lt;br /&gt;
   --- !ruby/object:First&lt;br /&gt;
   name: Tom&lt;br /&gt;
   age: 25&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Converting Ruby Objects to JSON format: ====&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
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. Ruby 1.8.7 distribution is not bundled with json gem. However, in Ruby 1.9.2, the json gem is bundled with the core Ruby distribution.&lt;br /&gt;
The JSON library can be installed using Ruby Gems[http://rubygems.org/] as shown below:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;# gem install json&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A JSON string for serialization can be created by using the JSON.generate method:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
        require 'json'&lt;br /&gt;
        my_hash = {:Welcome =&amp;gt; &amp;quot;Ruby&amp;quot;}&lt;br /&gt;
        puts JSON.generate(my_hash) =&amp;gt; &amp;quot;{\&amp;quot;WELCOME\&amp;quot;:\&amp;quot;RUBY\&amp;quot;}&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
'''Output:'''&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;{&amp;quot;{\&amp;quot;Welcome\&amp;quot;:\&amp;quot;Ruby\&amp;quot;}&amp;quot;=&amp;gt;&amp;quot;{\&amp;quot;WELCOME\&amp;quot;:\&amp;quot;RUBY\&amp;quot;}&amp;quot;}&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A JSON string received from another program can be parsed by using JSON.parse&lt;br /&gt;
Ruby thus converts String to Hash.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
        require 'json'&lt;br /&gt;
        my_hash = JSON.parse('{&amp;quot;Welcome&amp;quot;: &amp;quot;Ruby&amp;quot;}')&lt;br /&gt;
        puts my_hash[&amp;quot;Welcome&amp;quot;] =&amp;gt; &amp;quot;Ruby&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Converting Ruby Objects to Binary Formats ===&lt;br /&gt;
Binary Serialization is another form of serialization in Ruby which is not in human readable form. 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. Binary Serialization  is done using Marshal which is built into Ruby and the code for it is written in Ruby's Marshal module(marshal.c) and thus no additional files are required in order to use it. The _dump and _load methods defined in marshal are used for serialization. Using marshal module the following types of objects can not be serialized: bindings, procedure objects, singleton objects, instances of IO objects and interfaces.&lt;br /&gt;
&lt;br /&gt;
Since the Binary Serialized data is not in human readable form, the following guidelines need to be followed.&lt;br /&gt;
     1.Use print instead of puts[http://ruby-doc.org/core-2.0.0/ARGF.html#] when serialized objects are written to a file in order to avoid new line characters to be written &lt;br /&gt;
       in the file.&lt;br /&gt;
     &lt;br /&gt;
     2.Use a record separator in order to differentiate between two objects.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Binary Serialization Example:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    class Animal&lt;br /&gt;
      def initialize  name, age&lt;br /&gt;
      @name = name&lt;br /&gt;
      @age=age&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
   class Cat &amp;lt; Animal&lt;br /&gt;
    def to_s&lt;br /&gt;
     &amp;quot;In Cat C: #{@name} \t #{@age}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
  c = Cat.new(&amp;quot;Kitty Kat&amp;quot;,5)&lt;br /&gt;
  puts &amp;quot;Before Serialization&amp;quot;&lt;br /&gt;
  puts c&lt;br /&gt;
  #puts d&lt;br /&gt;
  serialize_cat= Marshal.dump(c) #dumps the serialized cat object into serialize_cat&lt;br /&gt;
  puts &amp;quot;\nAfter Serialization:\n #{serialize_cat}&amp;quot;&lt;br /&gt;
  deserialize_cat= Marshal::load(serialize_cat) #deserializes the cat object and loads it back into deserialize_cat&lt;br /&gt;
  puts &amp;quot;\nAfter Deserialization\n #{deserialize_cat}&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   Before Serialization&lt;br /&gt;
   In Cat C: Kitty Kat 	 5&lt;br /&gt;
   After Serialization:&lt;br /&gt;
   oCat:&lt;br /&gt;
   @nameI&amp;quot;Kitty Kat:ET:	@agei&lt;br /&gt;
   After Deserialization&lt;br /&gt;
   In Cat C: Kitty Kat 	 5&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Similar to YAML, Marshal can also be used to dump data into a file. The above example showing the serialization using YAML::dump can be written using marshal as shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    f = File.open( 'first.yml', 'w' )&lt;br /&gt;
    Marshal.dump( [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;], f )&lt;br /&gt;
    f.close	&lt;br /&gt;
    File.open( 'first.yml' ){ |f|	&lt;br /&gt;
    $arr= Marshal.load(f)&lt;br /&gt;
    }	&lt;br /&gt;
    p( $arr )&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;]&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice that in the above example there is no &amp;quot;require&amp;quot; statement as opposed to the earlier example of writing serialized data into files using YAML. This is because unlike YAML, marshal is built-in Ruby and  no external libraries is required in order to use its functionality.&lt;br /&gt;
&lt;br /&gt;
Marshal can also be used to custom serialize an object i.e. it provides an option to omit the variables of an object that are not required in the serialized data. The following program shows the use of marshal_dump method for achieving custom serialization.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
      class First&lt;br /&gt;
      def initialize(name, age, country)&lt;br /&gt;
         @name = name&lt;br /&gt;
         @age = age&lt;br /&gt;
         @country=country&lt;br /&gt;
      end&lt;br /&gt;
      def to_s&lt;br /&gt;
            &amp;quot;In First: #{@name}, #{@age}, #{@country}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
      def marshal_dump&lt;br /&gt;
            [@name,@age]  #@country will not be serialized&lt;br /&gt;
      end&lt;br /&gt;
      def marshal_load(data)&lt;br /&gt;
       @name=data[0]&lt;br /&gt;
       @age=data[1]&lt;br /&gt;
       @country=&amp;quot;United States of America&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
      end&lt;br /&gt;
 &lt;br /&gt;
      x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
      puts x&lt;br /&gt;
  &lt;br /&gt;
      marshal_data = Marshal.dump( x ) &lt;br /&gt;
      y = Marshal.load( marshal_data ) &lt;br /&gt;
      p( y.to_s )&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    In First: Tom, 25, USA&lt;br /&gt;
    &amp;quot;In First: Tom, 25, United States of America&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== '''Serialization Performance in Ruby''' ==&lt;br /&gt;
The different serialization formats described above differ in the efficiency at which they can serialize and deserialize data and thus while serializing large amount of data their efficiency is taken into account. The .report[http://www.ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html] method in Ruby can be used to evaluate the performance of these serialization patterns. Marshal, as it handles data in binary format, is the most efficient form of serialization in Ruby. The following example compares the performance of marshal, JSON and YAML format.[http://www.pauldix.net/2008/08/serializing-dat.html]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   require 'benchmark'&lt;br /&gt;
   require 'rubygems'&lt;br /&gt;
   require 'json'&lt;br /&gt;
   require 'yaml'&lt;br /&gt;
   include Benchmark&lt;br /&gt;
   class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
      @name=name&lt;br /&gt;
      @age=age&lt;br /&gt;
      @country=country&lt;br /&gt;
     end&lt;br /&gt;
   end  &lt;br /&gt;
    x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
    puts &amp;quot;            User     System     Total      Real&amp;quot;&lt;br /&gt;
   benchmark do |t|&lt;br /&gt;
    print &amp;quot;Marshal:&amp;quot;&lt;br /&gt;
    t.report{1000.times do; Marshal.load(Marshal.dump(x));end}&lt;br /&gt;
    print &amp;quot;JSON:   &amp;quot;&lt;br /&gt;
    t.report{1000.times do; JSON.load(JSON.dump(x));end}&lt;br /&gt;
    print &amp;quot;YAML:   &amp;quot;&lt;br /&gt;
    t.report{1000.times do; YAML.load(YAML.dump(x));end}&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
              User     System     Total      Real&lt;br /&gt;
  Marshal:   0.010000   0.000000   0.010000 (  0.015408)&lt;br /&gt;
  JSON:      0.020000   0.010000   0.030000 (  0.022462)&lt;br /&gt;
  YAML:      0.460000   0.010000   0.470000 (  0.472149)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output of the above example shows that, given large chunks of data, marshal is comparatively more efficient format for serialization in Ruby.&lt;br /&gt;
&lt;br /&gt;
== Serialization in Object-Oriented Languages: Comparison ==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Sl.No !! Ruby !! Java !! .Net Framework !! C++&lt;br /&gt;
|-&lt;br /&gt;
| 1 || Ruby provides a built in module called 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 || Although, there is no built in support for serialization in C++, it can be achieved by using Boost libraries[http://www.boost.org/doc/libs/1_36_0/libs/serialization/example/demo.cpp]&lt;br /&gt;
|-&lt;br /&gt;
| 2 || The built in module of Ruby (Marshal) does not support platform independence, however, it can be achieved by using external libraries like [http://yaml.org YAML] and [http://www.w3schools.com/json/ JSON] || Similarly, Java's built in serialization is also not platform independent and in order to use serialization in Java across Ruby platform, jruby library should be used.  || .Net used Remoting technology to make it platform independent. || Serialization using the Boost libraries is not platform independent.&lt;br /&gt;
|-&lt;br /&gt;
| 3 || YAML provides a method [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/syck/rdoc/Object.html (to_yaml_properties)] which can be used to select the variables who's value is need to be serialized. With Marshal, we need to write a method named marshal_dump defining the variables of an object that has to be serialized. || Provides an option for serializing only the required 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 data 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 || Serialization using the Boost libraries is custom and thus the user can specify the part of the objects to be serialized.&lt;br /&gt;
|-&lt;br /&gt;
| 4 || Bindings, procedure objects, singleton objects, instances of IO objects and interfaces can not be serialized. Serializing these objects throws TypeError exceptions. || Thread, OutputStream and its subclasses, and Socket are not serializable in Java.[http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html] || Objects like DataRow are non serializable in .NET. Whether an object is serializable or not can be checked using the [http://msdn.microsoft.com/en-us/library/system.type.isserializable(v=vs.110).aspx?cs-save-lang=1&amp;amp;cs-lang=cpp#code-snippet-1 Type.IsSerializable] || There are no such objects in C++.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== See Also==&lt;br /&gt;
&lt;br /&gt;
1. [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/json/rdoc/JSON.html Serialization in Ruby JSON]&lt;br /&gt;
&lt;br /&gt;
2. [http://api.rubyonrails.org/classes/ActiveModel/Serialization.html Serialization in Rails]&lt;br /&gt;
&lt;br /&gt;
3. [http://gregmoreno.wordpress.com/2011/01/27/preventing-model-explosion-via-rails-serialization/ Article on Rails Serialization]&lt;br /&gt;
&lt;br /&gt;
4. [http://en.wikipedia.org/wiki/Google_Protocol_Buffers Protocol Buffers]&lt;br /&gt;
&lt;br /&gt;
5. [https://rubygems.org/gems/ruby-protocol-buffers Ruby Protocol Buffers Gem]&lt;br /&gt;
&lt;br /&gt;
6. [http://en.wikipedia.org/wiki/Apache_Avro Avro]&lt;br /&gt;
&lt;br /&gt;
7. [http://rubygems.org/gems/avro Ruby Avro Gem]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
1. [http://en.wikipedia.org/wiki/Serialization Serilization in General]&lt;br /&gt;
&lt;br /&gt;
2. [http://en.wikipedia.org/wiki/Marshalling_%28computer_science%29 Marshaling]&lt;br /&gt;
&lt;br /&gt;
3. [http://techbrahmana.blogspot.com/2012/03/rails-cookie-handling-serialization-and.html Rails Cookie Handling]&lt;br /&gt;
&lt;br /&gt;
4. [http://yaml.org/ YAML]&lt;br /&gt;
&lt;br /&gt;
5. [http://www.ruby-doc.org/core-2.1.0/Marshal.html Marshal module]&lt;br /&gt;
&lt;br /&gt;
6. [http://json.org/ JSON]&lt;br /&gt;
&lt;br /&gt;
7. [http://rubygems.org/ Ruby Gems]&lt;br /&gt;
&lt;br /&gt;
8. [http://ruby-doc.org/core-2.0.0/ARGF.html# Ruby ARGF Documentation]&lt;br /&gt;
&lt;br /&gt;
9. [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html Ruby Documentation for Benchmark Gem]&lt;br /&gt;
&lt;br /&gt;
10. [http://www.pauldix.net/2008/08/serializing-dat.html Serialization Performance Comparison]&lt;br /&gt;
&lt;br /&gt;
11. [http://www.boost.org/doc/libs/1_36_0/libs/serialization/example/demo.cpp Boost Libraries]&lt;br /&gt;
&lt;br /&gt;
12. [http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html Java Serialization]&lt;br /&gt;
&lt;br /&gt;
13. [http://www.w3schools.com/json/ JSON Tutorial]&lt;br /&gt;
&lt;br /&gt;
14. [http://www.skorks.com/2010/04/serializing-and-deserializing-objects-with-ruby/ Serializing and De-serializing in Ruby]&lt;br /&gt;
&lt;br /&gt;
15. [http://www.codeproject.com/Articles/33296/Serialization-and-De-serialization Serialization and De-serialization]&lt;br /&gt;
&lt;br /&gt;
16. [http://msdn.microsoft.com/en-us/library/182eeyhh.aspx XML Serialization]&lt;br /&gt;
&lt;br /&gt;
17. [http://www.tutorialspoint.com/java/java_serialization.htm Java Serialization Tutorial]&lt;br /&gt;
&lt;br /&gt;
18. [http://www.sapphiresteel.com/ruby-programming/The-Book-Of-Ruby The Book of Ruby]&lt;br /&gt;
&lt;br /&gt;
19. [http://www.codeproject.com/Articles/20962/Introducing-Serialization-in-NET Serialization in .Net]&lt;br /&gt;
&lt;br /&gt;
20. [http://ruby.about.com/od/advancedruby/ss/Serialization-In-Ruby-Yaml.html Serialization in Ruby YAML]&lt;br /&gt;
&lt;br /&gt;
21. [http://ruby.about.com/od/tasks/a/The-Json-Gem.html Installing JSON gem]&lt;br /&gt;
&lt;br /&gt;
22. [http://www.ruby-doc.org/stdlib-2.0.0/libdoc/json/rdoc/JSON.html Serialization in Ruby JSON]&lt;/div&gt;</summary>
		<author><name>Mmehta2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1d_mm&amp;diff=83397</id>
		<title>CSC/ECE 517 Spring 2014/ch1a 1d mm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1d_mm&amp;diff=83397"/>
		<updated>2014-02-18T01:58:36Z</updated>

		<summary type="html">&lt;p&gt;Mmehta2: /* Converting Ruby Objects to Binary Formats */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== '''Serialization''' ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Serialization'''[http://en.wikipedia.org/wiki/Serialization] is a process of converting a data structure or an object into a stream of bytes or string to facilitate storage in memory, file (persistence storage) or transmission over a network. The process of Serialization is also referred to as ''Marshalling''[http://en.wikipedia.org/wiki/Marshalling_%28computer_science%29]. 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. &lt;br /&gt;
&lt;br /&gt;
'''Basic Advantages of Serialization''':&lt;br /&gt;
&lt;br /&gt;
1. Communication between two or more processes on same machine. Object state can be saved and shared in a persistent or in-memory store.&lt;br /&gt;
&lt;br /&gt;
2. Communication between processes on different machines. Serialization facilitates the transmission of an object through a network.&lt;br /&gt;
&lt;br /&gt;
3. Creating a clone of an object. &lt;br /&gt;
&lt;br /&gt;
4. Cross-platform compatibility. Object can be serialized in a common format that is understood by multiple platforms. For example, JSON and XML.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''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''.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Few Practical Applications for Serialization'''&lt;br /&gt;
&lt;br /&gt;
1. [http://en.wikipedia.org/wiki/HTTP#HTTP_session HTTP Session] Replication by sharing session objects across web servers for handling failover scenarios&lt;br /&gt;
&lt;br /&gt;
2. Serialization facilitates communication in [http://en.wikipedia.org/wiki/Java_remote_method_invocation Remote Method Invocation] or [http://en.wikipedia.org/wiki/Remote_procedure_call Remote procedure calls]&lt;br /&gt;
&lt;br /&gt;
3. Rails Cookie Handling[http://techbrahmana.blogspot.com/2012/03/rails-cookie-handling-serialization-and.html]. Cookies are stored marshalled/unmarshalled to and from client machines. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:SerializationChart.jpg]]&lt;br /&gt;
&lt;br /&gt;
== '''Serialization in Ruby:''' ==&lt;br /&gt;
&lt;br /&gt;
Ruby provides serialization capabilities through its module, [http://www.ruby-doc.org/core-2.0.0/Marshal.html Marshal]. There are also other libraries like YAML and JSON which can be used in Ruby to generate serialized objects for purposes like platform independence and human readable formats.&lt;br /&gt;
&lt;br /&gt;
== '''Types of Serialization''' ==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
=== Converting Ruby Objects in Human Readable Formats ===&lt;br /&gt;
The conversion of Ruby objects into YAML and JSON formats are explained below.&lt;br /&gt;
&lt;br /&gt;
==== Converting Ruby Objects to YAML format ====&lt;br /&gt;
&lt;br /&gt;
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 binary format. 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. In order to use it in Ruby, the yaml.rb file is required to be loaded which provides methods for converting objects into yaml format and creating .yml files.&lt;br /&gt;
&lt;br /&gt;
'''Examples of serialization using YAML:'''&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;nowiki&amp;gt; #Serialization using YAML's to_yaml method&lt;br /&gt;
 &lt;br /&gt;
  require &amp;quot;yaml&amp;quot;&lt;br /&gt;
     class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
	@name = name&lt;br /&gt;
	@age = age&lt;br /&gt;
	@country=country&lt;br /&gt;
     end&lt;br /&gt;
      def to_s&lt;br /&gt;
	&amp;quot;In First:\n#{@name}, #{@age}, #{@country}\n&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
  x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
  puts x&lt;br /&gt;
  puts x.to_yaml&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   In First:&lt;br /&gt;
   Tom, 25, USA&lt;br /&gt;
   --- !ruby/object:First&lt;br /&gt;
   name: Tom&lt;br /&gt;
   age: 25&lt;br /&gt;
   country: USA&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The above code displays the object x, first as a string and then in the yaml format. &lt;br /&gt;
&lt;br /&gt;
'''Saving YAML data into a file:'''&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt; # Serialization using YAML::dump&lt;br /&gt;
&lt;br /&gt;
require 'yaml'&lt;br /&gt;
&lt;br /&gt;
f = File.open( 'first.yml', 'w' )&lt;br /&gt;
YAML.dump( [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;], f )&lt;br /&gt;
f.close&lt;br /&gt;
		&lt;br /&gt;
File.open( 'first.yml' ){ |f|	&lt;br /&gt;
    $arr= YAML.load(f)&lt;br /&gt;
}	&lt;br /&gt;
&lt;br /&gt;
p( $arr )&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;]&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The dump function can be used to serialize the data and save it into a file in YAML format. As shown in the above example the data in YAML format can be de-serialized using the load function.&lt;br /&gt;
&lt;br /&gt;
YAML libraries also provides an option of selecting only those variables of the object that are needed to be serialized. This is done using the to_yaml_properties method as shown in the below example.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;nowiki&amp;gt;  #Custom Serialization using YAML&lt;br /&gt;
 &lt;br /&gt;
  require &amp;quot;yaml&amp;quot;&lt;br /&gt;
     class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
	@name = name&lt;br /&gt;
	@age = age&lt;br /&gt;
	@country=country&lt;br /&gt;
     end&lt;br /&gt;
     def to_s&lt;br /&gt;
	&amp;quot;In First:\n#{@name}, #{@age}, #{@country}\n&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
      def to_yaml_properties&lt;br /&gt;
	   [&amp;quot;@name&amp;quot;,&amp;quot;@age&amp;quot;]  #@country will not be serialized&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
  x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
  puts x&lt;br /&gt;
  puts x.to_yaml&lt;br /&gt;
  &amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   In First:&lt;br /&gt;
   Tom, 25, USA&lt;br /&gt;
   --- !ruby/object:First&lt;br /&gt;
   name: Tom&lt;br /&gt;
   age: 25&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Converting Ruby Objects to JSON format: ====&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
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. Ruby 1.8.7 distribution is not bundled with json gem. However, in Ruby 1.9.2, the json gem is bundled with the core Ruby distribution.&lt;br /&gt;
The JSON library can be installed using Ruby Gems[http://rubygems.org/] as shown below:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;# gem install json&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A JSON string for serialization can be created by using the JSON.generate method:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
        require 'json'&lt;br /&gt;
        my_hash = {:Welcome =&amp;gt; &amp;quot;Ruby&amp;quot;}&lt;br /&gt;
        puts JSON.generate(my_hash) =&amp;gt; &amp;quot;{\&amp;quot;WELCOME\&amp;quot;:\&amp;quot;RUBY\&amp;quot;}&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
'''Output:'''&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;{&amp;quot;{\&amp;quot;Welcome\&amp;quot;:\&amp;quot;Ruby\&amp;quot;}&amp;quot;=&amp;gt;&amp;quot;{\&amp;quot;WELCOME\&amp;quot;:\&amp;quot;RUBY\&amp;quot;}&amp;quot;}&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A JSON string received from another program can be parsed by using JSON.parse&lt;br /&gt;
Ruby thus converts String to Hash.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
        require 'json'&lt;br /&gt;
        my_hash = JSON.parse('{&amp;quot;Welcome&amp;quot;: &amp;quot;Ruby&amp;quot;}')&lt;br /&gt;
        puts my_hash[&amp;quot;Welcome&amp;quot;] =&amp;gt; &amp;quot;Ruby&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Converting Ruby Objects to Binary Formats ===&lt;br /&gt;
Binary Serialization is another form of serialization in Ruby which is not in human readable form. 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. Binary Serialization  is done using Marshal which is built into Ruby and the code for it is written in Ruby's Marshal module(marshal.c) and thus no additional files are required in order to use it. The _dump and _load methods defined in marshal are used for serialization. Using marshal module the following types of objects can not be serialized: bindings, procedure objects, singleton objects, instances of IO objects and interfaces.&lt;br /&gt;
&lt;br /&gt;
Since the Binary Serialized data is not in human readable form, the following guidelines need to be followed.&lt;br /&gt;
     1.Use print instead of puts[http://ruby-doc.org/core-2.0.0/ARGF.html#] when serialized objects are written to a file in order to avoid new line characters to be written &lt;br /&gt;
       in the file.&lt;br /&gt;
     &lt;br /&gt;
     2.Use a record separator in order to differentiate between two objects.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Binary Serialization Example:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    class Animal&lt;br /&gt;
      def initialize  name, age&lt;br /&gt;
      @name = name&lt;br /&gt;
      @age=age&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
   class Cat &amp;lt; Animal&lt;br /&gt;
    def to_s&lt;br /&gt;
     &amp;quot;In Cat C: #{@name} \t #{@age}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
  c = Cat.new(&amp;quot;Kitty Kat&amp;quot;,5)&lt;br /&gt;
  puts &amp;quot;Before Serialization&amp;quot;&lt;br /&gt;
  puts c&lt;br /&gt;
  #puts d&lt;br /&gt;
  serialize_cat= Marshal.dump(c) #dumps the serialized cat object into serialize_cat&lt;br /&gt;
  puts &amp;quot;\nAfter Serialization:\n #{serialize_cat}&amp;quot;&lt;br /&gt;
  deserialize_cat= Marshal::load(serialize_cat) #deserializes the cat object and loads it back into deserialize_cat&lt;br /&gt;
  puts &amp;quot;\nAfter Deserialization\n #{deserialize_cat}&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   Before Serialization&lt;br /&gt;
   In Cat C: Kitty Kat 	 5&lt;br /&gt;
   After Serialization:&lt;br /&gt;
   oCat:&lt;br /&gt;
   @nameI&amp;quot;Kitty Kat:ET:	@agei&lt;br /&gt;
   After Deserialization&lt;br /&gt;
   In Cat C: Kitty Kat 	 5&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Similar to YAML, Marshal can also be used to dump data into a file. The above example showing the serialization using YAML::dump can be written using marshal as shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    f = File.open( 'first.yml', 'w' )&lt;br /&gt;
    Marshal.dump( [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;], f )&lt;br /&gt;
    f.close	&lt;br /&gt;
    File.open( 'first.yml' ){ |f|	&lt;br /&gt;
    $arr= Marshal.load(f)&lt;br /&gt;
    }	&lt;br /&gt;
    p( $arr )&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;]&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice that in the above example there is no &amp;quot;require&amp;quot; statement as opposed to the earlier example of writing serialized data into files using YAML. This is because unlike YAML, marshal is built-in Ruby and  no external libraries is required in order to use its functionality.&lt;br /&gt;
&lt;br /&gt;
Marshal can also be used to custom serialize an object i.e. it provides an option to omit the variables of an object that are not required in the serialized data. The following program shows the use of marshal_dump method for achieving custom serialization.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
      class First&lt;br /&gt;
      def initialize(name, age, country)&lt;br /&gt;
         @name = name&lt;br /&gt;
         @age = age&lt;br /&gt;
         @country=country&lt;br /&gt;
      end&lt;br /&gt;
      def to_s&lt;br /&gt;
            &amp;quot;In First: #{@name}, #{@age}, #{@country}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
      def marshal_dump&lt;br /&gt;
            [@name,@age]  #@country will not be serialized&lt;br /&gt;
      end&lt;br /&gt;
      def marshal_load(data)&lt;br /&gt;
       @name=data[0]&lt;br /&gt;
       @age=data[1]&lt;br /&gt;
       @country=&amp;quot;United States of America&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
      end&lt;br /&gt;
 &lt;br /&gt;
      x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
      puts x&lt;br /&gt;
  &lt;br /&gt;
      marshal_data = Marshal.dump( x ) &lt;br /&gt;
      y = Marshal.load( marshal_data ) &lt;br /&gt;
      p( y.to_s )&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    In First: Tom, 25, USA&lt;br /&gt;
    &amp;quot;In First: Tom, 25, United States of America&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== '''Serialization Performance in Ruby''' ==&lt;br /&gt;
The different serialization formats described above differ in the efficiency at which they can serialize and deserialize data and thus while serializing large amount of data their efficiency is taken into account. The .report[http://www.ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html] method in Ruby can be used to evaluate the performance of these serialization patterns. Marshal, as it handles data in binary format, is the most efficient form of serialization in Ruby. The following example compares the performance of marshal, JSON and YAML format.[http://www.pauldix.net/2008/08/serializing-dat.html]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   require 'benchmark'&lt;br /&gt;
   require 'rubygems'&lt;br /&gt;
   require 'json'&lt;br /&gt;
   require 'yaml'&lt;br /&gt;
   include Benchmark&lt;br /&gt;
   class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
      @name=name&lt;br /&gt;
      @age=age&lt;br /&gt;
      @country=country&lt;br /&gt;
     end&lt;br /&gt;
   end  &lt;br /&gt;
    x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
    puts &amp;quot;            User     System     Total      Real&amp;quot;&lt;br /&gt;
   benchmark do |t|&lt;br /&gt;
    print &amp;quot;Marshal:&amp;quot;&lt;br /&gt;
    t.report{1000.times do; Marshal.load(Marshal.dump(x));end}&lt;br /&gt;
    print &amp;quot;JSON:   &amp;quot;&lt;br /&gt;
    t.report{1000.times do; JSON.load(JSON.dump(x));end}&lt;br /&gt;
    print &amp;quot;YAML:   &amp;quot;&lt;br /&gt;
    t.report{1000.times do; YAML.load(YAML.dump(x));end}&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
              User     System     Total      Real&lt;br /&gt;
  Marshal:   0.010000   0.000000   0.010000 (  0.015408)&lt;br /&gt;
  JSON:      0.020000   0.010000   0.030000 (  0.022462)&lt;br /&gt;
  YAML:      0.460000   0.010000   0.470000 (  0.472149)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output of the above example shows that, given large chunks of data, marshal is comparatively more efficient format for serialization in Ruby.&lt;br /&gt;
&lt;br /&gt;
== Serialization in Object-Oriented Languages: Comparison ==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Sl.No !! Ruby !! Java !! .Net Framework !! C++&lt;br /&gt;
|-&lt;br /&gt;
| 1 || Ruby provides a built in 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 || Although, there is no built in support for serialization in C++, it can be achieved by using Boost libraries[http://www.boost.org/doc/libs/1_36_0/libs/serialization/example/demo.cpp]&lt;br /&gt;
|-&lt;br /&gt;
| 2 || The built in module of Ruby [http://www.ruby-doc.org/core-2.0.0/Marshal.html (Marshal)] does not support platform independence, however, it can be achieved by using external libraries like [http://yaml.org YAML] and [http://www.w3schools.com/json/ JSON] || Similarly, Java's built in serialization is also not platform independent and in order to use serialization in Java across Ruby platform, jruby library should be used.  || .Net used Remoting technology to make it platform independent. || Serialization using the Boost libraries is not platform independent.&lt;br /&gt;
|-&lt;br /&gt;
| 3 || YAML provides a method [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/syck/rdoc/Object.html (to_yaml_properties)] which can be used to select the variables who's value is need to be serialized. With Marshal, we need to write a method named marshal_dump defining the variables of an object that has to be serialized. || Provides an option for serializing only the required 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 data 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 || Serialization using the Boost libraries is custom and thus the user can specify the part of the objects to be serialized.&lt;br /&gt;
|-&lt;br /&gt;
| 4 || Bindings, procedure objects, singleton objects, instances of IO objects and interfaces can not be serialized. Serializing these objects throws TypeError exceptions. || Thread, OutputStream and its subclasses, and Socket are not serializable in Java.[http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html] || Objects like DataRow are non serializable in .NET. Whether an object is serializable or not can be checked using the [http://msdn.microsoft.com/en-us/library/system.type.isserializable(v=vs.110).aspx?cs-save-lang=1&amp;amp;cs-lang=cpp#code-snippet-1 Type.IsSerializable] || There are no such objects in C++.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== See Also==&lt;br /&gt;
&lt;br /&gt;
1. [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/json/rdoc/JSON.html Serialization in Ruby JSON]&lt;br /&gt;
&lt;br /&gt;
2. [http://api.rubyonrails.org/classes/ActiveModel/Serialization.html Serialization in Rails]&lt;br /&gt;
&lt;br /&gt;
3. [http://gregmoreno.wordpress.com/2011/01/27/preventing-model-explosion-via-rails-serialization/ Article on Rails Serialization]&lt;br /&gt;
&lt;br /&gt;
4. [http://en.wikipedia.org/wiki/Google_Protocol_Buffers Protocol Buffers]&lt;br /&gt;
&lt;br /&gt;
5. [https://rubygems.org/gems/ruby-protocol-buffers Ruby Protocol Buffers Gem]&lt;br /&gt;
&lt;br /&gt;
6. [http://en.wikipedia.org/wiki/Apache_Avro Avro]&lt;br /&gt;
&lt;br /&gt;
7. [http://rubygems.org/gems/avro Ruby Avro Gem]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
1. [http://en.wikipedia.org/wiki/Serialization Serilization in General]&lt;br /&gt;
&lt;br /&gt;
2. [http://en.wikipedia.org/wiki/Marshalling_%28computer_science%29 Marshaling]&lt;br /&gt;
&lt;br /&gt;
3. [http://techbrahmana.blogspot.com/2012/03/rails-cookie-handling-serialization-and.html Rails Cookie Handling]&lt;br /&gt;
&lt;br /&gt;
4. [http://yaml.org/ YAML]&lt;br /&gt;
&lt;br /&gt;
5. [http://www.ruby-doc.org/core-2.1.0/Marshal.html Marshal module]&lt;br /&gt;
&lt;br /&gt;
6. [http://json.org/ JSON]&lt;br /&gt;
&lt;br /&gt;
7. [http://rubygems.org/ Ruby Gems]&lt;br /&gt;
&lt;br /&gt;
8. [http://ruby-doc.org/core-2.0.0/ARGF.html# Ruby ARGF Documentation]&lt;br /&gt;
&lt;br /&gt;
9. [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html Ruby Documentation for Benchmark Gem]&lt;br /&gt;
&lt;br /&gt;
10. [http://www.pauldix.net/2008/08/serializing-dat.html Serialization Performance Comparison]&lt;br /&gt;
&lt;br /&gt;
11. [http://www.boost.org/doc/libs/1_36_0/libs/serialization/example/demo.cpp Boost Libraries]&lt;br /&gt;
&lt;br /&gt;
12. [http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html Java Serialization]&lt;br /&gt;
&lt;br /&gt;
13. [http://www.w3schools.com/json/ JSON Tutorial]&lt;br /&gt;
&lt;br /&gt;
14. [http://www.skorks.com/2010/04/serializing-and-deserializing-objects-with-ruby/ Serializing and De-serializing in Ruby]&lt;br /&gt;
&lt;br /&gt;
15. [http://www.codeproject.com/Articles/33296/Serialization-and-De-serialization Serialization and De-serialization]&lt;br /&gt;
&lt;br /&gt;
16. [http://msdn.microsoft.com/en-us/library/182eeyhh.aspx XML Serialization]&lt;br /&gt;
&lt;br /&gt;
17. [http://www.tutorialspoint.com/java/java_serialization.htm Java Serialization Tutorial]&lt;br /&gt;
&lt;br /&gt;
18. [http://www.sapphiresteel.com/ruby-programming/The-Book-Of-Ruby The Book of Ruby]&lt;br /&gt;
&lt;br /&gt;
19. [http://www.codeproject.com/Articles/20962/Introducing-Serialization-in-NET Serialization in .Net]&lt;br /&gt;
&lt;br /&gt;
20. [http://ruby.about.com/od/advancedruby/ss/Serialization-In-Ruby-Yaml.html Serialization in Ruby YAML]&lt;br /&gt;
&lt;br /&gt;
21. [http://ruby.about.com/od/tasks/a/The-Json-Gem.html Installing JSON gem]&lt;br /&gt;
&lt;br /&gt;
22. [http://www.ruby-doc.org/stdlib-2.0.0/libdoc/json/rdoc/JSON.html Serialization in Ruby JSON]&lt;/div&gt;</summary>
		<author><name>Mmehta2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1d_mm&amp;diff=83396</id>
		<title>CSC/ECE 517 Spring 2014/ch1a 1d mm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1d_mm&amp;diff=83396"/>
		<updated>2014-02-18T01:58:03Z</updated>

		<summary type="html">&lt;p&gt;Mmehta2: /* Converting Ruby Objects to YAML format */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== '''Serialization''' ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Serialization'''[http://en.wikipedia.org/wiki/Serialization] is a process of converting a data structure or an object into a stream of bytes or string to facilitate storage in memory, file (persistence storage) or transmission over a network. The process of Serialization is also referred to as ''Marshalling''[http://en.wikipedia.org/wiki/Marshalling_%28computer_science%29]. 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. &lt;br /&gt;
&lt;br /&gt;
'''Basic Advantages of Serialization''':&lt;br /&gt;
&lt;br /&gt;
1. Communication between two or more processes on same machine. Object state can be saved and shared in a persistent or in-memory store.&lt;br /&gt;
&lt;br /&gt;
2. Communication between processes on different machines. Serialization facilitates the transmission of an object through a network.&lt;br /&gt;
&lt;br /&gt;
3. Creating a clone of an object. &lt;br /&gt;
&lt;br /&gt;
4. Cross-platform compatibility. Object can be serialized in a common format that is understood by multiple platforms. For example, JSON and XML.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''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''.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Few Practical Applications for Serialization'''&lt;br /&gt;
&lt;br /&gt;
1. [http://en.wikipedia.org/wiki/HTTP#HTTP_session HTTP Session] Replication by sharing session objects across web servers for handling failover scenarios&lt;br /&gt;
&lt;br /&gt;
2. Serialization facilitates communication in [http://en.wikipedia.org/wiki/Java_remote_method_invocation Remote Method Invocation] or [http://en.wikipedia.org/wiki/Remote_procedure_call Remote procedure calls]&lt;br /&gt;
&lt;br /&gt;
3. Rails Cookie Handling[http://techbrahmana.blogspot.com/2012/03/rails-cookie-handling-serialization-and.html]. Cookies are stored marshalled/unmarshalled to and from client machines. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:SerializationChart.jpg]]&lt;br /&gt;
&lt;br /&gt;
== '''Serialization in Ruby:''' ==&lt;br /&gt;
&lt;br /&gt;
Ruby provides serialization capabilities through its module, [http://www.ruby-doc.org/core-2.0.0/Marshal.html Marshal]. There are also other libraries like YAML and JSON which can be used in Ruby to generate serialized objects for purposes like platform independence and human readable formats.&lt;br /&gt;
&lt;br /&gt;
== '''Types of Serialization''' ==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
=== Converting Ruby Objects in Human Readable Formats ===&lt;br /&gt;
The conversion of Ruby objects into YAML and JSON formats are explained below.&lt;br /&gt;
&lt;br /&gt;
==== Converting Ruby Objects to YAML format ====&lt;br /&gt;
&lt;br /&gt;
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 binary format. 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. In order to use it in Ruby, the yaml.rb file is required to be loaded which provides methods for converting objects into yaml format and creating .yml files.&lt;br /&gt;
&lt;br /&gt;
'''Examples of serialization using YAML:'''&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;nowiki&amp;gt; #Serialization using YAML's to_yaml method&lt;br /&gt;
 &lt;br /&gt;
  require &amp;quot;yaml&amp;quot;&lt;br /&gt;
     class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
	@name = name&lt;br /&gt;
	@age = age&lt;br /&gt;
	@country=country&lt;br /&gt;
     end&lt;br /&gt;
      def to_s&lt;br /&gt;
	&amp;quot;In First:\n#{@name}, #{@age}, #{@country}\n&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
  x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
  puts x&lt;br /&gt;
  puts x.to_yaml&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   In First:&lt;br /&gt;
   Tom, 25, USA&lt;br /&gt;
   --- !ruby/object:First&lt;br /&gt;
   name: Tom&lt;br /&gt;
   age: 25&lt;br /&gt;
   country: USA&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The above code displays the object x, first as a string and then in the yaml format. &lt;br /&gt;
&lt;br /&gt;
'''Saving YAML data into a file:'''&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt; # Serialization using YAML::dump&lt;br /&gt;
&lt;br /&gt;
require 'yaml'&lt;br /&gt;
&lt;br /&gt;
f = File.open( 'first.yml', 'w' )&lt;br /&gt;
YAML.dump( [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;], f )&lt;br /&gt;
f.close&lt;br /&gt;
		&lt;br /&gt;
File.open( 'first.yml' ){ |f|	&lt;br /&gt;
    $arr= YAML.load(f)&lt;br /&gt;
}	&lt;br /&gt;
&lt;br /&gt;
p( $arr )&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;]&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The dump function can be used to serialize the data and save it into a file in YAML format. As shown in the above example the data in YAML format can be de-serialized using the load function.&lt;br /&gt;
&lt;br /&gt;
YAML libraries also provides an option of selecting only those variables of the object that are needed to be serialized. This is done using the to_yaml_properties method as shown in the below example.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;nowiki&amp;gt;  #Custom Serialization using YAML&lt;br /&gt;
 &lt;br /&gt;
  require &amp;quot;yaml&amp;quot;&lt;br /&gt;
     class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
	@name = name&lt;br /&gt;
	@age = age&lt;br /&gt;
	@country=country&lt;br /&gt;
     end&lt;br /&gt;
     def to_s&lt;br /&gt;
	&amp;quot;In First:\n#{@name}, #{@age}, #{@country}\n&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
      def to_yaml_properties&lt;br /&gt;
	   [&amp;quot;@name&amp;quot;,&amp;quot;@age&amp;quot;]  #@country will not be serialized&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
  x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
  puts x&lt;br /&gt;
  puts x.to_yaml&lt;br /&gt;
  &amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   In First:&lt;br /&gt;
   Tom, 25, USA&lt;br /&gt;
   --- !ruby/object:First&lt;br /&gt;
   name: Tom&lt;br /&gt;
   age: 25&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Converting Ruby Objects to JSON format: ====&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
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. Ruby 1.8.7 distribution is not bundled with json gem. However, in Ruby 1.9.2, the json gem is bundled with the core Ruby distribution.&lt;br /&gt;
The JSON library can be installed using Ruby Gems[http://rubygems.org/] as shown below:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;# gem install json&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A JSON string for serialization can be created by using the JSON.generate method:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
        require 'json'&lt;br /&gt;
        my_hash = {:Welcome =&amp;gt; &amp;quot;Ruby&amp;quot;}&lt;br /&gt;
        puts JSON.generate(my_hash) =&amp;gt; &amp;quot;{\&amp;quot;WELCOME\&amp;quot;:\&amp;quot;RUBY\&amp;quot;}&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
'''Output:'''&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;{&amp;quot;{\&amp;quot;Welcome\&amp;quot;:\&amp;quot;Ruby\&amp;quot;}&amp;quot;=&amp;gt;&amp;quot;{\&amp;quot;WELCOME\&amp;quot;:\&amp;quot;RUBY\&amp;quot;}&amp;quot;}&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A JSON string received from another program can be parsed by using JSON.parse&lt;br /&gt;
Ruby thus converts String to Hash.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
        require 'json'&lt;br /&gt;
        my_hash = JSON.parse('{&amp;quot;Welcome&amp;quot;: &amp;quot;Ruby&amp;quot;}')&lt;br /&gt;
        puts my_hash[&amp;quot;Welcome&amp;quot;] =&amp;gt; &amp;quot;Ruby&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Converting Ruby Objects to Binary Formats ===&lt;br /&gt;
Binary Serialization is another form of serialization in Ruby which is not in human readable form. 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. Binary Serialization  is done using [http://www.ruby-doc.org/core-2.0.0/Marshal.html Marshal] which is built into Ruby and the code for it is written in Ruby's Marshal module(marshal.c) and thus no additional files are required in order to use it. The _dump and _load methods defined in marshal are used for serialization. Using marshal module the following types of objects can not be serialized: bindings, procedure objects, singleton objects, instances of IO objects and interfaces.&lt;br /&gt;
&lt;br /&gt;
Since the Binary Serialized data is not in human readable form, the following guidelines need to be followed.&lt;br /&gt;
     1.Use print instead of puts[http://ruby-doc.org/core-2.0.0/ARGF.html#] when serialized objects are written to a file in order to avoid new line characters to be written &lt;br /&gt;
       in the file.&lt;br /&gt;
     &lt;br /&gt;
     2.Use a record separator in order to differentiate between two objects.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Binary Serialization Example:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    class Animal&lt;br /&gt;
      def initialize  name, age&lt;br /&gt;
      @name = name&lt;br /&gt;
      @age=age&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
   class Cat &amp;lt; Animal&lt;br /&gt;
    def to_s&lt;br /&gt;
     &amp;quot;In Cat C: #{@name} \t #{@age}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
  c = Cat.new(&amp;quot;Kitty Kat&amp;quot;,5)&lt;br /&gt;
  puts &amp;quot;Before Serialization&amp;quot;&lt;br /&gt;
  puts c&lt;br /&gt;
  #puts d&lt;br /&gt;
  serialize_cat= Marshal.dump(c) #dumps the serialized cat object into serialize_cat&lt;br /&gt;
  puts &amp;quot;\nAfter Serialization:\n #{serialize_cat}&amp;quot;&lt;br /&gt;
  deserialize_cat= Marshal::load(serialize_cat) #deserializes the cat object and loads it back into deserialize_cat&lt;br /&gt;
  puts &amp;quot;\nAfter Deserialization\n #{deserialize_cat}&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   Before Serialization&lt;br /&gt;
   In Cat C: Kitty Kat 	 5&lt;br /&gt;
   After Serialization:&lt;br /&gt;
   oCat:&lt;br /&gt;
   @nameI&amp;quot;Kitty Kat:ET:	@agei&lt;br /&gt;
   After Deserialization&lt;br /&gt;
   In Cat C: Kitty Kat 	 5&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Similar to YAML, Marshal can also be used to dump data into a file. The above example showing the serialization using YAML::dump can be written using marshal as shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    f = File.open( 'first.yml', 'w' )&lt;br /&gt;
    Marshal.dump( [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;], f )&lt;br /&gt;
    f.close	&lt;br /&gt;
    File.open( 'first.yml' ){ |f|	&lt;br /&gt;
    $arr= Marshal.load(f)&lt;br /&gt;
    }	&lt;br /&gt;
    p( $arr )&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;]&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice that in the above example there is no &amp;quot;require&amp;quot; statement as opposed to the earlier example of writing serialized data into files using YAML. This is because unlike YAML, marshal is built-in Ruby and  no external libraries is required in order to use its functionality.&lt;br /&gt;
&lt;br /&gt;
Marshal can also be used to custom serialize an object i.e. it provides an option to omit the variables of an object that are not required in the serialized data. The following program shows the use of marshal_dump method for achieving custom serialization.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
      class First&lt;br /&gt;
      def initialize(name, age, country)&lt;br /&gt;
         @name = name&lt;br /&gt;
         @age = age&lt;br /&gt;
         @country=country&lt;br /&gt;
      end&lt;br /&gt;
      def to_s&lt;br /&gt;
            &amp;quot;In First: #{@name}, #{@age}, #{@country}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
      def marshal_dump&lt;br /&gt;
            [@name,@age]  #@country will not be serialized&lt;br /&gt;
      end&lt;br /&gt;
      def marshal_load(data)&lt;br /&gt;
       @name=data[0]&lt;br /&gt;
       @age=data[1]&lt;br /&gt;
       @country=&amp;quot;United States of America&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
      end&lt;br /&gt;
 &lt;br /&gt;
      x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
      puts x&lt;br /&gt;
  &lt;br /&gt;
      marshal_data = Marshal.dump( x ) &lt;br /&gt;
      y = Marshal.load( marshal_data ) &lt;br /&gt;
      p( y.to_s )&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    In First: Tom, 25, USA&lt;br /&gt;
    &amp;quot;In First: Tom, 25, United States of America&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== '''Serialization Performance in Ruby''' ==&lt;br /&gt;
The different serialization formats described above differ in the efficiency at which they can serialize and deserialize data and thus while serializing large amount of data their efficiency is taken into account. The .report[http://www.ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html] method in Ruby can be used to evaluate the performance of these serialization patterns. Marshal, as it handles data in binary format, is the most efficient form of serialization in Ruby. The following example compares the performance of marshal, JSON and YAML format.[http://www.pauldix.net/2008/08/serializing-dat.html]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   require 'benchmark'&lt;br /&gt;
   require 'rubygems'&lt;br /&gt;
   require 'json'&lt;br /&gt;
   require 'yaml'&lt;br /&gt;
   include Benchmark&lt;br /&gt;
   class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
      @name=name&lt;br /&gt;
      @age=age&lt;br /&gt;
      @country=country&lt;br /&gt;
     end&lt;br /&gt;
   end  &lt;br /&gt;
    x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
    puts &amp;quot;            User     System     Total      Real&amp;quot;&lt;br /&gt;
   benchmark do |t|&lt;br /&gt;
    print &amp;quot;Marshal:&amp;quot;&lt;br /&gt;
    t.report{1000.times do; Marshal.load(Marshal.dump(x));end}&lt;br /&gt;
    print &amp;quot;JSON:   &amp;quot;&lt;br /&gt;
    t.report{1000.times do; JSON.load(JSON.dump(x));end}&lt;br /&gt;
    print &amp;quot;YAML:   &amp;quot;&lt;br /&gt;
    t.report{1000.times do; YAML.load(YAML.dump(x));end}&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
              User     System     Total      Real&lt;br /&gt;
  Marshal:   0.010000   0.000000   0.010000 (  0.015408)&lt;br /&gt;
  JSON:      0.020000   0.010000   0.030000 (  0.022462)&lt;br /&gt;
  YAML:      0.460000   0.010000   0.470000 (  0.472149)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output of the above example shows that, given large chunks of data, marshal is comparatively more efficient format for serialization in Ruby.&lt;br /&gt;
&lt;br /&gt;
== Serialization in Object-Oriented Languages: Comparison ==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Sl.No !! Ruby !! Java !! .Net Framework !! C++&lt;br /&gt;
|-&lt;br /&gt;
| 1 || Ruby provides a built in 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 || Although, there is no built in support for serialization in C++, it can be achieved by using Boost libraries[http://www.boost.org/doc/libs/1_36_0/libs/serialization/example/demo.cpp]&lt;br /&gt;
|-&lt;br /&gt;
| 2 || The built in module of Ruby [http://www.ruby-doc.org/core-2.0.0/Marshal.html (Marshal)] does not support platform independence, however, it can be achieved by using external libraries like [http://yaml.org YAML] and [http://www.w3schools.com/json/ JSON] || Similarly, Java's built in serialization is also not platform independent and in order to use serialization in Java across Ruby platform, jruby library should be used.  || .Net used Remoting technology to make it platform independent. || Serialization using the Boost libraries is not platform independent.&lt;br /&gt;
|-&lt;br /&gt;
| 3 || YAML provides a method [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/syck/rdoc/Object.html (to_yaml_properties)] which can be used to select the variables who's value is need to be serialized. With Marshal, we need to write a method named marshal_dump defining the variables of an object that has to be serialized. || Provides an option for serializing only the required 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 data 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 || Serialization using the Boost libraries is custom and thus the user can specify the part of the objects to be serialized.&lt;br /&gt;
|-&lt;br /&gt;
| 4 || Bindings, procedure objects, singleton objects, instances of IO objects and interfaces can not be serialized. Serializing these objects throws TypeError exceptions. || Thread, OutputStream and its subclasses, and Socket are not serializable in Java.[http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html] || Objects like DataRow are non serializable in .NET. Whether an object is serializable or not can be checked using the [http://msdn.microsoft.com/en-us/library/system.type.isserializable(v=vs.110).aspx?cs-save-lang=1&amp;amp;cs-lang=cpp#code-snippet-1 Type.IsSerializable] || There are no such objects in C++.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== See Also==&lt;br /&gt;
&lt;br /&gt;
1. [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/json/rdoc/JSON.html Serialization in Ruby JSON]&lt;br /&gt;
&lt;br /&gt;
2. [http://api.rubyonrails.org/classes/ActiveModel/Serialization.html Serialization in Rails]&lt;br /&gt;
&lt;br /&gt;
3. [http://gregmoreno.wordpress.com/2011/01/27/preventing-model-explosion-via-rails-serialization/ Article on Rails Serialization]&lt;br /&gt;
&lt;br /&gt;
4. [http://en.wikipedia.org/wiki/Google_Protocol_Buffers Protocol Buffers]&lt;br /&gt;
&lt;br /&gt;
5. [https://rubygems.org/gems/ruby-protocol-buffers Ruby Protocol Buffers Gem]&lt;br /&gt;
&lt;br /&gt;
6. [http://en.wikipedia.org/wiki/Apache_Avro Avro]&lt;br /&gt;
&lt;br /&gt;
7. [http://rubygems.org/gems/avro Ruby Avro Gem]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
1. [http://en.wikipedia.org/wiki/Serialization Serilization in General]&lt;br /&gt;
&lt;br /&gt;
2. [http://en.wikipedia.org/wiki/Marshalling_%28computer_science%29 Marshaling]&lt;br /&gt;
&lt;br /&gt;
3. [http://techbrahmana.blogspot.com/2012/03/rails-cookie-handling-serialization-and.html Rails Cookie Handling]&lt;br /&gt;
&lt;br /&gt;
4. [http://yaml.org/ YAML]&lt;br /&gt;
&lt;br /&gt;
5. [http://www.ruby-doc.org/core-2.1.0/Marshal.html Marshal module]&lt;br /&gt;
&lt;br /&gt;
6. [http://json.org/ JSON]&lt;br /&gt;
&lt;br /&gt;
7. [http://rubygems.org/ Ruby Gems]&lt;br /&gt;
&lt;br /&gt;
8. [http://ruby-doc.org/core-2.0.0/ARGF.html# Ruby ARGF Documentation]&lt;br /&gt;
&lt;br /&gt;
9. [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html Ruby Documentation for Benchmark Gem]&lt;br /&gt;
&lt;br /&gt;
10. [http://www.pauldix.net/2008/08/serializing-dat.html Serialization Performance Comparison]&lt;br /&gt;
&lt;br /&gt;
11. [http://www.boost.org/doc/libs/1_36_0/libs/serialization/example/demo.cpp Boost Libraries]&lt;br /&gt;
&lt;br /&gt;
12. [http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html Java Serialization]&lt;br /&gt;
&lt;br /&gt;
13. [http://www.w3schools.com/json/ JSON Tutorial]&lt;br /&gt;
&lt;br /&gt;
14. [http://www.skorks.com/2010/04/serializing-and-deserializing-objects-with-ruby/ Serializing and De-serializing in Ruby]&lt;br /&gt;
&lt;br /&gt;
15. [http://www.codeproject.com/Articles/33296/Serialization-and-De-serialization Serialization and De-serialization]&lt;br /&gt;
&lt;br /&gt;
16. [http://msdn.microsoft.com/en-us/library/182eeyhh.aspx XML Serialization]&lt;br /&gt;
&lt;br /&gt;
17. [http://www.tutorialspoint.com/java/java_serialization.htm Java Serialization Tutorial]&lt;br /&gt;
&lt;br /&gt;
18. [http://www.sapphiresteel.com/ruby-programming/The-Book-Of-Ruby The Book of Ruby]&lt;br /&gt;
&lt;br /&gt;
19. [http://www.codeproject.com/Articles/20962/Introducing-Serialization-in-NET Serialization in .Net]&lt;br /&gt;
&lt;br /&gt;
20. [http://ruby.about.com/od/advancedruby/ss/Serialization-In-Ruby-Yaml.html Serialization in Ruby YAML]&lt;br /&gt;
&lt;br /&gt;
21. [http://ruby.about.com/od/tasks/a/The-Json-Gem.html Installing JSON gem]&lt;br /&gt;
&lt;br /&gt;
22. [http://www.ruby-doc.org/stdlib-2.0.0/libdoc/json/rdoc/JSON.html Serialization in Ruby JSON]&lt;/div&gt;</summary>
		<author><name>Mmehta2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1d_mm&amp;diff=83395</id>
		<title>CSC/ECE 517 Spring 2014/ch1a 1d mm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1d_mm&amp;diff=83395"/>
		<updated>2014-02-18T01:57:36Z</updated>

		<summary type="html">&lt;p&gt;Mmehta2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== '''Serialization''' ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Serialization'''[http://en.wikipedia.org/wiki/Serialization] is a process of converting a data structure or an object into a stream of bytes or string to facilitate storage in memory, file (persistence storage) or transmission over a network. The process of Serialization is also referred to as ''Marshalling''[http://en.wikipedia.org/wiki/Marshalling_%28computer_science%29]. 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. &lt;br /&gt;
&lt;br /&gt;
'''Basic Advantages of Serialization''':&lt;br /&gt;
&lt;br /&gt;
1. Communication between two or more processes on same machine. Object state can be saved and shared in a persistent or in-memory store.&lt;br /&gt;
&lt;br /&gt;
2. Communication between processes on different machines. Serialization facilitates the transmission of an object through a network.&lt;br /&gt;
&lt;br /&gt;
3. Creating a clone of an object. &lt;br /&gt;
&lt;br /&gt;
4. Cross-platform compatibility. Object can be serialized in a common format that is understood by multiple platforms. For example, JSON and XML.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''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''.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Few Practical Applications for Serialization'''&lt;br /&gt;
&lt;br /&gt;
1. [http://en.wikipedia.org/wiki/HTTP#HTTP_session HTTP Session] Replication by sharing session objects across web servers for handling failover scenarios&lt;br /&gt;
&lt;br /&gt;
2. Serialization facilitates communication in [http://en.wikipedia.org/wiki/Java_remote_method_invocation Remote Method Invocation] or [http://en.wikipedia.org/wiki/Remote_procedure_call Remote procedure calls]&lt;br /&gt;
&lt;br /&gt;
3. Rails Cookie Handling[http://techbrahmana.blogspot.com/2012/03/rails-cookie-handling-serialization-and.html]. Cookies are stored marshalled/unmarshalled to and from client machines. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:SerializationChart.jpg]]&lt;br /&gt;
&lt;br /&gt;
== '''Serialization in Ruby:''' ==&lt;br /&gt;
&lt;br /&gt;
Ruby provides serialization capabilities through its module, [http://www.ruby-doc.org/core-2.0.0/Marshal.html Marshal]. There are also other libraries like YAML and JSON which can be used in Ruby to generate serialized objects for purposes like platform independence and human readable formats.&lt;br /&gt;
&lt;br /&gt;
== '''Types of Serialization''' ==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
=== Converting Ruby Objects in Human Readable Formats ===&lt;br /&gt;
The conversion of Ruby objects into YAML and JSON formats are explained below.&lt;br /&gt;
&lt;br /&gt;
==== Converting Ruby Objects to YAML format ====&lt;br /&gt;
&lt;br /&gt;
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. 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. In order to use it in Ruby, the yaml.rb file is required to be loaded which provides methods for converting objects into yaml format and creating .yml files.&lt;br /&gt;
&lt;br /&gt;
'''Examples of serialization using YAML:'''&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;nowiki&amp;gt; #Serialization using YAML's to_yaml method&lt;br /&gt;
 &lt;br /&gt;
  require &amp;quot;yaml&amp;quot;&lt;br /&gt;
     class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
	@name = name&lt;br /&gt;
	@age = age&lt;br /&gt;
	@country=country&lt;br /&gt;
     end&lt;br /&gt;
      def to_s&lt;br /&gt;
	&amp;quot;In First:\n#{@name}, #{@age}, #{@country}\n&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
  x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
  puts x&lt;br /&gt;
  puts x.to_yaml&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   In First:&lt;br /&gt;
   Tom, 25, USA&lt;br /&gt;
   --- !ruby/object:First&lt;br /&gt;
   name: Tom&lt;br /&gt;
   age: 25&lt;br /&gt;
   country: USA&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The above code displays the object x, first as a string and then in the yaml format. &lt;br /&gt;
&lt;br /&gt;
'''Saving YAML data into a file:'''&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt; # Serialization using YAML::dump&lt;br /&gt;
&lt;br /&gt;
require 'yaml'&lt;br /&gt;
&lt;br /&gt;
f = File.open( 'first.yml', 'w' )&lt;br /&gt;
YAML.dump( [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;], f )&lt;br /&gt;
f.close&lt;br /&gt;
		&lt;br /&gt;
File.open( 'first.yml' ){ |f|	&lt;br /&gt;
    $arr= YAML.load(f)&lt;br /&gt;
}	&lt;br /&gt;
&lt;br /&gt;
p( $arr )&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;]&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The dump function can be used to serialize the data and save it into a file in YAML format. As shown in the above example the data in YAML format can be de-serialized using the load function.&lt;br /&gt;
&lt;br /&gt;
YAML libraries also provides an option of selecting only those variables of the object that are needed to be serialized. This is done using the to_yaml_properties method as shown in the below example.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;nowiki&amp;gt;  #Custom Serialization using YAML&lt;br /&gt;
 &lt;br /&gt;
  require &amp;quot;yaml&amp;quot;&lt;br /&gt;
     class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
	@name = name&lt;br /&gt;
	@age = age&lt;br /&gt;
	@country=country&lt;br /&gt;
     end&lt;br /&gt;
     def to_s&lt;br /&gt;
	&amp;quot;In First:\n#{@name}, #{@age}, #{@country}\n&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
      def to_yaml_properties&lt;br /&gt;
	   [&amp;quot;@name&amp;quot;,&amp;quot;@age&amp;quot;]  #@country will not be serialized&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
  x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
  puts x&lt;br /&gt;
  puts x.to_yaml&lt;br /&gt;
  &amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   In First:&lt;br /&gt;
   Tom, 25, USA&lt;br /&gt;
   --- !ruby/object:First&lt;br /&gt;
   name: Tom&lt;br /&gt;
   age: 25&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Converting Ruby Objects to JSON format: ====&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
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. Ruby 1.8.7 distribution is not bundled with json gem. However, in Ruby 1.9.2, the json gem is bundled with the core Ruby distribution.&lt;br /&gt;
The JSON library can be installed using Ruby Gems[http://rubygems.org/] as shown below:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;# gem install json&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A JSON string for serialization can be created by using the JSON.generate method:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
        require 'json'&lt;br /&gt;
        my_hash = {:Welcome =&amp;gt; &amp;quot;Ruby&amp;quot;}&lt;br /&gt;
        puts JSON.generate(my_hash) =&amp;gt; &amp;quot;{\&amp;quot;WELCOME\&amp;quot;:\&amp;quot;RUBY\&amp;quot;}&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
'''Output:'''&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;{&amp;quot;{\&amp;quot;Welcome\&amp;quot;:\&amp;quot;Ruby\&amp;quot;}&amp;quot;=&amp;gt;&amp;quot;{\&amp;quot;WELCOME\&amp;quot;:\&amp;quot;RUBY\&amp;quot;}&amp;quot;}&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A JSON string received from another program can be parsed by using JSON.parse&lt;br /&gt;
Ruby thus converts String to Hash.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
        require 'json'&lt;br /&gt;
        my_hash = JSON.parse('{&amp;quot;Welcome&amp;quot;: &amp;quot;Ruby&amp;quot;}')&lt;br /&gt;
        puts my_hash[&amp;quot;Welcome&amp;quot;] =&amp;gt; &amp;quot;Ruby&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Converting Ruby Objects to Binary Formats ===&lt;br /&gt;
Binary Serialization is another form of serialization in Ruby which is not in human readable form. 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. Binary Serialization  is done using [http://www.ruby-doc.org/core-2.0.0/Marshal.html Marshal] which is built into Ruby and the code for it is written in Ruby's Marshal module(marshal.c) and thus no additional files are required in order to use it. The _dump and _load methods defined in marshal are used for serialization. Using marshal module the following types of objects can not be serialized: bindings, procedure objects, singleton objects, instances of IO objects and interfaces.&lt;br /&gt;
&lt;br /&gt;
Since the Binary Serialized data is not in human readable form, the following guidelines need to be followed.&lt;br /&gt;
     1.Use print instead of puts[http://ruby-doc.org/core-2.0.0/ARGF.html#] when serialized objects are written to a file in order to avoid new line characters to be written &lt;br /&gt;
       in the file.&lt;br /&gt;
     &lt;br /&gt;
     2.Use a record separator in order to differentiate between two objects.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Binary Serialization Example:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    class Animal&lt;br /&gt;
      def initialize  name, age&lt;br /&gt;
      @name = name&lt;br /&gt;
      @age=age&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
   class Cat &amp;lt; Animal&lt;br /&gt;
    def to_s&lt;br /&gt;
     &amp;quot;In Cat C: #{@name} \t #{@age}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
  c = Cat.new(&amp;quot;Kitty Kat&amp;quot;,5)&lt;br /&gt;
  puts &amp;quot;Before Serialization&amp;quot;&lt;br /&gt;
  puts c&lt;br /&gt;
  #puts d&lt;br /&gt;
  serialize_cat= Marshal.dump(c) #dumps the serialized cat object into serialize_cat&lt;br /&gt;
  puts &amp;quot;\nAfter Serialization:\n #{serialize_cat}&amp;quot;&lt;br /&gt;
  deserialize_cat= Marshal::load(serialize_cat) #deserializes the cat object and loads it back into deserialize_cat&lt;br /&gt;
  puts &amp;quot;\nAfter Deserialization\n #{deserialize_cat}&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   Before Serialization&lt;br /&gt;
   In Cat C: Kitty Kat 	 5&lt;br /&gt;
   After Serialization:&lt;br /&gt;
   oCat:&lt;br /&gt;
   @nameI&amp;quot;Kitty Kat:ET:	@agei&lt;br /&gt;
   After Deserialization&lt;br /&gt;
   In Cat C: Kitty Kat 	 5&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Similar to YAML, Marshal can also be used to dump data into a file. The above example showing the serialization using YAML::dump can be written using marshal as shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    f = File.open( 'first.yml', 'w' )&lt;br /&gt;
    Marshal.dump( [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;], f )&lt;br /&gt;
    f.close	&lt;br /&gt;
    File.open( 'first.yml' ){ |f|	&lt;br /&gt;
    $arr= Marshal.load(f)&lt;br /&gt;
    }	&lt;br /&gt;
    p( $arr )&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;]&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice that in the above example there is no &amp;quot;require&amp;quot; statement as opposed to the earlier example of writing serialized data into files using YAML. This is because unlike YAML, marshal is built-in Ruby and  no external libraries is required in order to use its functionality.&lt;br /&gt;
&lt;br /&gt;
Marshal can also be used to custom serialize an object i.e. it provides an option to omit the variables of an object that are not required in the serialized data. The following program shows the use of marshal_dump method for achieving custom serialization.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
      class First&lt;br /&gt;
      def initialize(name, age, country)&lt;br /&gt;
         @name = name&lt;br /&gt;
         @age = age&lt;br /&gt;
         @country=country&lt;br /&gt;
      end&lt;br /&gt;
      def to_s&lt;br /&gt;
            &amp;quot;In First: #{@name}, #{@age}, #{@country}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
      def marshal_dump&lt;br /&gt;
            [@name,@age]  #@country will not be serialized&lt;br /&gt;
      end&lt;br /&gt;
      def marshal_load(data)&lt;br /&gt;
       @name=data[0]&lt;br /&gt;
       @age=data[1]&lt;br /&gt;
       @country=&amp;quot;United States of America&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
      end&lt;br /&gt;
 &lt;br /&gt;
      x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
      puts x&lt;br /&gt;
  &lt;br /&gt;
      marshal_data = Marshal.dump( x ) &lt;br /&gt;
      y = Marshal.load( marshal_data ) &lt;br /&gt;
      p( y.to_s )&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    In First: Tom, 25, USA&lt;br /&gt;
    &amp;quot;In First: Tom, 25, United States of America&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== '''Serialization Performance in Ruby''' ==&lt;br /&gt;
The different serialization formats described above differ in the efficiency at which they can serialize and deserialize data and thus while serializing large amount of data their efficiency is taken into account. The .report[http://www.ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html] method in Ruby can be used to evaluate the performance of these serialization patterns. Marshal, as it handles data in binary format, is the most efficient form of serialization in Ruby. The following example compares the performance of marshal, JSON and YAML format.[http://www.pauldix.net/2008/08/serializing-dat.html]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   require 'benchmark'&lt;br /&gt;
   require 'rubygems'&lt;br /&gt;
   require 'json'&lt;br /&gt;
   require 'yaml'&lt;br /&gt;
   include Benchmark&lt;br /&gt;
   class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
      @name=name&lt;br /&gt;
      @age=age&lt;br /&gt;
      @country=country&lt;br /&gt;
     end&lt;br /&gt;
   end  &lt;br /&gt;
    x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
    puts &amp;quot;            User     System     Total      Real&amp;quot;&lt;br /&gt;
   benchmark do |t|&lt;br /&gt;
    print &amp;quot;Marshal:&amp;quot;&lt;br /&gt;
    t.report{1000.times do; Marshal.load(Marshal.dump(x));end}&lt;br /&gt;
    print &amp;quot;JSON:   &amp;quot;&lt;br /&gt;
    t.report{1000.times do; JSON.load(JSON.dump(x));end}&lt;br /&gt;
    print &amp;quot;YAML:   &amp;quot;&lt;br /&gt;
    t.report{1000.times do; YAML.load(YAML.dump(x));end}&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
              User     System     Total      Real&lt;br /&gt;
  Marshal:   0.010000   0.000000   0.010000 (  0.015408)&lt;br /&gt;
  JSON:      0.020000   0.010000   0.030000 (  0.022462)&lt;br /&gt;
  YAML:      0.460000   0.010000   0.470000 (  0.472149)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output of the above example shows that, given large chunks of data, marshal is comparatively more efficient format for serialization in Ruby.&lt;br /&gt;
&lt;br /&gt;
== Serialization in Object-Oriented Languages: Comparison ==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Sl.No !! Ruby !! Java !! .Net Framework !! C++&lt;br /&gt;
|-&lt;br /&gt;
| 1 || Ruby provides a built in 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 || Although, there is no built in support for serialization in C++, it can be achieved by using Boost libraries[http://www.boost.org/doc/libs/1_36_0/libs/serialization/example/demo.cpp]&lt;br /&gt;
|-&lt;br /&gt;
| 2 || The built in module of Ruby [http://www.ruby-doc.org/core-2.0.0/Marshal.html (Marshal)] does not support platform independence, however, it can be achieved by using external libraries like [http://yaml.org YAML] and [http://www.w3schools.com/json/ JSON] || Similarly, Java's built in serialization is also not platform independent and in order to use serialization in Java across Ruby platform, jruby library should be used.  || .Net used Remoting technology to make it platform independent. || Serialization using the Boost libraries is not platform independent.&lt;br /&gt;
|-&lt;br /&gt;
| 3 || YAML provides a method [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/syck/rdoc/Object.html (to_yaml_properties)] which can be used to select the variables who's value is need to be serialized. With Marshal, we need to write a method named marshal_dump defining the variables of an object that has to be serialized. || Provides an option for serializing only the required 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 data 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 || Serialization using the Boost libraries is custom and thus the user can specify the part of the objects to be serialized.&lt;br /&gt;
|-&lt;br /&gt;
| 4 || Bindings, procedure objects, singleton objects, instances of IO objects and interfaces can not be serialized. Serializing these objects throws TypeError exceptions. || Thread, OutputStream and its subclasses, and Socket are not serializable in Java.[http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html] || Objects like DataRow are non serializable in .NET. Whether an object is serializable or not can be checked using the [http://msdn.microsoft.com/en-us/library/system.type.isserializable(v=vs.110).aspx?cs-save-lang=1&amp;amp;cs-lang=cpp#code-snippet-1 Type.IsSerializable] || There are no such objects in C++.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== See Also==&lt;br /&gt;
&lt;br /&gt;
1. [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/json/rdoc/JSON.html Serialization in Ruby JSON]&lt;br /&gt;
&lt;br /&gt;
2. [http://api.rubyonrails.org/classes/ActiveModel/Serialization.html Serialization in Rails]&lt;br /&gt;
&lt;br /&gt;
3. [http://gregmoreno.wordpress.com/2011/01/27/preventing-model-explosion-via-rails-serialization/ Article on Rails Serialization]&lt;br /&gt;
&lt;br /&gt;
4. [http://en.wikipedia.org/wiki/Google_Protocol_Buffers Protocol Buffers]&lt;br /&gt;
&lt;br /&gt;
5. [https://rubygems.org/gems/ruby-protocol-buffers Ruby Protocol Buffers Gem]&lt;br /&gt;
&lt;br /&gt;
6. [http://en.wikipedia.org/wiki/Apache_Avro Avro]&lt;br /&gt;
&lt;br /&gt;
7. [http://rubygems.org/gems/avro Ruby Avro Gem]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
1. [http://en.wikipedia.org/wiki/Serialization Serilization in General]&lt;br /&gt;
&lt;br /&gt;
2. [http://en.wikipedia.org/wiki/Marshalling_%28computer_science%29 Marshaling]&lt;br /&gt;
&lt;br /&gt;
3. [http://techbrahmana.blogspot.com/2012/03/rails-cookie-handling-serialization-and.html Rails Cookie Handling]&lt;br /&gt;
&lt;br /&gt;
4. [http://yaml.org/ YAML]&lt;br /&gt;
&lt;br /&gt;
5. [http://www.ruby-doc.org/core-2.1.0/Marshal.html Marshal module]&lt;br /&gt;
&lt;br /&gt;
6. [http://json.org/ JSON]&lt;br /&gt;
&lt;br /&gt;
7. [http://rubygems.org/ Ruby Gems]&lt;br /&gt;
&lt;br /&gt;
8. [http://ruby-doc.org/core-2.0.0/ARGF.html# Ruby ARGF Documentation]&lt;br /&gt;
&lt;br /&gt;
9. [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html Ruby Documentation for Benchmark Gem]&lt;br /&gt;
&lt;br /&gt;
10. [http://www.pauldix.net/2008/08/serializing-dat.html Serialization Performance Comparison]&lt;br /&gt;
&lt;br /&gt;
11. [http://www.boost.org/doc/libs/1_36_0/libs/serialization/example/demo.cpp Boost Libraries]&lt;br /&gt;
&lt;br /&gt;
12. [http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html Java Serialization]&lt;br /&gt;
&lt;br /&gt;
13. [http://www.w3schools.com/json/ JSON Tutorial]&lt;br /&gt;
&lt;br /&gt;
14. [http://www.skorks.com/2010/04/serializing-and-deserializing-objects-with-ruby/ Serializing and De-serializing in Ruby]&lt;br /&gt;
&lt;br /&gt;
15. [http://www.codeproject.com/Articles/33296/Serialization-and-De-serialization Serialization and De-serialization]&lt;br /&gt;
&lt;br /&gt;
16. [http://msdn.microsoft.com/en-us/library/182eeyhh.aspx XML Serialization]&lt;br /&gt;
&lt;br /&gt;
17. [http://www.tutorialspoint.com/java/java_serialization.htm Java Serialization Tutorial]&lt;br /&gt;
&lt;br /&gt;
18. [http://www.sapphiresteel.com/ruby-programming/The-Book-Of-Ruby The Book of Ruby]&lt;br /&gt;
&lt;br /&gt;
19. [http://www.codeproject.com/Articles/20962/Introducing-Serialization-in-NET Serialization in .Net]&lt;br /&gt;
&lt;br /&gt;
20. [http://ruby.about.com/od/advancedruby/ss/Serialization-In-Ruby-Yaml.html Serialization in Ruby YAML]&lt;br /&gt;
&lt;br /&gt;
21. [http://ruby.about.com/od/tasks/a/The-Json-Gem.html Installing JSON gem]&lt;br /&gt;
&lt;br /&gt;
22. [http://www.ruby-doc.org/stdlib-2.0.0/libdoc/json/rdoc/JSON.html Serialization in Ruby JSON]&lt;/div&gt;</summary>
		<author><name>Mmehta2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1d_mm&amp;diff=83316</id>
		<title>CSC/ECE 517 Spring 2014/ch1a 1d mm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1d_mm&amp;diff=83316"/>
		<updated>2014-02-11T04:10:35Z</updated>

		<summary type="html">&lt;p&gt;Mmehta2: /* Serialization in OOLS Languages: Comparison */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== '''Serialization''' ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Serialization'''[http://en.wikipedia.org/wiki/Serialization] is a process of converting a data structure or an object into a stream of bytes or string to facilitate storage in memory, file(persistence storage) or transmission over a network. The process of Serialization is also referred to as ''Marshalling''[http://en.wikipedia.org/wiki/Marshalling_%28computer_science%29]. 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. &lt;br /&gt;
&lt;br /&gt;
'''Basic Advantages of Serialization''':&lt;br /&gt;
&lt;br /&gt;
1. Communication between two or more processes on same machine. Object state can be saved and shared in a persistent or in-memory store.&lt;br /&gt;
&lt;br /&gt;
2. Communication between processes on different machines. Serialization facilitates the transmission of an object through a network.&lt;br /&gt;
&lt;br /&gt;
3. Creating a clone of an object. &lt;br /&gt;
&lt;br /&gt;
4. Cross-platform compatibility. Object can be serialized in a common format that is understood by multiple platforms. Eg. JSON, XML.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''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''.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Few Practical Applications for Serialization'''&lt;br /&gt;
&lt;br /&gt;
1. HTTP Session Replication by sharing session objects across web servers for handling failover scenarios&lt;br /&gt;
&lt;br /&gt;
2. Serialization facilitates communication in Remote Method Invocation or Remote procedure calls &lt;br /&gt;
&lt;br /&gt;
3. Rails Cookie Handling[http://techbrahmana.blogspot.com/2012/03/rails-cookie-handling-serialization-and.html]. Cookies are stored marshalled/unmarshalled to and from client machines. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:SerializationChart.jpg]]&lt;br /&gt;
&lt;br /&gt;
== '''Serialization in Ruby:''' ==&lt;br /&gt;
&lt;br /&gt;
Ruby provides serialization capabilities through its module, [http://www.ruby-doc.org/core-2.0.0/Marshal.html Marshal]. There are also other libraries like YAML and JSON which can be used in Ruby to generate serialized objects for purposes like platform independence and human readable formats.&lt;br /&gt;
&lt;br /&gt;
== '''Types of Serialization''' ==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
=== Converting Ruby Objects in Human Readable Formats ===&lt;br /&gt;
The conversion of Ruby objects into YAML and JSON formats are explained below.&lt;br /&gt;
&lt;br /&gt;
==== Converting Ruby Objects to YAML format ====&lt;br /&gt;
&lt;br /&gt;
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. 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. In order to use it in Ruby, the yaml.rb file is required to be loaded which provides methods for converting objects into yaml format and creating .yml files.&lt;br /&gt;
&lt;br /&gt;
'''Examples of serialization using YAML:'''&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;nowiki&amp;gt; #Serialization using YAML's to_yaml method&lt;br /&gt;
 &lt;br /&gt;
  require &amp;quot;yaml&amp;quot;&lt;br /&gt;
     class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
	@name = name&lt;br /&gt;
	@age = age&lt;br /&gt;
	@country=country&lt;br /&gt;
     end&lt;br /&gt;
      def to_s&lt;br /&gt;
	&amp;quot;In First:\n#{@name}, #{@age}, #{@country}\n&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
  x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
  puts x&lt;br /&gt;
  puts x.to_yaml&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   In First:&lt;br /&gt;
   Tom, 25, USA&lt;br /&gt;
   --- !ruby/object:First&lt;br /&gt;
   name: Tom&lt;br /&gt;
   age: 25&lt;br /&gt;
   country: USA&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The above code displays the object x, first as a string and then in the yaml format. &lt;br /&gt;
&lt;br /&gt;
'''Saving YAML data into a file:'''&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt; # Serialization using YAML::dump&lt;br /&gt;
&lt;br /&gt;
require 'yaml'&lt;br /&gt;
&lt;br /&gt;
f = File.open( 'first.yml', 'w' )&lt;br /&gt;
YAML.dump( [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;], f )&lt;br /&gt;
f.close&lt;br /&gt;
		&lt;br /&gt;
File.open( 'first.yml' ){ |f|	&lt;br /&gt;
    $arr= YAML.load(f)&lt;br /&gt;
}	&lt;br /&gt;
&lt;br /&gt;
p( $arr )&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;]&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The dump function can be used to serialize the data and save it into a file in YAML format. As shown in the above example the data in YAML format can be de-serialized using the load function.&lt;br /&gt;
&lt;br /&gt;
YAML libraries also provides an option of selecting only those variables of the object that are needed to be serialized. This is done using the to_yaml_properties method as shown in the below example.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;nowiki&amp;gt;  #Custom Serialization using YAML&lt;br /&gt;
 &lt;br /&gt;
  require &amp;quot;yaml&amp;quot;&lt;br /&gt;
     class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
	@name = name&lt;br /&gt;
	@age = age&lt;br /&gt;
	@country=country&lt;br /&gt;
     end&lt;br /&gt;
     def to_s&lt;br /&gt;
	&amp;quot;In First:\n#{@name}, #{@age}, #{@country}\n&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
      def to_yaml_properties&lt;br /&gt;
	   [&amp;quot;@name&amp;quot;,&amp;quot;@age&amp;quot;]  #@country will not be serialized&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
  x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
  puts x&lt;br /&gt;
  puts x.to_yaml&lt;br /&gt;
  &amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   In First:&lt;br /&gt;
   Tom, 25, USA&lt;br /&gt;
   --- !ruby/object:First&lt;br /&gt;
   name: Tom&lt;br /&gt;
   age: 25&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Converting Ruby Objects to JSON format: ====&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
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. Ruby 1.8.7 distribution is not bundled with json gem. However, in Ruby 1.9.2, the json gem is bundled with the core Ruby distribution.&lt;br /&gt;
The JSON library can be installed using Ruby Gems[http://rubygems.org/] as shown below:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;# gem install json&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A JSON string for serialization can be created by using the JSON.generate method:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
        require 'json'&lt;br /&gt;
        my_hash = {:Welcome =&amp;gt; &amp;quot;Ruby&amp;quot;}&lt;br /&gt;
        puts JSON.generate(my_hash) =&amp;gt; &amp;quot;{\&amp;quot;WELCOME\&amp;quot;:\&amp;quot;RUBY\&amp;quot;}&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
'''Output:'''&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;{&amp;quot;{\&amp;quot;Welcome\&amp;quot;:\&amp;quot;Ruby\&amp;quot;}&amp;quot;=&amp;gt;&amp;quot;{\&amp;quot;WELCOME\&amp;quot;:\&amp;quot;RUBY\&amp;quot;}&amp;quot;}&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A JSON string received from another program can be parsed by using JSON.parse&lt;br /&gt;
Ruby thus converts String to Hash.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
        require 'json'&lt;br /&gt;
        my_hash = JSON.parse('{&amp;quot;Welcome&amp;quot;: &amp;quot;Ruby&amp;quot;}')&lt;br /&gt;
        puts my_hash[&amp;quot;Welcome&amp;quot;] =&amp;gt; &amp;quot;Ruby&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Converting Ruby Objects to Binary Formats ===&lt;br /&gt;
Binary Serialization is another form of serialization in Ruby which is not in human readable form. 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. Binary Serialization  is done using [http://www.ruby-doc.org/core-2.0.0/Marshal.html Marshal] which is built into Ruby and the code for it is written in Ruby's Marshal module(marshal.c) and thus no additional files are required in order to use it. The _dump and _load methods defined in marshal are used for serialization. Using marshal module the following types of objects can not be serialized: bindings, procedure objects, singleton objects, instances of IO objects and interfaces.&lt;br /&gt;
&lt;br /&gt;
Since the Binary Serialized data is not in human readable form, the following guidelines need to be followed.&lt;br /&gt;
     1.Use print instead of puts[http://ruby-doc.org/core-2.0.0/ARGF.html#] when serialized objects are written to a file in order to avoid new line characters to be written &lt;br /&gt;
       in the file.&lt;br /&gt;
     &lt;br /&gt;
     2.Use a record separator in order to differentiate between two objects.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Binary Serialization Example:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    class Animal&lt;br /&gt;
      def initialize  name, age&lt;br /&gt;
      @name = name&lt;br /&gt;
      @age=age&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
   class Cat &amp;lt; Animal&lt;br /&gt;
    def to_s&lt;br /&gt;
     &amp;quot;In Cat C: #{@name} \t #{@age}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
  c = Cat.new(&amp;quot;Kitty Kat&amp;quot;,5)&lt;br /&gt;
  puts &amp;quot;Before Serialization&amp;quot;&lt;br /&gt;
  puts c&lt;br /&gt;
  #puts d&lt;br /&gt;
  serialize_cat= Marshal.dump(c) #dumps the serialized cat object into serialize_cat&lt;br /&gt;
  puts &amp;quot;\nAfter Serialization:\n #{serialize_cat}&amp;quot;&lt;br /&gt;
  deserialize_cat= Marshal::load(serialize_cat) #deserializes the cat object and loads it back into deserialize_cat&lt;br /&gt;
  puts &amp;quot;\nAfter Deserialization\n #{deserialize_cat}&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   Before Serialization&lt;br /&gt;
   In Cat C: Kitty Kat 	 5&lt;br /&gt;
   After Serialization:&lt;br /&gt;
   oCat:&lt;br /&gt;
   @nameI&amp;quot;Kitty Kat:ET:	@agei&lt;br /&gt;
   After Deserialization&lt;br /&gt;
   In Cat C: Kitty Kat 	 5&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Similar to YAML, Marshal can also be used to dump data into a file. The above example showing the serialization using YAML::dump can be written using marshal as shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    f = File.open( 'first.yml', 'w' )&lt;br /&gt;
    Marshal.dump( [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;], f )&lt;br /&gt;
    f.close	&lt;br /&gt;
    File.open( 'first.yml' ){ |f|	&lt;br /&gt;
    $arr= Marshal.load(f)&lt;br /&gt;
    }	&lt;br /&gt;
    p( $arr )&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;]&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice that in the above example there is no &amp;quot;require&amp;quot; statement as opposed to the earlier example of writing serialized data into files using YAML. This is because unlike YAML, marshal is built-in Ruby and  no external libraries is required in order to use its functionality.&lt;br /&gt;
&lt;br /&gt;
Marshal can also be used to custom serialize an object i.e. it provides an option to omit the variables of an object that are not required in the serialized data. The following program shows the use of marshal_dump method for achieving custom serialization.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
      class First&lt;br /&gt;
      def initialize(name, age, country)&lt;br /&gt;
         @name = name&lt;br /&gt;
         @age = age&lt;br /&gt;
         @country=country&lt;br /&gt;
      end&lt;br /&gt;
      def to_s&lt;br /&gt;
            &amp;quot;In First: #{@name}, #{@age}, #{@country}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
      def marshal_dump&lt;br /&gt;
            [@name,@age]  #@country will not be serialized&lt;br /&gt;
      end&lt;br /&gt;
      def marshal_load(data)&lt;br /&gt;
       @name=data[0]&lt;br /&gt;
       @age=data[1]&lt;br /&gt;
       @country=&amp;quot;United States of America&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
      end&lt;br /&gt;
 &lt;br /&gt;
      x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
      puts x&lt;br /&gt;
  &lt;br /&gt;
      marshal_data = Marshal.dump( x ) &lt;br /&gt;
      y = Marshal.load( marshal_data ) &lt;br /&gt;
      p( y.to_s )&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    In First: Tom, 25, USA&lt;br /&gt;
    &amp;quot;In First: Tom, 25, United States of America&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== '''Serialization Performance in Ruby''' ==&lt;br /&gt;
The different serialization formats described above differ in the efficiency at which they can serialize and deserialize data and thus while serializing large amount of data their efficiency is taken into account. The .report[http://www.ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html] method in Ruby can be used to evaluate the performance of these serialization patterns. Marshal, as it handles data in binary format, is the most efficient form of serialization in Ruby. The following example compares the performance of marshal, JSON and YAML format.[http://www.pauldix.net/2008/08/serializing-dat.html]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   require 'benchmark'&lt;br /&gt;
   require 'rubygems'&lt;br /&gt;
   require 'json'&lt;br /&gt;
   require 'yaml'&lt;br /&gt;
   include Benchmark&lt;br /&gt;
   class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
      @name=name&lt;br /&gt;
      @age=age&lt;br /&gt;
      @country=country&lt;br /&gt;
     end&lt;br /&gt;
   end  &lt;br /&gt;
    x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
    puts &amp;quot;            User     System     Total      Real&amp;quot;&lt;br /&gt;
   benchmark do |t|&lt;br /&gt;
    print &amp;quot;Marshal:&amp;quot;&lt;br /&gt;
    t.report{1000.times do; Marshal.load(Marshal.dump(x));end}&lt;br /&gt;
    print &amp;quot;JSON:   &amp;quot;&lt;br /&gt;
    t.report{1000.times do; JSON.load(JSON.dump(x));end}&lt;br /&gt;
    print &amp;quot;YAML:   &amp;quot;&lt;br /&gt;
    t.report{1000.times do; YAML.load(YAML.dump(x));end}&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
              User     System     Total      Real&lt;br /&gt;
  Marshal:   0.010000   0.000000   0.010000 (  0.015408)&lt;br /&gt;
  JSON:      0.020000   0.010000   0.030000 (  0.022462)&lt;br /&gt;
  YAML:      0.460000   0.010000   0.470000 (  0.472149)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output of the above example shows that, given large chunks of data, marshal is comparatively more efficient format for serialization in Ruby.&lt;br /&gt;
&lt;br /&gt;
== Serialization in Object-Oriented Languages: Comparison ==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Sl.No !! Ruby !! Java !! .Net Framework !! C++&lt;br /&gt;
|-&lt;br /&gt;
| 1 || Ruby provides a built in 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 || Although, there is no built in support for serialization in C++, it can be achieved by using Boost libraries[http://www.boost.org/doc/libs/1_36_0/libs/serialization/example/demo.cpp]&lt;br /&gt;
|-&lt;br /&gt;
| 2 || The built in module of Ruby [http://www.ruby-doc.org/core-2.0.0/Marshal.html (Marshal)] does not support platform independence, however, it can be achieved by using external libraries like [http://yaml.org YAML] and [http://www.w3schools.com/json/ JSON] || Similarly, Java's built in serialization is also not platform independent and in order to use serialization in Java across Ruby platform, jruby library should be used.  || .Net used Remoting technology to make it platform independent. || Serialization using the Boost libraries is not platform independent.&lt;br /&gt;
|-&lt;br /&gt;
| 3 || YAML provides a method [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/syck/rdoc/Object.html (to_yaml_properties)] which can be used to select the variables who's value is need to be serialized. With Marshal, we need to write a method named marshal_dump defining the variables of an object that has to be serialized. || Provides an option for serializing only the required 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 data 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 || Serialization using the Boost libraries is custom and thus the user can specify the part of the objects to be serialized.&lt;br /&gt;
|-&lt;br /&gt;
| 4 || Bindings, procedure objects, singleton objects, instances of IO objects and interfaces can not be serialized. Serializing these objects throws TypeError exceptions. || Thread, OutputStream and its subclasses, and Socket are not serializable in Java.[http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html] || Objects like DataRow are non serializable in .NET. Whether an object is serializable or not can be checked using the [http://msdn.microsoft.com/en-us/library/system.type.isserializable(v=vs.110).aspx?cs-save-lang=1&amp;amp;cs-lang=cpp#code-snippet-1 Type.IsSerializable] || There are no such objects in C++.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== See Also==&lt;br /&gt;
&lt;br /&gt;
1. [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/json/rdoc/JSON.html Serialization in Ruby JSON]&lt;br /&gt;
&lt;br /&gt;
2. [http://api.rubyonrails.org/classes/ActiveModel/Serialization.html Serialization in Rails]&lt;br /&gt;
&lt;br /&gt;
3. [http://gregmoreno.wordpress.com/2011/01/27/preventing-model-explosion-via-rails-serialization/ Article on Rails Serialization]&lt;br /&gt;
&lt;br /&gt;
4. [http://en.wikipedia.org/wiki/Google_Protocol_Buffers Protocol Buffers]&lt;br /&gt;
&lt;br /&gt;
5. [https://rubygems.org/gems/ruby-protocol-buffers Ruby Protocol Buffers Gem]&lt;br /&gt;
&lt;br /&gt;
6. [http://en.wikipedia.org/wiki/Apache_Avro Avro]&lt;br /&gt;
&lt;br /&gt;
7. [http://rubygems.org/gems/avro Ruby Avro Gem]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
1. [http://en.wikipedia.org/wiki/Serialization Serilization in General]&lt;br /&gt;
&lt;br /&gt;
2. [http://en.wikipedia.org/wiki/Marshalling_%28computer_science%29 Marshaling]&lt;br /&gt;
&lt;br /&gt;
3. [http://techbrahmana.blogspot.com/2012/03/rails-cookie-handling-serialization-and.html Rails Cookie Handling]&lt;br /&gt;
&lt;br /&gt;
4. [http://yaml.org/ YAML]&lt;br /&gt;
&lt;br /&gt;
5. [http://www.ruby-doc.org/core-2.1.0/Marshal.html Marshal module]&lt;br /&gt;
&lt;br /&gt;
6. [http://json.org/ JSON]&lt;br /&gt;
&lt;br /&gt;
7. [http://rubygems.org/ Ruby Gems]&lt;br /&gt;
&lt;br /&gt;
8. [http://ruby-doc.org/core-2.0.0/ARGF.html# Ruby ARGF Documentation]&lt;br /&gt;
&lt;br /&gt;
9. [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html Ruby Documentation for Benchmark Gem]&lt;br /&gt;
&lt;br /&gt;
10. [http://www.pauldix.net/2008/08/serializing-dat.html Serialization Performance Comparison]&lt;br /&gt;
&lt;br /&gt;
11. [http://www.boost.org/doc/libs/1_36_0/libs/serialization/example/demo.cpp Boost Libraries]&lt;br /&gt;
&lt;br /&gt;
12. [http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html Java Serialization]&lt;br /&gt;
&lt;br /&gt;
13. [http://www.w3schools.com/json/ JSON Tutorial]&lt;br /&gt;
&lt;br /&gt;
14. [http://www.skorks.com/2010/04/serializing-and-deserializing-objects-with-ruby/ Serializing and De-serializing in Ruby]&lt;br /&gt;
&lt;br /&gt;
15. [http://www.codeproject.com/Articles/33296/Serialization-and-De-serialization Serialization and De-serialization]&lt;br /&gt;
&lt;br /&gt;
16. [http://msdn.microsoft.com/en-us/library/182eeyhh.aspx XML Serialization]&lt;br /&gt;
&lt;br /&gt;
17. [http://www.tutorialspoint.com/java/java_serialization.htm Java Serialization Tutorial]&lt;br /&gt;
&lt;br /&gt;
18. [http://www.sapphiresteel.com/ruby-programming/The-Book-Of-Ruby The Book of Ruby]&lt;br /&gt;
&lt;br /&gt;
19. [http://www.codeproject.com/Articles/20962/Introducing-Serialization-in-NET Serialization in .Net]&lt;br /&gt;
&lt;br /&gt;
20. [http://ruby.about.com/od/advancedruby/ss/Serialization-In-Ruby-Yaml.html Serialization in Ruby YAML]&lt;br /&gt;
&lt;br /&gt;
21. [http://ruby.about.com/od/tasks/a/The-Json-Gem.html Installing JSON gem]&lt;br /&gt;
&lt;br /&gt;
22. [http://www.ruby-doc.org/stdlib-2.0.0/libdoc/json/rdoc/JSON.html Serialization in Ruby JSON]&lt;/div&gt;</summary>
		<author><name>Mmehta2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1d_mm&amp;diff=83314</id>
		<title>CSC/ECE 517 Spring 2014/ch1a 1d mm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1d_mm&amp;diff=83314"/>
		<updated>2014-02-11T04:08:21Z</updated>

		<summary type="html">&lt;p&gt;Mmehta2: /* Serialization */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== '''Serialization''' ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Serialization'''[http://en.wikipedia.org/wiki/Serialization] is a process of converting a data structure or an object into a stream of bytes or string to facilitate storage in memory, file(persistence storage) or transmission over a network. The process of Serialization is also referred to as ''Marshalling''[http://en.wikipedia.org/wiki/Marshalling_%28computer_science%29]. 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. &lt;br /&gt;
&lt;br /&gt;
'''Basic Advantages of Serialization''':&lt;br /&gt;
&lt;br /&gt;
1. Communication between two or more processes on same machine. Object state can be saved and shared in a persistent or in-memory store.&lt;br /&gt;
&lt;br /&gt;
2. Communication between processes on different machines. Serialization facilitates the transmission of an object through a network.&lt;br /&gt;
&lt;br /&gt;
3. Creating a clone of an object. &lt;br /&gt;
&lt;br /&gt;
4. Cross-platform compatibility. Object can be serialized in a common format that is understood by multiple platforms. Eg. JSON, XML.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''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''.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Few Practical Applications for Serialization'''&lt;br /&gt;
&lt;br /&gt;
1. HTTP Session Replication by sharing session objects across web servers for handling failover scenarios&lt;br /&gt;
&lt;br /&gt;
2. Serialization facilitates communication in Remote Method Invocation or Remote procedure calls &lt;br /&gt;
&lt;br /&gt;
3. Rails Cookie Handling[http://techbrahmana.blogspot.com/2012/03/rails-cookie-handling-serialization-and.html]. Cookies are stored marshalled/unmarshalled to and from client machines. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:SerializationChart.jpg]]&lt;br /&gt;
&lt;br /&gt;
== '''Serialization in Ruby:''' ==&lt;br /&gt;
&lt;br /&gt;
Ruby provides serialization capabilities through its module, [http://www.ruby-doc.org/core-2.0.0/Marshal.html Marshal]. There are also other libraries like YAML and JSON which can be used in Ruby to generate serialized objects for purposes like platform independence and human readable formats.&lt;br /&gt;
&lt;br /&gt;
== '''Types of Serialization''' ==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
=== Converting Ruby Objects in Human Readable Formats ===&lt;br /&gt;
The conversion of Ruby objects into YAML and JSON formats are explained below.&lt;br /&gt;
&lt;br /&gt;
==== Converting Ruby Objects to YAML format ====&lt;br /&gt;
&lt;br /&gt;
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. 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. In order to use it in Ruby, the yaml.rb file is required to be loaded which provides methods for converting objects into yaml format and creating .yml files.&lt;br /&gt;
&lt;br /&gt;
'''Examples of serialization using YAML:'''&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;nowiki&amp;gt; #Serialization using YAML's to_yaml method&lt;br /&gt;
 &lt;br /&gt;
  require &amp;quot;yaml&amp;quot;&lt;br /&gt;
     class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
	@name = name&lt;br /&gt;
	@age = age&lt;br /&gt;
	@country=country&lt;br /&gt;
     end&lt;br /&gt;
      def to_s&lt;br /&gt;
	&amp;quot;In First:\n#{@name}, #{@age}, #{@country}\n&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
  x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
  puts x&lt;br /&gt;
  puts x.to_yaml&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   In First:&lt;br /&gt;
   Tom, 25, USA&lt;br /&gt;
   --- !ruby/object:First&lt;br /&gt;
   name: Tom&lt;br /&gt;
   age: 25&lt;br /&gt;
   country: USA&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The above code displays the object x, first as a string and then in the yaml format. &lt;br /&gt;
&lt;br /&gt;
'''Saving YAML data into a file:'''&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt; # Serialization using YAML::dump&lt;br /&gt;
&lt;br /&gt;
require 'yaml'&lt;br /&gt;
&lt;br /&gt;
f = File.open( 'first.yml', 'w' )&lt;br /&gt;
YAML.dump( [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;], f )&lt;br /&gt;
f.close&lt;br /&gt;
		&lt;br /&gt;
File.open( 'first.yml' ){ |f|	&lt;br /&gt;
    $arr= YAML.load(f)&lt;br /&gt;
}	&lt;br /&gt;
&lt;br /&gt;
p( $arr )&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;]&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The dump function can be used to serialize the data and save it into a file in YAML format. As shown in the above example the data in YAML format can be de-serialized using the load function.&lt;br /&gt;
&lt;br /&gt;
YAML libraries also provides an option of selecting only those variables of the object that are needed to be serialized. This is done using the to_yaml_properties method as shown in the below example.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;nowiki&amp;gt;  #Custom Serialization using YAML&lt;br /&gt;
 &lt;br /&gt;
  require &amp;quot;yaml&amp;quot;&lt;br /&gt;
     class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
	@name = name&lt;br /&gt;
	@age = age&lt;br /&gt;
	@country=country&lt;br /&gt;
     end&lt;br /&gt;
     def to_s&lt;br /&gt;
	&amp;quot;In First:\n#{@name}, #{@age}, #{@country}\n&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
      def to_yaml_properties&lt;br /&gt;
	   [&amp;quot;@name&amp;quot;,&amp;quot;@age&amp;quot;]  #@country will not be serialized&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
  x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
  puts x&lt;br /&gt;
  puts x.to_yaml&lt;br /&gt;
  &amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   In First:&lt;br /&gt;
   Tom, 25, USA&lt;br /&gt;
   --- !ruby/object:First&lt;br /&gt;
   name: Tom&lt;br /&gt;
   age: 25&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Converting Ruby Objects to JSON format: ====&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
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. Ruby 1.8.7 distribution is not bundled with json gem. However, in Ruby 1.9.2, the json gem is bundled with the core Ruby distribution.&lt;br /&gt;
The JSON library can be installed using Ruby Gems[http://rubygems.org/] as shown below:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;# gem install json&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A JSON string for serialization can be created by using the JSON.generate method:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
        require 'json'&lt;br /&gt;
        my_hash = {:Welcome =&amp;gt; &amp;quot;Ruby&amp;quot;}&lt;br /&gt;
        puts JSON.generate(my_hash) =&amp;gt; &amp;quot;{\&amp;quot;WELCOME\&amp;quot;:\&amp;quot;RUBY\&amp;quot;}&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
'''Output:'''&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;{&amp;quot;{\&amp;quot;Welcome\&amp;quot;:\&amp;quot;Ruby\&amp;quot;}&amp;quot;=&amp;gt;&amp;quot;{\&amp;quot;WELCOME\&amp;quot;:\&amp;quot;RUBY\&amp;quot;}&amp;quot;}&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A JSON string received from another program can be parsed by using JSON.parse&lt;br /&gt;
Ruby thus converts String to Hash.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
        require 'json'&lt;br /&gt;
        my_hash = JSON.parse('{&amp;quot;Welcome&amp;quot;: &amp;quot;Ruby&amp;quot;}')&lt;br /&gt;
        puts my_hash[&amp;quot;Welcome&amp;quot;] =&amp;gt; &amp;quot;Ruby&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Converting Ruby Objects to Binary Formats ===&lt;br /&gt;
Binary Serialization is another form of serialization in Ruby which is not in human readable form. 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. Binary Serialization  is done using [http://www.ruby-doc.org/core-2.0.0/Marshal.html Marshal] which is built into Ruby and the code for it is written in Ruby's Marshal module(marshal.c) and thus no additional files are required in order to use it. The _dump and _load methods defined in marshal are used for serialization. Using marshal module the following types of objects can not be serialized: bindings, procedure objects, singleton objects, instances of IO objects and interfaces.&lt;br /&gt;
&lt;br /&gt;
Since the Binary Serialized data is not in human readable form, the following guidelines need to be followed.&lt;br /&gt;
     1.Use print instead of puts[http://ruby-doc.org/core-2.0.0/ARGF.html#] when serialized objects are written to a file in order to avoid new line characters to be written &lt;br /&gt;
       in the file.&lt;br /&gt;
     &lt;br /&gt;
     2.Use a record separator in order to differentiate between two objects.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Binary Serialization Example:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    class Animal&lt;br /&gt;
      def initialize  name, age&lt;br /&gt;
      @name = name&lt;br /&gt;
      @age=age&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
   class Cat &amp;lt; Animal&lt;br /&gt;
    def to_s&lt;br /&gt;
     &amp;quot;In Cat C: #{@name} \t #{@age}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
  c = Cat.new(&amp;quot;Kitty Kat&amp;quot;,5)&lt;br /&gt;
  puts &amp;quot;Before Serialization&amp;quot;&lt;br /&gt;
  puts c&lt;br /&gt;
  #puts d&lt;br /&gt;
  serialize_cat= Marshal.dump(c) #dumps the serialized cat object into serialize_cat&lt;br /&gt;
  puts &amp;quot;\nAfter Serialization:\n #{serialize_cat}&amp;quot;&lt;br /&gt;
  deserialize_cat= Marshal::load(serialize_cat) #deserializes the cat object and loads it back into deserialize_cat&lt;br /&gt;
  puts &amp;quot;\nAfter Deserialization\n #{deserialize_cat}&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   Before Serialization&lt;br /&gt;
   In Cat C: Kitty Kat 	 5&lt;br /&gt;
   After Serialization:&lt;br /&gt;
   oCat:&lt;br /&gt;
   @nameI&amp;quot;Kitty Kat:ET:	@agei&lt;br /&gt;
   After Deserialization&lt;br /&gt;
   In Cat C: Kitty Kat 	 5&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Similar to YAML, Marshal can also be used to dump data into a file. The above example showing the serialization using YAML::dump can be written using marshal as shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    f = File.open( 'first.yml', 'w' )&lt;br /&gt;
    Marshal.dump( [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;], f )&lt;br /&gt;
    f.close	&lt;br /&gt;
    File.open( 'first.yml' ){ |f|	&lt;br /&gt;
    $arr= Marshal.load(f)&lt;br /&gt;
    }	&lt;br /&gt;
    p( $arr )&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;]&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice that in the above example there is no &amp;quot;require&amp;quot; statement as opposed to the earlier example of writing serialized data into files using YAML. This is because unlike YAML, marshal is built-in Ruby and  no external libraries is required in order to use its functionality.&lt;br /&gt;
&lt;br /&gt;
Marshal can also be used to custom serialize an object i.e. it provides an option to omit the variables of an object that are not required in the serialized data. The following program shows the use of marshal_dump method for achieving custom serialization.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
      class First&lt;br /&gt;
      def initialize(name, age, country)&lt;br /&gt;
         @name = name&lt;br /&gt;
         @age = age&lt;br /&gt;
         @country=country&lt;br /&gt;
      end&lt;br /&gt;
      def to_s&lt;br /&gt;
            &amp;quot;In First: #{@name}, #{@age}, #{@country}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
      def marshal_dump&lt;br /&gt;
            [@name,@age]  #@country will not be serialized&lt;br /&gt;
      end&lt;br /&gt;
      def marshal_load(data)&lt;br /&gt;
       @name=data[0]&lt;br /&gt;
       @age=data[1]&lt;br /&gt;
       @country=&amp;quot;United States of America&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
      end&lt;br /&gt;
 &lt;br /&gt;
      x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
      puts x&lt;br /&gt;
  &lt;br /&gt;
      marshal_data = Marshal.dump( x ) &lt;br /&gt;
      y = Marshal.load( marshal_data ) &lt;br /&gt;
      p( y.to_s )&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    In First: Tom, 25, USA&lt;br /&gt;
    &amp;quot;In First: Tom, 25, United States of America&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== '''Serialization Performance in Ruby''' ==&lt;br /&gt;
The different serialization formats described above differ in the efficiency at which they can serialize and deserialize data and thus while serializing large amount of data their efficiency is taken into account. The .report[http://www.ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html] method in Ruby can be used to evaluate the performance of these serialization patterns. Marshal, as it handles data in binary format, is the most efficient form of serialization in Ruby. The following example compares the performance of marshal, JSON and YAML format.[http://www.pauldix.net/2008/08/serializing-dat.html]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   require 'benchmark'&lt;br /&gt;
   require 'rubygems'&lt;br /&gt;
   require 'json'&lt;br /&gt;
   require 'yaml'&lt;br /&gt;
   include Benchmark&lt;br /&gt;
   class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
      @name=name&lt;br /&gt;
      @age=age&lt;br /&gt;
      @country=country&lt;br /&gt;
     end&lt;br /&gt;
   end  &lt;br /&gt;
    x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
    puts &amp;quot;            User     System     Total      Real&amp;quot;&lt;br /&gt;
   benchmark do |t|&lt;br /&gt;
    print &amp;quot;Marshal:&amp;quot;&lt;br /&gt;
    t.report{1000.times do; Marshal.load(Marshal.dump(x));end}&lt;br /&gt;
    print &amp;quot;JSON:   &amp;quot;&lt;br /&gt;
    t.report{1000.times do; JSON.load(JSON.dump(x));end}&lt;br /&gt;
    print &amp;quot;YAML:   &amp;quot;&lt;br /&gt;
    t.report{1000.times do; YAML.load(YAML.dump(x));end}&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
              User     System     Total      Real&lt;br /&gt;
  Marshal:   0.010000   0.000000   0.010000 (  0.015408)&lt;br /&gt;
  JSON:      0.020000   0.010000   0.030000 (  0.022462)&lt;br /&gt;
  YAML:      0.460000   0.010000   0.470000 (  0.472149)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output of the above example shows that, given large chunks of data, marshal is comparatively more efficient format for serialization in Ruby.&lt;br /&gt;
&lt;br /&gt;
== Serialization in OOLS Languages: Comparison ==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Sl.No !! Ruby !! Java !! .Net Framework !! C++&lt;br /&gt;
|-&lt;br /&gt;
| 1 || Ruby provides a built in 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 || Although, there is no built in support for serialization in C++, it can be achieved by using Boost libraries[http://www.boost.org/doc/libs/1_36_0/libs/serialization/example/demo.cpp]&lt;br /&gt;
|-&lt;br /&gt;
| 2 || The built in module of Ruby [http://www.ruby-doc.org/core-2.0.0/Marshal.html (Marshal)] does not support platform independence, however, it can be achieved by using external libraries like [http://yaml.org YAML] and [http://www.w3schools.com/json/ JSON] || Similarly, Java's built in serialization is also not platform independent and in order to use serialization in Java across Ruby platform, jruby library should be used.  || .Net used Remoting technology to make it platform independent. || Serialization using the Boost libraries is not platform independent.&lt;br /&gt;
|-&lt;br /&gt;
| 3 || YAML provides a method [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/syck/rdoc/Object.html (to_yaml_properties)] which can be used to select the variables who's value is need to be serialized. With Marshal, we need to write a method named marshal_dump defining the variables of an object that has to be serialized. || Provides an option for serializing only the required 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 data 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 || Serialization using the Boost libraries is custom and thus the user can specify the part of the objects to be serialized.&lt;br /&gt;
|-&lt;br /&gt;
| 4 || Bindings, procedure objects, singleton objects, instances of IO objects and interfaces can not be serialized. Serializing these objects throws TypeError exceptions. || Thread, OutputStream and its subclasses, and Socket are not serializable in Java.[http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html] || Objects like DataRow are non serializable in .NET. Whether an object is serializable or not can be checked using the [http://msdn.microsoft.com/en-us/library/system.type.isserializable(v=vs.110).aspx?cs-save-lang=1&amp;amp;cs-lang=cpp#code-snippet-1 Type.IsSerializable] || There are no such objects in C++.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== See Also==&lt;br /&gt;
&lt;br /&gt;
1. [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/json/rdoc/JSON.html Serialization in Ruby JSON]&lt;br /&gt;
&lt;br /&gt;
2. [http://api.rubyonrails.org/classes/ActiveModel/Serialization.html Serialization in Rails]&lt;br /&gt;
&lt;br /&gt;
3. [http://gregmoreno.wordpress.com/2011/01/27/preventing-model-explosion-via-rails-serialization/ Article on Rails Serialization]&lt;br /&gt;
&lt;br /&gt;
4. [http://en.wikipedia.org/wiki/Google_Protocol_Buffers Protocol Buffers]&lt;br /&gt;
&lt;br /&gt;
5. [https://rubygems.org/gems/ruby-protocol-buffers Ruby Protocol Buffers Gem]&lt;br /&gt;
&lt;br /&gt;
6. [http://en.wikipedia.org/wiki/Apache_Avro Avro]&lt;br /&gt;
&lt;br /&gt;
7. [http://rubygems.org/gems/avro Ruby Avro Gem]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
1. [http://en.wikipedia.org/wiki/Serialization Serilization in General]&lt;br /&gt;
&lt;br /&gt;
2. [http://en.wikipedia.org/wiki/Marshalling_%28computer_science%29 Marshaling]&lt;br /&gt;
&lt;br /&gt;
3. [http://techbrahmana.blogspot.com/2012/03/rails-cookie-handling-serialization-and.html Rails Cookie Handling]&lt;br /&gt;
&lt;br /&gt;
4. [http://yaml.org/ YAML]&lt;br /&gt;
&lt;br /&gt;
5. [http://www.ruby-doc.org/core-2.1.0/Marshal.html Marshal module]&lt;br /&gt;
&lt;br /&gt;
6. [http://json.org/ JSON]&lt;br /&gt;
&lt;br /&gt;
7. [http://rubygems.org/ Ruby Gems]&lt;br /&gt;
&lt;br /&gt;
8. [http://ruby-doc.org/core-2.0.0/ARGF.html# Ruby ARGF Documentation]&lt;br /&gt;
&lt;br /&gt;
9. [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html Ruby Documentation for Benchmark Gem]&lt;br /&gt;
&lt;br /&gt;
10. [http://www.pauldix.net/2008/08/serializing-dat.html Serialization Performance Comparison]&lt;br /&gt;
&lt;br /&gt;
11. [http://www.boost.org/doc/libs/1_36_0/libs/serialization/example/demo.cpp Boost Libraries]&lt;br /&gt;
&lt;br /&gt;
12. [http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html Java Serialization]&lt;br /&gt;
&lt;br /&gt;
13. [http://www.w3schools.com/json/ JSON Tutorial]&lt;br /&gt;
&lt;br /&gt;
14. [http://www.skorks.com/2010/04/serializing-and-deserializing-objects-with-ruby/ Serializing and De-serializing in Ruby]&lt;br /&gt;
&lt;br /&gt;
15. [http://www.codeproject.com/Articles/33296/Serialization-and-De-serialization Serialization and De-serialization]&lt;br /&gt;
&lt;br /&gt;
16. [http://msdn.microsoft.com/en-us/library/182eeyhh.aspx XML Serialization]&lt;br /&gt;
&lt;br /&gt;
17. [http://www.tutorialspoint.com/java/java_serialization.htm Java Serialization Tutorial]&lt;br /&gt;
&lt;br /&gt;
18. [http://www.sapphiresteel.com/ruby-programming/The-Book-Of-Ruby The Book of Ruby]&lt;br /&gt;
&lt;br /&gt;
19. [http://www.codeproject.com/Articles/20962/Introducing-Serialization-in-NET Serialization in .Net]&lt;br /&gt;
&lt;br /&gt;
20. [http://ruby.about.com/od/advancedruby/ss/Serialization-In-Ruby-Yaml.html Serialization in Ruby YAML]&lt;br /&gt;
&lt;br /&gt;
21. [http://ruby.about.com/od/tasks/a/The-Json-Gem.html Installing JSON gem]&lt;br /&gt;
&lt;br /&gt;
22. [http://www.ruby-doc.org/stdlib-2.0.0/libdoc/json/rdoc/JSON.html Serialization in Ruby JSON]&lt;/div&gt;</summary>
		<author><name>Mmehta2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1d_mm&amp;diff=83295</id>
		<title>CSC/ECE 517 Spring 2014/ch1a 1d mm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1d_mm&amp;diff=83295"/>
		<updated>2014-02-11T03:51:43Z</updated>

		<summary type="html">&lt;p&gt;Mmehta2: /* Serialization Performance in Ruby */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== '''Serialization''' ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Serialization'''[http://en.wikipedia.org/wiki/Serialization] is a process of converting a data structure or an object into a stream of bytes or string to facilitate storage in memory, file(persistence storage) or transmission over a network. The process of Serialization is also referred to as ''Marshalling''[http://en.wikipedia.org/wiki/Marshalling_%28computer_science%29]. 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. &lt;br /&gt;
&lt;br /&gt;
'''Basic Advantages of Serialization''':&lt;br /&gt;
&lt;br /&gt;
1. Communication between two or more processes on same machine. Object state can be saved and shared in a persistent or in-memory store.&lt;br /&gt;
&lt;br /&gt;
2. Communication between processes on different machines. Serialization facilitates the transmission of an object through a network.&lt;br /&gt;
&lt;br /&gt;
3. Creating a clone of an object. &lt;br /&gt;
&lt;br /&gt;
4. Cross-platform compatibility. Object can be serialized in a common format that is understood by multiple platforms. Eg. JSON, XML.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''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''.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Few Practical Applications for Serialization'''&lt;br /&gt;
&lt;br /&gt;
1. HTTP Session Replication by sharing session objects across web servers for handling failover scenarios&lt;br /&gt;
&lt;br /&gt;
2. Serialization facilitates communication in Remote Method Invocation or Remote procedure calls &lt;br /&gt;
&lt;br /&gt;
3. Rails Cookie Handling[http://techbrahmana.blogspot.com/2012/03/rails-cookie-handling-serialization-and.html]. Cookies are stored marshalled/unmarshalled to and from client machines. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:SerializationChart.jpg]]&lt;br /&gt;
&lt;br /&gt;
== '''Serialization in Ruby:''' ==&lt;br /&gt;
&lt;br /&gt;
Ruby provides serialization capabilities through its module, [http://www.ruby-doc.org/core-2.0.0/Marshal.html Marshal]. There are also other libraries like YAML and JSON which can be used in Ruby to generate serialized objects for purposes like platform independence and human readable formats.&lt;br /&gt;
&lt;br /&gt;
== '''Types of Serialization''' ==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
=== Converting Ruby Objects in Human Readable Formats ===&lt;br /&gt;
The conversion of Ruby objects into YAML and JSON formats are explained below.&lt;br /&gt;
&lt;br /&gt;
==== Converting Ruby Objects to YAML format ====&lt;br /&gt;
&lt;br /&gt;
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. 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. In order to use it in Ruby, the yaml.rb file is required to be loaded which provides methods for converting objects into yaml format and creating .yml files.&lt;br /&gt;
&lt;br /&gt;
'''Examples of serialization using YAML:'''&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;nowiki&amp;gt; #Serialization using YAML's to_yaml method&lt;br /&gt;
 &lt;br /&gt;
  require &amp;quot;yaml&amp;quot;&lt;br /&gt;
     class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
	@name = name&lt;br /&gt;
	@age = age&lt;br /&gt;
	@country=country&lt;br /&gt;
     end&lt;br /&gt;
      def to_s&lt;br /&gt;
	&amp;quot;In First:\n#{@name}, #{@age}, #{@country}\n&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
  x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
  puts x&lt;br /&gt;
  puts x.to_yaml&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   In First:&lt;br /&gt;
   Tom, 25, USA&lt;br /&gt;
   --- !ruby/object:First&lt;br /&gt;
   name: Tom&lt;br /&gt;
   age: 25&lt;br /&gt;
   country: USA&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The above code displays the object x, first as a string and then in the yaml format. &lt;br /&gt;
&lt;br /&gt;
'''Saving YAML data into a file:'''&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt; # Serialization using YAML::dump&lt;br /&gt;
&lt;br /&gt;
require 'yaml'&lt;br /&gt;
&lt;br /&gt;
f = File.open( 'first.yml', 'w' )&lt;br /&gt;
YAML.dump( [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;], f )&lt;br /&gt;
f.close&lt;br /&gt;
		&lt;br /&gt;
File.open( 'first.yml' ){ |f|	&lt;br /&gt;
    $arr= YAML.load(f)&lt;br /&gt;
}	&lt;br /&gt;
&lt;br /&gt;
p( $arr )&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;]&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The dump function can be used to serialize the data and save it into a file in YAML format. As shown in the above example the data in YAML format can be de-serialized using the load function.&lt;br /&gt;
&lt;br /&gt;
YAML libraries also provides an option of selecting only those variables of the object that are needed to be serialized. This is done using the to_yaml_properties method as shown in the below example.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;nowiki&amp;gt;  #Custom Serialization using YAML&lt;br /&gt;
 &lt;br /&gt;
  require &amp;quot;yaml&amp;quot;&lt;br /&gt;
     class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
	@name = name&lt;br /&gt;
	@age = age&lt;br /&gt;
	@country=country&lt;br /&gt;
     end&lt;br /&gt;
     def to_s&lt;br /&gt;
	&amp;quot;In First:\n#{@name}, #{@age}, #{@country}\n&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
      def to_yaml_properties&lt;br /&gt;
	   [&amp;quot;@name&amp;quot;,&amp;quot;@age&amp;quot;]  #@country will not be serialized&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
  x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
  puts x&lt;br /&gt;
  puts x.to_yaml&lt;br /&gt;
  &amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   In First:&lt;br /&gt;
   Tom, 25, USA&lt;br /&gt;
   --- !ruby/object:First&lt;br /&gt;
   name: Tom&lt;br /&gt;
   age: 25&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Converting Ruby Objects to JSON format: ====&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
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. Ruby 1.8.7 distribution is not bundled with json gem. However, in Ruby 1.9.2, the json gem is bundled with the core Ruby distribution.&lt;br /&gt;
The JSON library can be installed using Ruby Gems[http://rubygems.org/] as shown below:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;# gem install json&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A JSON string for serialization can be created by using the JSON.generate method:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
        require 'json'&lt;br /&gt;
        my_hash = {:Welcome =&amp;gt; &amp;quot;Ruby&amp;quot;}&lt;br /&gt;
        puts JSON.generate(my_hash) =&amp;gt; &amp;quot;{\&amp;quot;WELCOME\&amp;quot;:\&amp;quot;RUBY\&amp;quot;}&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
'''Output:'''&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;{&amp;quot;{\&amp;quot;Welcome\&amp;quot;:\&amp;quot;Ruby\&amp;quot;}&amp;quot;=&amp;gt;&amp;quot;{\&amp;quot;WELCOME\&amp;quot;:\&amp;quot;RUBY\&amp;quot;}&amp;quot;}&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A JSON string received from another program can be parsed by using JSON.parse&lt;br /&gt;
Ruby thus converts String to Hash.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
        require 'json'&lt;br /&gt;
        my_hash = JSON.parse('{&amp;quot;Welcome&amp;quot;: &amp;quot;Ruby&amp;quot;}')&lt;br /&gt;
        puts my_hash[&amp;quot;Welcome&amp;quot;] =&amp;gt; &amp;quot;Ruby&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Converting Ruby Objects to Binary Formats ===&lt;br /&gt;
Binary Serialization is another form of serialization in Ruby which is not in human readable form. 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. Binary Serialization  is done using [http://www.ruby-doc.org/core-2.0.0/Marshal.html Marshal] which is built into Ruby and the code for it is written in Ruby's Marshal module(marshal.c) and thus no additional files are required in order to use it. The _dump and _load methods defined in marshal are used for serialization. Using marshal module the following types of objects can not be serialized: bindings, procedure objects, singleton objects, instances of IO objects and interfaces.&lt;br /&gt;
&lt;br /&gt;
Since the Binary Serialized data is not in human readable form, the following guidelines need to be followed.&lt;br /&gt;
     1.Use print instead of puts[http://ruby-doc.org/core-2.0.0/ARGF.html#] when serialized objects are written to a file in order to avoid new line characters to be written &lt;br /&gt;
       in the file.&lt;br /&gt;
     &lt;br /&gt;
     2.Use a record separator in order to differentiate between two objects.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Binary Serialization Example:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    class Animal&lt;br /&gt;
      def initialize  name, age&lt;br /&gt;
      @name = name&lt;br /&gt;
      @age=age&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
   class Cat &amp;lt; Animal&lt;br /&gt;
    def to_s&lt;br /&gt;
     &amp;quot;In Cat C: #{@name} \t #{@age}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
  c = Cat.new(&amp;quot;Kitty Kat&amp;quot;,5)&lt;br /&gt;
  puts &amp;quot;Before Serialization&amp;quot;&lt;br /&gt;
  puts c&lt;br /&gt;
  #puts d&lt;br /&gt;
  serialize_cat= Marshal.dump(c) #dumps the serialized cat object into serialize_cat&lt;br /&gt;
  puts &amp;quot;\nAfter Serialization:\n #{serialize_cat}&amp;quot;&lt;br /&gt;
  deserialize_cat= Marshal::load(serialize_cat) #deserializes the cat object and loads it back into deserialize_cat&lt;br /&gt;
  puts &amp;quot;\nAfter Deserialization\n #{deserialize_cat}&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   Before Serialization&lt;br /&gt;
   In Cat C: Kitty Kat 	 5&lt;br /&gt;
   After Serialization:&lt;br /&gt;
   oCat:&lt;br /&gt;
   @nameI&amp;quot;Kitty Kat:ET:	@agei&lt;br /&gt;
   After Deserialization&lt;br /&gt;
   In Cat C: Kitty Kat 	 5&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Similar to YAML, Marshal can also be used to dump data into a file. The above example showing the serialization using YAML::dump can be written using marshal as shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    f = File.open( 'first.yml', 'w' )&lt;br /&gt;
    Marshal.dump( [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;], f )&lt;br /&gt;
    f.close	&lt;br /&gt;
    File.open( 'first.yml' ){ |f|	&lt;br /&gt;
    $arr= Marshal.load(f)&lt;br /&gt;
    }	&lt;br /&gt;
    p( $arr )&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;]&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice that in the above example there is no &amp;quot;require&amp;quot; statement as opposed to the earlier example of writing serialized data into files using YAML. This is because unlike YAML, marshal is built-in Ruby and  no external libraries is required in order to use its functionality.&lt;br /&gt;
&lt;br /&gt;
Marshal can also be used to custom serialize an object i.e. it provides an option to omit the variables of an object that are not required in the serialized data. The following program shows the use of marshal_dump method for achieving custom serialization.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
      class First&lt;br /&gt;
      def initialize(name, age, country)&lt;br /&gt;
         @name = name&lt;br /&gt;
         @age = age&lt;br /&gt;
         @country=country&lt;br /&gt;
      end&lt;br /&gt;
      def to_s&lt;br /&gt;
            &amp;quot;In First: #{@name}, #{@age}, #{@country}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
      def marshal_dump&lt;br /&gt;
            [@name,@age]  #@country will not be serialized&lt;br /&gt;
      end&lt;br /&gt;
      def marshal_load(data)&lt;br /&gt;
       @name=data[0]&lt;br /&gt;
       @age=data[1]&lt;br /&gt;
       @country=&amp;quot;United States of America&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
      end&lt;br /&gt;
 &lt;br /&gt;
      x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
      puts x&lt;br /&gt;
  &lt;br /&gt;
      marshal_data = Marshal.dump( x ) &lt;br /&gt;
      y = Marshal.load( marshal_data ) &lt;br /&gt;
      p( y.to_s )&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    In First: Tom, 25, USA&lt;br /&gt;
    &amp;quot;In First: Tom, 25, United States of America&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== '''Serialization Performance in Ruby''' ==&lt;br /&gt;
The different serialization formats described above differ in the efficiency at which they can serialize and deserialize data and thus while serializing large amount of data their efficiency is taken into account. The .report[http://www.ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html] method in Ruby can be used to evaluate the performance of these serialization patterns. Marshal, as it handles data in binary format, is the most efficient form of serialization in Ruby. The following example compares the performance of marshal, JSON and YAML format.[http://www.pauldix.net/2008/08/serializing-dat.html]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   require 'benchmark'&lt;br /&gt;
   require 'rubygems'&lt;br /&gt;
   require 'json'&lt;br /&gt;
   require 'yaml'&lt;br /&gt;
   include Benchmark&lt;br /&gt;
   class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
      @name=name&lt;br /&gt;
      @age=age&lt;br /&gt;
      @country=country&lt;br /&gt;
     end&lt;br /&gt;
   end  &lt;br /&gt;
    x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
    puts &amp;quot;            User     System     Total      Real&amp;quot;&lt;br /&gt;
   benchmark do |t|&lt;br /&gt;
    print &amp;quot;Marshal:&amp;quot;&lt;br /&gt;
    t.report{1000.times do; Marshal.load(Marshal.dump(x));end}&lt;br /&gt;
    print &amp;quot;JSON:   &amp;quot;&lt;br /&gt;
    t.report{1000.times do; JSON.load(JSON.dump(x));end}&lt;br /&gt;
    print &amp;quot;YAML:   &amp;quot;&lt;br /&gt;
    t.report{1000.times do; YAML.load(YAML.dump(x));end}&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
              User     System     Total      Real&lt;br /&gt;
  Marshal:   0.010000   0.000000   0.010000 (  0.015408)&lt;br /&gt;
  JSON:      0.020000   0.010000   0.030000 (  0.022462)&lt;br /&gt;
  YAML:      0.460000   0.010000   0.470000 (  0.472149)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output of the above example shows that, given large chunks of data, marshal is comparatively more efficient format for serialization in Ruby.&lt;br /&gt;
&lt;br /&gt;
== Serialization in OOLS Languages: Comparison ==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Sl.No !! Ruby !! Java !! .Net Framework !! C++&lt;br /&gt;
|-&lt;br /&gt;
| 1 || Ruby provides a built in 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 || Although, there is no built in support for serialization in C++, it can be achieved by using Boost libraries[http://www.boost.org/doc/libs/1_36_0/libs/serialization/example/demo.cpp]&lt;br /&gt;
|-&lt;br /&gt;
| 2 || The built in module of Ruby [http://www.ruby-doc.org/core-2.0.0/Marshal.html (Marshal)] does not support platform independence, however, it can be achieved by using external libraries like [http://yaml.org YAML] and [http://www.w3schools.com/json/ JSON] || Similarly, Java's built in serialization is also not platform independent and in order to use serialization in Java across Ruby platform, jruby library should be used.  || .Net used Remoting technology to make it platform independent. || Serialization using the Boost libraries is not platform independent.&lt;br /&gt;
|-&lt;br /&gt;
| 3 || YAML provides a method [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/syck/rdoc/Object.html (to_yaml_properties)] which can be used to select the variables who's value is need to be serialized. With Marshal, we need to write a method named marshal_dump defining the variables of an object that has to be serialized. || Provides an option for serializing only the required 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 data 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 || Serialization using the Boost libraries is custom and thus the user can specify the part of the objects to be serialized.&lt;br /&gt;
|-&lt;br /&gt;
| 4 || Bindings, procedure objects, singleton objects, instances of IO objects and interfaces can not be serialized. Serializing these objects throws TypeError exceptions. || Thread, OutputStream and its subclasses, and Socket are not serializable in Java.[http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html] || Objects like DataRow are non serializable in .NET. Whether an object is serializable or not can be checked using the [http://msdn.microsoft.com/en-us/library/system.type.isserializable(v=vs.110).aspx?cs-save-lang=1&amp;amp;cs-lang=cpp#code-snippet-1 Type.IsSerializable] || There are no such objects in C++.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== See Also==&lt;br /&gt;
&lt;br /&gt;
1. [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/json/rdoc/JSON.html Serialization in Ruby JSON]&lt;br /&gt;
&lt;br /&gt;
2. [http://api.rubyonrails.org/classes/ActiveModel/Serialization.html Serialization in Rails]&lt;br /&gt;
&lt;br /&gt;
3. [http://gregmoreno.wordpress.com/2011/01/27/preventing-model-explosion-via-rails-serialization/ Article on Rails Serialization]&lt;br /&gt;
&lt;br /&gt;
4. [http://en.wikipedia.org/wiki/Google_Protocol_Buffers Protocol Buffers]&lt;br /&gt;
&lt;br /&gt;
5. [https://rubygems.org/gems/ruby-protocol-buffers Ruby Protocol Buffers Gem]&lt;br /&gt;
&lt;br /&gt;
6. [http://en.wikipedia.org/wiki/Apache_Avro Avro]&lt;br /&gt;
&lt;br /&gt;
7. [http://rubygems.org/gems/avro Ruby Avro Gem]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
1. [http://en.wikipedia.org/wiki/Serialization Serilization in General]&lt;br /&gt;
&lt;br /&gt;
2. [http://en.wikipedia.org/wiki/Marshalling_%28computer_science%29 Marshaling]&lt;br /&gt;
&lt;br /&gt;
3. [http://techbrahmana.blogspot.com/2012/03/rails-cookie-handling-serialization-and.html Rails Cookie Handling]&lt;br /&gt;
&lt;br /&gt;
4. [http://yaml.org/ YAML]&lt;br /&gt;
&lt;br /&gt;
5. [http://www.ruby-doc.org/core-2.1.0/Marshal.html Marshal module]&lt;br /&gt;
&lt;br /&gt;
6. [http://json.org/ JSON]&lt;br /&gt;
&lt;br /&gt;
7. [http://rubygems.org/ Ruby Gems]&lt;br /&gt;
&lt;br /&gt;
8. [http://ruby-doc.org/core-2.0.0/ARGF.html# Ruby ARGF Documentation]&lt;br /&gt;
&lt;br /&gt;
9. [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html Ruby Documentation for Benchmark Gem]&lt;br /&gt;
&lt;br /&gt;
10. [http://www.pauldix.net/2008/08/serializing-dat.html Serialization Performance Comparison]&lt;br /&gt;
&lt;br /&gt;
11. [http://www.boost.org/doc/libs/1_36_0/libs/serialization/example/demo.cpp Boost Libraries]&lt;br /&gt;
&lt;br /&gt;
12. [http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html Java Serialization]&lt;br /&gt;
&lt;br /&gt;
13. [http://www.w3schools.com/json/ JSON Tutorial]&lt;br /&gt;
&lt;br /&gt;
14. [http://www.skorks.com/2010/04/serializing-and-deserializing-objects-with-ruby/ Serializing and De-serializing in Ruby]&lt;br /&gt;
&lt;br /&gt;
15. [http://www.codeproject.com/Articles/33296/Serialization-and-De-serialization Serialization and De-serialization]&lt;br /&gt;
&lt;br /&gt;
16. [http://msdn.microsoft.com/en-us/library/182eeyhh.aspx XML Serialization]&lt;br /&gt;
&lt;br /&gt;
17. [http://www.tutorialspoint.com/java/java_serialization.htm Java Serialization Tutorial]&lt;br /&gt;
&lt;br /&gt;
18. [http://www.sapphiresteel.com/ruby-programming/The-Book-Of-Ruby The Book of Ruby]&lt;br /&gt;
&lt;br /&gt;
19. [http://www.codeproject.com/Articles/20962/Introducing-Serialization-in-NET Serialization in .Net]&lt;br /&gt;
&lt;br /&gt;
20. [http://ruby.about.com/od/advancedruby/ss/Serialization-In-Ruby-Yaml.html Serialization in Ruby YAML]&lt;br /&gt;
&lt;br /&gt;
21. [http://ruby.about.com/od/tasks/a/The-Json-Gem.html Installing JSON gem]&lt;br /&gt;
&lt;br /&gt;
22. [http://www.ruby-doc.org/stdlib-2.0.0/libdoc/json/rdoc/JSON.html Serialization in Ruby JSON]&lt;/div&gt;</summary>
		<author><name>Mmehta2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1d_mm&amp;diff=83288</id>
		<title>CSC/ECE 517 Spring 2014/ch1a 1d mm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1d_mm&amp;diff=83288"/>
		<updated>2014-02-11T03:47:04Z</updated>

		<summary type="html">&lt;p&gt;Mmehta2: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== '''Serialization''' ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Serialization'''[http://en.wikipedia.org/wiki/Serialization] is a process of converting a data structure or an object into a stream of bytes or string to facilitate storage in memory, file(persistence storage) or transmission over a network. The process of Serialization is also referred to as ''Marshalling''[http://en.wikipedia.org/wiki/Marshalling_%28computer_science%29]. 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. &lt;br /&gt;
&lt;br /&gt;
'''Basic Advantages of Serialization''':&lt;br /&gt;
&lt;br /&gt;
1. Communication between two or more processes on same machine. Object state can be saved and shared in a persistent or in-memory store.&lt;br /&gt;
&lt;br /&gt;
2. Communication between processes on different machines. Serialization facilitates the transmission of an object through a network.&lt;br /&gt;
&lt;br /&gt;
3. Creating a clone of an object. &lt;br /&gt;
&lt;br /&gt;
4. Cross-platform compatibility. Object can be serialized in a common format that is understood by multiple platforms. Eg. JSON, XML.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''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''.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Few Practical Applications for Serialization'''&lt;br /&gt;
&lt;br /&gt;
1. HTTP Session Replication by sharing session objects across web servers for handling failover scenarios&lt;br /&gt;
&lt;br /&gt;
2. Serialization facilitates communication in Remote Method Invocation or Remote procedure calls &lt;br /&gt;
&lt;br /&gt;
3. Rails Cookie Handling[http://techbrahmana.blogspot.com/2012/03/rails-cookie-handling-serialization-and.html]. Cookies are stored marshalled/unmarshalled to and from client machines. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:SerializationChart.jpg]]&lt;br /&gt;
&lt;br /&gt;
== '''Serialization in Ruby:''' ==&lt;br /&gt;
&lt;br /&gt;
Ruby provides serialization capabilities through its module, [http://www.ruby-doc.org/core-2.0.0/Marshal.html Marshal]. There are also other libraries like YAML and JSON which can be used in Ruby to generate serialized objects for purposes like platform independence and human readable formats.&lt;br /&gt;
&lt;br /&gt;
== '''Types of Serialization''' ==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
=== Converting Ruby Objects in Human Readable Formats ===&lt;br /&gt;
The conversion of Ruby objects into YAML and JSON formats are explained below.&lt;br /&gt;
&lt;br /&gt;
==== Converting Ruby Objects to YAML format ====&lt;br /&gt;
&lt;br /&gt;
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. 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. In order to use it in Ruby, the yaml.rb file is required to be loaded which provides methods for converting objects into yaml format and creating .yml files.&lt;br /&gt;
&lt;br /&gt;
'''Examples of serialization using YAML:'''&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;nowiki&amp;gt; #Serialization using YAML's to_yaml method&lt;br /&gt;
 &lt;br /&gt;
  require &amp;quot;yaml&amp;quot;&lt;br /&gt;
     class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
	@name = name&lt;br /&gt;
	@age = age&lt;br /&gt;
	@country=country&lt;br /&gt;
     end&lt;br /&gt;
      def to_s&lt;br /&gt;
	&amp;quot;In First:\n#{@name}, #{@age}, #{@country}\n&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
  x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
  puts x&lt;br /&gt;
  puts x.to_yaml&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   In First:&lt;br /&gt;
   Tom, 25, USA&lt;br /&gt;
   --- !ruby/object:First&lt;br /&gt;
   name: Tom&lt;br /&gt;
   age: 25&lt;br /&gt;
   country: USA&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The above code displays the object x, first as a string and then in the yaml format. &lt;br /&gt;
&lt;br /&gt;
'''Saving YAML data into a file:'''&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt; # Serialization using YAML::dump&lt;br /&gt;
&lt;br /&gt;
require 'yaml'&lt;br /&gt;
&lt;br /&gt;
f = File.open( 'first.yml', 'w' )&lt;br /&gt;
YAML.dump( [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;], f )&lt;br /&gt;
f.close&lt;br /&gt;
		&lt;br /&gt;
File.open( 'first.yml' ){ |f|	&lt;br /&gt;
    $arr= YAML.load(f)&lt;br /&gt;
}	&lt;br /&gt;
&lt;br /&gt;
p( $arr )&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;]&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The dump function can be used to serialize the data and save it into a file in YAML format. As shown in the above example the data in YAML format can be de-serialized using the load function.&lt;br /&gt;
&lt;br /&gt;
YAML libraries also provides an option of selecting only those variables of the object that are needed to be serialized. This is done using the to_yaml_properties method as shown in the below example.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;nowiki&amp;gt;  #Custom Serialization using YAML&lt;br /&gt;
 &lt;br /&gt;
  require &amp;quot;yaml&amp;quot;&lt;br /&gt;
     class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
	@name = name&lt;br /&gt;
	@age = age&lt;br /&gt;
	@country=country&lt;br /&gt;
     end&lt;br /&gt;
     def to_s&lt;br /&gt;
	&amp;quot;In First:\n#{@name}, #{@age}, #{@country}\n&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
      def to_yaml_properties&lt;br /&gt;
	   [&amp;quot;@name&amp;quot;,&amp;quot;@age&amp;quot;]  #@country will not be serialized&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
  x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
  puts x&lt;br /&gt;
  puts x.to_yaml&lt;br /&gt;
  &amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   In First:&lt;br /&gt;
   Tom, 25, USA&lt;br /&gt;
   --- !ruby/object:First&lt;br /&gt;
   name: Tom&lt;br /&gt;
   age: 25&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Converting Ruby Objects to JSON format: ====&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
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. Ruby 1.8.7 distribution is not bundled with json gem. However, in Ruby 1.9.2, the json gem is bundled with the core Ruby distribution.&lt;br /&gt;
The JSON library can be installed using Ruby Gems[http://rubygems.org/] as shown below:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;# gem install json&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A JSON string for serialization can be created by using the JSON.generate method:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
        require 'json'&lt;br /&gt;
        my_hash = {:Welcome =&amp;gt; &amp;quot;Ruby&amp;quot;}&lt;br /&gt;
        puts JSON.generate(my_hash) =&amp;gt; &amp;quot;{\&amp;quot;WELCOME\&amp;quot;:\&amp;quot;RUBY\&amp;quot;}&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
'''Output:'''&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;{&amp;quot;{\&amp;quot;Welcome\&amp;quot;:\&amp;quot;Ruby\&amp;quot;}&amp;quot;=&amp;gt;&amp;quot;{\&amp;quot;WELCOME\&amp;quot;:\&amp;quot;RUBY\&amp;quot;}&amp;quot;}&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A JSON string received from another program can be parsed by using JSON.parse&lt;br /&gt;
Ruby thus converts String to Hash.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
        require 'json'&lt;br /&gt;
        my_hash = JSON.parse('{&amp;quot;Welcome&amp;quot;: &amp;quot;Ruby&amp;quot;}')&lt;br /&gt;
        puts my_hash[&amp;quot;Welcome&amp;quot;] =&amp;gt; &amp;quot;Ruby&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Converting Ruby Objects to Binary Formats ===&lt;br /&gt;
Binary Serialization is another form of serialization in Ruby which is not in human readable form. 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. Binary Serialization  is done using [http://www.ruby-doc.org/core-2.0.0/Marshal.html Marshal] which is built into Ruby and the code for it is written in Ruby's Marshal module(marshal.c) and thus no additional files are required in order to use it. The _dump and _load methods defined in marshal are used for serialization. Using marshal module the following types of objects can not be serialized: bindings, procedure objects, singleton objects, instances of IO objects and interfaces.&lt;br /&gt;
&lt;br /&gt;
Since the Binary Serialized data is not in human readable form, the following guidelines need to be followed.&lt;br /&gt;
     1.Use print instead of puts[http://ruby-doc.org/core-2.0.0/ARGF.html#] when serialized objects are written to a file in order to avoid new line characters to be written &lt;br /&gt;
       in the file.&lt;br /&gt;
     &lt;br /&gt;
     2.Use a record separator in order to differentiate between two objects.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Binary Serialization Example:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    class Animal&lt;br /&gt;
      def initialize  name, age&lt;br /&gt;
      @name = name&lt;br /&gt;
      @age=age&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
   class Cat &amp;lt; Animal&lt;br /&gt;
    def to_s&lt;br /&gt;
     &amp;quot;In Cat C: #{@name} \t #{@age}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
  c = Cat.new(&amp;quot;Kitty Kat&amp;quot;,5)&lt;br /&gt;
  puts &amp;quot;Before Serialization&amp;quot;&lt;br /&gt;
  puts c&lt;br /&gt;
  #puts d&lt;br /&gt;
  serialize_cat= Marshal.dump(c) #dumps the serialized cat object into serialize_cat&lt;br /&gt;
  puts &amp;quot;\nAfter Serialization:\n #{serialize_cat}&amp;quot;&lt;br /&gt;
  deserialize_cat= Marshal::load(serialize_cat) #deserializes the cat object and loads it back into deserialize_cat&lt;br /&gt;
  puts &amp;quot;\nAfter Deserialization\n #{deserialize_cat}&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   Before Serialization&lt;br /&gt;
   In Cat C: Kitty Kat 	 5&lt;br /&gt;
   After Serialization:&lt;br /&gt;
   oCat:&lt;br /&gt;
   @nameI&amp;quot;Kitty Kat:ET:	@agei&lt;br /&gt;
   After Deserialization&lt;br /&gt;
   In Cat C: Kitty Kat 	 5&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Similar to YAML, Marshal can also be used to dump data into a file. The above example showing the serialization using YAML::dump can be written using marshal as shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    f = File.open( 'first.yml', 'w' )&lt;br /&gt;
    Marshal.dump( [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;], f )&lt;br /&gt;
    f.close	&lt;br /&gt;
    File.open( 'first.yml' ){ |f|	&lt;br /&gt;
    $arr= Marshal.load(f)&lt;br /&gt;
    }	&lt;br /&gt;
    p( $arr )&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;]&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice that in the above example there is no &amp;quot;require&amp;quot; statement as opposed to the earlier example of writing serialized data into files using YAML. This is because unlike YAML, marshal is built-in Ruby and  no external libraries is required in order to use its functionality.&lt;br /&gt;
&lt;br /&gt;
Marshal can also be used to custom serialize an object i.e. it provides an option to omit the variables of an object that are not required in the serialized data. The following program shows the use of marshal_dump method for achieving custom serialization.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
      class First&lt;br /&gt;
      def initialize(name, age, country)&lt;br /&gt;
         @name = name&lt;br /&gt;
         @age = age&lt;br /&gt;
         @country=country&lt;br /&gt;
      end&lt;br /&gt;
      def to_s&lt;br /&gt;
            &amp;quot;In First: #{@name}, #{@age}, #{@country}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
      def marshal_dump&lt;br /&gt;
            [@name,@age]  #@country will not be serialized&lt;br /&gt;
      end&lt;br /&gt;
      def marshal_load(data)&lt;br /&gt;
       @name=data[0]&lt;br /&gt;
       @age=data[1]&lt;br /&gt;
       @country=&amp;quot;United States of America&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
      end&lt;br /&gt;
 &lt;br /&gt;
      x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
      puts x&lt;br /&gt;
  &lt;br /&gt;
      marshal_data = Marshal.dump( x ) &lt;br /&gt;
      y = Marshal.load( marshal_data ) &lt;br /&gt;
      p( y.to_s )&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    In First: Tom, 25, USA&lt;br /&gt;
    &amp;quot;In First: Tom, 25, United States of America&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== '''Serialization Performance in Ruby''' ==&lt;br /&gt;
The different serialization formats described above differ in the efficiency at which they can serialize and deserialize data and thus while serializing large amount of data their efficiency is taken into account. The .report[http://www.ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html] method in Ruby can be used to evaluate the performance of these serialization patterns. Marshal, as it handles data in binary format, is the most efficient form of serialization in Ruby. The following example compares the performance of marshal, JSON and YAML format.[http://www.pauldix.net/2008/08/serializing-dat.html]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   require 'benchmark'&lt;br /&gt;
   require 'rubygems'&lt;br /&gt;
   require 'json'&lt;br /&gt;
   require 'yaml'&lt;br /&gt;
   include Benchmark&lt;br /&gt;
   class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
      @name=name&lt;br /&gt;
      @age=age&lt;br /&gt;
      @country=country&lt;br /&gt;
     end&lt;br /&gt;
   end  &lt;br /&gt;
    x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
   benchmark do |t|&lt;br /&gt;
    print &amp;quot;Marshal:&amp;quot;&lt;br /&gt;
    t.report{1000.times do; Marshal.load(Marshal.dump(x));end}&lt;br /&gt;
    print &amp;quot;JSON:&amp;quot;&lt;br /&gt;
    t.report{1000.times do; JSON.load(JSON.dump(x));end}&lt;br /&gt;
    print &amp;quot;YAML:&amp;quot;&lt;br /&gt;
    t.report{1000.times do; YAML.load(YAML.dump(x));end}&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   Marshal:   0.020000   0.000000   0.020000 (  0.015473)&lt;br /&gt;
   JSON:   0.020000   0.000000   0.020000 (  0.023934)&lt;br /&gt;
   YAML:   0.460000   0.010000   0.470000 (  0.476826)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output of the above example shows that, given large chunks of data, marshal is comparatively more efficient format for serialization in Ruby.&lt;br /&gt;
&lt;br /&gt;
== Serialization in OOLS Languages: Comparison ==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Sl.No !! Ruby !! Java !! .Net Framework !! C++&lt;br /&gt;
|-&lt;br /&gt;
| 1 || Ruby provides a built in 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 || Although, there is no built in support for serialization in C++, it can be achieved by using Boost libraries[http://www.boost.org/doc/libs/1_36_0/libs/serialization/example/demo.cpp]&lt;br /&gt;
|-&lt;br /&gt;
| 2 || The built in module of Ruby [http://www.ruby-doc.org/core-2.0.0/Marshal.html (Marshal)] does not support platform independence, however, it can be achieved by using external libraries like [http://yaml.org YAML] and [http://www.w3schools.com/json/ JSON] || Similarly, Java's built in serialization is also not platform independent and in order to use serialization in Java across Ruby platform, jruby library should be used.  || .Net used Remoting technology to make it platform independent. || Serialization using the Boost libraries is not platform independent.&lt;br /&gt;
|-&lt;br /&gt;
| 3 || YAML provides a method [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/syck/rdoc/Object.html (to_yaml_properties)] which can be used to select the variables who's value is need to be serialized. With Marshal, we need to write a method named marshal_dump defining the variables of an object that has to be serialized. || Provides an option for serializing only the required 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 data 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 || Serialization using the Boost libraries is custom and thus the user can specify the part of the objects to be serialized.&lt;br /&gt;
|-&lt;br /&gt;
| 4 || Bindings, procedure objects, singleton objects, instances of IO objects and interfaces can not be serialized. Serializing these objects throws TypeError exceptions. || Thread, OutputStream and its subclasses, and Socket are not serializable in Java.[http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html] || Objects like DataRow are non serializable in .NET. Whether an object is serializable or not can be checked using the [http://msdn.microsoft.com/en-us/library/system.type.isserializable(v=vs.110).aspx?cs-save-lang=1&amp;amp;cs-lang=cpp#code-snippet-1 Type.IsSerializable] || There are no such objects in C++.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== See Also==&lt;br /&gt;
&lt;br /&gt;
1. [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/json/rdoc/JSON.html Serialization in Ruby JSON]&lt;br /&gt;
&lt;br /&gt;
2. [http://api.rubyonrails.org/classes/ActiveModel/Serialization.html Serialization in Rails]&lt;br /&gt;
&lt;br /&gt;
3. [http://gregmoreno.wordpress.com/2011/01/27/preventing-model-explosion-via-rails-serialization/ Article on Rails Serialization]&lt;br /&gt;
&lt;br /&gt;
4. [http://en.wikipedia.org/wiki/Google_Protocol_Buffers Protocol Buffers]&lt;br /&gt;
&lt;br /&gt;
5. [https://rubygems.org/gems/ruby-protocol-buffers Ruby Protocol Buffers Gem]&lt;br /&gt;
&lt;br /&gt;
6. [http://en.wikipedia.org/wiki/Apache_Avro Avro]&lt;br /&gt;
&lt;br /&gt;
7. [http://rubygems.org/gems/avro Ruby Avro Gem]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
1. [http://en.wikipedia.org/wiki/Serialization Serilization in General]&lt;br /&gt;
&lt;br /&gt;
2. [http://en.wikipedia.org/wiki/Marshalling_%28computer_science%29 Marshaling]&lt;br /&gt;
&lt;br /&gt;
3. [http://techbrahmana.blogspot.com/2012/03/rails-cookie-handling-serialization-and.html Rails Cookie Handling]&lt;br /&gt;
&lt;br /&gt;
4. [http://yaml.org/ YAML]&lt;br /&gt;
&lt;br /&gt;
5. [http://www.ruby-doc.org/core-2.1.0/Marshal.html Marshal module]&lt;br /&gt;
&lt;br /&gt;
6. [http://json.org/ JSON]&lt;br /&gt;
&lt;br /&gt;
7. [http://rubygems.org/ Ruby Gems]&lt;br /&gt;
&lt;br /&gt;
8. [http://ruby-doc.org/core-2.0.0/ARGF.html# Ruby ARGF Documentation]&lt;br /&gt;
&lt;br /&gt;
9. [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html Ruby Documentation for Benchmark Gem]&lt;br /&gt;
&lt;br /&gt;
10. [http://www.pauldix.net/2008/08/serializing-dat.html Serialization Performance Comparison]&lt;br /&gt;
&lt;br /&gt;
11. [http://www.boost.org/doc/libs/1_36_0/libs/serialization/example/demo.cpp Boost Libraries]&lt;br /&gt;
&lt;br /&gt;
12. [http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html Java Serialization]&lt;br /&gt;
&lt;br /&gt;
13. [http://www.w3schools.com/json/ JSON Tutorial]&lt;br /&gt;
&lt;br /&gt;
14. [http://www.skorks.com/2010/04/serializing-and-deserializing-objects-with-ruby/ Serializing and De-serializing in Ruby]&lt;br /&gt;
&lt;br /&gt;
15. [http://www.codeproject.com/Articles/33296/Serialization-and-De-serialization Serialization and De-serialization]&lt;br /&gt;
&lt;br /&gt;
16. [http://msdn.microsoft.com/en-us/library/182eeyhh.aspx XML Serialization]&lt;br /&gt;
&lt;br /&gt;
17. [http://www.tutorialspoint.com/java/java_serialization.htm Java Serialization Tutorial]&lt;br /&gt;
&lt;br /&gt;
18. [http://www.sapphiresteel.com/ruby-programming/The-Book-Of-Ruby The Book of Ruby]&lt;br /&gt;
&lt;br /&gt;
19. [http://www.codeproject.com/Articles/20962/Introducing-Serialization-in-NET Serialization in .Net]&lt;br /&gt;
&lt;br /&gt;
20. [http://ruby.about.com/od/advancedruby/ss/Serialization-In-Ruby-Yaml.html Serialization in Ruby YAML]&lt;br /&gt;
&lt;br /&gt;
21. [http://ruby.about.com/od/tasks/a/The-Json-Gem.html Installing JSON gem]&lt;br /&gt;
&lt;br /&gt;
22. [http://www.ruby-doc.org/stdlib-2.0.0/libdoc/json/rdoc/JSON.html Serialization in Ruby JSON]&lt;/div&gt;</summary>
		<author><name>Mmehta2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1d_mm&amp;diff=83286</id>
		<title>CSC/ECE 517 Spring 2014/ch1a 1d mm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1d_mm&amp;diff=83286"/>
		<updated>2014-02-11T03:45:25Z</updated>

		<summary type="html">&lt;p&gt;Mmehta2: /* Serialization Performance in Ruby */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== '''Serialization''' ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Serialization'''[http://en.wikipedia.org/wiki/Serialization] is a process of converting a data structure or an object into a stream of bytes or string to facilitate storage in memory, file(persistence storage) or transmission over a network. The process of Serialization is also referred to as ''Marshalling''[http://en.wikipedia.org/wiki/Marshalling_%28computer_science%29]. 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. &lt;br /&gt;
&lt;br /&gt;
'''Basic Advantages of Serialization''':&lt;br /&gt;
&lt;br /&gt;
1. Communication between two or more processes on same machine. Object state can be saved and shared in a persistent or in-memory store.&lt;br /&gt;
&lt;br /&gt;
2. Communication between processes on different machines. Serialization facilitates the transmission of an object through a network.&lt;br /&gt;
&lt;br /&gt;
3. Creating a clone of an object. &lt;br /&gt;
&lt;br /&gt;
4. Cross-platform compatibility. Object can be serialized in a common format that is understood by multiple platforms. Eg. JSON, XML.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''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''.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Few Practical Applications for Serialization'''&lt;br /&gt;
&lt;br /&gt;
1. HTTP Session Replication by sharing session objects across web servers for handling failover scenarios&lt;br /&gt;
&lt;br /&gt;
2. Serialization facilitates communication in Remote Method Invocation or Remote procedure calls &lt;br /&gt;
&lt;br /&gt;
3. Rails Cookie Handling[http://techbrahmana.blogspot.com/2012/03/rails-cookie-handling-serialization-and.html]. Cookies are stored marshalled/unmarshalled to and from client machines. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:SerializationChart.jpg]]&lt;br /&gt;
&lt;br /&gt;
== '''Serialization in Ruby:''' ==&lt;br /&gt;
&lt;br /&gt;
Ruby provides serialization capabilities through its module, [http://www.ruby-doc.org/core-2.0.0/Marshal.html Marshal]. There are also other libraries like YAML and JSON which can be used in Ruby to generate serialized objects for purposes like platform independence and human readable formats.&lt;br /&gt;
&lt;br /&gt;
== '''Types of Serialization''' ==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
=== Converting Ruby Objects in Human Readable Formats ===&lt;br /&gt;
The conversion of Ruby objects into YAML and JSON formats are explained below.&lt;br /&gt;
&lt;br /&gt;
==== Converting Ruby Objects to YAML format ====&lt;br /&gt;
&lt;br /&gt;
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. 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. In order to use it in Ruby, the yaml.rb file is required to be loaded which provides methods for converting objects into yaml format and creating .yml files.&lt;br /&gt;
&lt;br /&gt;
'''Examples of serialization using YAML:'''&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;nowiki&amp;gt; #Serialization using YAML's to_yaml method&lt;br /&gt;
 &lt;br /&gt;
  require &amp;quot;yaml&amp;quot;&lt;br /&gt;
     class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
	@name = name&lt;br /&gt;
	@age = age&lt;br /&gt;
	@country=country&lt;br /&gt;
     end&lt;br /&gt;
      def to_s&lt;br /&gt;
	&amp;quot;In First:\n#{@name}, #{@age}, #{@country}\n&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
  x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
  puts x&lt;br /&gt;
  puts x.to_yaml&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   In First:&lt;br /&gt;
   Tom, 25, USA&lt;br /&gt;
   --- !ruby/object:First&lt;br /&gt;
   name: Tom&lt;br /&gt;
   age: 25&lt;br /&gt;
   country: USA&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The above code displays the object x, first as a string and then in the yaml format. &lt;br /&gt;
&lt;br /&gt;
'''Saving YAML data into a file:'''&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt; # Serialization using YAML::dump&lt;br /&gt;
&lt;br /&gt;
require 'yaml'&lt;br /&gt;
&lt;br /&gt;
f = File.open( 'first.yml', 'w' )&lt;br /&gt;
YAML.dump( [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;], f )&lt;br /&gt;
f.close&lt;br /&gt;
		&lt;br /&gt;
File.open( 'first.yml' ){ |f|	&lt;br /&gt;
    $arr= YAML.load(f)&lt;br /&gt;
}	&lt;br /&gt;
&lt;br /&gt;
p( $arr )&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;]&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The dump function can be used to serialize the data and save it into a file in YAML format. As shown in the above example the data in YAML format can be de-serialized using the load function.&lt;br /&gt;
&lt;br /&gt;
YAML libraries also provides an option of selecting only those variables of the object that are needed to be serialized. This is done using the to_yaml_properties method as shown in the below example.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;nowiki&amp;gt;  #Custom Serialization using YAML&lt;br /&gt;
 &lt;br /&gt;
  require &amp;quot;yaml&amp;quot;&lt;br /&gt;
     class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
	@name = name&lt;br /&gt;
	@age = age&lt;br /&gt;
	@country=country&lt;br /&gt;
     end&lt;br /&gt;
     def to_s&lt;br /&gt;
	&amp;quot;In First:\n#{@name}, #{@age}, #{@country}\n&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
      def to_yaml_properties&lt;br /&gt;
	   [&amp;quot;@name&amp;quot;,&amp;quot;@age&amp;quot;]  #@country will not be serialized&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
  x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
  puts x&lt;br /&gt;
  puts x.to_yaml&lt;br /&gt;
  &amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   In First:&lt;br /&gt;
   Tom, 25, USA&lt;br /&gt;
   --- !ruby/object:First&lt;br /&gt;
   name: Tom&lt;br /&gt;
   age: 25&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Converting Ruby Objects to JSON format: ====&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
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. Ruby 1.8.7 distribution is not bundled with json gem. However, in Ruby 1.9.2, the json gem is bundled with the core Ruby distribution.&lt;br /&gt;
The JSON library can be installed using Ruby Gems[http://rubygems.org/] as shown below:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;# gem install json&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A JSON string for serialization can be created by using the JSON.generate method:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
        require 'json'&lt;br /&gt;
        my_hash = {:Welcome =&amp;gt; &amp;quot;Ruby&amp;quot;}&lt;br /&gt;
        puts JSON.generate(my_hash) =&amp;gt; &amp;quot;{\&amp;quot;WELCOME\&amp;quot;:\&amp;quot;RUBY\&amp;quot;}&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
'''Output:'''&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;{&amp;quot;{\&amp;quot;Welcome\&amp;quot;:\&amp;quot;Ruby\&amp;quot;}&amp;quot;=&amp;gt;&amp;quot;{\&amp;quot;WELCOME\&amp;quot;:\&amp;quot;RUBY\&amp;quot;}&amp;quot;}&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A JSON string received from another program can be parsed by using JSON.parse&lt;br /&gt;
Ruby thus converts String to Hash.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
        require 'json'&lt;br /&gt;
        my_hash = JSON.parse('{&amp;quot;Welcome&amp;quot;: &amp;quot;Ruby&amp;quot;}')&lt;br /&gt;
        puts my_hash[&amp;quot;Welcome&amp;quot;] =&amp;gt; &amp;quot;Ruby&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Converting Ruby Objects to Binary Formats ===&lt;br /&gt;
Binary Serialization is another form of serialization in Ruby which is not in human readable form. 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. Binary Serialization  is done using [http://www.ruby-doc.org/core-2.0.0/Marshal.html Marshal] which is built into Ruby and the code for it is written in Ruby's Marshal module(marshal.c) and thus no additional files are required in order to use it. The _dump and _load methods defined in marshal are used for serialization. Using marshal module the following types of objects can not be serialized: bindings, procedure objects, singleton objects, instances of IO objects and interfaces.&lt;br /&gt;
&lt;br /&gt;
Since the Binary Serialized data is not in human readable form, the following guidelines need to be followed.&lt;br /&gt;
     1.Use print instead of puts[http://ruby-doc.org/core-2.0.0/ARGF.html#] when serialized objects are written to a file in order to avoid new line characters to be written &lt;br /&gt;
       in the file.&lt;br /&gt;
     &lt;br /&gt;
     2.Use a record separator in order to differentiate between two objects.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Binary Serialization Example:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    class Animal&lt;br /&gt;
      def initialize  name, age&lt;br /&gt;
      @name = name&lt;br /&gt;
      @age=age&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
   class Cat &amp;lt; Animal&lt;br /&gt;
    def to_s&lt;br /&gt;
     &amp;quot;In Cat C: #{@name} \t #{@age}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
  c = Cat.new(&amp;quot;Kitty Kat&amp;quot;,5)&lt;br /&gt;
  puts &amp;quot;Before Serialization&amp;quot;&lt;br /&gt;
  puts c&lt;br /&gt;
  #puts d&lt;br /&gt;
  serialize_cat= Marshal.dump(c) #dumps the serialized cat object into serialize_cat&lt;br /&gt;
  puts &amp;quot;\nAfter Serialization:\n #{serialize_cat}&amp;quot;&lt;br /&gt;
  deserialize_cat= Marshal::load(serialize_cat) #deserializes the cat object and loads it back into deserialize_cat&lt;br /&gt;
  puts &amp;quot;\nAfter Deserialization\n #{deserialize_cat}&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   Before Serialization&lt;br /&gt;
   In Cat C: Kitty Kat 	 5&lt;br /&gt;
   After Serialization:&lt;br /&gt;
   oCat:&lt;br /&gt;
   @nameI&amp;quot;Kitty Kat:ET:	@agei&lt;br /&gt;
   After Deserialization&lt;br /&gt;
   In Cat C: Kitty Kat 	 5&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Similar to YAML, Marshal can also be used to dump data into a file. The above example showing the serialization using YAML::dump can be written using marshal as shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    f = File.open( 'first.yml', 'w' )&lt;br /&gt;
    Marshal.dump( [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;], f )&lt;br /&gt;
    f.close	&lt;br /&gt;
    File.open( 'first.yml' ){ |f|	&lt;br /&gt;
    $arr= Marshal.load(f)&lt;br /&gt;
    }	&lt;br /&gt;
    p( $arr )&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;]&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice that in the above example there is no &amp;quot;require&amp;quot; statement as opposed to the earlier example of writing serialized data into files using YAML. This is because unlike YAML, marshal is built-in Ruby and  no external libraries is required in order to use its functionality.&lt;br /&gt;
&lt;br /&gt;
Marshal can also be used to custom serialize an object i.e. it provides an option to omit the variables of an object that are not required in the serialized data. The following program shows the use of marshal_dump method for achieving custom serialization.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
      class First&lt;br /&gt;
      def initialize(name, age, country)&lt;br /&gt;
         @name = name&lt;br /&gt;
         @age = age&lt;br /&gt;
         @country=country&lt;br /&gt;
      end&lt;br /&gt;
      def to_s&lt;br /&gt;
            &amp;quot;In First: #{@name}, #{@age}, #{@country}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
      def marshal_dump&lt;br /&gt;
            [@name,@age]  #@country will not be serialized&lt;br /&gt;
      end&lt;br /&gt;
      def marshal_load(data)&lt;br /&gt;
       @name=data[0]&lt;br /&gt;
       @age=data[1]&lt;br /&gt;
       @country=&amp;quot;United States of America&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
      end&lt;br /&gt;
 &lt;br /&gt;
      x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
      puts x&lt;br /&gt;
  &lt;br /&gt;
      marshal_data = Marshal.dump( x ) &lt;br /&gt;
      y = Marshal.load( marshal_data ) &lt;br /&gt;
      p( y.to_s )&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    In First: Tom, 25, USA&lt;br /&gt;
    &amp;quot;In First: Tom, 25, United States of America&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== '''Serialization Performance in Ruby''' ==&lt;br /&gt;
The different serialization formats described above differ in the efficiency at which they can serialize and deserialize data and thus while serializing large amount of data their efficiency is taken into account. The .report[http://www.ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html] method in Ruby can be used to evaluate the performance of these serialization patterns. Marshal, as it handles data in binary format, is the most efficient form of serialization in Ruby. The following example compares the performance of marshal, JSON and YAML format.[http://www.pauldix.net/2008/08/serializing-dat.html]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   require 'benchmark'&lt;br /&gt;
   require 'rubygems'&lt;br /&gt;
   require 'json'&lt;br /&gt;
   require 'yaml'&lt;br /&gt;
   include Benchmark&lt;br /&gt;
   class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
      @name=name&lt;br /&gt;
      @age=age&lt;br /&gt;
      @country=country&lt;br /&gt;
     end&lt;br /&gt;
   end  &lt;br /&gt;
    x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
   benchmark do |t|&lt;br /&gt;
    print &amp;quot;Marshal:&amp;quot;&lt;br /&gt;
    t.report{1000.times do; Marshal.load(Marshal.dump(x));end}&lt;br /&gt;
    print &amp;quot;JSON:&amp;quot;&lt;br /&gt;
    t.report{1000.times do; JSON.load(JSON.dump(x));end}&lt;br /&gt;
    print &amp;quot;YAML:&amp;quot;&lt;br /&gt;
    t.report{1000.times do; YAML.load(YAML.dump(x));end}&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   Marshal:   0.020000   0.000000   0.020000 (  0.015473)&lt;br /&gt;
   JSON:   0.020000   0.000000   0.020000 (  0.023934)&lt;br /&gt;
   YAML:   0.460000   0.010000   0.470000 (  0.476826)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output of the above example shows that, given large chunks of data, marshal is comparatively more efficient format for serialization in Ruby.&lt;br /&gt;
&lt;br /&gt;
== Serialization in OOLS Languages: Comparison ==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Sl.No !! Ruby !! Java !! .Net Framework !! C++&lt;br /&gt;
|-&lt;br /&gt;
| 1 || Ruby provides a built in 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 || Although, there is no built in support for serialization in C++, it can be achieved by using Boost libraries[http://www.boost.org/doc/libs/1_36_0/libs/serialization/example/demo.cpp]&lt;br /&gt;
|-&lt;br /&gt;
| 2 || The built in module of Ruby [http://www.ruby-doc.org/core-2.0.0/Marshal.html (Marshal)] does not support platform independence, however, it can be achieved by using external libraries like [http://yaml.org YAML] and [http://www.w3schools.com/json/ JSON] || Similarly, Java's built in serialization is also not platform independent and in order to use serialization in Java across Ruby platform, jruby library should be used.  || .Net used Remoting technology to make it platform independent. || Serialization using the Boost libraries is not platform independent.&lt;br /&gt;
|-&lt;br /&gt;
| 3 || YAML provides a method [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/syck/rdoc/Object.html (to_yaml_properties)] which can be used to select the variables who's value is need to be serialized. With Marshal, we need to write a method named marshal_dump defining the variables of an object that has to be serialized. || Provides an option for serializing only the required 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 data 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 || Serialization using the Boost libraries is custom and thus the user can specify the part of the objects to be serialized.&lt;br /&gt;
|-&lt;br /&gt;
| 4 || Bindings, procedure objects, singleton objects, instances of IO objects and interfaces can not be serialized. Serializing these objects throws TypeError exceptions. || Thread, OutputStream and its subclasses, and Socket are not serializable in Java.[http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html] || Objects like DataRow are non serializable in .NET. Whether an object is serializable or not can be checked using the [http://msdn.microsoft.com/en-us/library/system.type.isserializable(v=vs.110).aspx?cs-save-lang=1&amp;amp;cs-lang=cpp#code-snippet-1 Type.IsSerializable] || There are no such objects in C++.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== See Also==&lt;br /&gt;
&lt;br /&gt;
1. [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/json/rdoc/JSON.html Serialization in Ruby JSON]&lt;br /&gt;
&lt;br /&gt;
2. [http://api.rubyonrails.org/classes/ActiveModel/Serialization.html Serialization in Rails]&lt;br /&gt;
&lt;br /&gt;
3. [http://gregmoreno.wordpress.com/2011/01/27/preventing-model-explosion-via-rails-serialization/ Article on Rails Serialization]&lt;br /&gt;
&lt;br /&gt;
4. [http://en.wikipedia.org/wiki/Google_Protocol_Buffers Protocol Buffers]&lt;br /&gt;
&lt;br /&gt;
5. [https://rubygems.org/gems/ruby-protocol-buffers Ruby Protocol Buffers Gem]&lt;br /&gt;
&lt;br /&gt;
6. [http://en.wikipedia.org/wiki/Apache_Avro Avro]&lt;br /&gt;
&lt;br /&gt;
7. [http://rubygems.org/gems/avro Ruby Avro Gem]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
1. [http://en.wikipedia.org/wiki/Serialization Serilization in General]&lt;br /&gt;
&lt;br /&gt;
2. [http://en.wikipedia.org/wiki/Marshalling_%28computer_science%29 Marshaling]&lt;br /&gt;
&lt;br /&gt;
3. [http://techbrahmana.blogspot.com/2012/03/rails-cookie-handling-serialization-and.html Rails Cookie Handling]&lt;br /&gt;
&lt;br /&gt;
4. [http://yaml.org/ YAML]&lt;br /&gt;
&lt;br /&gt;
5. [http://www.ruby-doc.org/core-2.1.0/Marshal.html Marshal module]&lt;br /&gt;
&lt;br /&gt;
6. [http://json.org/ JSON]&lt;br /&gt;
&lt;br /&gt;
7. [http://rubygems.org/ Ruby Gems]&lt;br /&gt;
&lt;br /&gt;
8. [http://ruby-doc.org/core-2.0.0/ARGF.html# Ruby ARGF Documentation]&lt;br /&gt;
&lt;br /&gt;
9. [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html Ruby Documentation for Benchmark Gem]&lt;br /&gt;
&lt;br /&gt;
10. [http://www.boost.org/doc/libs/1_36_0/libs/serialization/example/demo.cpp Boost Libraries]&lt;br /&gt;
&lt;br /&gt;
11. [http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html Java Serialization]&lt;br /&gt;
&lt;br /&gt;
12. [http://www.w3schools.com/json/ JSON Tutorial]&lt;br /&gt;
&lt;br /&gt;
13. [http://www.skorks.com/2010/04/serializing-and-deserializing-objects-with-ruby/ Serializing and De-serializing in Ruby]&lt;br /&gt;
&lt;br /&gt;
14. [http://www.codeproject.com/Articles/33296/Serialization-and-De-serialization Serialization and De-serialization]&lt;br /&gt;
&lt;br /&gt;
15. [http://msdn.microsoft.com/en-us/library/182eeyhh.aspx XML Serialization]&lt;br /&gt;
&lt;br /&gt;
16. [http://www.tutorialspoint.com/java/java_serialization.htm Java Serialization Tutorial]&lt;br /&gt;
&lt;br /&gt;
17. [http://www.sapphiresteel.com/ruby-programming/The-Book-Of-Ruby The Book of Ruby]&lt;br /&gt;
&lt;br /&gt;
18. [http://www.codeproject.com/Articles/20962/Introducing-Serialization-in-NET Serialization in .Net]&lt;br /&gt;
&lt;br /&gt;
19. [http://ruby.about.com/od/advancedruby/ss/Serialization-In-Ruby-Yaml.html Serialization in Ruby YAML]&lt;br /&gt;
&lt;br /&gt;
20. [http://ruby.about.com/od/tasks/a/The-Json-Gem.html Installing JSON gem]&lt;br /&gt;
&lt;br /&gt;
21. [http://www.ruby-doc.org/stdlib-2.0.0/libdoc/json/rdoc/JSON.html Serialization in Ruby JSON]&lt;/div&gt;</summary>
		<author><name>Mmehta2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1d_mm&amp;diff=83281</id>
		<title>CSC/ECE 517 Spring 2014/ch1a 1d mm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1d_mm&amp;diff=83281"/>
		<updated>2014-02-11T03:38:44Z</updated>

		<summary type="html">&lt;p&gt;Mmehta2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== '''Serialization''' ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Serialization'''[http://en.wikipedia.org/wiki/Serialization] is a process of converting a data structure or an object into a stream of bytes or string to facilitate storage in memory, file(persistence storage) or transmission over a network. The process of Serialization is also referred to as ''Marshalling''[http://en.wikipedia.org/wiki/Marshalling_%28computer_science%29]. 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. &lt;br /&gt;
&lt;br /&gt;
'''Basic Advantages of Serialization''':&lt;br /&gt;
&lt;br /&gt;
1. Communication between two or more processes on same machine. Object state can be saved and shared in a persistent or in-memory store.&lt;br /&gt;
&lt;br /&gt;
2. Communication between processes on different machines. Serialization facilitates the transmission of an object through a network.&lt;br /&gt;
&lt;br /&gt;
3. Creating a clone of an object. &lt;br /&gt;
&lt;br /&gt;
4. Cross-platform compatibility. Object can be serialized in a common format that is understood by multiple platforms. Eg. JSON, XML.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''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''.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Few Practical Applications for Serialization'''&lt;br /&gt;
&lt;br /&gt;
1. HTTP Session Replication by sharing session objects across web servers for handling failover scenarios&lt;br /&gt;
&lt;br /&gt;
2. Serialization facilitates communication in Remote Method Invocation or Remote procedure calls &lt;br /&gt;
&lt;br /&gt;
3. Rails Cookie Handling[http://techbrahmana.blogspot.com/2012/03/rails-cookie-handling-serialization-and.html]. Cookies are stored marshalled/unmarshalled to and from client machines. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:SerializationChart.jpg]]&lt;br /&gt;
&lt;br /&gt;
== '''Serialization in Ruby:''' ==&lt;br /&gt;
&lt;br /&gt;
Ruby provides serialization capabilities through its module, [http://www.ruby-doc.org/core-2.0.0/Marshal.html Marshal]. There are also other libraries like YAML and JSON which can be used in Ruby to generate serialized objects for purposes like platform independence and human readable formats.&lt;br /&gt;
&lt;br /&gt;
== '''Types of Serialization''' ==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
=== Converting Ruby Objects in Human Readable Formats ===&lt;br /&gt;
The conversion of Ruby objects into YAML and JSON formats are explained below.&lt;br /&gt;
&lt;br /&gt;
==== Converting Ruby Objects to YAML format ====&lt;br /&gt;
&lt;br /&gt;
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. 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. In order to use it in Ruby, the yaml.rb file is required to be loaded which provides methods for converting objects into yaml format and creating .yml files.&lt;br /&gt;
&lt;br /&gt;
'''Examples of serialization using YAML:'''&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;nowiki&amp;gt; #Serialization using YAML's to_yaml method&lt;br /&gt;
 &lt;br /&gt;
  require &amp;quot;yaml&amp;quot;&lt;br /&gt;
     class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
	@name = name&lt;br /&gt;
	@age = age&lt;br /&gt;
	@country=country&lt;br /&gt;
     end&lt;br /&gt;
      def to_s&lt;br /&gt;
	&amp;quot;In First:\n#{@name}, #{@age}, #{@country}\n&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
  x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
  puts x&lt;br /&gt;
  puts x.to_yaml&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   In First:&lt;br /&gt;
   Tom, 25, USA&lt;br /&gt;
   --- !ruby/object:First&lt;br /&gt;
   name: Tom&lt;br /&gt;
   age: 25&lt;br /&gt;
   country: USA&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The above code displays the object x, first as a string and then in the yaml format. &lt;br /&gt;
&lt;br /&gt;
'''Saving YAML data into a file:'''&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt; # Serialization using YAML::dump&lt;br /&gt;
&lt;br /&gt;
require 'yaml'&lt;br /&gt;
&lt;br /&gt;
f = File.open( 'first.yml', 'w' )&lt;br /&gt;
YAML.dump( [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;], f )&lt;br /&gt;
f.close&lt;br /&gt;
		&lt;br /&gt;
File.open( 'first.yml' ){ |f|	&lt;br /&gt;
    $arr= YAML.load(f)&lt;br /&gt;
}	&lt;br /&gt;
&lt;br /&gt;
p( $arr )&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;]&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The dump function can be used to serialize the data and save it into a file in YAML format. As shown in the above example the data in YAML format can be de-serialized using the load function.&lt;br /&gt;
&lt;br /&gt;
YAML libraries also provides an option of selecting only those variables of the object that are needed to be serialized. This is done using the to_yaml_properties method as shown in the below example.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;nowiki&amp;gt;  #Custom Serialization using YAML&lt;br /&gt;
 &lt;br /&gt;
  require &amp;quot;yaml&amp;quot;&lt;br /&gt;
     class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
	@name = name&lt;br /&gt;
	@age = age&lt;br /&gt;
	@country=country&lt;br /&gt;
     end&lt;br /&gt;
     def to_s&lt;br /&gt;
	&amp;quot;In First:\n#{@name}, #{@age}, #{@country}\n&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
      def to_yaml_properties&lt;br /&gt;
	   [&amp;quot;@name&amp;quot;,&amp;quot;@age&amp;quot;]  #@country will not be serialized&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
  x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
  puts x&lt;br /&gt;
  puts x.to_yaml&lt;br /&gt;
  &amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   In First:&lt;br /&gt;
   Tom, 25, USA&lt;br /&gt;
   --- !ruby/object:First&lt;br /&gt;
   name: Tom&lt;br /&gt;
   age: 25&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Converting Ruby Objects to JSON format: ====&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
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. Ruby 1.8.7 distribution is not bundled with json gem. However, in Ruby 1.9.2, the json gem is bundled with the core Ruby distribution.&lt;br /&gt;
The JSON library can be installed using Ruby Gems[http://rubygems.org/] as shown below:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;# gem install json&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A JSON string for serialization can be created by using the JSON.generate method:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
        require 'json'&lt;br /&gt;
        my_hash = {:Welcome =&amp;gt; &amp;quot;Ruby&amp;quot;}&lt;br /&gt;
        puts JSON.generate(my_hash) =&amp;gt; &amp;quot;{\&amp;quot;WELCOME\&amp;quot;:\&amp;quot;RUBY\&amp;quot;}&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
'''Output:'''&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;{&amp;quot;{\&amp;quot;Welcome\&amp;quot;:\&amp;quot;Ruby\&amp;quot;}&amp;quot;=&amp;gt;&amp;quot;{\&amp;quot;WELCOME\&amp;quot;:\&amp;quot;RUBY\&amp;quot;}&amp;quot;}&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A JSON string received from another program can be parsed by using JSON.parse&lt;br /&gt;
Ruby thus converts String to Hash.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
        require 'json'&lt;br /&gt;
        my_hash = JSON.parse('{&amp;quot;Welcome&amp;quot;: &amp;quot;Ruby&amp;quot;}')&lt;br /&gt;
        puts my_hash[&amp;quot;Welcome&amp;quot;] =&amp;gt; &amp;quot;Ruby&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Converting Ruby Objects to Binary Formats ===&lt;br /&gt;
Binary Serialization is another form of serialization in Ruby which is not in human readable form. 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. Binary Serialization  is done using [http://www.ruby-doc.org/core-2.0.0/Marshal.html Marshal] which is built into Ruby and the code for it is written in Ruby's Marshal module(marshal.c) and thus no additional files are required in order to use it. The _dump and _load methods defined in marshal are used for serialization. Using marshal module the following types of objects can not be serialized: bindings, procedure objects, singleton objects, instances of IO objects and interfaces.&lt;br /&gt;
&lt;br /&gt;
Since the Binary Serialized data is not in human readable form, the following guidelines need to be followed.&lt;br /&gt;
     1.Use print instead of puts[http://ruby-doc.org/core-2.0.0/ARGF.html#] when serialized objects are written to a file in order to avoid new line characters to be written &lt;br /&gt;
       in the file.&lt;br /&gt;
     &lt;br /&gt;
     2.Use a record separator in order to differentiate between two objects.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Binary Serialization Example:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    class Animal&lt;br /&gt;
      def initialize  name, age&lt;br /&gt;
      @name = name&lt;br /&gt;
      @age=age&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
   class Cat &amp;lt; Animal&lt;br /&gt;
    def to_s&lt;br /&gt;
     &amp;quot;In Cat C: #{@name} \t #{@age}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
  c = Cat.new(&amp;quot;Kitty Kat&amp;quot;,5)&lt;br /&gt;
  puts &amp;quot;Before Serialization&amp;quot;&lt;br /&gt;
  puts c&lt;br /&gt;
  #puts d&lt;br /&gt;
  serialize_cat= Marshal.dump(c) #dumps the serialized cat object into serialize_cat&lt;br /&gt;
  puts &amp;quot;\nAfter Serialization:\n #{serialize_cat}&amp;quot;&lt;br /&gt;
  deserialize_cat= Marshal::load(serialize_cat) #deserializes the cat object and loads it back into deserialize_cat&lt;br /&gt;
  puts &amp;quot;\nAfter Deserialization\n #{deserialize_cat}&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   Before Serialization&lt;br /&gt;
   In Cat C: Kitty Kat 	 5&lt;br /&gt;
   After Serialization:&lt;br /&gt;
   oCat:&lt;br /&gt;
   @nameI&amp;quot;Kitty Kat:ET:	@agei&lt;br /&gt;
   After Deserialization&lt;br /&gt;
   In Cat C: Kitty Kat 	 5&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Similar to YAML, Marshal can also be used to dump data into a file. The above example showing the serialization using YAML::dump can be written using marshal as shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    f = File.open( 'first.yml', 'w' )&lt;br /&gt;
    Marshal.dump( [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;], f )&lt;br /&gt;
    f.close	&lt;br /&gt;
    File.open( 'first.yml' ){ |f|	&lt;br /&gt;
    $arr= Marshal.load(f)&lt;br /&gt;
    }	&lt;br /&gt;
    p( $arr )&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;]&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice that in the above example there is no &amp;quot;require&amp;quot; statement as opposed to the earlier example of writing serialized data into files using YAML. This is because unlike YAML, marshal is built-in Ruby and  no external libraries is required in order to use its functionality.&lt;br /&gt;
&lt;br /&gt;
Marshal can also be used to custom serialize an object i.e. it provides an option to omit the variables of an object that are not required in the serialized data. The following program shows the use of marshal_dump method for achieving custom serialization.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
      class First&lt;br /&gt;
      def initialize(name, age, country)&lt;br /&gt;
         @name = name&lt;br /&gt;
         @age = age&lt;br /&gt;
         @country=country&lt;br /&gt;
      end&lt;br /&gt;
      def to_s&lt;br /&gt;
            &amp;quot;In First: #{@name}, #{@age}, #{@country}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
      def marshal_dump&lt;br /&gt;
            [@name,@age]  #@country will not be serialized&lt;br /&gt;
      end&lt;br /&gt;
      def marshal_load(data)&lt;br /&gt;
       @name=data[0]&lt;br /&gt;
       @age=data[1]&lt;br /&gt;
       @country=&amp;quot;United States of America&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
      end&lt;br /&gt;
 &lt;br /&gt;
      x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
      puts x&lt;br /&gt;
  &lt;br /&gt;
      marshal_data = Marshal.dump( x ) &lt;br /&gt;
      y = Marshal.load( marshal_data ) &lt;br /&gt;
      p( y.to_s )&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    In First: Tom, 25, USA&lt;br /&gt;
    &amp;quot;In First: Tom, 25, United States of America&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== '''Serialization Performance in Ruby''' ==&lt;br /&gt;
The different serialization formats described above differ in the efficiency at which they can serialize and deserialize data and thus while serializing large amount of data their efficiency is taken into account. The .report[http://www.ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html] method in Ruby can be used to evaluate the performance of these serialization patterns. Marshal, as it handles data in binary format, is the most efficient form of serialization in Ruby. The following example compares the performance of marshal, JSON and YAML format.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   require 'benchmark'&lt;br /&gt;
   require 'rubygems'&lt;br /&gt;
   require 'json'&lt;br /&gt;
   require 'yaml'&lt;br /&gt;
   include Benchmark&lt;br /&gt;
   class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
      @name=name&lt;br /&gt;
      @age=age&lt;br /&gt;
      @country=country&lt;br /&gt;
     end&lt;br /&gt;
   end  &lt;br /&gt;
    x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
   benchmark do |t|&lt;br /&gt;
    print &amp;quot;Marshal:&amp;quot;&lt;br /&gt;
    t.report{1000.times do; Marshal.load(Marshal.dump(x));end}&lt;br /&gt;
    print &amp;quot;JSON:&amp;quot;&lt;br /&gt;
    t.report{1000.times do; JSON.load(JSON.dump(x));end}&lt;br /&gt;
    print &amp;quot;YAML:&amp;quot;&lt;br /&gt;
    t.report{1000.times do; YAML.load(YAML.dump(x));end}&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   Marshal:   0.020000   0.000000   0.020000 (  0.015473)&lt;br /&gt;
   JSON:   0.020000   0.000000   0.020000 (  0.023934)&lt;br /&gt;
   YAML:   0.460000   0.010000   0.470000 (  0.476826)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output of the above example shows that, given large chunks of data, marshal is comparatively more efficient format for serialization in Ruby.&lt;br /&gt;
&lt;br /&gt;
== Serialization in OOLS Languages: Comparison ==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Sl.No !! Ruby !! Java !! .Net Framework !! C++&lt;br /&gt;
|-&lt;br /&gt;
| 1 || Ruby provides a built in 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 || Although, there is no built in support for serialization in C++, it can be achieved by using Boost libraries[http://www.boost.org/doc/libs/1_36_0/libs/serialization/example/demo.cpp]&lt;br /&gt;
|-&lt;br /&gt;
| 2 || The built in module of Ruby [http://www.ruby-doc.org/core-2.0.0/Marshal.html (Marshal)] does not support platform independence, however, it can be achieved by using external libraries like [http://yaml.org YAML] and [http://www.w3schools.com/json/ JSON] || Similarly, Java's built in serialization is also not platform independent and in order to use serialization in Java across Ruby platform, jruby library should be used.  || .Net used Remoting technology to make it platform independent. || Serialization using the Boost libraries is not platform independent.&lt;br /&gt;
|-&lt;br /&gt;
| 3 || YAML provides a method [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/syck/rdoc/Object.html (to_yaml_properties)] which can be used to select the variables who's value is need to be serialized. With Marshal, we need to write a method named marshal_dump defining the variables of an object that has to be serialized. || Provides an option for serializing only the required 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 data 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 || Serialization using the Boost libraries is custom and thus the user can specify the part of the objects to be serialized.&lt;br /&gt;
|-&lt;br /&gt;
| 4 || Bindings, procedure objects, singleton objects, instances of IO objects and interfaces can not be serialized. Serializing these objects throws TypeError exceptions. || Thread, OutputStream and its subclasses, and Socket are not serializable in Java.[http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html] || Objects like DataRow are non serializable in .NET. Whether an object is serializable or not can be checked using the [http://msdn.microsoft.com/en-us/library/system.type.isserializable(v=vs.110).aspx?cs-save-lang=1&amp;amp;cs-lang=cpp#code-snippet-1 Type.IsSerializable] || There are no such objects in C++.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== See Also==&lt;br /&gt;
&lt;br /&gt;
1. [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/json/rdoc/JSON.html Serialization in Ruby JSON]&lt;br /&gt;
&lt;br /&gt;
2. [http://api.rubyonrails.org/classes/ActiveModel/Serialization.html Serialization in Rails]&lt;br /&gt;
&lt;br /&gt;
3. [http://gregmoreno.wordpress.com/2011/01/27/preventing-model-explosion-via-rails-serialization/ Article on Rails Serialization]&lt;br /&gt;
&lt;br /&gt;
4. [http://en.wikipedia.org/wiki/Google_Protocol_Buffers Protocol Buffers]&lt;br /&gt;
&lt;br /&gt;
5. [https://rubygems.org/gems/ruby-protocol-buffers Ruby Protocol Buffers Gem]&lt;br /&gt;
&lt;br /&gt;
6. [http://en.wikipedia.org/wiki/Apache_Avro Avro]&lt;br /&gt;
&lt;br /&gt;
7. [http://rubygems.org/gems/avro Ruby Avro Gem]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
1. [http://en.wikipedia.org/wiki/Serialization Serilization in General]&lt;br /&gt;
&lt;br /&gt;
2. [http://en.wikipedia.org/wiki/Marshalling_%28computer_science%29 Marshaling]&lt;br /&gt;
&lt;br /&gt;
3. [http://techbrahmana.blogspot.com/2012/03/rails-cookie-handling-serialization-and.html Rails Cookie Handling]&lt;br /&gt;
&lt;br /&gt;
4. [http://yaml.org/ YAML]&lt;br /&gt;
&lt;br /&gt;
5. [http://www.ruby-doc.org/core-2.1.0/Marshal.html Marshal module]&lt;br /&gt;
&lt;br /&gt;
6. [http://json.org/ JSON]&lt;br /&gt;
&lt;br /&gt;
7. [http://rubygems.org/ Ruby Gems]&lt;br /&gt;
&lt;br /&gt;
8. [http://ruby-doc.org/core-2.0.0/ARGF.html# Ruby ARGF Documentation]&lt;br /&gt;
&lt;br /&gt;
9. [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html Ruby Documentation for Benchmark Gem]&lt;br /&gt;
&lt;br /&gt;
10. [http://www.boost.org/doc/libs/1_36_0/libs/serialization/example/demo.cpp Boost Libraries]&lt;br /&gt;
&lt;br /&gt;
11. [http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html Java Serialization]&lt;br /&gt;
&lt;br /&gt;
12. [http://www.w3schools.com/json/ JSON Tutorial]&lt;br /&gt;
&lt;br /&gt;
13. [http://www.skorks.com/2010/04/serializing-and-deserializing-objects-with-ruby/ Serializing and De-serializing in Ruby]&lt;br /&gt;
&lt;br /&gt;
14. [http://www.codeproject.com/Articles/33296/Serialization-and-De-serialization Serialization and De-serialization]&lt;br /&gt;
&lt;br /&gt;
15. [http://msdn.microsoft.com/en-us/library/182eeyhh.aspx XML Serialization]&lt;br /&gt;
&lt;br /&gt;
16. [http://www.tutorialspoint.com/java/java_serialization.htm Java Serialization Tutorial]&lt;br /&gt;
&lt;br /&gt;
17. [http://www.sapphiresteel.com/ruby-programming/The-Book-Of-Ruby The Book of Ruby]&lt;br /&gt;
&lt;br /&gt;
18. [http://www.codeproject.com/Articles/20962/Introducing-Serialization-in-NET Serialization in .Net]&lt;br /&gt;
&lt;br /&gt;
19. [http://ruby.about.com/od/advancedruby/ss/Serialization-In-Ruby-Yaml.html Serialization in Ruby YAML]&lt;br /&gt;
&lt;br /&gt;
20. [http://ruby.about.com/od/tasks/a/The-Json-Gem.html Installing JSON gem]&lt;br /&gt;
&lt;br /&gt;
21. [http://www.ruby-doc.org/stdlib-2.0.0/libdoc/json/rdoc/JSON.html Serialization in Ruby JSON]&lt;/div&gt;</summary>
		<author><name>Mmehta2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1d_mm&amp;diff=83280</id>
		<title>CSC/ECE 517 Spring 2014/ch1a 1d mm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1d_mm&amp;diff=83280"/>
		<updated>2014-02-11T03:38:21Z</updated>

		<summary type="html">&lt;p&gt;Mmehta2: Created page with &amp;quot;== '''Serialization''' ==   '''Serialization'''[http://en.wikipedia.org/wiki/Serialization] is the process of converting an object or a group of objects into a stream of bytes or...&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== '''Serialization''' ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Serialization'''[http://en.wikipedia.org/wiki/Serialization] 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. &lt;br /&gt;
&lt;br /&gt;
'''Basic Advantages of Serialization''':&lt;br /&gt;
&lt;br /&gt;
1.Facilitates the easy transportation of an object through a network.&lt;br /&gt;
&lt;br /&gt;
2.Creates a clone of the object at the receiving end.&lt;br /&gt;
&lt;br /&gt;
3.Ability to transmit data across the network in a cross-platform-compatible format.&lt;br /&gt;
&lt;br /&gt;
4.Saving the data in a persistent or non-persistent storage medium in a non-proprietary format.&lt;br /&gt;
&lt;br /&gt;
'''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''.&lt;br /&gt;
&lt;br /&gt;
[[File:SerializationChart.jpg]]&lt;br /&gt;
&lt;br /&gt;
== '''Serialization in Ruby:''' ==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
== '''Types of Serialization''' ==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
=== Converting Ruby Objects in Human Readable Formats ===&lt;br /&gt;
The conversion of Ruby objects into YAML and JSON formats are explained below.&lt;br /&gt;
&lt;br /&gt;
==== Converting Ruby Objects to YAML format ====&lt;br /&gt;
&lt;br /&gt;
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,&amp;lt;&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;nowiki&amp;gt;require &amp;quot;yaml&amp;quot;&lt;br /&gt;
     class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
	@name = name&lt;br /&gt;
	@age = age&lt;br /&gt;
	@country=country&lt;br /&gt;
     end&lt;br /&gt;
&lt;br /&gt;
     def to_s&lt;br /&gt;
	&amp;quot;In First:\n#{@name}, #{@age}, #{@country}\n&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   class Second&lt;br /&gt;
     def initialize(address, details)&lt;br /&gt;
	@address = address&lt;br /&gt;
	@details = details&lt;br /&gt;
     end&lt;br /&gt;
 &lt;br /&gt;
     def to_s&lt;br /&gt;
	&amp;quot;In Second:\n#{@details.to_s}#{@address}\n&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
  x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
  y = Second.new(&amp;quot;St. Marks Street&amp;quot;, x)&lt;br /&gt;
  puts y&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
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]).&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
In Second:&amp;lt;br&amp;gt;&lt;br /&gt;
In First:&amp;lt;br&amp;gt;&lt;br /&gt;
Tom, 25, USA&amp;lt;br&amp;gt;&lt;br /&gt;
St. Marks Street&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
We use the below code to serialize out object tree.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;serialized_object = YAML::dump(y)&lt;br /&gt;
puts serialized_object&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The dump function serializes the object tree and stores the data in the YAML format in the variable serialized_object.&lt;br /&gt;
 &lt;br /&gt;
Data in the serialized (YAML) format looks like this:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt; &lt;br /&gt;
 --- !ruby/object:Second&lt;br /&gt;
 address: St. Marks Street&lt;br /&gt;
 details: !ruby/object:First&lt;br /&gt;
 name: Tom&lt;br /&gt;
 age: 25&lt;br /&gt;
 country: USA&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Now, to de-serialize the data, we use load function.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;puts YAML::load(serialized_object)&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The data is converted back to Ruby object tree.&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;In Second:&lt;br /&gt;
In First:&lt;br /&gt;
Tom, 25, USA&lt;br /&gt;
St. Marks Street&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
Thus we get back our original Object tree.&lt;br /&gt;
&lt;br /&gt;
==== Converting Ruby Objects to JSON format: ====&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
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]&lt;br /&gt;
The JSON library can be installed using Ruby Gems[http://rubygems.org/] like shown below:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;# gem install json&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
We can create a JSON string for serialization by using the JSON.generate method as below:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
        require 'json'&lt;br /&gt;
        my_hash = {:Welcome =&amp;gt; &amp;quot;Ruby&amp;quot;}&lt;br /&gt;
        puts JSON.generate(my_hash) =&amp;gt; &amp;quot;{\&amp;quot;WELCOME\&amp;quot;:\&amp;quot;RUBY\&amp;quot;}&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
'''Output:'''&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;{&amp;quot;{\&amp;quot;Welcome\&amp;quot;:\&amp;quot;Ruby\&amp;quot;}&amp;quot;=&amp;gt;&amp;quot;{\&amp;quot;WELCOME\&amp;quot;:\&amp;quot;RUBY\&amp;quot;}&amp;quot;}&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
We can parse the JSON string received from another program by using JSON.parse&lt;br /&gt;
Ruby thus converts String to Hash.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
        require 'json'&lt;br /&gt;
        my_hash = JSON.parse('{&amp;quot;Welcome&amp;quot;: &amp;quot;Ruby&amp;quot;}')&lt;br /&gt;
        puts my_hash[&amp;quot;Welcome&amp;quot;] =&amp;gt; &amp;quot;Ruby&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Converting Ruby Objects to Binary Formats ===&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
Since the Binary Serialized data is not in human readable form, there are two essential guidelines that need to be followed. They are :&lt;br /&gt;
     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 &lt;br /&gt;
       in the file.&lt;br /&gt;
     &lt;br /&gt;
     2.Use a record separator in order to differentiate between two objects.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;Binary Serialization Example &amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    class Animal&lt;br /&gt;
     def initialize  name, age&lt;br /&gt;
       @name = name&lt;br /&gt;
       @age=age&lt;br /&gt;
       puts &amp;quot;#{self.class.name}&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
    end&lt;br /&gt;
    class Cat &amp;lt; Animal&lt;br /&gt;
     def to_s&lt;br /&gt;
      &amp;quot;In Cat C: #{@name} \t #{@age}&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
    end&lt;br /&gt;
    class Dog &amp;lt; Animal&lt;br /&gt;
     def to_s&lt;br /&gt;
      puts &amp;quot;In Dog D: #{@name} \t #{@age}&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
    end&lt;br /&gt;
   d = Dog.new(&amp;quot;Doggy Dig&amp;quot;, 4)&lt;br /&gt;
   c = Cat.new(&amp;quot;Kitty Kat&amp;quot;,5)&lt;br /&gt;
   puts &amp;quot;Before Serialization&amp;quot;&lt;br /&gt;
   puts c&lt;br /&gt;
   puts d&lt;br /&gt;
   serialize_cat= Marshal.dump(c) #dumps the serialized cat object into serialize_cat&lt;br /&gt;
   serialize_dog= Marshal.dump(d) #dumps the serialized dog object into serialize_dog&lt;br /&gt;
   deserialize_cat= Marshal::load(serialize_cat) #deserializes the cat object and loads it back into deserialize_cat&lt;br /&gt;
   deserialize_dog= Marshal::load(serialize_dog) #deserializes the dog object and loads it back into deserialize_dog&lt;br /&gt;
   puts &amp;quot;After Serialization #{deserialize_cat}&amp;quot;&lt;br /&gt;
   puts &amp;quot;After Dog Serialization #{deserialize_dog}&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   Before Serialization&lt;br /&gt;
   In Cat C: Kitty Kat 	 5&lt;br /&gt;
   In Dog D: Doggy Dig 	 4&lt;br /&gt;
   After Serialization In Cat C: Kitty Kat 	 5&lt;br /&gt;
   After Dog Serialization In Dog D: Doggy Dig 	 4&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Serialization in OOLS Languages: Comparison ==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Sl.No !! Ruby !! Java !! .Net Framework&lt;br /&gt;
|-&lt;br /&gt;
| 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 &lt;br /&gt;
|-&lt;br /&gt;
| 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.&lt;br /&gt;
|-&lt;br /&gt;
| 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&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== See Also==&lt;br /&gt;
&lt;br /&gt;
1. [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/json/rdoc/JSON.html Serialization in Ruby JSON]&lt;br /&gt;
&lt;br /&gt;
2. [http://api.rubyonrails.org/classes/ActiveModel/Serialization.html Serialization in Rails]&lt;br /&gt;
&lt;br /&gt;
3. [http://gregmoreno.wordpress.com/2011/01/27/preventing-model-explosion-via-rails-serialization/ Article on Rails Serialization]&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
1. [http://en.wikipedia.org/wiki/Serialization Serilization in General]&lt;br /&gt;
&lt;br /&gt;
2. [http://yaml.org YAML]&lt;br /&gt;
&lt;br /&gt;
3. [http://www.w3schools.com/json/ JSON]&lt;br /&gt;
&lt;br /&gt;
4. [http://www.skorks.com/2010/04/serializing-and-deserializing-objects-with-ruby/ Serializing and De-serializing in Ruby]&lt;br /&gt;
&lt;br /&gt;
5. [http://www.codeproject.com/Articles/33296/Serialization-and-De-serialization Serialization and De-serialization]&lt;br /&gt;
&lt;br /&gt;
6. [http://www.ruby-doc.org/core-2.0.0/Marshal.html Marshal]&lt;br /&gt;
&lt;br /&gt;
7. [http://www.waset.org/journals/waset/v60/v60-39.pdf Object Serialization Techniques]&lt;br /&gt;
&lt;br /&gt;
8. [http://msdn.microsoft.com/en-us/library/182eeyhh.aspx XML Serialization]&lt;br /&gt;
&lt;br /&gt;
9. [http://www.tutorialspoint.com/java/java_serialization.htm Java Serialization]&lt;br /&gt;
&lt;br /&gt;
10.[http://www.codeproject.com/Articles/20962/Introducing-Serialization-in-NET Serialization in .Net]&lt;br /&gt;
&lt;br /&gt;
11.[http://json.org JSON]&lt;br /&gt;
&lt;br /&gt;
12.[http://ruby.about.com/od/advancedruby/ss/Serialization-In-Ruby-Yaml.html Serialization in Ruby YAML]&lt;br /&gt;
&lt;br /&gt;
13.[http://ruby.about.com/od/tasks/a/The-Json-Gem.html Installing JSON gem]&lt;/div&gt;</summary>
		<author><name>Mmehta2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014&amp;diff=83275</id>
		<title>CSC/ECE 517 Spring 2014</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014&amp;diff=83275"/>
		<updated>2014-02-11T03:33:38Z</updated>

		<summary type="html">&lt;p&gt;Mmehta2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;*[[CSC/ECE_517_Fall_2012/example_page]]&lt;br /&gt;
*[[CSC/ECE 517 Spring 2014/ch1 1w1e rm]]&lt;br /&gt;
*[[CSC/ECE 517 Spring 2014/ch1 1w1h jg ]]&lt;br /&gt;
*[[CSC/ECE 517 Spring 2014/ch1 1w1b np]]&lt;br /&gt;
*[[CSC/ECE 517 Spring 2014/ch1 1w1f mj]]&lt;br /&gt;
*[[CSC/ECE 517 Spring 2014/ch1a 1d mm]]&lt;br /&gt;
*[[CSC/ECE 517 Spring 2014/ch1a 1c yj]]&lt;/div&gt;</summary>
		<author><name>Mmehta2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1_1w1d_mm&amp;diff=83272</id>
		<title>CSC/ECE 517 Spring 2014/ch1 1w1d mm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1_1w1d_mm&amp;diff=83272"/>
		<updated>2014-02-11T03:30:28Z</updated>

		<summary type="html">&lt;p&gt;Mmehta2: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== '''Serialization''' ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Serialization'''[http://en.wikipedia.org/wiki/Serialization] is a process of converting a data structure or an object into a stream of bytes or string to facilitate storage in memory, file(persistence storage) or transmission over a network. The process of Serialization is also referred to as ''Marshalling''[http://en.wikipedia.org/wiki/Marshalling_%28computer_science%29]. 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. &lt;br /&gt;
&lt;br /&gt;
'''Basic Advantages of Serialization''':&lt;br /&gt;
&lt;br /&gt;
1. Communication between two or more processes on same machine. Object state can be saved and shared in a persistent or in-memory store.&lt;br /&gt;
&lt;br /&gt;
2. Communication between processes on different machines. Serialization facilitates the transmission of an object through a network.&lt;br /&gt;
&lt;br /&gt;
3. Creating a clone of an object. &lt;br /&gt;
&lt;br /&gt;
4. Cross-platform compatibility. Object can be serialized in a common format that is understood by multiple platforms. Eg. JSON, XML.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''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''.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Few Practical Applications for Serialization'''&lt;br /&gt;
&lt;br /&gt;
1. HTTP Session Replication by sharing session objects across web servers for handling failover scenarios&lt;br /&gt;
&lt;br /&gt;
2. Serialization facilitates communication in Remote Method Invocation or Remote procedure calls &lt;br /&gt;
&lt;br /&gt;
3. Rails Cookie Handling[http://techbrahmana.blogspot.com/2012/03/rails-cookie-handling-serialization-and.html]. Cookies are stored marshalled/unmarshalled to and from client machines. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:SerializationChart.jpg]]&lt;br /&gt;
&lt;br /&gt;
== '''Serialization in Ruby:''' ==&lt;br /&gt;
&lt;br /&gt;
Ruby provides serialization capabilities through its module, [http://www.ruby-doc.org/core-2.0.0/Marshal.html Marshal]. There are also other libraries like YAML and JSON which can be used in Ruby to generate serialized objects for purposes like platform independence and human readable formats.&lt;br /&gt;
&lt;br /&gt;
== '''Types of Serialization''' ==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
=== Converting Ruby Objects in Human Readable Formats ===&lt;br /&gt;
The conversion of Ruby objects into YAML and JSON formats are explained below.&lt;br /&gt;
&lt;br /&gt;
==== Converting Ruby Objects to YAML format ====&lt;br /&gt;
&lt;br /&gt;
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. 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. In order to use it in Ruby, the yaml.rb file is required to be loaded which provides methods for converting objects into yaml format and creating .yml files.&lt;br /&gt;
&lt;br /&gt;
'''Examples of serialization using YAML:'''&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;nowiki&amp;gt; #Serialization using YAML's to_yaml method&lt;br /&gt;
 &lt;br /&gt;
  require &amp;quot;yaml&amp;quot;&lt;br /&gt;
     class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
	@name = name&lt;br /&gt;
	@age = age&lt;br /&gt;
	@country=country&lt;br /&gt;
     end&lt;br /&gt;
      def to_s&lt;br /&gt;
	&amp;quot;In First:\n#{@name}, #{@age}, #{@country}\n&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
  x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
  puts x&lt;br /&gt;
  puts x.to_yaml&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   In First:&lt;br /&gt;
   Tom, 25, USA&lt;br /&gt;
   --- !ruby/object:First&lt;br /&gt;
   name: Tom&lt;br /&gt;
   age: 25&lt;br /&gt;
   country: USA&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The above code displays the object x, first as a string and then in the yaml format. &lt;br /&gt;
&lt;br /&gt;
'''Saving YAML data into a file:'''&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt; # Serialization using YAML::dump&lt;br /&gt;
&lt;br /&gt;
require 'yaml'&lt;br /&gt;
&lt;br /&gt;
f = File.open( 'first.yml', 'w' )&lt;br /&gt;
YAML.dump( [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;], f )&lt;br /&gt;
f.close&lt;br /&gt;
		&lt;br /&gt;
File.open( 'first.yml' ){ |f|	&lt;br /&gt;
    $arr= YAML.load(f)&lt;br /&gt;
}	&lt;br /&gt;
&lt;br /&gt;
p( $arr )&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;]&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The dump function can be used to serialize the data and save it into a file in YAML format. As shown in the above example the data in YAML format can be de-serialized using the load function.&lt;br /&gt;
&lt;br /&gt;
YAML libraries also provides an option of selecting only those variables of the object that are needed to be serialized. This is done using the to_yaml_properties method as shown in the below example.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;nowiki&amp;gt;  #Custom Serialization using YAML&lt;br /&gt;
 &lt;br /&gt;
  require &amp;quot;yaml&amp;quot;&lt;br /&gt;
     class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
	@name = name&lt;br /&gt;
	@age = age&lt;br /&gt;
	@country=country&lt;br /&gt;
     end&lt;br /&gt;
     def to_s&lt;br /&gt;
	&amp;quot;In First:\n#{@name}, #{@age}, #{@country}\n&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
      def to_yaml_properties&lt;br /&gt;
	   [&amp;quot;@name&amp;quot;,&amp;quot;@age&amp;quot;]  #@country will not be serialized&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
  x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
  puts x&lt;br /&gt;
  puts x.to_yaml&lt;br /&gt;
  &amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   In First:&lt;br /&gt;
   Tom, 25, USA&lt;br /&gt;
   --- !ruby/object:First&lt;br /&gt;
   name: Tom&lt;br /&gt;
   age: 25&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Converting Ruby Objects to JSON format: ====&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
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. Ruby 1.8.7 distribution is not bundled with json gem. However, in Ruby 1.9.2, the json gem is bundled with the core Ruby distribution.&lt;br /&gt;
The JSON library can be installed using Ruby Gems[http://rubygems.org/] as shown below:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;# gem install json&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A JSON string for serialization can be created by using the JSON.generate method:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
        require 'json'&lt;br /&gt;
        my_hash = {:Welcome =&amp;gt; &amp;quot;Ruby&amp;quot;}&lt;br /&gt;
        puts JSON.generate(my_hash) =&amp;gt; &amp;quot;{\&amp;quot;WELCOME\&amp;quot;:\&amp;quot;RUBY\&amp;quot;}&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
'''Output:'''&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;{&amp;quot;{\&amp;quot;Welcome\&amp;quot;:\&amp;quot;Ruby\&amp;quot;}&amp;quot;=&amp;gt;&amp;quot;{\&amp;quot;WELCOME\&amp;quot;:\&amp;quot;RUBY\&amp;quot;}&amp;quot;}&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A JSON string received from another program can be parsed by using JSON.parse&lt;br /&gt;
Ruby thus converts String to Hash.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
        require 'json'&lt;br /&gt;
        my_hash = JSON.parse('{&amp;quot;Welcome&amp;quot;: &amp;quot;Ruby&amp;quot;}')&lt;br /&gt;
        puts my_hash[&amp;quot;Welcome&amp;quot;] =&amp;gt; &amp;quot;Ruby&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Converting Ruby Objects to Binary Formats ===&lt;br /&gt;
Binary Serialization is another form of serialization in Ruby which is not in human readable form. 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. Binary Serialization  is done using [http://www.ruby-doc.org/core-2.0.0/Marshal.html Marshal] which is built into Ruby and the code for it is written in Ruby's Marshal module(marshal.c) and thus no additional files are required in order to use it. The _dump and _load methods defined in marshal are used for serialization. Using marshal module the following types of objects can not be serialized: bindings, procedure objects, singleton objects, instances of IO objects and interfaces.&lt;br /&gt;
&lt;br /&gt;
Since the Binary Serialized data is not in human readable form, the following guidelines need to be followed.&lt;br /&gt;
     1.Use print instead of puts[http://ruby-doc.org/core-2.0.0/ARGF.html#] when serialized objects are written to a file in order to avoid new line characters to be written &lt;br /&gt;
       in the file.&lt;br /&gt;
     &lt;br /&gt;
     2.Use a record separator in order to differentiate between two objects.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Binary Serialization Example:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    class Animal&lt;br /&gt;
      def initialize  name, age&lt;br /&gt;
      @name = name&lt;br /&gt;
      @age=age&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
   class Cat &amp;lt; Animal&lt;br /&gt;
    def to_s&lt;br /&gt;
     &amp;quot;In Cat C: #{@name} \t #{@age}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
  c = Cat.new(&amp;quot;Kitty Kat&amp;quot;,5)&lt;br /&gt;
  puts &amp;quot;Before Serialization&amp;quot;&lt;br /&gt;
  puts c&lt;br /&gt;
  #puts d&lt;br /&gt;
  serialize_cat= Marshal.dump(c) #dumps the serialized cat object into serialize_cat&lt;br /&gt;
  puts &amp;quot;\nAfter Serialization:\n #{serialize_cat}&amp;quot;&lt;br /&gt;
  deserialize_cat= Marshal::load(serialize_cat) #deserializes the cat object and loads it back into deserialize_cat&lt;br /&gt;
  puts &amp;quot;\nAfter Deserialization\n #{deserialize_cat}&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   Before Serialization&lt;br /&gt;
   In Cat C: Kitty Kat 	 5&lt;br /&gt;
   After Serialization:&lt;br /&gt;
   oCat:&lt;br /&gt;
   @nameI&amp;quot;Kitty Kat:ET:	@agei&lt;br /&gt;
   After Deserialization&lt;br /&gt;
   In Cat C: Kitty Kat 	 5&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Similar to YAML, Marshal can also be used to dump data into a file. The above example showing the serialization using YAML::dump can be written using marshal as shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    f = File.open( 'first.yml', 'w' )&lt;br /&gt;
    Marshal.dump( [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;], f )&lt;br /&gt;
    f.close	&lt;br /&gt;
    File.open( 'first.yml' ){ |f|	&lt;br /&gt;
    $arr= Marshal.load(f)&lt;br /&gt;
    }	&lt;br /&gt;
    p( $arr )&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;]&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice that in the above example there is no &amp;quot;require&amp;quot; statement as opposed to the earlier example of writing serialized data into files using YAML. This is because unlike YAML, marshal is built-in Ruby and  no external libraries is required in order to use its functionality.&lt;br /&gt;
&lt;br /&gt;
Marshal can also be used to custom serialize an object i.e. it provides an option to omit the variables of an object that are not required in the serialized data. The following program shows the use of marshal_dump method for achieving custom serialization.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
      class First&lt;br /&gt;
      def initialize(name, age, country)&lt;br /&gt;
         @name = name&lt;br /&gt;
         @age = age&lt;br /&gt;
         @country=country&lt;br /&gt;
      end&lt;br /&gt;
      def to_s&lt;br /&gt;
            &amp;quot;In First: #{@name}, #{@age}, #{@country}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
      def marshal_dump&lt;br /&gt;
            [@name,@age]  #@country will not be serialized&lt;br /&gt;
      end&lt;br /&gt;
      def marshal_load(data)&lt;br /&gt;
       @name=data[0]&lt;br /&gt;
       @age=data[1]&lt;br /&gt;
       @country=&amp;quot;United States of America&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
      end&lt;br /&gt;
 &lt;br /&gt;
      x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
      puts x&lt;br /&gt;
  &lt;br /&gt;
      marshal_data = Marshal.dump( x ) &lt;br /&gt;
      y = Marshal.load( marshal_data ) &lt;br /&gt;
      p( y.to_s )&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    In First: Tom, 25, USA&lt;br /&gt;
    &amp;quot;In First: Tom, 25, United States of America&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== '''Serialization Performance in Ruby''' ==&lt;br /&gt;
The different serialization formats described above differ in the efficiency at which they can serialize and deserialize data and thus while serializing large amount of data their efficiency is taken into account. The .report[http://www.ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html] method in Ruby can be used to evaluate the performance of these serialization patterns. Marshal, as it handles data in binary format, is the most efficient form of serialization in Ruby. The following example compares the performance of marshal, JSON and YAML format.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   require 'benchmark'&lt;br /&gt;
   require 'rubygems'&lt;br /&gt;
   require 'json'&lt;br /&gt;
   require 'yaml'&lt;br /&gt;
   include Benchmark&lt;br /&gt;
   class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
      @name=name&lt;br /&gt;
      @age=age&lt;br /&gt;
      @country=country&lt;br /&gt;
     end&lt;br /&gt;
   end  &lt;br /&gt;
    x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
   benchmark do |t|&lt;br /&gt;
    print &amp;quot;Marshal:&amp;quot;&lt;br /&gt;
    t.report{1000.times do; Marshal.load(Marshal.dump(x));end}&lt;br /&gt;
    print &amp;quot;JSON:&amp;quot;&lt;br /&gt;
    t.report{1000.times do; JSON.load(JSON.dump(x));end}&lt;br /&gt;
    print &amp;quot;YAML:&amp;quot;&lt;br /&gt;
    t.report{1000.times do; YAML.load(YAML.dump(x));end}&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   Marshal:   0.020000   0.000000   0.020000 (  0.015473)&lt;br /&gt;
   JSON:   0.020000   0.000000   0.020000 (  0.023934)&lt;br /&gt;
   YAML:   0.460000   0.010000   0.470000 (  0.476826)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output of the above example shows that, given large chunks of data, marshal is comparatively more efficient format for serialization in Ruby.&lt;br /&gt;
&lt;br /&gt;
== Serialization in OOLS Languages: Comparison ==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Sl.No !! Ruby !! Java !! .Net Framework !! C++&lt;br /&gt;
|-&lt;br /&gt;
| 1 || Ruby provides a built in 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 || Although, there is no built in support for serialization in C++, it can be achieved by using Boost libraries[http://www.boost.org/doc/libs/1_36_0/libs/serialization/example/demo.cpp]&lt;br /&gt;
|-&lt;br /&gt;
| 2 || The built in module of Ruby [http://www.ruby-doc.org/core-2.0.0/Marshal.html (Marshal)] does not support platform independence, however, it can be achieved by using external libraries like [http://yaml.org YAML] and [http://www.w3schools.com/json/ JSON] || Similarly, Java's built in serialization is also not platform independent and in order to use serialization in Java across Ruby platform, jruby library should be used.  || .Net used Remoting technology to make it platform independent. || Serialization using the Boost libraries is not platform independent.&lt;br /&gt;
|-&lt;br /&gt;
| 3 || YAML provides a method [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/syck/rdoc/Object.html (to_yaml_properties)] which can be used to select the variables who's value is need to be serialized. With Marshal, we need to write a method named marshal_dump defining the variables of an object that has to be serialized. || Provides an option for serializing only the required 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 data 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 || Serialization using the Boost libraries is custom and thus the user can specify the part of the objects to be serialized.&lt;br /&gt;
|-&lt;br /&gt;
| 4 || Bindings, procedure objects, singleton objects, instances of IO objects and interfaces can not be serialized. Serializing these objects throws TypeError exceptions. || Thread, OutputStream and its subclasses, and Socket are not serializable in Java.[http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html] || Objects like DataRow are non serializable in .NET. Whether an object is serializable or not can be checked using the [http://msdn.microsoft.com/en-us/library/system.type.isserializable(v=vs.110).aspx?cs-save-lang=1&amp;amp;cs-lang=cpp#code-snippet-1 Type.IsSerializable] || There are no such objects in C++.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== See Also==&lt;br /&gt;
&lt;br /&gt;
1. [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/json/rdoc/JSON.html Serialization in Ruby JSON]&lt;br /&gt;
&lt;br /&gt;
2. [http://api.rubyonrails.org/classes/ActiveModel/Serialization.html Serialization in Rails]&lt;br /&gt;
&lt;br /&gt;
3. [http://gregmoreno.wordpress.com/2011/01/27/preventing-model-explosion-via-rails-serialization/ Article on Rails Serialization]&lt;br /&gt;
&lt;br /&gt;
4. [http://en.wikipedia.org/wiki/Google_Protocol_Buffers Protocol Buffers]&lt;br /&gt;
&lt;br /&gt;
5. [https://rubygems.org/gems/ruby-protocol-buffers Ruby Protocol Buffers Gem]&lt;br /&gt;
&lt;br /&gt;
6. [http://en.wikipedia.org/wiki/Apache_Avro Avro]&lt;br /&gt;
&lt;br /&gt;
7. [http://rubygems.org/gems/avro Ruby Avro Gem]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
1. [http://en.wikipedia.org/wiki/Serialization Serilization in General]&lt;br /&gt;
&lt;br /&gt;
2. [http://en.wikipedia.org/wiki/Marshalling_%28computer_science%29 Marshaling]&lt;br /&gt;
&lt;br /&gt;
3. [http://techbrahmana.blogspot.com/2012/03/rails-cookie-handling-serialization-and.html Rails Cookie Handling]&lt;br /&gt;
&lt;br /&gt;
4. [http://yaml.org/ YAML]&lt;br /&gt;
&lt;br /&gt;
5. [http://www.ruby-doc.org/core-2.1.0/Marshal.html Marshal module]&lt;br /&gt;
&lt;br /&gt;
6. [http://json.org/ JSON]&lt;br /&gt;
&lt;br /&gt;
7. [http://rubygems.org/ Ruby Gems]&lt;br /&gt;
&lt;br /&gt;
8. [http://ruby-doc.org/core-2.0.0/ARGF.html# Ruby ARGF Documentation]&lt;br /&gt;
&lt;br /&gt;
9. [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html Ruby Documentation for Benchmark Gem]&lt;br /&gt;
&lt;br /&gt;
10. [http://www.boost.org/doc/libs/1_36_0/libs/serialization/example/demo.cpp Boost Libraries]&lt;br /&gt;
&lt;br /&gt;
11. [http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html Java Serialization]&lt;br /&gt;
&lt;br /&gt;
12. [http://www.w3schools.com/json/ JSON Tutorial]&lt;br /&gt;
&lt;br /&gt;
13. [http://www.skorks.com/2010/04/serializing-and-deserializing-objects-with-ruby/ Serializing and De-serializing in Ruby]&lt;br /&gt;
&lt;br /&gt;
14. [http://www.codeproject.com/Articles/33296/Serialization-and-De-serialization Serialization and De-serialization]&lt;br /&gt;
&lt;br /&gt;
15. [http://msdn.microsoft.com/en-us/library/182eeyhh.aspx XML Serialization]&lt;br /&gt;
&lt;br /&gt;
16. [http://www.tutorialspoint.com/java/java_serialization.htm Java Serialization Tutorial]&lt;br /&gt;
&lt;br /&gt;
17. [http://www.sapphiresteel.com/ruby-programming/The-Book-Of-Ruby The Book of Ruby]&lt;br /&gt;
&lt;br /&gt;
18. [http://www.codeproject.com/Articles/20962/Introducing-Serialization-in-NET Serialization in .Net]&lt;br /&gt;
&lt;br /&gt;
19. [http://ruby.about.com/od/advancedruby/ss/Serialization-In-Ruby-Yaml.html Serialization in Ruby YAML]&lt;br /&gt;
&lt;br /&gt;
20. [http://ruby.about.com/od/tasks/a/The-Json-Gem.html Installing JSON gem]&lt;br /&gt;
&lt;br /&gt;
21. [http://www.ruby-doc.org/stdlib-2.0.0/libdoc/json/rdoc/JSON.html Serialization in Ruby JSON]&lt;/div&gt;</summary>
		<author><name>Mmehta2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1_1w1d_mm&amp;diff=83270</id>
		<title>CSC/ECE 517 Spring 2014/ch1 1w1d mm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1_1w1d_mm&amp;diff=83270"/>
		<updated>2014-02-11T03:28:53Z</updated>

		<summary type="html">&lt;p&gt;Mmehta2: /* Serialization Performance in Ruby */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== '''Serialization''' ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Serialization'''[http://en.wikipedia.org/wiki/Serialization] is a process of converting a data structure or an object into a stream of bytes or string to facilitate storage in memory, file(persistence storage) or transmission over a network. The process of Serialization is also referred to as ''Marshalling''[http://en.wikipedia.org/wiki/Marshalling_%28computer_science%29]. 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. &lt;br /&gt;
&lt;br /&gt;
'''Basic Advantages of Serialization''':&lt;br /&gt;
&lt;br /&gt;
1. Communication between two or more processes on same machine. Object state can be saved and shared in a persistent or in-memory store.&lt;br /&gt;
&lt;br /&gt;
2. Communication between processes on different machines. Serialization facilitates the transmission of an object through a network.&lt;br /&gt;
&lt;br /&gt;
3. Creating a clone of an object. &lt;br /&gt;
&lt;br /&gt;
4. Cross-platform compatibility. Object can be serialized in a common format that is understood by multiple platforms. Eg. JSON, XML.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''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''.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Few Practical Applications for Serialization'''&lt;br /&gt;
&lt;br /&gt;
1. HTTP Session Replication by sharing session objects across web servers for handling failover scenarios&lt;br /&gt;
&lt;br /&gt;
2. Serialization facilitates communication in Remote Method Invocation or Remote procedure calls &lt;br /&gt;
&lt;br /&gt;
3. Rails Cookie Handling[http://techbrahmana.blogspot.com/2012/03/rails-cookie-handling-serialization-and.html]. Cookies are stored marshalled/unmarshalled to and from client machines. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:SerializationChart.jpg]]&lt;br /&gt;
&lt;br /&gt;
== '''Serialization in Ruby:''' ==&lt;br /&gt;
&lt;br /&gt;
Ruby provides serialization capabilities through its module, [http://www.ruby-doc.org/core-2.0.0/Marshal.html Marshal]. There are also other libraries like YAML and JSON which can be used in Ruby to generate serialized objects for purposes like platform independence and human readable formats.&lt;br /&gt;
&lt;br /&gt;
== '''Types of Serialization''' ==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
=== Converting Ruby Objects in Human Readable Formats ===&lt;br /&gt;
The conversion of Ruby objects into YAML and JSON formats are explained below.&lt;br /&gt;
&lt;br /&gt;
==== Converting Ruby Objects to YAML format ====&lt;br /&gt;
&lt;br /&gt;
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. 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. In order to use it in Ruby, the yaml.rb file is required to be loaded which provides methods for converting objects into yaml format and creating .yml files.&lt;br /&gt;
&lt;br /&gt;
'''Examples of serialization using YAML:'''&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;nowiki&amp;gt; #Serialization using YAML's to_yaml method&lt;br /&gt;
 &lt;br /&gt;
  require &amp;quot;yaml&amp;quot;&lt;br /&gt;
     class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
	@name = name&lt;br /&gt;
	@age = age&lt;br /&gt;
	@country=country&lt;br /&gt;
     end&lt;br /&gt;
      def to_s&lt;br /&gt;
	&amp;quot;In First:\n#{@name}, #{@age}, #{@country}\n&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
  x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
  puts x&lt;br /&gt;
  puts x.to_yaml&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   In First:&lt;br /&gt;
   Tom, 25, USA&lt;br /&gt;
   --- !ruby/object:First&lt;br /&gt;
   name: Tom&lt;br /&gt;
   age: 25&lt;br /&gt;
   country: USA&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The above code displays the object x, first as a string and then in the yaml format. &lt;br /&gt;
&lt;br /&gt;
'''Saving YAML data into a file:'''&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt; # Serialization using YAML::dump&lt;br /&gt;
&lt;br /&gt;
require 'yaml'&lt;br /&gt;
&lt;br /&gt;
f = File.open( 'first.yml', 'w' )&lt;br /&gt;
YAML.dump( [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;], f )&lt;br /&gt;
f.close&lt;br /&gt;
		&lt;br /&gt;
File.open( 'first.yml' ){ |f|	&lt;br /&gt;
    $arr= YAML.load(f)&lt;br /&gt;
}	&lt;br /&gt;
&lt;br /&gt;
p( $arr )&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;]&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The dump function can be used to serialize the data and save it into a file in YAML format. As shown in the above example the data in YAML format can be de-serialized using the load function.&lt;br /&gt;
&lt;br /&gt;
YAML libraries also provides an option of selecting only those variables of the object that are needed to be serialized. This is done using the to_yaml_properties method as shown in the below example.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;nowiki&amp;gt;  #Custom Serialization using YAML&lt;br /&gt;
 &lt;br /&gt;
  require &amp;quot;yaml&amp;quot;&lt;br /&gt;
     class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
	@name = name&lt;br /&gt;
	@age = age&lt;br /&gt;
	@country=country&lt;br /&gt;
     end&lt;br /&gt;
     def to_s&lt;br /&gt;
	&amp;quot;In First:\n#{@name}, #{@age}, #{@country}\n&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
      def to_yaml_properties&lt;br /&gt;
	   [&amp;quot;@name&amp;quot;,&amp;quot;@age&amp;quot;]  #@country will not be serialized&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
  x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
  puts x&lt;br /&gt;
  puts x.to_yaml&lt;br /&gt;
  &amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   In First:&lt;br /&gt;
   Tom, 25, USA&lt;br /&gt;
   --- !ruby/object:First&lt;br /&gt;
   name: Tom&lt;br /&gt;
   age: 25&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Converting Ruby Objects to JSON format: ====&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
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. Ruby 1.8.7 distribution is not bundled with json gem. However, in Ruby 1.9.2, the json gem is bundled with the core Ruby distribution.&lt;br /&gt;
The JSON library can be installed using Ruby Gems[http://rubygems.org/] as shown below:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;# gem install json&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A JSON string for serialization can be created by using the JSON.generate method:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
        require 'json'&lt;br /&gt;
        my_hash = {:Welcome =&amp;gt; &amp;quot;Ruby&amp;quot;}&lt;br /&gt;
        puts JSON.generate(my_hash) =&amp;gt; &amp;quot;{\&amp;quot;WELCOME\&amp;quot;:\&amp;quot;RUBY\&amp;quot;}&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
'''Output:'''&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;{&amp;quot;{\&amp;quot;Welcome\&amp;quot;:\&amp;quot;Ruby\&amp;quot;}&amp;quot;=&amp;gt;&amp;quot;{\&amp;quot;WELCOME\&amp;quot;:\&amp;quot;RUBY\&amp;quot;}&amp;quot;}&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A JSON string received from another program can be parsed by using JSON.parse&lt;br /&gt;
Ruby thus converts String to Hash.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
        require 'json'&lt;br /&gt;
        my_hash = JSON.parse('{&amp;quot;Welcome&amp;quot;: &amp;quot;Ruby&amp;quot;}')&lt;br /&gt;
        puts my_hash[&amp;quot;Welcome&amp;quot;] =&amp;gt; &amp;quot;Ruby&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Converting Ruby Objects to Binary Formats ===&lt;br /&gt;
Binary Serialization is another form of serialization in Ruby which is not in human readable form. 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. Binary Serialization  is done using [http://www.ruby-doc.org/core-2.0.0/Marshal.html Marshal] which is built into Ruby and the code for it is written in Ruby's Marshal module(marshal.c) and thus no additional files are required in order to use it. The _dump and _load methods defined in marshal are used for serialization. Using marshal module the following types of objects can not be serialized: bindings, procedure objects, singleton objects, instances of IO objects and interfaces.&lt;br /&gt;
&lt;br /&gt;
Since the Binary Serialized data is not in human readable form, the following guidelines need to be followed.&lt;br /&gt;
     1.Use print instead of puts[http://ruby-doc.org/core-2.0.0/ARGF.html#] when serialized objects are written to a file in order to avoid new line characters to be written &lt;br /&gt;
       in the file.&lt;br /&gt;
     &lt;br /&gt;
     2.Use a record separator in order to differentiate between two objects.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Binary Serialization Example:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    class Animal&lt;br /&gt;
      def initialize  name, age&lt;br /&gt;
      @name = name&lt;br /&gt;
      @age=age&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
   class Cat &amp;lt; Animal&lt;br /&gt;
    def to_s&lt;br /&gt;
     &amp;quot;In Cat C: #{@name} \t #{@age}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
  c = Cat.new(&amp;quot;Kitty Kat&amp;quot;,5)&lt;br /&gt;
  puts &amp;quot;Before Serialization&amp;quot;&lt;br /&gt;
  puts c&lt;br /&gt;
  #puts d&lt;br /&gt;
  serialize_cat= Marshal.dump(c) #dumps the serialized cat object into serialize_cat&lt;br /&gt;
  puts &amp;quot;\nAfter Serialization:\n #{serialize_cat}&amp;quot;&lt;br /&gt;
  deserialize_cat= Marshal::load(serialize_cat) #deserializes the cat object and loads it back into deserialize_cat&lt;br /&gt;
  puts &amp;quot;\nAfter Deserialization\n #{deserialize_cat}&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   Before Serialization&lt;br /&gt;
   In Cat C: Kitty Kat 	 5&lt;br /&gt;
   After Serialization:&lt;br /&gt;
   oCat:&lt;br /&gt;
   @nameI&amp;quot;Kitty Kat:ET:	@agei&lt;br /&gt;
   After Deserialization&lt;br /&gt;
   In Cat C: Kitty Kat 	 5&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Similar to YAML, Marshal can also be used to dump data into a file. The above example showing the serialization using YAML::dump can be written using marshal as shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    f = File.open( 'first.yml', 'w' )&lt;br /&gt;
    Marshal.dump( [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;], f )&lt;br /&gt;
    f.close	&lt;br /&gt;
    File.open( 'first.yml' ){ |f|	&lt;br /&gt;
    $arr= Marshal.load(f)&lt;br /&gt;
    }	&lt;br /&gt;
    p( $arr )&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;]&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice that in the above example there is no &amp;quot;require&amp;quot; statement as opposed to the earlier example of writing serialized data into files using YAML. This is because unlike YAML, marshal is built-in Ruby and  no external libraries is required in order to use its functionality.&lt;br /&gt;
&lt;br /&gt;
Marshal can also be used to custom serialize an object i.e. it provides an option to omit the variables of an object that are not required in the serialized data. The following program shows the use of marshal_dump method for achieving custom serialization.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
      class First&lt;br /&gt;
      def initialize(name, age, country)&lt;br /&gt;
         @name = name&lt;br /&gt;
         @age = age&lt;br /&gt;
         @country=country&lt;br /&gt;
      end&lt;br /&gt;
      def to_s&lt;br /&gt;
            &amp;quot;In First: #{@name}, #{@age}, #{@country}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
      def marshal_dump&lt;br /&gt;
            [@name,@age]  #@country will not be serialized&lt;br /&gt;
      end&lt;br /&gt;
      def marshal_load(data)&lt;br /&gt;
       @name=data[0]&lt;br /&gt;
       @age=data[1]&lt;br /&gt;
       @country=&amp;quot;United States of America&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
      end&lt;br /&gt;
 &lt;br /&gt;
      x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
      puts x&lt;br /&gt;
  &lt;br /&gt;
      marshal_data = Marshal.dump( x ) &lt;br /&gt;
      y = Marshal.load( marshal_data ) &lt;br /&gt;
      p( y.to_s )&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    In First: Tom, 25, USA&lt;br /&gt;
    &amp;quot;In First: Tom, 25, United States of America&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== '''Serialization Performance in Ruby''' ==&lt;br /&gt;
The different serialization formats described above differ in the efficiency at which they can serialize and deserialize data and thus while serializing large amount of data their efficiency is taken into account. The .report[http://www.ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html] method in Ruby can be used to evaluate the performance of these serialization patterns. Marshal, as it handles data in binary format, is the most efficient form of serialization in Ruby. The following example compares the performance of marshal, JSON and YAML format.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   require 'benchmark'&lt;br /&gt;
   require 'rubygems'&lt;br /&gt;
   require 'json'&lt;br /&gt;
   require 'yaml'&lt;br /&gt;
   include Benchmark&lt;br /&gt;
   class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
      @name=name&lt;br /&gt;
      @age=age&lt;br /&gt;
      @country=country&lt;br /&gt;
     end&lt;br /&gt;
   end  &lt;br /&gt;
    x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
   benchmark do |t|&lt;br /&gt;
    print &amp;quot;Marshal:&amp;quot;&lt;br /&gt;
    t.report{1000.times do; Marshal.load(Marshal.dump(x));end}&lt;br /&gt;
    print &amp;quot;JSON:&amp;quot;&lt;br /&gt;
    t.report{1000.times do; JSON.load(JSON.dump(x));end}&lt;br /&gt;
    print &amp;quot;YAML:&amp;quot;&lt;br /&gt;
    t.report{1000.times do; YAML.load(YAML.dump(x));end}&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   Marshal:   0.020000   0.000000   0.020000 (  0.015473)&lt;br /&gt;
   JSON:   0.020000   0.000000   0.020000 (  0.023934)&lt;br /&gt;
   YAML:   0.460000   0.010000   0.470000 (  0.476826)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output of the above example shows that, given large chunks of data, marshal is comparatively more efficient format for serialization in Ruby.&lt;br /&gt;
&lt;br /&gt;
== Serialization in OOLS Languages: Comparison ==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Sl.No !! Ruby !! Java !! .Net Framework !! C++&lt;br /&gt;
|-&lt;br /&gt;
| 1 || Ruby provides a built in 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 || Although, there is no built in support for serialization in C++, it can be achieved by using Boost libraries[http://www.boost.org/doc/libs/1_36_0/libs/serialization/example/demo.cpp]&lt;br /&gt;
|-&lt;br /&gt;
| 2 || The built in module of Ruby [http://www.ruby-doc.org/core-2.0.0/Marshal.html (Marshal)] does not support platform independence, however, it can be achieved by using external libraries like [http://yaml.org YAML] and [http://www.w3schools.com/json/ JSON] || Similarly, Java's built in serialization is also not platform independent and in order to use serialization in Java across Ruby platform, jruby library should be used.  || .Net used Remoting technology to make it platform independent. || Serialization using the Boost libraries is not platform independent.&lt;br /&gt;
|-&lt;br /&gt;
| 3 || YAML provides a method [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/syck/rdoc/Object.html (to_yaml_properties)] which can be used to select the variables who's value is need to be serialized. With Marshal, we need to write a method named marshal_dump defining the variables of an object that has to be serialized. || Provides an option for serializing only the required 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 data 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 || Serialization using the Boost libraries is custom and thus the user can specify the part of the objects to be serialized.&lt;br /&gt;
|-&lt;br /&gt;
| 4 || Bindings, procedure objects, singleton objects, instances of IO objects and interfaces can not be serialized. Serializing these objects throws TypeError exceptions. || Thread, OutputStream and its subclasses, and Socket are not serializable in Java.[http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html] || Objects like DataRow are non serializable in .NET. Whether an object is serializable or not can be checked using the [http://msdn.microsoft.com/en-us/library/system.type.isserializable(v=vs.110).aspx?cs-save-lang=1&amp;amp;cs-lang=cpp#code-snippet-1 Type.IsSerializable] || There are no such objects in C++.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== See Also==&lt;br /&gt;
&lt;br /&gt;
1. [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/json/rdoc/JSON.html Serialization in Ruby JSON]&lt;br /&gt;
&lt;br /&gt;
2. [http://api.rubyonrails.org/classes/ActiveModel/Serialization.html Serialization in Rails]&lt;br /&gt;
&lt;br /&gt;
3. [http://gregmoreno.wordpress.com/2011/01/27/preventing-model-explosion-via-rails-serialization/ Article on Rails Serialization]&lt;br /&gt;
&lt;br /&gt;
4. [http://en.wikipedia.org/wiki/Google_Protocol_Buffers Protocol Buffers]&lt;br /&gt;
&lt;br /&gt;
5. [https://rubygems.org/gems/ruby-protocol-buffers Ruby Protocol Buffers Gem]&lt;br /&gt;
&lt;br /&gt;
6. [http://en.wikipedia.org/wiki/Apache_Avro Avro]&lt;br /&gt;
&lt;br /&gt;
7. [http://rubygems.org/gems/avro Ruby Avro Gem]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
1. [http://en.wikipedia.org/wiki/Serialization Serilization in General]&lt;br /&gt;
&lt;br /&gt;
2. [http://en.wikipedia.org/wiki/Marshalling_%28computer_science%29 Marshaling]&lt;br /&gt;
&lt;br /&gt;
3. [http://techbrahmana.blogspot.com/2012/03/rails-cookie-handling-serialization-and.html Rails Cookie Handling]&lt;br /&gt;
&lt;br /&gt;
4. [http://yaml.org/ YAML]&lt;br /&gt;
&lt;br /&gt;
5. [http://www.ruby-doc.org/core-2.1.0/Marshal.html Marshal module]&lt;br /&gt;
&lt;br /&gt;
6. [http://json.org/ JSON]&lt;br /&gt;
&lt;br /&gt;
7. [http://rubygems.org/ Ruby Gems]&lt;br /&gt;
&lt;br /&gt;
8. [http://ruby-doc.org/core-2.0.0/ARGF.html# Ruby ARGF Documentation]&lt;br /&gt;
&lt;br /&gt;
9. [http://www.boost.org/doc/libs/1_36_0/libs/serialization/example/demo.cpp Boost Libraries]&lt;br /&gt;
&lt;br /&gt;
10. [http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html Java Serialization]&lt;br /&gt;
&lt;br /&gt;
11. [http://www.w3schools.com/json/ JSON Tutorial]&lt;br /&gt;
&lt;br /&gt;
12. [http://www.skorks.com/2010/04/serializing-and-deserializing-objects-with-ruby/ Serializing and De-serializing in Ruby]&lt;br /&gt;
&lt;br /&gt;
13. [http://www.codeproject.com/Articles/33296/Serialization-and-De-serialization Serialization and De-serialization]&lt;br /&gt;
&lt;br /&gt;
14. [http://msdn.microsoft.com/en-us/library/182eeyhh.aspx XML Serialization]&lt;br /&gt;
&lt;br /&gt;
15. [http://www.tutorialspoint.com/java/java_serialization.htm Java Serialization Tutorial]&lt;br /&gt;
&lt;br /&gt;
16. [http://www.sapphiresteel.com/ruby-programming/The-Book-Of-Ruby The Book of Ruby]&lt;br /&gt;
&lt;br /&gt;
17. [http://www.codeproject.com/Articles/20962/Introducing-Serialization-in-NET Serialization in .Net]&lt;br /&gt;
&lt;br /&gt;
18. [http://ruby.about.com/od/advancedruby/ss/Serialization-In-Ruby-Yaml.html Serialization in Ruby YAML]&lt;br /&gt;
&lt;br /&gt;
19. [http://ruby.about.com/od/tasks/a/The-Json-Gem.html Installing JSON gem]&lt;br /&gt;
&lt;br /&gt;
20. [http://www.ruby-doc.org/stdlib-2.0.0/libdoc/json/rdoc/JSON.html Serialization in Ruby JSON]&lt;/div&gt;</summary>
		<author><name>Mmehta2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1_1w1d_mm&amp;diff=83267</id>
		<title>CSC/ECE 517 Spring 2014/ch1 1w1d mm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1_1w1d_mm&amp;diff=83267"/>
		<updated>2014-02-11T03:27:18Z</updated>

		<summary type="html">&lt;p&gt;Mmehta2: /* Serialization Performance in Ruby */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== '''Serialization''' ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Serialization'''[http://en.wikipedia.org/wiki/Serialization] is a process of converting a data structure or an object into a stream of bytes or string to facilitate storage in memory, file(persistence storage) or transmission over a network. The process of Serialization is also referred to as ''Marshalling''[http://en.wikipedia.org/wiki/Marshalling_%28computer_science%29]. 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. &lt;br /&gt;
&lt;br /&gt;
'''Basic Advantages of Serialization''':&lt;br /&gt;
&lt;br /&gt;
1. Communication between two or more processes on same machine. Object state can be saved and shared in a persistent or in-memory store.&lt;br /&gt;
&lt;br /&gt;
2. Communication between processes on different machines. Serialization facilitates the transmission of an object through a network.&lt;br /&gt;
&lt;br /&gt;
3. Creating a clone of an object. &lt;br /&gt;
&lt;br /&gt;
4. Cross-platform compatibility. Object can be serialized in a common format that is understood by multiple platforms. Eg. JSON, XML.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''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''.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Few Practical Applications for Serialization'''&lt;br /&gt;
&lt;br /&gt;
1. HTTP Session Replication by sharing session objects across web servers for handling failover scenarios&lt;br /&gt;
&lt;br /&gt;
2. Serialization facilitates communication in Remote Method Invocation or Remote procedure calls &lt;br /&gt;
&lt;br /&gt;
3. Rails Cookie Handling[http://techbrahmana.blogspot.com/2012/03/rails-cookie-handling-serialization-and.html]. Cookies are stored marshalled/unmarshalled to and from client machines. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:SerializationChart.jpg]]&lt;br /&gt;
&lt;br /&gt;
== '''Serialization in Ruby:''' ==&lt;br /&gt;
&lt;br /&gt;
Ruby provides serialization capabilities through its module, [http://www.ruby-doc.org/core-2.0.0/Marshal.html Marshal]. There are also other libraries like YAML and JSON which can be used in Ruby to generate serialized objects for purposes like platform independence and human readable formats.&lt;br /&gt;
&lt;br /&gt;
== '''Types of Serialization''' ==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
=== Converting Ruby Objects in Human Readable Formats ===&lt;br /&gt;
The conversion of Ruby objects into YAML and JSON formats are explained below.&lt;br /&gt;
&lt;br /&gt;
==== Converting Ruby Objects to YAML format ====&lt;br /&gt;
&lt;br /&gt;
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. 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. In order to use it in Ruby, the yaml.rb file is required to be loaded which provides methods for converting objects into yaml format and creating .yml files.&lt;br /&gt;
&lt;br /&gt;
'''Examples of serialization using YAML:'''&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;nowiki&amp;gt; #Serialization using YAML's to_yaml method&lt;br /&gt;
 &lt;br /&gt;
  require &amp;quot;yaml&amp;quot;&lt;br /&gt;
     class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
	@name = name&lt;br /&gt;
	@age = age&lt;br /&gt;
	@country=country&lt;br /&gt;
     end&lt;br /&gt;
      def to_s&lt;br /&gt;
	&amp;quot;In First:\n#{@name}, #{@age}, #{@country}\n&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
  x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
  puts x&lt;br /&gt;
  puts x.to_yaml&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   In First:&lt;br /&gt;
   Tom, 25, USA&lt;br /&gt;
   --- !ruby/object:First&lt;br /&gt;
   name: Tom&lt;br /&gt;
   age: 25&lt;br /&gt;
   country: USA&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The above code displays the object x, first as a string and then in the yaml format. &lt;br /&gt;
&lt;br /&gt;
'''Saving YAML data into a file:'''&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt; # Serialization using YAML::dump&lt;br /&gt;
&lt;br /&gt;
require 'yaml'&lt;br /&gt;
&lt;br /&gt;
f = File.open( 'first.yml', 'w' )&lt;br /&gt;
YAML.dump( [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;], f )&lt;br /&gt;
f.close&lt;br /&gt;
		&lt;br /&gt;
File.open( 'first.yml' ){ |f|	&lt;br /&gt;
    $arr= YAML.load(f)&lt;br /&gt;
}	&lt;br /&gt;
&lt;br /&gt;
p( $arr )&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;]&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The dump function can be used to serialize the data and save it into a file in YAML format. As shown in the above example the data in YAML format can be de-serialized using the load function.&lt;br /&gt;
&lt;br /&gt;
YAML libraries also provides an option of selecting only those variables of the object that are needed to be serialized. This is done using the to_yaml_properties method as shown in the below example.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;nowiki&amp;gt;  #Custom Serialization using YAML&lt;br /&gt;
 &lt;br /&gt;
  require &amp;quot;yaml&amp;quot;&lt;br /&gt;
     class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
	@name = name&lt;br /&gt;
	@age = age&lt;br /&gt;
	@country=country&lt;br /&gt;
     end&lt;br /&gt;
     def to_s&lt;br /&gt;
	&amp;quot;In First:\n#{@name}, #{@age}, #{@country}\n&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
      def to_yaml_properties&lt;br /&gt;
	   [&amp;quot;@name&amp;quot;,&amp;quot;@age&amp;quot;]  #@country will not be serialized&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
  x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
  puts x&lt;br /&gt;
  puts x.to_yaml&lt;br /&gt;
  &amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   In First:&lt;br /&gt;
   Tom, 25, USA&lt;br /&gt;
   --- !ruby/object:First&lt;br /&gt;
   name: Tom&lt;br /&gt;
   age: 25&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Converting Ruby Objects to JSON format: ====&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
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. Ruby 1.8.7 distribution is not bundled with json gem. However, in Ruby 1.9.2, the json gem is bundled with the core Ruby distribution.&lt;br /&gt;
The JSON library can be installed using Ruby Gems[http://rubygems.org/] as shown below:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;# gem install json&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A JSON string for serialization can be created by using the JSON.generate method:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
        require 'json'&lt;br /&gt;
        my_hash = {:Welcome =&amp;gt; &amp;quot;Ruby&amp;quot;}&lt;br /&gt;
        puts JSON.generate(my_hash) =&amp;gt; &amp;quot;{\&amp;quot;WELCOME\&amp;quot;:\&amp;quot;RUBY\&amp;quot;}&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
'''Output:'''&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;{&amp;quot;{\&amp;quot;Welcome\&amp;quot;:\&amp;quot;Ruby\&amp;quot;}&amp;quot;=&amp;gt;&amp;quot;{\&amp;quot;WELCOME\&amp;quot;:\&amp;quot;RUBY\&amp;quot;}&amp;quot;}&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A JSON string received from another program can be parsed by using JSON.parse&lt;br /&gt;
Ruby thus converts String to Hash.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
        require 'json'&lt;br /&gt;
        my_hash = JSON.parse('{&amp;quot;Welcome&amp;quot;: &amp;quot;Ruby&amp;quot;}')&lt;br /&gt;
        puts my_hash[&amp;quot;Welcome&amp;quot;] =&amp;gt; &amp;quot;Ruby&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Converting Ruby Objects to Binary Formats ===&lt;br /&gt;
Binary Serialization is another form of serialization in Ruby which is not in human readable form. 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. Binary Serialization  is done using [http://www.ruby-doc.org/core-2.0.0/Marshal.html Marshal] which is built into Ruby and the code for it is written in Ruby's Marshal module(marshal.c) and thus no additional files are required in order to use it. The _dump and _load methods defined in marshal are used for serialization. Using marshal module the following types of objects can not be serialized: bindings, procedure objects, singleton objects, instances of IO objects and interfaces.&lt;br /&gt;
&lt;br /&gt;
Since the Binary Serialized data is not in human readable form, the following guidelines need to be followed.&lt;br /&gt;
     1.Use print instead of puts[http://ruby-doc.org/core-2.0.0/ARGF.html#] when serialized objects are written to a file in order to avoid new line characters to be written &lt;br /&gt;
       in the file.&lt;br /&gt;
     &lt;br /&gt;
     2.Use a record separator in order to differentiate between two objects.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Binary Serialization Example:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    class Animal&lt;br /&gt;
      def initialize  name, age&lt;br /&gt;
      @name = name&lt;br /&gt;
      @age=age&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
   class Cat &amp;lt; Animal&lt;br /&gt;
    def to_s&lt;br /&gt;
     &amp;quot;In Cat C: #{@name} \t #{@age}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
  c = Cat.new(&amp;quot;Kitty Kat&amp;quot;,5)&lt;br /&gt;
  puts &amp;quot;Before Serialization&amp;quot;&lt;br /&gt;
  puts c&lt;br /&gt;
  #puts d&lt;br /&gt;
  serialize_cat= Marshal.dump(c) #dumps the serialized cat object into serialize_cat&lt;br /&gt;
  puts &amp;quot;\nAfter Serialization:\n #{serialize_cat}&amp;quot;&lt;br /&gt;
  deserialize_cat= Marshal::load(serialize_cat) #deserializes the cat object and loads it back into deserialize_cat&lt;br /&gt;
  puts &amp;quot;\nAfter Deserialization\n #{deserialize_cat}&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   Before Serialization&lt;br /&gt;
   In Cat C: Kitty Kat 	 5&lt;br /&gt;
   After Serialization:&lt;br /&gt;
   oCat:&lt;br /&gt;
   @nameI&amp;quot;Kitty Kat:ET:	@agei&lt;br /&gt;
   After Deserialization&lt;br /&gt;
   In Cat C: Kitty Kat 	 5&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Similar to YAML, Marshal can also be used to dump data into a file. The above example showing the serialization using YAML::dump can be written using marshal as shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    f = File.open( 'first.yml', 'w' )&lt;br /&gt;
    Marshal.dump( [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;], f )&lt;br /&gt;
    f.close	&lt;br /&gt;
    File.open( 'first.yml' ){ |f|	&lt;br /&gt;
    $arr= Marshal.load(f)&lt;br /&gt;
    }	&lt;br /&gt;
    p( $arr )&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;]&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice that in the above example there is no &amp;quot;require&amp;quot; statement as opposed to the earlier example of writing serialized data into files using YAML. This is because unlike YAML, marshal is built-in Ruby and  no external libraries is required in order to use its functionality.&lt;br /&gt;
&lt;br /&gt;
Marshal can also be used to custom serialize an object i.e. it provides an option to omit the variables of an object that are not required in the serialized data. The following program shows the use of marshal_dump method for achieving custom serialization.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
      class First&lt;br /&gt;
      def initialize(name, age, country)&lt;br /&gt;
         @name = name&lt;br /&gt;
         @age = age&lt;br /&gt;
         @country=country&lt;br /&gt;
      end&lt;br /&gt;
      def to_s&lt;br /&gt;
            &amp;quot;In First: #{@name}, #{@age}, #{@country}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
      def marshal_dump&lt;br /&gt;
            [@name,@age]  #@country will not be serialized&lt;br /&gt;
      end&lt;br /&gt;
      def marshal_load(data)&lt;br /&gt;
       @name=data[0]&lt;br /&gt;
       @age=data[1]&lt;br /&gt;
       @country=&amp;quot;United States of America&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
      end&lt;br /&gt;
 &lt;br /&gt;
      x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
      puts x&lt;br /&gt;
  &lt;br /&gt;
      marshal_data = Marshal.dump( x ) &lt;br /&gt;
      y = Marshal.load( marshal_data ) &lt;br /&gt;
      p( y.to_s )&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    In First: Tom, 25, USA&lt;br /&gt;
    &amp;quot;In First: Tom, 25, United States of America&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== '''Serialization Performance in Ruby''' ==&lt;br /&gt;
The different serialization formats described above differ in the efficiency at which they can serialize and deserialize data and thus while serializing large amount of data their efficiency is taken into account. The .report method in Ruby can be used to evaluate the performance of these serialization patterns. Marshal, as it handles data in binary format, is the most efficient form of serialization in Ruby. The following example compares the performance of marshal, JSON and YAML format while processing a large array and a large hash.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   require 'benchmark'&lt;br /&gt;
   require 'rubygems'&lt;br /&gt;
   require 'json'&lt;br /&gt;
   require 'yaml'&lt;br /&gt;
   include Benchmark&lt;br /&gt;
   class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
      @name=name&lt;br /&gt;
      @age=age&lt;br /&gt;
      @country=country&lt;br /&gt;
     end&lt;br /&gt;
   end  &lt;br /&gt;
    x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
   benchmark do |t|&lt;br /&gt;
    print &amp;quot;Marshal:&amp;quot;&lt;br /&gt;
    t.report{1000.times do; Marshal.load(Marshal.dump(x));end}&lt;br /&gt;
    print &amp;quot;JSON:&amp;quot;&lt;br /&gt;
    t.report{1000.times do; JSON.load(JSON.dump(x));end}&lt;br /&gt;
    print &amp;quot;YAML:&amp;quot;&lt;br /&gt;
    t.report{1000.times do; YAML.load(YAML.dump(x));end}&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   Marshal:   0.020000   0.000000   0.020000 (  0.015473)&lt;br /&gt;
   JSON:   0.020000   0.000000   0.020000 (  0.023934)&lt;br /&gt;
   YAML:   0.460000   0.010000   0.470000 (  0.476826)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output of the above example shows that, given large chunks of data, marshal is comparatively more efficient format for serialization in Ruby.&lt;br /&gt;
&lt;br /&gt;
== Serialization in OOLS Languages: Comparison ==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Sl.No !! Ruby !! Java !! .Net Framework !! C++&lt;br /&gt;
|-&lt;br /&gt;
| 1 || Ruby provides a built in 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 || Although, there is no built in support for serialization in C++, it can be achieved by using Boost libraries[http://www.boost.org/doc/libs/1_36_0/libs/serialization/example/demo.cpp]&lt;br /&gt;
|-&lt;br /&gt;
| 2 || The built in module of Ruby [http://www.ruby-doc.org/core-2.0.0/Marshal.html (Marshal)] does not support platform independence, however, it can be achieved by using external libraries like [http://yaml.org YAML] and [http://www.w3schools.com/json/ JSON] || Similarly, Java's built in serialization is also not platform independent and in order to use serialization in Java across Ruby platform, jruby library should be used.  || .Net used Remoting technology to make it platform independent. || Serialization using the Boost libraries is not platform independent.&lt;br /&gt;
|-&lt;br /&gt;
| 3 || YAML provides a method [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/syck/rdoc/Object.html (to_yaml_properties)] which can be used to select the variables who's value is need to be serialized. With Marshal, we need to write a method named marshal_dump defining the variables of an object that has to be serialized. || Provides an option for serializing only the required 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 data 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 || Serialization using the Boost libraries is custom and thus the user can specify the part of the objects to be serialized.&lt;br /&gt;
|-&lt;br /&gt;
| 4 || Bindings, procedure objects, singleton objects, instances of IO objects and interfaces can not be serialized. Serializing these objects throws TypeError exceptions. || Thread, OutputStream and its subclasses, and Socket are not serializable in Java.[http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html] || Objects like DataRow are non serializable in .NET. Whether an object is serializable or not can be checked using the [http://msdn.microsoft.com/en-us/library/system.type.isserializable(v=vs.110).aspx?cs-save-lang=1&amp;amp;cs-lang=cpp#code-snippet-1 Type.IsSerializable] || There are no such objects in C++.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== See Also==&lt;br /&gt;
&lt;br /&gt;
1. [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/json/rdoc/JSON.html Serialization in Ruby JSON]&lt;br /&gt;
&lt;br /&gt;
2. [http://api.rubyonrails.org/classes/ActiveModel/Serialization.html Serialization in Rails]&lt;br /&gt;
&lt;br /&gt;
3. [http://gregmoreno.wordpress.com/2011/01/27/preventing-model-explosion-via-rails-serialization/ Article on Rails Serialization]&lt;br /&gt;
&lt;br /&gt;
4. [http://en.wikipedia.org/wiki/Google_Protocol_Buffers Protocol Buffers]&lt;br /&gt;
&lt;br /&gt;
5. [https://rubygems.org/gems/ruby-protocol-buffers Ruby Protocol Buffers Gem]&lt;br /&gt;
&lt;br /&gt;
6. [http://en.wikipedia.org/wiki/Apache_Avro Avro]&lt;br /&gt;
&lt;br /&gt;
7. [http://rubygems.org/gems/avro Ruby Avro Gem]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
1. [http://en.wikipedia.org/wiki/Serialization Serilization in General]&lt;br /&gt;
&lt;br /&gt;
2. [http://en.wikipedia.org/wiki/Marshalling_%28computer_science%29 Marshaling]&lt;br /&gt;
&lt;br /&gt;
3. [http://techbrahmana.blogspot.com/2012/03/rails-cookie-handling-serialization-and.html Rails Cookie Handling]&lt;br /&gt;
&lt;br /&gt;
4. [http://yaml.org/ YAML]&lt;br /&gt;
&lt;br /&gt;
5. [http://www.ruby-doc.org/core-2.1.0/Marshal.html Marshal module]&lt;br /&gt;
&lt;br /&gt;
6. [http://json.org/ JSON]&lt;br /&gt;
&lt;br /&gt;
7. [http://rubygems.org/ Ruby Gems]&lt;br /&gt;
&lt;br /&gt;
8. [http://ruby-doc.org/core-2.0.0/ARGF.html# Ruby ARGF Documentation]&lt;br /&gt;
&lt;br /&gt;
9. [http://www.boost.org/doc/libs/1_36_0/libs/serialization/example/demo.cpp Boost Libraries]&lt;br /&gt;
&lt;br /&gt;
10. [http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html Java Serialization]&lt;br /&gt;
&lt;br /&gt;
11. [http://www.w3schools.com/json/ JSON Tutorial]&lt;br /&gt;
&lt;br /&gt;
12. [http://www.skorks.com/2010/04/serializing-and-deserializing-objects-with-ruby/ Serializing and De-serializing in Ruby]&lt;br /&gt;
&lt;br /&gt;
13. [http://www.codeproject.com/Articles/33296/Serialization-and-De-serialization Serialization and De-serialization]&lt;br /&gt;
&lt;br /&gt;
14. [http://msdn.microsoft.com/en-us/library/182eeyhh.aspx XML Serialization]&lt;br /&gt;
&lt;br /&gt;
15. [http://www.tutorialspoint.com/java/java_serialization.htm Java Serialization Tutorial]&lt;br /&gt;
&lt;br /&gt;
16. [http://www.sapphiresteel.com/ruby-programming/The-Book-Of-Ruby The Book of Ruby]&lt;br /&gt;
&lt;br /&gt;
17. [http://www.codeproject.com/Articles/20962/Introducing-Serialization-in-NET Serialization in .Net]&lt;br /&gt;
&lt;br /&gt;
18. [http://ruby.about.com/od/advancedruby/ss/Serialization-In-Ruby-Yaml.html Serialization in Ruby YAML]&lt;br /&gt;
&lt;br /&gt;
19. [http://ruby.about.com/od/tasks/a/The-Json-Gem.html Installing JSON gem]&lt;br /&gt;
&lt;br /&gt;
20. [http://www.ruby-doc.org/stdlib-2.0.0/libdoc/json/rdoc/JSON.html Serialization in Ruby JSON]&lt;/div&gt;</summary>
		<author><name>Mmehta2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1_1w1d_mm&amp;diff=83266</id>
		<title>CSC/ECE 517 Spring 2014/ch1 1w1d mm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1_1w1d_mm&amp;diff=83266"/>
		<updated>2014-02-11T03:27:06Z</updated>

		<summary type="html">&lt;p&gt;Mmehta2: /* Serialization Performance in Ruby */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== '''Serialization''' ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Serialization'''[http://en.wikipedia.org/wiki/Serialization] is a process of converting a data structure or an object into a stream of bytes or string to facilitate storage in memory, file(persistence storage) or transmission over a network. The process of Serialization is also referred to as ''Marshalling''[http://en.wikipedia.org/wiki/Marshalling_%28computer_science%29]. 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. &lt;br /&gt;
&lt;br /&gt;
'''Basic Advantages of Serialization''':&lt;br /&gt;
&lt;br /&gt;
1. Communication between two or more processes on same machine. Object state can be saved and shared in a persistent or in-memory store.&lt;br /&gt;
&lt;br /&gt;
2. Communication between processes on different machines. Serialization facilitates the transmission of an object through a network.&lt;br /&gt;
&lt;br /&gt;
3. Creating a clone of an object. &lt;br /&gt;
&lt;br /&gt;
4. Cross-platform compatibility. Object can be serialized in a common format that is understood by multiple platforms. Eg. JSON, XML.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''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''.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Few Practical Applications for Serialization'''&lt;br /&gt;
&lt;br /&gt;
1. HTTP Session Replication by sharing session objects across web servers for handling failover scenarios&lt;br /&gt;
&lt;br /&gt;
2. Serialization facilitates communication in Remote Method Invocation or Remote procedure calls &lt;br /&gt;
&lt;br /&gt;
3. Rails Cookie Handling[http://techbrahmana.blogspot.com/2012/03/rails-cookie-handling-serialization-and.html]. Cookies are stored marshalled/unmarshalled to and from client machines. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:SerializationChart.jpg]]&lt;br /&gt;
&lt;br /&gt;
== '''Serialization in Ruby:''' ==&lt;br /&gt;
&lt;br /&gt;
Ruby provides serialization capabilities through its module, [http://www.ruby-doc.org/core-2.0.0/Marshal.html Marshal]. There are also other libraries like YAML and JSON which can be used in Ruby to generate serialized objects for purposes like platform independence and human readable formats.&lt;br /&gt;
&lt;br /&gt;
== '''Types of Serialization''' ==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
=== Converting Ruby Objects in Human Readable Formats ===&lt;br /&gt;
The conversion of Ruby objects into YAML and JSON formats are explained below.&lt;br /&gt;
&lt;br /&gt;
==== Converting Ruby Objects to YAML format ====&lt;br /&gt;
&lt;br /&gt;
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. 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. In order to use it in Ruby, the yaml.rb file is required to be loaded which provides methods for converting objects into yaml format and creating .yml files.&lt;br /&gt;
&lt;br /&gt;
'''Examples of serialization using YAML:'''&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;nowiki&amp;gt; #Serialization using YAML's to_yaml method&lt;br /&gt;
 &lt;br /&gt;
  require &amp;quot;yaml&amp;quot;&lt;br /&gt;
     class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
	@name = name&lt;br /&gt;
	@age = age&lt;br /&gt;
	@country=country&lt;br /&gt;
     end&lt;br /&gt;
      def to_s&lt;br /&gt;
	&amp;quot;In First:\n#{@name}, #{@age}, #{@country}\n&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
  x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
  puts x&lt;br /&gt;
  puts x.to_yaml&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   In First:&lt;br /&gt;
   Tom, 25, USA&lt;br /&gt;
   --- !ruby/object:First&lt;br /&gt;
   name: Tom&lt;br /&gt;
   age: 25&lt;br /&gt;
   country: USA&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The above code displays the object x, first as a string and then in the yaml format. &lt;br /&gt;
&lt;br /&gt;
'''Saving YAML data into a file:'''&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt; # Serialization using YAML::dump&lt;br /&gt;
&lt;br /&gt;
require 'yaml'&lt;br /&gt;
&lt;br /&gt;
f = File.open( 'first.yml', 'w' )&lt;br /&gt;
YAML.dump( [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;], f )&lt;br /&gt;
f.close&lt;br /&gt;
		&lt;br /&gt;
File.open( 'first.yml' ){ |f|	&lt;br /&gt;
    $arr= YAML.load(f)&lt;br /&gt;
}	&lt;br /&gt;
&lt;br /&gt;
p( $arr )&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;]&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The dump function can be used to serialize the data and save it into a file in YAML format. As shown in the above example the data in YAML format can be de-serialized using the load function.&lt;br /&gt;
&lt;br /&gt;
YAML libraries also provides an option of selecting only those variables of the object that are needed to be serialized. This is done using the to_yaml_properties method as shown in the below example.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;nowiki&amp;gt;  #Custom Serialization using YAML&lt;br /&gt;
 &lt;br /&gt;
  require &amp;quot;yaml&amp;quot;&lt;br /&gt;
     class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
	@name = name&lt;br /&gt;
	@age = age&lt;br /&gt;
	@country=country&lt;br /&gt;
     end&lt;br /&gt;
     def to_s&lt;br /&gt;
	&amp;quot;In First:\n#{@name}, #{@age}, #{@country}\n&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
      def to_yaml_properties&lt;br /&gt;
	   [&amp;quot;@name&amp;quot;,&amp;quot;@age&amp;quot;]  #@country will not be serialized&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
  x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
  puts x&lt;br /&gt;
  puts x.to_yaml&lt;br /&gt;
  &amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   In First:&lt;br /&gt;
   Tom, 25, USA&lt;br /&gt;
   --- !ruby/object:First&lt;br /&gt;
   name: Tom&lt;br /&gt;
   age: 25&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Converting Ruby Objects to JSON format: ====&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
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. Ruby 1.8.7 distribution is not bundled with json gem. However, in Ruby 1.9.2, the json gem is bundled with the core Ruby distribution.&lt;br /&gt;
The JSON library can be installed using Ruby Gems[http://rubygems.org/] as shown below:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;# gem install json&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A JSON string for serialization can be created by using the JSON.generate method:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
        require 'json'&lt;br /&gt;
        my_hash = {:Welcome =&amp;gt; &amp;quot;Ruby&amp;quot;}&lt;br /&gt;
        puts JSON.generate(my_hash) =&amp;gt; &amp;quot;{\&amp;quot;WELCOME\&amp;quot;:\&amp;quot;RUBY\&amp;quot;}&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
'''Output:'''&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;{&amp;quot;{\&amp;quot;Welcome\&amp;quot;:\&amp;quot;Ruby\&amp;quot;}&amp;quot;=&amp;gt;&amp;quot;{\&amp;quot;WELCOME\&amp;quot;:\&amp;quot;RUBY\&amp;quot;}&amp;quot;}&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A JSON string received from another program can be parsed by using JSON.parse&lt;br /&gt;
Ruby thus converts String to Hash.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
        require 'json'&lt;br /&gt;
        my_hash = JSON.parse('{&amp;quot;Welcome&amp;quot;: &amp;quot;Ruby&amp;quot;}')&lt;br /&gt;
        puts my_hash[&amp;quot;Welcome&amp;quot;] =&amp;gt; &amp;quot;Ruby&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Converting Ruby Objects to Binary Formats ===&lt;br /&gt;
Binary Serialization is another form of serialization in Ruby which is not in human readable form. 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. Binary Serialization  is done using [http://www.ruby-doc.org/core-2.0.0/Marshal.html Marshal] which is built into Ruby and the code for it is written in Ruby's Marshal module(marshal.c) and thus no additional files are required in order to use it. The _dump and _load methods defined in marshal are used for serialization. Using marshal module the following types of objects can not be serialized: bindings, procedure objects, singleton objects, instances of IO objects and interfaces.&lt;br /&gt;
&lt;br /&gt;
Since the Binary Serialized data is not in human readable form, the following guidelines need to be followed.&lt;br /&gt;
     1.Use print instead of puts[http://ruby-doc.org/core-2.0.0/ARGF.html#] when serialized objects are written to a file in order to avoid new line characters to be written &lt;br /&gt;
       in the file.&lt;br /&gt;
     &lt;br /&gt;
     2.Use a record separator in order to differentiate between two objects.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Binary Serialization Example:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    class Animal&lt;br /&gt;
      def initialize  name, age&lt;br /&gt;
      @name = name&lt;br /&gt;
      @age=age&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
   class Cat &amp;lt; Animal&lt;br /&gt;
    def to_s&lt;br /&gt;
     &amp;quot;In Cat C: #{@name} \t #{@age}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
  c = Cat.new(&amp;quot;Kitty Kat&amp;quot;,5)&lt;br /&gt;
  puts &amp;quot;Before Serialization&amp;quot;&lt;br /&gt;
  puts c&lt;br /&gt;
  #puts d&lt;br /&gt;
  serialize_cat= Marshal.dump(c) #dumps the serialized cat object into serialize_cat&lt;br /&gt;
  puts &amp;quot;\nAfter Serialization:\n #{serialize_cat}&amp;quot;&lt;br /&gt;
  deserialize_cat= Marshal::load(serialize_cat) #deserializes the cat object and loads it back into deserialize_cat&lt;br /&gt;
  puts &amp;quot;\nAfter Deserialization\n #{deserialize_cat}&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   Before Serialization&lt;br /&gt;
   In Cat C: Kitty Kat 	 5&lt;br /&gt;
   After Serialization:&lt;br /&gt;
   oCat:&lt;br /&gt;
   @nameI&amp;quot;Kitty Kat:ET:	@agei&lt;br /&gt;
   After Deserialization&lt;br /&gt;
   In Cat C: Kitty Kat 	 5&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Similar to YAML, Marshal can also be used to dump data into a file. The above example showing the serialization using YAML::dump can be written using marshal as shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    f = File.open( 'first.yml', 'w' )&lt;br /&gt;
    Marshal.dump( [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;], f )&lt;br /&gt;
    f.close	&lt;br /&gt;
    File.open( 'first.yml' ){ |f|	&lt;br /&gt;
    $arr= Marshal.load(f)&lt;br /&gt;
    }	&lt;br /&gt;
    p( $arr )&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;]&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice that in the above example there is no &amp;quot;require&amp;quot; statement as opposed to the earlier example of writing serialized data into files using YAML. This is because unlike YAML, marshal is built-in Ruby and  no external libraries is required in order to use its functionality.&lt;br /&gt;
&lt;br /&gt;
Marshal can also be used to custom serialize an object i.e. it provides an option to omit the variables of an object that are not required in the serialized data. The following program shows the use of marshal_dump method for achieving custom serialization.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
      class First&lt;br /&gt;
      def initialize(name, age, country)&lt;br /&gt;
         @name = name&lt;br /&gt;
         @age = age&lt;br /&gt;
         @country=country&lt;br /&gt;
      end&lt;br /&gt;
      def to_s&lt;br /&gt;
            &amp;quot;In First: #{@name}, #{@age}, #{@country}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
      def marshal_dump&lt;br /&gt;
            [@name,@age]  #@country will not be serialized&lt;br /&gt;
      end&lt;br /&gt;
      def marshal_load(data)&lt;br /&gt;
       @name=data[0]&lt;br /&gt;
       @age=data[1]&lt;br /&gt;
       @country=&amp;quot;United States of America&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
      end&lt;br /&gt;
 &lt;br /&gt;
      x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
      puts x&lt;br /&gt;
  &lt;br /&gt;
      marshal_data = Marshal.dump( x ) &lt;br /&gt;
      y = Marshal.load( marshal_data ) &lt;br /&gt;
      p( y.to_s )&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    In First: Tom, 25, USA&lt;br /&gt;
    &amp;quot;In First: Tom, 25, United States of America&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== '''Serialization Performance in Ruby''' ==&lt;br /&gt;
The different serialization formats described above differ in the efficiency at which they can serialize and deserialize data and thus while serializing large amount of data their efficiency is taken into account. The .report method in Ruby can be used to evaluate the performance of these serialization patterns. Marshal, as it handles data in binary format, is the most efficient form of serialization in Ruby. The following example compares the performance of marshal, JSON and YAML format while processing a large array and a large hash.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   require 'benchmark'&lt;br /&gt;
   require 'rubygems'&lt;br /&gt;
   require 'json'&lt;br /&gt;
   require 'yaml'&lt;br /&gt;
   include Benchmark&lt;br /&gt;
   class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
      @name=name&lt;br /&gt;
      @age=age&lt;br /&gt;
      @country=country&lt;br /&gt;
     end&lt;br /&gt;
   end  &lt;br /&gt;
&lt;br /&gt;
    x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
   benchmark do |t|&lt;br /&gt;
    print &amp;quot;Marshal:&amp;quot;&lt;br /&gt;
    t.report{1000.times do; Marshal.load(Marshal.dump(x));end}&lt;br /&gt;
    print &amp;quot;JSON:&amp;quot;&lt;br /&gt;
    t.report{1000.times do; JSON.load(JSON.dump(x));end}&lt;br /&gt;
    print &amp;quot;YAML:&amp;quot;&lt;br /&gt;
    t.report{1000.times do; YAML.load(YAML.dump(x));end}&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   Marshal:   0.020000   0.000000   0.020000 (  0.015473)&lt;br /&gt;
   JSON:   0.020000   0.000000   0.020000 (  0.023934)&lt;br /&gt;
   YAML:   0.460000   0.010000   0.470000 (  0.476826)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output of the above example shows that, given large chunks of data, marshal is comparatively more efficient format for serialization in Ruby.&lt;br /&gt;
&lt;br /&gt;
== Serialization in OOLS Languages: Comparison ==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Sl.No !! Ruby !! Java !! .Net Framework !! C++&lt;br /&gt;
|-&lt;br /&gt;
| 1 || Ruby provides a built in 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 || Although, there is no built in support for serialization in C++, it can be achieved by using Boost libraries[http://www.boost.org/doc/libs/1_36_0/libs/serialization/example/demo.cpp]&lt;br /&gt;
|-&lt;br /&gt;
| 2 || The built in module of Ruby [http://www.ruby-doc.org/core-2.0.0/Marshal.html (Marshal)] does not support platform independence, however, it can be achieved by using external libraries like [http://yaml.org YAML] and [http://www.w3schools.com/json/ JSON] || Similarly, Java's built in serialization is also not platform independent and in order to use serialization in Java across Ruby platform, jruby library should be used.  || .Net used Remoting technology to make it platform independent. || Serialization using the Boost libraries is not platform independent.&lt;br /&gt;
|-&lt;br /&gt;
| 3 || YAML provides a method [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/syck/rdoc/Object.html (to_yaml_properties)] which can be used to select the variables who's value is need to be serialized. With Marshal, we need to write a method named marshal_dump defining the variables of an object that has to be serialized. || Provides an option for serializing only the required 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 data 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 || Serialization using the Boost libraries is custom and thus the user can specify the part of the objects to be serialized.&lt;br /&gt;
|-&lt;br /&gt;
| 4 || Bindings, procedure objects, singleton objects, instances of IO objects and interfaces can not be serialized. Serializing these objects throws TypeError exceptions. || Thread, OutputStream and its subclasses, and Socket are not serializable in Java.[http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html] || Objects like DataRow are non serializable in .NET. Whether an object is serializable or not can be checked using the [http://msdn.microsoft.com/en-us/library/system.type.isserializable(v=vs.110).aspx?cs-save-lang=1&amp;amp;cs-lang=cpp#code-snippet-1 Type.IsSerializable] || There are no such objects in C++.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== See Also==&lt;br /&gt;
&lt;br /&gt;
1. [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/json/rdoc/JSON.html Serialization in Ruby JSON]&lt;br /&gt;
&lt;br /&gt;
2. [http://api.rubyonrails.org/classes/ActiveModel/Serialization.html Serialization in Rails]&lt;br /&gt;
&lt;br /&gt;
3. [http://gregmoreno.wordpress.com/2011/01/27/preventing-model-explosion-via-rails-serialization/ Article on Rails Serialization]&lt;br /&gt;
&lt;br /&gt;
4. [http://en.wikipedia.org/wiki/Google_Protocol_Buffers Protocol Buffers]&lt;br /&gt;
&lt;br /&gt;
5. [https://rubygems.org/gems/ruby-protocol-buffers Ruby Protocol Buffers Gem]&lt;br /&gt;
&lt;br /&gt;
6. [http://en.wikipedia.org/wiki/Apache_Avro Avro]&lt;br /&gt;
&lt;br /&gt;
7. [http://rubygems.org/gems/avro Ruby Avro Gem]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
1. [http://en.wikipedia.org/wiki/Serialization Serilization in General]&lt;br /&gt;
&lt;br /&gt;
2. [http://en.wikipedia.org/wiki/Marshalling_%28computer_science%29 Marshaling]&lt;br /&gt;
&lt;br /&gt;
3. [http://techbrahmana.blogspot.com/2012/03/rails-cookie-handling-serialization-and.html Rails Cookie Handling]&lt;br /&gt;
&lt;br /&gt;
4. [http://yaml.org/ YAML]&lt;br /&gt;
&lt;br /&gt;
5. [http://www.ruby-doc.org/core-2.1.0/Marshal.html Marshal module]&lt;br /&gt;
&lt;br /&gt;
6. [http://json.org/ JSON]&lt;br /&gt;
&lt;br /&gt;
7. [http://rubygems.org/ Ruby Gems]&lt;br /&gt;
&lt;br /&gt;
8. [http://ruby-doc.org/core-2.0.0/ARGF.html# Ruby ARGF Documentation]&lt;br /&gt;
&lt;br /&gt;
9. [http://www.boost.org/doc/libs/1_36_0/libs/serialization/example/demo.cpp Boost Libraries]&lt;br /&gt;
&lt;br /&gt;
10. [http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html Java Serialization]&lt;br /&gt;
&lt;br /&gt;
11. [http://www.w3schools.com/json/ JSON Tutorial]&lt;br /&gt;
&lt;br /&gt;
12. [http://www.skorks.com/2010/04/serializing-and-deserializing-objects-with-ruby/ Serializing and De-serializing in Ruby]&lt;br /&gt;
&lt;br /&gt;
13. [http://www.codeproject.com/Articles/33296/Serialization-and-De-serialization Serialization and De-serialization]&lt;br /&gt;
&lt;br /&gt;
14. [http://msdn.microsoft.com/en-us/library/182eeyhh.aspx XML Serialization]&lt;br /&gt;
&lt;br /&gt;
15. [http://www.tutorialspoint.com/java/java_serialization.htm Java Serialization Tutorial]&lt;br /&gt;
&lt;br /&gt;
16. [http://www.sapphiresteel.com/ruby-programming/The-Book-Of-Ruby The Book of Ruby]&lt;br /&gt;
&lt;br /&gt;
17. [http://www.codeproject.com/Articles/20962/Introducing-Serialization-in-NET Serialization in .Net]&lt;br /&gt;
&lt;br /&gt;
18. [http://ruby.about.com/od/advancedruby/ss/Serialization-In-Ruby-Yaml.html Serialization in Ruby YAML]&lt;br /&gt;
&lt;br /&gt;
19. [http://ruby.about.com/od/tasks/a/The-Json-Gem.html Installing JSON gem]&lt;br /&gt;
&lt;br /&gt;
20. [http://www.ruby-doc.org/stdlib-2.0.0/libdoc/json/rdoc/JSON.html Serialization in Ruby JSON]&lt;/div&gt;</summary>
		<author><name>Mmehta2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1_1w1d_mm&amp;diff=83265</id>
		<title>CSC/ECE 517 Spring 2014/ch1 1w1d mm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1_1w1d_mm&amp;diff=83265"/>
		<updated>2014-02-11T03:26:52Z</updated>

		<summary type="html">&lt;p&gt;Mmehta2: /* Serialization Performance in Ruby */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== '''Serialization''' ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Serialization'''[http://en.wikipedia.org/wiki/Serialization] is a process of converting a data structure or an object into a stream of bytes or string to facilitate storage in memory, file(persistence storage) or transmission over a network. The process of Serialization is also referred to as ''Marshalling''[http://en.wikipedia.org/wiki/Marshalling_%28computer_science%29]. 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. &lt;br /&gt;
&lt;br /&gt;
'''Basic Advantages of Serialization''':&lt;br /&gt;
&lt;br /&gt;
1. Communication between two or more processes on same machine. Object state can be saved and shared in a persistent or in-memory store.&lt;br /&gt;
&lt;br /&gt;
2. Communication between processes on different machines. Serialization facilitates the transmission of an object through a network.&lt;br /&gt;
&lt;br /&gt;
3. Creating a clone of an object. &lt;br /&gt;
&lt;br /&gt;
4. Cross-platform compatibility. Object can be serialized in a common format that is understood by multiple platforms. Eg. JSON, XML.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''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''.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Few Practical Applications for Serialization'''&lt;br /&gt;
&lt;br /&gt;
1. HTTP Session Replication by sharing session objects across web servers for handling failover scenarios&lt;br /&gt;
&lt;br /&gt;
2. Serialization facilitates communication in Remote Method Invocation or Remote procedure calls &lt;br /&gt;
&lt;br /&gt;
3. Rails Cookie Handling[http://techbrahmana.blogspot.com/2012/03/rails-cookie-handling-serialization-and.html]. Cookies are stored marshalled/unmarshalled to and from client machines. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:SerializationChart.jpg]]&lt;br /&gt;
&lt;br /&gt;
== '''Serialization in Ruby:''' ==&lt;br /&gt;
&lt;br /&gt;
Ruby provides serialization capabilities through its module, [http://www.ruby-doc.org/core-2.0.0/Marshal.html Marshal]. There are also other libraries like YAML and JSON which can be used in Ruby to generate serialized objects for purposes like platform independence and human readable formats.&lt;br /&gt;
&lt;br /&gt;
== '''Types of Serialization''' ==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
=== Converting Ruby Objects in Human Readable Formats ===&lt;br /&gt;
The conversion of Ruby objects into YAML and JSON formats are explained below.&lt;br /&gt;
&lt;br /&gt;
==== Converting Ruby Objects to YAML format ====&lt;br /&gt;
&lt;br /&gt;
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. 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. In order to use it in Ruby, the yaml.rb file is required to be loaded which provides methods for converting objects into yaml format and creating .yml files.&lt;br /&gt;
&lt;br /&gt;
'''Examples of serialization using YAML:'''&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;nowiki&amp;gt; #Serialization using YAML's to_yaml method&lt;br /&gt;
 &lt;br /&gt;
  require &amp;quot;yaml&amp;quot;&lt;br /&gt;
     class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
	@name = name&lt;br /&gt;
	@age = age&lt;br /&gt;
	@country=country&lt;br /&gt;
     end&lt;br /&gt;
      def to_s&lt;br /&gt;
	&amp;quot;In First:\n#{@name}, #{@age}, #{@country}\n&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
  x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
  puts x&lt;br /&gt;
  puts x.to_yaml&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   In First:&lt;br /&gt;
   Tom, 25, USA&lt;br /&gt;
   --- !ruby/object:First&lt;br /&gt;
   name: Tom&lt;br /&gt;
   age: 25&lt;br /&gt;
   country: USA&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The above code displays the object x, first as a string and then in the yaml format. &lt;br /&gt;
&lt;br /&gt;
'''Saving YAML data into a file:'''&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt; # Serialization using YAML::dump&lt;br /&gt;
&lt;br /&gt;
require 'yaml'&lt;br /&gt;
&lt;br /&gt;
f = File.open( 'first.yml', 'w' )&lt;br /&gt;
YAML.dump( [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;], f )&lt;br /&gt;
f.close&lt;br /&gt;
		&lt;br /&gt;
File.open( 'first.yml' ){ |f|	&lt;br /&gt;
    $arr= YAML.load(f)&lt;br /&gt;
}	&lt;br /&gt;
&lt;br /&gt;
p( $arr )&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;]&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The dump function can be used to serialize the data and save it into a file in YAML format. As shown in the above example the data in YAML format can be de-serialized using the load function.&lt;br /&gt;
&lt;br /&gt;
YAML libraries also provides an option of selecting only those variables of the object that are needed to be serialized. This is done using the to_yaml_properties method as shown in the below example.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;nowiki&amp;gt;  #Custom Serialization using YAML&lt;br /&gt;
 &lt;br /&gt;
  require &amp;quot;yaml&amp;quot;&lt;br /&gt;
     class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
	@name = name&lt;br /&gt;
	@age = age&lt;br /&gt;
	@country=country&lt;br /&gt;
     end&lt;br /&gt;
     def to_s&lt;br /&gt;
	&amp;quot;In First:\n#{@name}, #{@age}, #{@country}\n&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
      def to_yaml_properties&lt;br /&gt;
	   [&amp;quot;@name&amp;quot;,&amp;quot;@age&amp;quot;]  #@country will not be serialized&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
  x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
  puts x&lt;br /&gt;
  puts x.to_yaml&lt;br /&gt;
  &amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   In First:&lt;br /&gt;
   Tom, 25, USA&lt;br /&gt;
   --- !ruby/object:First&lt;br /&gt;
   name: Tom&lt;br /&gt;
   age: 25&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Converting Ruby Objects to JSON format: ====&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
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. Ruby 1.8.7 distribution is not bundled with json gem. However, in Ruby 1.9.2, the json gem is bundled with the core Ruby distribution.&lt;br /&gt;
The JSON library can be installed using Ruby Gems[http://rubygems.org/] as shown below:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;# gem install json&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A JSON string for serialization can be created by using the JSON.generate method:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
        require 'json'&lt;br /&gt;
        my_hash = {:Welcome =&amp;gt; &amp;quot;Ruby&amp;quot;}&lt;br /&gt;
        puts JSON.generate(my_hash) =&amp;gt; &amp;quot;{\&amp;quot;WELCOME\&amp;quot;:\&amp;quot;RUBY\&amp;quot;}&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
'''Output:'''&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;{&amp;quot;{\&amp;quot;Welcome\&amp;quot;:\&amp;quot;Ruby\&amp;quot;}&amp;quot;=&amp;gt;&amp;quot;{\&amp;quot;WELCOME\&amp;quot;:\&amp;quot;RUBY\&amp;quot;}&amp;quot;}&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A JSON string received from another program can be parsed by using JSON.parse&lt;br /&gt;
Ruby thus converts String to Hash.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
        require 'json'&lt;br /&gt;
        my_hash = JSON.parse('{&amp;quot;Welcome&amp;quot;: &amp;quot;Ruby&amp;quot;}')&lt;br /&gt;
        puts my_hash[&amp;quot;Welcome&amp;quot;] =&amp;gt; &amp;quot;Ruby&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Converting Ruby Objects to Binary Formats ===&lt;br /&gt;
Binary Serialization is another form of serialization in Ruby which is not in human readable form. 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. Binary Serialization  is done using [http://www.ruby-doc.org/core-2.0.0/Marshal.html Marshal] which is built into Ruby and the code for it is written in Ruby's Marshal module(marshal.c) and thus no additional files are required in order to use it. The _dump and _load methods defined in marshal are used for serialization. Using marshal module the following types of objects can not be serialized: bindings, procedure objects, singleton objects, instances of IO objects and interfaces.&lt;br /&gt;
&lt;br /&gt;
Since the Binary Serialized data is not in human readable form, the following guidelines need to be followed.&lt;br /&gt;
     1.Use print instead of puts[http://ruby-doc.org/core-2.0.0/ARGF.html#] when serialized objects are written to a file in order to avoid new line characters to be written &lt;br /&gt;
       in the file.&lt;br /&gt;
     &lt;br /&gt;
     2.Use a record separator in order to differentiate between two objects.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Binary Serialization Example:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    class Animal&lt;br /&gt;
      def initialize  name, age&lt;br /&gt;
      @name = name&lt;br /&gt;
      @age=age&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
   class Cat &amp;lt; Animal&lt;br /&gt;
    def to_s&lt;br /&gt;
     &amp;quot;In Cat C: #{@name} \t #{@age}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
  c = Cat.new(&amp;quot;Kitty Kat&amp;quot;,5)&lt;br /&gt;
  puts &amp;quot;Before Serialization&amp;quot;&lt;br /&gt;
  puts c&lt;br /&gt;
  #puts d&lt;br /&gt;
  serialize_cat= Marshal.dump(c) #dumps the serialized cat object into serialize_cat&lt;br /&gt;
  puts &amp;quot;\nAfter Serialization:\n #{serialize_cat}&amp;quot;&lt;br /&gt;
  deserialize_cat= Marshal::load(serialize_cat) #deserializes the cat object and loads it back into deserialize_cat&lt;br /&gt;
  puts &amp;quot;\nAfter Deserialization\n #{deserialize_cat}&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   Before Serialization&lt;br /&gt;
   In Cat C: Kitty Kat 	 5&lt;br /&gt;
   After Serialization:&lt;br /&gt;
   oCat:&lt;br /&gt;
   @nameI&amp;quot;Kitty Kat:ET:	@agei&lt;br /&gt;
   After Deserialization&lt;br /&gt;
   In Cat C: Kitty Kat 	 5&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Similar to YAML, Marshal can also be used to dump data into a file. The above example showing the serialization using YAML::dump can be written using marshal as shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    f = File.open( 'first.yml', 'w' )&lt;br /&gt;
    Marshal.dump( [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;], f )&lt;br /&gt;
    f.close	&lt;br /&gt;
    File.open( 'first.yml' ){ |f|	&lt;br /&gt;
    $arr= Marshal.load(f)&lt;br /&gt;
    }	&lt;br /&gt;
    p( $arr )&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
'''Output''':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   [&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;]&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice that in the above example there is no &amp;quot;require&amp;quot; statement as opposed to the earlier example of writing serialized data into files using YAML. This is because unlike YAML, marshal is built-in Ruby and  no external libraries is required in order to use its functionality.&lt;br /&gt;
&lt;br /&gt;
Marshal can also be used to custom serialize an object i.e. it provides an option to omit the variables of an object that are not required in the serialized data. The following program shows the use of marshal_dump method for achieving custom serialization.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
      class First&lt;br /&gt;
      def initialize(name, age, country)&lt;br /&gt;
         @name = name&lt;br /&gt;
         @age = age&lt;br /&gt;
         @country=country&lt;br /&gt;
      end&lt;br /&gt;
      def to_s&lt;br /&gt;
            &amp;quot;In First: #{@name}, #{@age}, #{@country}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
      def marshal_dump&lt;br /&gt;
            [@name,@age]  #@country will not be serialized&lt;br /&gt;
      end&lt;br /&gt;
      def marshal_load(data)&lt;br /&gt;
       @name=data[0]&lt;br /&gt;
       @age=data[1]&lt;br /&gt;
       @country=&amp;quot;United States of America&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
      end&lt;br /&gt;
 &lt;br /&gt;
      x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
      puts x&lt;br /&gt;
  &lt;br /&gt;
      marshal_data = Marshal.dump( x ) &lt;br /&gt;
      y = Marshal.load( marshal_data ) &lt;br /&gt;
      p( y.to_s )&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    In First: Tom, 25, USA&lt;br /&gt;
    &amp;quot;In First: Tom, 25, United States of America&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== '''Serialization Performance in Ruby''' ==&lt;br /&gt;
The different serialization formats described above differ in the efficiency at which they can serialize and deserialize data and thus while serializing large amount of data their efficiency is taken into account. The .report method in Ruby can be used to evaluate the performance of these serialization patterns. Marshal, as it handles data in binary format, is the most efficient form of serialization in Ruby. The following example compares the performance of marshal, JSON and YAML format while processing a large array and a large hash.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   require 'benchmark'&lt;br /&gt;
   require 'rubygems'&lt;br /&gt;
   require 'json'&lt;br /&gt;
   require 'yaml'&lt;br /&gt;
   include Benchmark&lt;br /&gt;
   class First&lt;br /&gt;
     def initialize(name, age, country)&lt;br /&gt;
      @name=name&lt;br /&gt;
      @age=age&lt;br /&gt;
      @country=country&lt;br /&gt;
     end&lt;br /&gt;
   end  &lt;br /&gt;
&lt;br /&gt;
   x = First.new(&amp;quot;Tom&amp;quot;, 25, &amp;quot;USA&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
   benchmark do |t|&lt;br /&gt;
    print &amp;quot;Marshal:&amp;quot;&lt;br /&gt;
    t.report{1000.times do; Marshal.load(Marshal.dump(x));end}&lt;br /&gt;
    print &amp;quot;JSON:&amp;quot;&lt;br /&gt;
    t.report{1000.times do; JSON.load(JSON.dump(x));end}&lt;br /&gt;
    print &amp;quot;YAML:&amp;quot;&lt;br /&gt;
    t.report{1000.times do; YAML.load(YAML.dump(x));end}&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Output:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
   Marshal:   0.020000   0.000000   0.020000 (  0.015473)&lt;br /&gt;
   JSON:   0.020000   0.000000   0.020000 (  0.023934)&lt;br /&gt;
   YAML:   0.460000   0.010000   0.470000 (  0.476826)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output of the above example shows that, given large chunks of data, marshal is comparatively more efficient format for serialization in Ruby.&lt;br /&gt;
&lt;br /&gt;
== Serialization in OOLS Languages: Comparison ==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Sl.No !! Ruby !! Java !! .Net Framework !! C++&lt;br /&gt;
|-&lt;br /&gt;
| 1 || Ruby provides a built in 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 || Although, there is no built in support for serialization in C++, it can be achieved by using Boost libraries[http://www.boost.org/doc/libs/1_36_0/libs/serialization/example/demo.cpp]&lt;br /&gt;
|-&lt;br /&gt;
| 2 || The built in module of Ruby [http://www.ruby-doc.org/core-2.0.0/Marshal.html (Marshal)] does not support platform independence, however, it can be achieved by using external libraries like [http://yaml.org YAML] and [http://www.w3schools.com/json/ JSON] || Similarly, Java's built in serialization is also not platform independent and in order to use serialization in Java across Ruby platform, jruby library should be used.  || .Net used Remoting technology to make it platform independent. || Serialization using the Boost libraries is not platform independent.&lt;br /&gt;
|-&lt;br /&gt;
| 3 || YAML provides a method [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/syck/rdoc/Object.html (to_yaml_properties)] which can be used to select the variables who's value is need to be serialized. With Marshal, we need to write a method named marshal_dump defining the variables of an object that has to be serialized. || Provides an option for serializing only the required 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 data 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 || Serialization using the Boost libraries is custom and thus the user can specify the part of the objects to be serialized.&lt;br /&gt;
|-&lt;br /&gt;
| 4 || Bindings, procedure objects, singleton objects, instances of IO objects and interfaces can not be serialized. Serializing these objects throws TypeError exceptions. || Thread, OutputStream and its subclasses, and Socket are not serializable in Java.[http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html] || Objects like DataRow are non serializable in .NET. Whether an object is serializable or not can be checked using the [http://msdn.microsoft.com/en-us/library/system.type.isserializable(v=vs.110).aspx?cs-save-lang=1&amp;amp;cs-lang=cpp#code-snippet-1 Type.IsSerializable] || There are no such objects in C++.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== See Also==&lt;br /&gt;
&lt;br /&gt;
1. [http://www.ruby-doc.org/stdlib-1.9.3/libdoc/json/rdoc/JSON.html Serialization in Ruby JSON]&lt;br /&gt;
&lt;br /&gt;
2. [http://api.rubyonrails.org/classes/ActiveModel/Serialization.html Serialization in Rails]&lt;br /&gt;
&lt;br /&gt;
3. [http://gregmoreno.wordpress.com/2011/01/27/preventing-model-explosion-via-rails-serialization/ Article on Rails Serialization]&lt;br /&gt;
&lt;br /&gt;
4. [http://en.wikipedia.org/wiki/Google_Protocol_Buffers Protocol Buffers]&lt;br /&gt;
&lt;br /&gt;
5. [https://rubygems.org/gems/ruby-protocol-buffers Ruby Protocol Buffers Gem]&lt;br /&gt;
&lt;br /&gt;
6. [http://en.wikipedia.org/wiki/Apache_Avro Avro]&lt;br /&gt;
&lt;br /&gt;
7. [http://rubygems.org/gems/avro Ruby Avro Gem]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
1. [http://en.wikipedia.org/wiki/Serialization Serilization in General]&lt;br /&gt;
&lt;br /&gt;
2. [http://en.wikipedia.org/wiki/Marshalling_%28computer_science%29 Marshaling]&lt;br /&gt;
&lt;br /&gt;
3. [http://techbrahmana.blogspot.com/2012/03/rails-cookie-handling-serialization-and.html Rails Cookie Handling]&lt;br /&gt;
&lt;br /&gt;
4. [http://yaml.org/ YAML]&lt;br /&gt;
&lt;br /&gt;
5. [http://www.ruby-doc.org/core-2.1.0/Marshal.html Marshal module]&lt;br /&gt;
&lt;br /&gt;
6. [http://json.org/ JSON]&lt;br /&gt;
&lt;br /&gt;
7. [http://rubygems.org/ Ruby Gems]&lt;br /&gt;
&lt;br /&gt;
8. [http://ruby-doc.org/core-2.0.0/ARGF.html# Ruby ARGF Documentation]&lt;br /&gt;
&lt;br /&gt;
9. [http://www.boost.org/doc/libs/1_36_0/libs/serialization/example/demo.cpp Boost Libraries]&lt;br /&gt;
&lt;br /&gt;
10. [http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html Java Serialization]&lt;br /&gt;
&lt;br /&gt;
11. [http://www.w3schools.com/json/ JSON Tutorial]&lt;br /&gt;
&lt;br /&gt;
12. [http://www.skorks.com/2010/04/serializing-and-deserializing-objects-with-ruby/ Serializing and De-serializing in Ruby]&lt;br /&gt;
&lt;br /&gt;
13. [http://www.codeproject.com/Articles/33296/Serialization-and-De-serialization Serialization and De-serialization]&lt;br /&gt;
&lt;br /&gt;
14. [http://msdn.microsoft.com/en-us/library/182eeyhh.aspx XML Serialization]&lt;br /&gt;
&lt;br /&gt;
15. [http://www.tutorialspoint.com/java/java_serialization.htm Java Serialization Tutorial]&lt;br /&gt;
&lt;br /&gt;
16. [http://www.sapphiresteel.com/ruby-programming/The-Book-Of-Ruby The Book of Ruby]&lt;br /&gt;
&lt;br /&gt;
17. [http://www.codeproject.com/Articles/20962/Introducing-Serialization-in-NET Serialization in .Net]&lt;br /&gt;
&lt;br /&gt;
18. [http://ruby.about.com/od/advancedruby/ss/Serialization-In-Ruby-Yaml.html Serialization in Ruby YAML]&lt;br /&gt;
&lt;br /&gt;
19. [http://ruby.about.com/od/tasks/a/The-Json-Gem.html Installing JSON gem]&lt;br /&gt;
&lt;br /&gt;
20. [http://www.ruby-doc.org/stdlib-2.0.0/libdoc/json/rdoc/JSON.html Serialization in Ruby JSON]&lt;/div&gt;</summary>
		<author><name>Mmehta2</name></author>
	</entry>
</feed>