Welcome!

Java IoT Authors: Stackify Blog, Derek Weeks, Liz McMillan, Elizabeth White, Pat Romanski

Related Topics: Java IoT, Machine Learning , Apache

Java IoT: Blog Post

Java Serialization without Serializable Interface

As you know in case of Core Java to serialize an object it is mandatory to implement Serializable interface and serialVersionUID

The title of this article seems unpleasant as it talks about serializing java object graph without the use of Serializable interface. It is true in case of Core Java. We know that to serialize a java bean we have to implement Serializable interface ( let us not consider the Externalizable interface ).In this article I will show how it is possible to serialize a java object without implementing Serializable interface using a library from a famous organisation called Jboss. In this small post I will make you familiar with “Jboss Serialization”.

Technicalities
As you know in case of Core Java to serialize an object it is mandatory to implement Serializable interface and serialVersionUID( optional ). There are certain situations in the project development where we want to serialize the object but that particular object does not implement Serializable interface and we may not have access to the source code. In this case I am not saying that it is impossible to we will find it difficult to serialize the object graph. But the introduction of Jboss Serialization API made it very simple to serialize an object irrespective of the serialization specification.  To use “Jboss Serialization” you have to download the zip file available in jboss site. The link for download is given below in the references section. You have to note that jboss serialization api depends upon two other libraries called “log4j.jar” and “trove.jar”. You have to put the two jar files in the classpath along with “jboss-serialization.jar”. If you are using normal java project you should have three jar files in your library as mentioned below.

log4j.jar

trove.jar

jboss-serialization.jar

If you are using Maven, you have to use the following dependency as mentioned below.

<dependency>

<groupId>jboss</groupId>

<artifactId>jboss-serialization</artifactId>

<version>4.2.2.GA</version>

</dependency>

How to do

Let us consider a very small example of java Object graph ie Organisation object contains Employee object which has name and id as fields. The code is given below.

package com.ddlab.jboss.serilization;

/**

* This is a plain java class with the fields name and id.

*

* @author <a href="mailto:debadatta.mishra@gmail.com"> Debadatta Mishra (PIKU)

* @Since Jan 2013

*/

public class Employee {

private String name;

private String id;

// ~~ Getter and Setter Methods below

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getId() {

return id;

}

public void setId(String id) {

this.id = id;

}

}

 

 

 

 

package com.ddlab.jboss.serilization;

/**

* This is a plain java class with the field type [email protected] Employee}.

*

* @author <a href="mailto:debadatta.mishra@gmail.com"> Debadatta Mishra (PIKU)

* @Since Jan 2013

*/

public class Organisation {

private Employee emp;

public Employee getEmp() {

return emp;

}

public void setEmp(Employee emp) {

this.emp = emp;

}

}

In the above case you can mark that none of the classes implement Serializable interface. Ok, that’s great. To use jboss serialization and deserialization only replace “ObjectOutputStream” to “JBossObjectOutputStream” and “ObjectInputStream” to “JbossObjectInputStream”. Let us see concrete example code below.

package com.ddlab.jboss.serilization;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import org.jboss.serial.io.JBossObjectInputStream;

import org.jboss.serial.io.JBossObjectOutputStream;

/**

* This is a testharness class used to serialize and deserialize the object.

*

* @author <a href="mailto:debadatta.mishra@gmail.com"> Debadatta Mishra (PIKU)

* @Since Jan 2013

*/

