Welcome!

Java IoT Authors: Liz McMillan, Elizabeth White, Pat Romanski, Yeshim Deniz, Jason Bloomberg

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
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...
"When we talk about cloud without compromise what we're talking about is that when people think about 'I need the flexibility of the cloud' - it's the ability to create applications and run them in a cloud environment that's far more flexible,” explained Matthew Finnie, CTO of Interoute, in this SYS-CON.tv interview at 20th Cloud Expo, held June 6-8, 2017, at the Javits Center in New York City, NY.
In his session at @ThingsExpo, Eric Lachapelle, CEO of the Professional Evaluation and Certification Board (PECB), provided an overview of various initiatives to certify the security of connected devices and future trends in ensuring public trust of IoT. Eric Lachapelle is the Chief Executive Officer of the Professional Evaluation and Certification Board (PECB), an international certification body. His role is to help companies and individuals to achieve professional, accredited and worldwide re...
Amazon started as an online bookseller 20 years ago. Since then, it has evolved into a technology juggernaut that has disrupted multiple markets and industries and touches many aspects of our lives. It is a relentless technology and business model innovator driving disruption throughout numerous ecosystems. Amazon’s AWS revenues alone are approaching $16B a year making it one of the largest IT companies in the world. With dominant offerings in Cloud, IoT, eCommerce, Big Data, AI, Digital Assista...
When growing capacity and power in the data center, the architectural trade-offs between server scale-up vs. scale-out continue to be debated. Both approaches are valid: scale-out adds multiple, smaller servers running in a distributed computing model, while scale-up adds fewer, more powerful servers that are capable of running larger workloads. It’s worth noting that there are additional, unique advantages that scale-up architectures offer. One big advantage is large memory and compute capacity...
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...
IoT solutions exploit operational data generated by Internet-connected smart “things” for the purpose of gaining operational insight and producing “better outcomes” (for example, create new business models, eliminate unscheduled maintenance, etc.). The explosive proliferation of IoT solutions will result in an exponential growth in the volume of IoT data, precipitating significant Information Governance issues: who owns the IoT data, what are the rights/duties of IoT solutions adopters towards t...
With the introduction of IoT and Smart Living in every aspect of our lives, one question has become relevant: What are the security implications? To answer this, first we have to look and explore the security models of the technologies that IoT is founded upon. In his session at @ThingsExpo, Nevi Kaja, a Research Engineer at Ford Motor Company, discussed some of the security challenges of the IoT infrastructure and related how these aspects impact Smart Living. The material was delivered interac...
No hype cycles or predictions of zillions of things here. IoT is big. You get it. You know your business and have great ideas for a business transformation strategy. What comes next? Time to make it happen. In his session at @ThingsExpo, Jay Mason, Associate Partner at M&S Consulting, presented a step-by-step plan to develop your technology implementation strategy. He discussed the evaluation of communication standards and IoT messaging protocols, data analytics considerations, edge-to-cloud tec...
The Internet giants are fully embracing AI. All the services they offer to their customers are aimed at drawing a map of the world with the data they get. The AIs from these companies are used to build disruptive approaches that cannot be used by established enterprises, which are threatened by these disruptions. However, most leaders underestimate the effect this will have on their businesses. In his session at 21st Cloud Expo, Rene Buest, Director Market Research & Technology Evangelism at Ara...
Artificial intelligence, machine learning, neural networks. We’re in the midst of a wave of excitement around AI such as hasn’t been seen for a few decades. But those previous periods of inflated expectations led to troughs of disappointment. Will this time be different? Most likely. Applications of AI such as predictive analytics are already decreasing costs and improving reliability of industrial machinery. Furthermore, the funding and research going into AI now comes from a wide range of com...
We build IoT infrastructure products - when you have to integrate different devices, different systems and cloud you have to build an application to do that but we eliminate the need to build an application. Our products can integrate any device, any system, any cloud regardless of protocol," explained Peter Jung, Chief Product Officer at Pulzze Systems, in this SYS-CON.tv interview at @ThingsExpo, held November 1-3, 2016, at the Santa Clara Convention Center in Santa Clara, CA
With major technology companies and startups seriously embracing Cloud strategies, now is the perfect time to attend 21st Cloud Expo October 31 - November 2, 2017, at the Santa Clara Convention Center, CA, and June 12-14, 2018, at the Javits Center in New York City, NY, and learn what is going on, contribute to the discussions, and ensure that your enterprise is on the right path to Digital Transformation.
SYS-CON Events announced today that Enzu will exhibit at SYS-CON's 21st Int\ernational Cloud Expo®, which will take place October 31-November 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Enzu’s mission is to be the leading provider of enterprise cloud solutions worldwide. Enzu enables online businesses to use its IT infrastructure to their competitive advantage. By offering a suite of proven hosting and management services, Enzu wants companies to focus on the core of their ...
SYS-CON Events announced today that Cloud Academy named "Bronze Sponsor" of 21st International Cloud Expo which will take place October 31 - November 2, 2017 at the Santa Clara Convention Center in Santa Clara, CA. Cloud Academy is the industry’s most innovative, vendor-neutral cloud technology training platform. Cloud Academy provides continuous learning solutions for individuals and enterprise teams for Amazon Web Services, Microsoft Azure, Google Cloud Platform, and the most popular cloud com...
SYS-CON Events announced today that MobiDev, a client-oriented software development company, 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. MobiDev is a software company that develops and delivers turn-key mobile apps, websites, web services, and complex software systems for startups and enterprises. Since 2009 it has grown from a small group of passionate engineers and business...
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...
SYS-CON Events announced today that Ayehu will exhibit at SYS-CON's 21st International Cloud Expo®, which will take place on October 31 - November 2, 2017 at the Santa Clara Convention Center in Santa Clara California. Ayehu provides IT Process Automation & Orchestration solutions for IT and Security professionals to identify and resolve critical incidents and enable rapid containment, eradication, and recovery from cyber security breaches. Ayehu provides customers greater control over IT infras...
In his session at Cloud Expo, Alan Winters, an entertainment executive/TV producer turned serial entrepreneur, presented a success story of an entrepreneur who has both suffered through and benefited from offshore development across multiple businesses: The smart choice, or how to select the right offshore development partner Warning signs, or how to minimize chances of making the wrong choice Collaboration, or how to establish the most effective work processes Budget control, or how to ma...
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...