public class TestJbossSerilization {

public static void storeObject(Organisation org, String filePath) {

JBossObjectOutputStream objOut = null;

OutputStream out = null;

File file = new File(filePath);

try {

out = new FileOutputStream(file);

} catch (FileNotFoundException e) {

e.printStackTrace();

}

try {

objOut = new JBossObjectOutputStream(out);

objOut.writeObject(org);

} catch (IOException e) {

e.printStackTrace();

} finally {

try {

if (objOut != null)

objOut.close();

if (out != null)

out.close();

} catch (IOException e) {

e.printStackTrace();

}

}

System.out

.println("Object Organisation is serialized successfully in the path "

+ file.getAbsolutePath());

}

public static void retrieveObject(String filePath) {

InputStream inStream = null;

try {

inStream = new FileInputStream(filePath);

} catch (FileNotFoundException e) {

e.printStackTrace();

}

JBossObjectInputStream objIn = null;

try {

objIn = new JBossObjectInputStream(inStream);

} catch (IOException e) {

e.printStackTrace();

}

Organisation org;

try {

org = (Organisation) objIn.readObject();

System.out.println("Employee Name : " + org.getEmp().getName());

System.out.println("Employee Id : " + org.getEmp().getId());

} catch (IOException e) {

e.printStackTrace();

} catch (ClassNotFoundException e) {

e.printStackTrace();

} finally {

try {

if (objIn != null)

objIn.close();

if (inStream != null)

inStream.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

public static void main(String[] args) throws Exception {

String filePath = "data/org.ser";

Organisation org = new Organisation();

Employee emp = new Employee();

emp.setId("111");

emp.setName("Deba");

org.setEmp(emp);

/*

* Serialize the object graph

*/

storeObject(org, filePath);

/*

* De-serialize the object graph

*/

retrieveObject(filePath);

}

}

You can download the complete project from the following link.

https://www.dropbox.com/s/drkl2rqxizt1x3a/jbossserialization.zip

So we have achieved serialization of java object graph without implementing Serializable interface. There are certain benefits of using it.

  1. There is no need to implement Serializable interface.
  2. No need to define serialVersionUID.
  3. Since it uses the concept of smart cloning, the process of serialization if 10 times faster.

Conclusion

I hope you will enjoy my article on serialization. You can download and learn for your personal use only. For any queries contact me on [email protected].

References

http://www.jboss.org/serialization

http://www.jboss.org/file-access/default/members/serialization/downloads/jboss-serialization-1.0.3.GA.zip

http://mvnrepository.com/artifact/jboss/jboss-serialization/4.2.2.GA

http://en.wikipedia.org/wiki/Serialization

More Stories By Debadatta Mishra

Debadatta Mishra is a senior Java developer with five years of experience in the field of Java and related technologies. He has written many articles on Java-related technologies on the Internet.

@ThingsExpo Stories
When shopping for a new data processing platform for IoT solutions, many development teams want to be able to test-drive options before making a choice. Yet when evaluating an IoT solution, it’s simply not feasible to do so at scale with physical devices. Building a sensor simulator is the next best choice; however, generating a realistic simulation at very high TPS with ease of configurability is a formidable challenge. When dealing with multiple application or transport protocols, you would be...
SYS-CON Events announced today that Datera, that offers a radically new data management architecture, has been named "Exhibitor" of SYS-CON's 21st International Cloud Expo ®, which will take place on Oct 31 - Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Datera is transforming the traditional datacenter model through modern cloud simplicity. The technology industry is at another major inflection point. The rise of mobile, the Internet of Things, data storage and Big...
SYS-CON Events announced today that GrapeUp, the leading provider of rapid product development at the speed of business, will exhibit at SYS-CON's 21st International Cloud Expo®, which will take place October 31-November 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Grape Up is a software company, specialized in cloud native application development and professional services related to Cloud Foundry PaaS. With five expert teams that operate in various sectors of the market acr...
In the enterprise today, connected IoT devices are everywhere – both inside and outside corporate environments. The need to identify, manage, control and secure a quickly growing web of connections and outside devices is making the already challenging task of security even more important, and onerous. In his session at @ThingsExpo, Rich Boyer, CISO and Chief Architect for Security at NTT i3, discussed new ways of thinking and the approaches needed to address the emerging challenges of security i...
SYS-CON Events announced today that CA Technologies has been named "Platinum Sponsor" of SYS-CON's 21st International Cloud Expo®, which will take place October 31-November 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. CA Technologies helps customers succeed in a future where every business - from apparel to energy - is being rewritten by software. From planning to development to management to security, CA creates software that fuels transformation for companies in the applic...
In his opening keynote at 20th Cloud Expo, Michael Maximilien, Research Scientist, Architect, and Engineer at IBM, discussed the full potential of the cloud and social data requires artificial intelligence. By mixing Cloud Foundry and the rich set of Watson services, IBM's Bluemix is the best cloud operating system for enterprises today, providing rapid development and deployment of applications that can take advantage of the rich catalog of Watson services to help drive insights from the vast t...
There is only one world-class Cloud event on earth, and that is Cloud Expo – which returns to Silicon Valley for the 21st Cloud Expo at the Santa Clara Convention Center, October 31 - November 2, 2017. Every Global 2000 enterprise in the world is now integrating cloud computing in some form into its IT development and operations. Midsize and small businesses are also migrating to the cloud in increasing numbers. Companies are each developing their unique mix of cloud technologies and service...
WebRTC is great technology to build your own communication tools. It will be even more exciting experience it with advanced devices, such as a 360 Camera, 360 microphone, and a depth sensor camera. In his session at @ThingsExpo, Masashi Ganeko, a manager at INFOCOM Corporation, will introduce two experimental projects from his team and what they learned from them. "Shotoku Tamago" uses the robot audition software HARK to track speakers in 360 video of a remote party. "Virtual Teleport" uses a...
Recently, IoT seems emerging as a solution vehicle for data analytics on real-world scenarios from setting a room temperature setting to predicting a component failure of an aircraft. Compared with developing an application or deploying a cloud service, is an IoT solution unique? If so, how? How does a typical IoT solution architecture consist? And what are the essential components and how are they relevant to each other? How does the security play out? What are the best practices in formulating...
Internet of @ThingsExpo, taking place October 31 - November 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA, is co-located with 21st Cloud Expo and will feature technical sessions from a rock star conference faculty and the leading industry players in the world. The Internet of Things (IoT) is the most profound change in personal and enterprise IT since the creation of the Worldwide Web more than 20 years ago. All major researchers estimate there will be tens of billions devic...
An increasing number of companies are creating products that combine data with analytical capabilities. Running interactive queries on Big Data requires complex architectures to store and query data effectively, typically involving data streams, an choosing efficient file format/database and multiple independent systems that are tied together through custom-engineered pipelines. In his session at @BigDataExpo at @ThingsExpo, Tomer Levi, a senior software engineer at Intel’s Advanced Analytics ...
In his session at @ThingsExpo, Arvind Radhakrishnen discussed how IoT offers new business models in banking and financial services organizations with the capability to revolutionize products, payments, channels, business processes and asset management built on strong architectural foundation. The following topics were covered: How IoT stands to impact various business parameters including customer experience, cost and risk management within BFS organizations.
SYS-CON Events announced today that Elastifile will exhibit at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 - Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Elastifile Cloud File System (ECFS) is software-defined data infrastructure designed for seamless and efficient management of dynamic workloads across heterogeneous environments. Elastifile provides the architecture needed to optimize your hybrid cloud environment, by facilitating efficient...
SYS-CON Events announced today that Golden Gate University will exhibit at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Since 1901, non-profit Golden Gate University (GGU) has been helping adults achieve their professional goals by providing high quality, practice-based undergraduate and graduate educational programs in law, taxation, business and related professions. Many of its courses are taug...
"We provide IoT solutions. We provide the most compatible solutions for many applications. Our solutions are industry agnostic and also protocol agnostic," explained Richard Han, Head of Sales and Marketing and Engineering at Systena America, in this SYS-CON.tv interview at @ThingsExpo, held June 6-8, 2017, at the Javits Center in New York City, NY.
SYS-CON Events announced today that DXWorldExpo has been named “Global Sponsor” of SYS-CON's 21st International Cloud Expo, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Digital Transformation is the key issue driving the global enterprise IT business. Digital Transformation is most prominent among Global 2000 enterprises and government institutions.
21st International Cloud Expo, taking place October 31 - November 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA, will feature technical sessions from a rock star conference faculty and the leading industry players in the world. Cloud computing is now being embraced by a majority of enterprises of all sizes. Yesterday's debate about public vs. private has transformed into the reality of hybrid cloud: a recent survey shows that 74% of enterprises have a hybrid cloud strategy. Me...
Recently, WebRTC has a lot of eyes from market. The use cases of WebRTC are expanding - video chat, online education, online health care etc. Not only for human-to-human communication, but also IoT use cases such as machine to human use cases can be seen recently. One of the typical use-case is remote camera monitoring. With WebRTC, people can have interoperability and flexibility for deploying monitoring service. However, the benefit of WebRTC for IoT is not only its convenience and interopera...
SYS-CON Events announced today that Secure Channels, a cybersecurity firm, will exhibit at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Secure Channels, Inc. offers several products and solutions to its many clients, helping them protect critical data from being compromised and access to computer networks from the unauthorized. The company develops comprehensive data encryption security strategie...
From 2013, NTT Communications has been providing cPaaS service, SkyWay. Its customer’s expectations for leveraging WebRTC technology are not only typical real-time communication use cases such as Web conference, remote education, but also IoT use cases such as remote camera monitoring, smart-glass, and robotic. Because of this, NTT Communications has numerous IoT business use-cases that its customers are developing on top of PaaS. WebRTC will lead IoT businesses to be more innovative and address...