{"id":43714,"date":"2015-09-18T21:51:22","date_gmt":"2015-09-18T18:51:22","guid":{"rendered":"http:\/\/www.javacodegeeks.com\/?p=43714"},"modified":"2023-12-06T12:17:00","modified_gmt":"2023-12-06T10:17:00","slug":"built-in-serialization-techniques","status":"publish","type":"post","link":"https:\/\/www.javacodegeeks.com\/2015\/09\/built-in-serialization-techniques.html","title":{"rendered":"Built-in Serialization techniques"},"content":{"rendered":"<p><em>This article is part of our Academy Course titled <a href=\"http:\/\/www.javacodegeeks.com\/2015\/09\/advanced-java.html\">Advanced Java<\/a>.<\/p>\n<p>This course is designed to help you make the most effective use of Java. It discusses advanced topics, including object creation, concurrency, serialization, reflection and many more. It will guide you through your journey to Java mastery! Check it out <a href=\"http:\/\/www.javacodegeeks.com\/2015\/09\/advanced-java.html\">here<\/a>!<\/em><\/p>\n<div class=\"toc\">\n<h4>Table Of Contents<\/h4>\n<dl>\n<dt><a href=\"#intro\">1. Introduction<\/a><\/dt>\n<dt><a href=\"#serializable\">2. Serializable interface<\/a><\/dt>\n<dt><a href=\"#externalizable\">3. Externalizable interface<\/a><\/dt>\n<dt><a href=\"#more\">4. More about Serializable interface<\/a><\/dt>\n<dt><a href=\"#rmi\">5. Serializability and Remote Method Invocation (RMI)<\/a><\/dt>\n<dt><a href=\"#jaxb\">6. JAXB<\/a><\/dt>\n<dt><a href=\"#jsonp\">7. JSON-P<\/a><\/dt>\n<dt><a href=\"#cost\">8. Cost of serialization<\/a><\/dt>\n<dt><a href=\"#beyond\">9. Beyond Java standard library and specifications<\/a><\/dt>\n<dt><a href=\"#next\">10. What\u2019s next<\/a><\/dt>\n<dt><a href=\"#download\">11. Download the Source code<\/a><\/dt>\n<\/dl>\n<\/div>\n<h2><a name=\"intro\"><\/a>1. Introduction<\/h2>\n<p>This part of the tutorial is going to be solely devoted to <b>serialization<\/b>: the process of translating Java objects into a format that can be used to store and be reconstructed later in the same (or another) environment (<a href=\"http:\/\/en.wikipedia.org\/wiki\/Serialization\" target=\"_blank\" rel=\"noopener\">http:\/\/en.wikipedia.org\/wiki\/Serialization<\/a>). <b>Serialization<\/b> not only allows saving and loading Java objects to\/from the persistent storage, but is also a very important component of modern distributed systems communication.<\/p>\n<p><b>Serialization<\/b> is not easy, but effective <b>serialization<\/b> is even harder. Besides the Java standard library, there are many serialization techniques and frameworks available: some of them are using compact binary representation, others put the readability on the first place. Although we are going to mention many alternatives along the way, our attention will be concentrated on the ones from Java standard library (and latest specifications):  <code>Serializable<\/code>, <code>Externalizable<\/code>, Java Architecture for XML Binding (<b>JAXB<\/b>, <a href=\"https:\/\/jcp.org\/en\/jsr\/detail?id=222\" target=\"_blank\" rel=\"noopener\">JSR-222<\/a>) and Java API for JSON Processing (<b>JSON-P<\/b>, <a href=\"https:\/\/jcp.org\/en\/jsr\/detail?id=353\" target=\"_blank\" rel=\"noopener\">JSR-353<\/a>).<\/p>\n<h2><a name=\"serializable\"><\/a>2. Serializable interface<\/h2>\n<p>Arguably, the easiest way in Java to mark the class as available for serialization is by implementing the <code>java.io.Serializable<\/code> interface. For example:<\/p>\n<pre class=\"brush:java\">\npublic class SerializableExample implements Serializable {\n}\n\n<\/pre>\n<p>The serialization runtime associates with each serializable class a special version number, called a <b>serial version UID<\/b>, which is used during <b>deserialization<\/b> (the process opposite to <b>serialization<\/b>) to make sure that the loaded classes for the serialized object are compatible. In case the compatibility has been compromised, the <code>InvalidClassException<\/code> will be raised.<\/p>\n<p>A serializable class may introduce its own <b>serial version UID<\/b> explicitly by declaring a field with name <code>serialVersionUID<\/code> that must be <code>static<\/code>, <code>final<\/code>, and of type <code>long<\/code>. For example:<\/p>\n<pre class=\"brush:java\">\n\npublic class SerializableExample implements Serializable {\n    private static final long serialVersionUID = 8894f47504319602864L;   \n}\n\n<\/pre>\n<p>However, if a serializable class does not explicitly declare a <code>serialVersionUID<\/code> field, then the serialization runtime will generate a default <code>serialVersionUID<\/code> field for that class. It is worth to know that it is strongly recommended by all classes implementing <code>Serializable<\/code> to explicitly declare the <code>serialVersionUID<\/code> field, because the default <code>serialVersionUID<\/code> generation heavily relies on intrinsic class details and may vary depending on Java compiler implementation and its version. As such, to guarantee a consistent behavior, a serializable class must always declare an explicit <code>serialVersionUID<\/code> field.<\/p>\n<p>Once the class becomes serializable (implements <code>Serializable<\/code> and declares <code>serialVersionUID<\/code>), it could be stored and retrieved using, for example,  <code>ObjectOutputStream<\/code> \/ <code>ObjectInputStream<\/code>:<\/p>\n<pre class=\"brush:java\">\nfinal Path storage = new File( \"object.ser\" ).toPath();\n\ntry( final ObjectOutputStream out = \n        new ObjectOutputStream( Files.newOutputStream( storage ) ) ) {\n    out.writeObject( new SerializableExample() );\n}\n\n<\/pre>\n<p>Once stored, it could be retrieved in a similar way, for example:<\/p>\n<pre class=\"brush:java\">\ntry( final ObjectInputStream in = \n        new ObjectInputStream( Files.newInputStream( storage ) ) ) {\n    final SerializableExample instance = ( SerializableExample )in.readObject();\n    \/\/ Some implementation here\n}\n\n<\/pre>\n<p>As we can see, the <code>Serializable<\/code> interface does not provide a lot of control over what should be serialized and how (with exception of <code>transient<\/code> keyword which marks the fields as non-serializable). Moreover, it limits the flexibility of changing the internal class representation as it could break the serialization \/ deserialization process. That is why another interface, <code>Externalizable<\/code>, has been introduced.<\/p>\n<h2><a name=\"externalizable\"><\/a>3. Externalizable interface<\/h2>\n<p>In contrast to <code>Serializable<\/code> interface, <code>Externalizable<\/code> delegates to the class the responsibility of how it should be serialized and deserialized. It has only two methods and here is its declaration from the Java standard library:<\/p>\n<pre class=\"brush:java\">\npublic interface Externalizable extends java.io.Serializable {\n    void writeExternal(ObjectOutput out) throws IOException;\n    void readExternal(ObjectInput in) throws IOException, ClassNotFoundException;\n}\n\n<\/pre>\n<p>In turn, every class which implements <code>Externalizable<\/code> interface should provide the implementation of these two methods. Let us take a look on the example:<\/p>\n<pre class=\"brush:java\">\npublic class ExternalizableExample implements Externalizable {\n    private String str;\n    private int number;\n    private SerializableExample obj;\n        \n    @Override\n    public void readExternal(final ObjectInput in) \n            throws IOException, ClassNotFoundException {\n        setStr(in.readUTF());\n        setNumber(in.readInt());\n        setObj(( SerializableExample )in.readObject());\n    }\n    \n    @Override\n    public void writeExternal(final ObjectOutput out) \n            throws IOException {\n        out.writeUTF(getStr());\n        out.writeInt(getNumber());\n        out.writeObject(getObj());\n    }\n}\n\n<\/pre>\n<p>Similarly to the classes implementing <code>Serializable<\/code>, the classes implementing <code>Externalizable<\/code> could be stored and retrieved using, for example,  <code>ObjectOutputStream<\/code> \/ <code>ObjectInputStream<\/code>:<div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<pre class=\"brush:java\">\nfinal Path storage = new File( \"extobject.ser\" ).toPath();\n        \nfinal ExternalizableExample instance = new ExternalizableExample();\ninstance.setStr( \"Sample String\" );\ninstance.setNumber( 10 );\ninstance.setObj( new SerializableExample() );\n        \ntry( final ObjectOutputStream out = \n        new ObjectOutputStream( Files.newOutputStream( storage ) ) ) {\n    out.writeObject( instance );\n}\n        \ntry( final ObjectInputStream in = \n        new ObjectInputStream( Files.newInputStream( storage ) ) ) {\n    final ExternalizableExample obj = ( ExternalizableExample )in.readObject();\n    \/\/ Some implementation here\n}\n\n<\/pre>\n<p>The <code>Externalizable<\/code> interface allows a fine-grained serialization \/ deserialization customization in the cases when the simpler approach with <code>Serializable<\/code> interface does not work well.<\/p>\n<h2><a name=\"more\"><\/a>4. More about Serializable interface<\/h2>\n<p>In the previous section we mentioned that the <code>Serializable<\/code> interface does not provide a lot of control over what should be serialized and how. In fact, it is not completely true (at least when <code>ObjectOutputStream<\/code> \/ <code>ObjectInputStream<\/code> are used). There are some special methods which any serializable class can implement in order to control the default serialization and deserialization.<\/p>\n<pre class=\"brush:java\">\nprivate void writeObject(ObjectOutputStream out) throws IOException;\n\n<\/pre>\n<p>This method is responsible for writing the state of the object for its particular class so that the corresponding <code>readObject<\/code> method can restore it (the default mechanism for saving the Object&#8217;s fields can be invoked by calling <code>out.defaultWriteObject<\/code>).<\/p>\n<pre class=\"brush:java\">\nprivate void readObject(ObjectInputStream in) throws IOException,ClassNotFoundException;\n\n<\/pre>\n<p>This method is responsible for reading from the stream and restoring the state of the object (the default mechanism for restoring the Object&#8217;s fields can be invoked by calling <code>in.defaultReadObject<\/code>). <\/p>\n<pre class=\"brush:java\">\nprivate void readObjectNoData() throws ObjectStreamException;\n\n<\/pre>\n<p>This method is responsible for initializing the state of the object in the case when the serialization stream does not list the given class as a superclass of the object being deserialized. <\/p>\n<pre class=\"brush:java\">\nObject writeReplace() throws ObjectStreamException;\n\n<\/pre>\n<p>This method is used when serializable classes need to designate an alternative object to be used when writing an object to the stream.<\/p>\n<pre class=\"brush:java\">\n\nObject readResolve() throws ObjectStreamException;\n\n<\/pre>\n<p>And lastly, this method is used when serializable classes need to designate a replacement when an instance of it is read from the stream.<\/p>\n<p>The default serialization mechanism (using <code>Serializable<\/code> interface) could get really cumbersome in Java once you know the intrinsic implementation details and those special methods to use. More code you are writing to support serialization, more likely more bugs and vulnerabilities will show off. <\/p>\n<p>However, there is a way to reduce those risks by employing quite simple pattern named <b>Serialization Proxy<\/b>, which is based on utilizing <code>writeReplace<\/code> and <code>readResolve<\/code> methods. The basic idea of this pattern is to introduce dedicated companion class for serialization (usually as <code>private static<\/code> inner class), which complements the class required to be serialized. Let us take a look on this example:<\/p>\n<pre class=\"brush:java\">\npublic class SerializationProxyExample implements Serializable {\n    private static final long serialVersionUID = 6163321482548364831L;\n\n    private String str;\n    private int number;        \n    \n    public SerializationProxyExample( final String str, final int number) {\n        this.setStr(str);\n        this.setNumber(number);\n    }\n\n    private void readObject(ObjectInputStream stream) throws InvalidObjectException {\n        throw new InvalidObjectException( \"Serialization Proxy is expected\" );\n    }\n    \n    private Object writeReplace() {\n        return new SerializationProxy( this );\n    }\n    \n    \/\/ Setters and getters here\n}\n\n<\/pre>\n<p>When the instances of this class are being serialized, the class <code>SerializationProxyExample<\/code> implementation provides the replacement object (instance of the <code>SerializationProxy<\/code> class) instead. It means that instances of the <code>SerializationProxyExample<\/code> class will never be serialized (and deserialized) directly. It also explains why the <code>readObject<\/code> method raises an exception in case a deserialization attempt somehow happens. Now, let us take a look on the companion <code>SerializationProxy<\/code> class:<\/p>\n<pre class=\"brush:java\">\nprivate static class SerializationProxy implements Serializable {\n    private static final long serialVersionUID = 8368440585226546959L;\n\n    private String str;\n    private int number;\n        \n    public SerializationProxy( final SerializationProxyExample instance ) {\n        this.str = instance.getStr();\n        this.number = instance.getNumber();\n    }\n        \n    private Object readResolve() {\n        return new SerializationProxyExample(str, number); \/\/ Uses public constructor\n    }\n}\n\n<\/pre>\n<p>In our somewhat simplified case, the <code>SerializationProxy<\/code> class just duplicates all the fields of the <code>SerializationProxyExample<\/code> (but it could be much complicated than that). Consequently, when the instances of this class are being deserialized, the <code>readResolve<\/code> method is called and <code>SerializationProxy<\/code> provides the replacement as well, this time in a shape of <code>SerializationProxyExample<\/code> instance. As such, the <code>SerializationProxy<\/code> class serves as a serialization proxy for <code>SerializationProxyExample<\/code> class.<\/p>\n<h2><a name=\"rmi\"><\/a>5. Serializability and Remote Method Invocation (RMI)<\/h2>\n<p>For quite some time, Java Remote Method Invocation (<b>RMI<\/b>) was the only mechanism available for building distributed applications on Java platform. <b>RMI<\/b> provides all the heavy lifting and makes it possible to transparently invoke the methods of remote Java objects from other JVMs on the same host or on different physical (or virtual) hosts. In the foundation of <b>RMI<\/b> lays object serialization, which is used to marshal (serialize) and unmarshal (deserialize) method parameters.<\/p>\n<p><b>RMI<\/b> is still being used in many Java applications nowadays, but it lesser and lesser becomes a choice because of its complexity and communication restrictions (most of the firewalls do block <b>RMI<\/b> ports). To get more details about <b>RMI<\/b> please refer to <a href=\"http:\/\/www.oracle.com\/technetwork\/java\/javase\/tech\/index-jsp-136424.html\" target=\"_blank\" rel=\"noopener\">official documentation<\/a>.<\/p>\n<h2><a name=\"jaxb\"><\/a>6. JAXB<\/h2>\n<p>Java Architecture for XML Binding, or just <b>JAXB<\/b>, is probably the oldest alternative serialization mechanism available to Java developers. Underneath, it uses XML as the serialization format, provides a wide range of customization options and includes a lot of annotations which makes <b>JAXB<\/b> very appealing and easy to use (annotations are covered in <b>part 5<\/b> of the tutorial, <b><a href=\"http:\/\/www.javacodegeeks.com\/2015\/09\/how-and-when-to-use-enums-and-annotations\/\">How and when to use Enums and Annotations<\/a><\/b>). <\/p>\n<p>Let us take a look on a quite simplified example of the plain old Java class (POJO) annotated with <b>JAXB<\/b> annotations:<\/p>\n<pre class=\"brush:java\">\nimport java.math.BigDecimal;\nimport javax.xml.bind.annotation.XmlAccessType;\nimport javax.xml.bind.annotation.XmlAccessorType;\nimport javax.xml.bind.annotation.XmlElement;\nimport javax.xml.bind.annotation.XmlRootElement;\n\n@XmlAccessorType( XmlAccessType.FIELD )\n@XmlRootElement( name = \"example\" )\npublic class JaxbExample {\n    @XmlElement(required = true) private String str;\n    @XmlElement(required = true) private BigDecimal number;\n    \n    \/\/ Setters and getters here\n}\n<\/pre>\n<p>To serialize the instance of this class into XML format using <b>JAXB<\/b> infrastructure, the only thing needed is the instance of the marshaller (or serializer), for example:<\/p>\n<pre class=\"brush:java\">\nfinal JAXBContext context = JAXBContext.newInstance( JaxbExample.class );        \nfinal Marshaller marshaller = context.createMarshaller();\n     \nfinal JaxbExample example = new JaxbExample();\nexample.setStr( \"Some string\" );\nexample.setNumber( new BigDecimal( 12.33d, MathContext.DECIMAL64 ) );\n        \ntry( final StringWriter writer = new StringWriter() ) {\n    marshaller.marshal( example, writer );\n}\n<\/pre>\n<p>Here is the XML representation of the <code>JaxbExample<\/code> class instance from the example above:<\/p>\n<pre class=\"brush:xml\">\n&lt;?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?&gt;\n&lt;example&gt;\n    &lt;str&gt;Some string&lt;\/str&gt;\n    &lt;number&gt;12.33000000000000&lt;\/number&gt;\n&lt;\/example&gt;\n\n<\/pre>\n<p>Following the same principle, the instances of the class could be deserialized back from XML representation into the Java objects using the instance of the unmarshaller (or deserializer), for example:<\/p>\n<pre class=\"brush:java\">\nfinal JAXBContext context = JAXBContext.newInstance( JaxbExample.class );\n        \nfinal String xml = \"\" +\n    \"&lt;?xml version=\\\\\"1.0\\\\\" encoding=\\\\\"UTF-8\\\\\" standalone=\\\\\"yes\\\\\"?&gt;\" +\n    \"&lt;example&gt;\" +\n    \"    &lt;str&gt;Some string&lt;\/str&gt;\" +\n    \"    &lt;number&gt;12.33000000000000&lt;\/number&gt;\" +\n    \"&lt;\/example&gt;\";\n        \nfinal Unmarshaller unmarshaller = context.createUnmarshaller();\ntry( final StringReader reader = new StringReader( xml ) ) {\n    final JaxbExample example = ( JaxbExample )unmarshaller.unmarshal( reader );\n    \/\/ Some implementaion here\n}\n<\/pre>\n<p>As we can see, <b>JAXB<\/b> is quite easy to use and the XML format is still quite popular choice nowadays. However, one of the fundamental pitfalls of XML is verbosity: quite often the necessary XML structural elements significantly surpass the effective data payload.<br \/>\n[ulp id=&#8217;w6F4W4SAMiyTapBF&#8217;]<br \/>\n&nbsp;<\/p>\n<h2><a name=\"jsonp\"><\/a>7. JSON-P<\/h2>\n<p>Since 2013, Java developers are able to use <b>JSON<\/b> as the serialization format, by virtue of newly introduced Java API for JSON Processing (<b>JSON-P<\/b>). <\/p>\n<p>As of now, <b>JSON-P<\/b> is not a part of Java standard library although there are many discussions to include native <b>JSON<\/b> support into the language in the upcoming <b>Java 9<\/b> release (<a href=\"http:\/\/openjdk.java.net\/jeps\/198\">http:\/\/openjdk.java.net\/jeps\/198<\/a>). Nevertheless, it is there and available as part of Java <b>JSON Processing Reference Implementation<\/b> (<a href=\"https:\/\/jsonp.java.net\/\">https:\/\/jsonp.java.net\/<\/a>). <\/p>\n<p>In contrast to <b>JAXB<\/b>, there is nothing required to be added to the class to make it suitable for <b>JSON<\/b> serialization, for example:<\/p>\n<pre class=\"brush:java\">\npublic class JsonExample {\n    private String str;\n    private BigDecimal number;\n    \/\/ Setters and getters here\n}\n<\/pre>\n<p>The serialization is not as transparent as with <b>JAXB<\/b>, and requires a bit of code to be written for each class intended to be serialized into <b>JSON<\/b>, for example:<\/p>\n<pre class=\"brush:java\">\nfinal JsonExample example = new JsonExample();\nexample.setStr( \"Some string\" );\nexample.setNumber( new BigDecimal( 12.33d, MathContext.DECIMAL64 ) );\n        \ntry( final StringWriter writer = new StringWriter() ) {\n    Json.createWriter(writer).write( \n        Json.createObjectBuilder()\n            .add(\"str\", example.getStr() )\n            .add(\"number\", example.getNumber() )\n            .build()\n        );\n}\n<\/pre>\n<p>And here is the <b>JSON<\/b> representation of the <code>JsonExample<\/code> class instance from the example above:<\/p>\n<pre class=\"brush:java\">\n{\n    \"str\":\"Some string\",\n    \"number\":12.33000000000000\n}\n\n<\/pre>\n<p>The deserialization process goes in the same vein:<\/p>\n<pre class=\"brush:java\">\nfinal String json = \"{\\\\\"str\\\\\":\\\\\"Some string\\\\\",\\\\\"number\\\\\":12.33000000000000}\";  \n      \ntry( final StringReader reader = new StringReader( json ) ) {\n    final JsonObject obj = Json.createReader( reader ).readObject();\n    final JsonExample example = new JsonExample();\n    example.setStr( obj.getString( \"str\" ) );\n    example.setNumber( obj.getJsonNumber( \"number\" ).bigDecimalValue() );\n}\n<\/pre>\n<p>It is fair to say that at the moment <b>JSON<\/b> support in Java is pretty basic. Nonetheless it is a great thing to have and Java community is working on enriching the <b>JSON<\/b> support by introducing <b>Java API for JSON Binding<\/b> (JSON-B, <a href=\"https:\/\/jcp.org\/en\/jsr\/detail?id=367\" target=\"_blank\" rel=\"noopener\">JSR-367<\/a>). With this API the serialization and deserialization of the Java objects to\/from <b>JSON<\/b> should be as transparent as <b>JAXB<\/b> has.<\/p>\n<h2><a name=\"cost\"><\/a>8. Cost of serialization<\/h2>\n<p>It is very important to understand that though serialization \/ deserialization looks simple in Java, it is not free and depending on the data model and data access patterns may consume quite a lot of network bandwidth, memory and CPU resources. More to that, nevertheless Java has some kind of versioning support for the serializable classes (using <b>serial version UID<\/b> as we have seen in the section <a href=\"#serializable\" target=\"_blank\" rel=\"noopener\">Serializable interface<\/a>), it does make the development process much harder as developers are on their own to figure out how to manage data model evolution.<\/p>\n<p>To add another point, Java serialization does not work well outside of JVM world. It is a significant constraint for the modern distributed applications which are built using multiple programming languages and runtimes.<\/p>\n<p>That explains why many alternative serialization frameworks and solutions have emerged and became very popular choice in the Java ecosystem.<\/p>\n<h2><a name=\"beyond\"><\/a>9. Beyond Java standard library and specifications<\/h2>\n<p>In this section we are going to look on alternative solutions for painless and effective Java serialization, starting from the <b>Fast-serialization<\/b> project (<a href=\"http:\/\/ruedigermoeller.github.io\/fast-serialization\/\" target=\"_blank\" rel=\"noopener\">http:\/\/ruedigermoeller.github.io\/fast-serialization\/<\/a>): the fast Java serialization drop in-replacement. The usage of <b>Fast-serialization<\/b> is not much different from what Java standard library provides but it claims to be much faster and more effective.<\/p>\n<p>Another set of frameworks has a different take on the problem. They are based on structured data definition (or protocol) and serialize data into compact binary representation (the corresponding data model could be even generated from the definition). Aside from that, those frameworks are going far beyond just Java platform and can be used for cross-language \/ cross-platform serialization. The most known Java libraries in this space are <b>Google Protocol Buffers<\/b> (<a href=\"https:\/\/developers.google.com\/protocol-buffers\/\" target=\"_blank\" rel=\"noopener\">https:\/\/developers.google.com\/protocol-buffers\/<\/a>), <b>Apache Avro<\/b> (<a href=\"http:\/\/avro.apache.org\/\" target=\"_blank\" rel=\"noopener\">http:\/\/avro.apache.org\/<\/a>) and <b>Apache Thrift<\/b> (<a href=\"https:\/\/thrift.apache.org\/\" target=\"_blank\" rel=\"noopener\">https:\/\/thrift.apache.org\/<\/a>).<\/p>\n<h2><a name=\"next\"><\/a>10. What\u2019s next<\/h2>\n<p>In this part of the tutorial we have discussed the built-in serialization techniques provided by the Java language and its runtime. We have seen how important serialization is today, when mostly every single application being built is a part of larger distributed system and needs to communicate with the rest of it (or with other external systems). In the next part of the tutorial we are going to talk about reflection and dynamic languages support in Java.<\/p>\n<h2><a name=\"download\"><\/a>11. Download the Source code<\/h2>\n<p>You can dowload the source code of this course here: <strong><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2015\/09\/advanced-java-part-10.zip\">advanced-java-part-10<\/a><\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This article is part of our Academy Course titled Advanced Java. This course is designed to help you make the most effective use of Java. It discusses advanced topics, including object creation, concurrency, serialization, reflection and many more. It will guide you through your journey to Java mastery! Check it out here! Table Of Contents &hellip;<\/p>\n","protected":false},"author":141,"featured_media":148,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[343,88,580,557,65],"class_list":["post-43714","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-core-java","tag-annotations","tag-concurrency","tag-generics","tag-reflection","tag-serialization"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Built-in Serialization techniques - Java Code Geeks<\/title>\n<meta name=\"description\" content=\"This article is part of our Academy Course titled Advanced Java. This course is designed to help you make the most effective use of Java. It discusses\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.javacodegeeks.com\/2015\/09\/built-in-serialization-techniques.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Built-in Serialization techniques - Java Code Geeks\" \/>\n<meta property=\"og:description\" content=\"This article is part of our Academy Course titled Advanced Java. This course is designed to help you make the most effective use of Java. It discusses\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.javacodegeeks.com\/2015\/09\/built-in-serialization-techniques.html\" \/>\n<meta property=\"og:site_name\" content=\"Java Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/javacodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2015-09-18T18:51:22+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-12-06T10:17:00+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Andrey Redko\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:site\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Andrey Redko\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"9 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/built-in-serialization-techniques.html#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/built-in-serialization-techniques.html\"},\"author\":{\"name\":\"Andrey Redko\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/771a6504862edc45322776832cbce413\"},\"headline\":\"Built-in Serialization techniques\",\"datePublished\":\"2015-09-18T18:51:22+00:00\",\"dateModified\":\"2023-12-06T10:17:00+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/built-in-serialization-techniques.html\"},\"wordCount\":1965,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/built-in-serialization-techniques.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/java-logo.jpg\",\"keywords\":[\"Annotations\",\"Concurrency\",\"Generics\",\"Reflection\",\"Serialization\"],\"articleSection\":[\"Core Java\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/built-in-serialization-techniques.html#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/built-in-serialization-techniques.html\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/built-in-serialization-techniques.html\",\"name\":\"Built-in Serialization techniques - Java Code Geeks\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/built-in-serialization-techniques.html#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/built-in-serialization-techniques.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/java-logo.jpg\",\"datePublished\":\"2015-09-18T18:51:22+00:00\",\"dateModified\":\"2023-12-06T10:17:00+00:00\",\"description\":\"This article is part of our Academy Course titled Advanced Java. This course is designed to help you make the most effective use of Java. It discusses\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/built-in-serialization-techniques.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/built-in-serialization-techniques.html\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/built-in-serialization-techniques.html#primaryimage\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/java-logo.jpg\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/java-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/built-in-serialization-techniques.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Core Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\\\/core-java\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Built-in Serialization techniques\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"name\":\"Java Code Geeks\",\"description\":\"Java Developers Resource Center\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"alternateName\":\"JCG\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.javacodegeeks.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/javacodegeeks\",\"https:\\\/\\\/x.com\\\/javacodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/771a6504862edc45322776832cbce413\",\"name\":\"Andrey Redko\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/16419ce8394173028eddaeb992859862bab50cfcf74589fa9bb9a3dd8bb27518?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/16419ce8394173028eddaeb992859862bab50cfcf74589fa9bb9a3dd8bb27518?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/16419ce8394173028eddaeb992859862bab50cfcf74589fa9bb9a3dd8bb27518?s=96&d=mm&r=g\",\"caption\":\"Andrey Redko\"},\"description\":\"Andriy is a well-grounded software developer with more then 12 years of practical experience using Java\\\/EE, C#\\\/.NET, C++, Groovy, Ruby, functional programming (Scala), databases (MySQL, PostgreSQL, Oracle) and NoSQL solutions (MongoDB, Redis).\",\"sameAs\":[\"http:\\\/\\\/aredko.blogspot.com\\\/\",\"http:\\\/\\\/ca.linkedin.com\\\/in\\\/aredko\"],\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/author\\\/andrey-redko\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Built-in Serialization techniques - Java Code Geeks","description":"This article is part of our Academy Course titled Advanced Java. This course is designed to help you make the most effective use of Java. It discusses","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.javacodegeeks.com\/2015\/09\/built-in-serialization-techniques.html","og_locale":"en_US","og_type":"article","og_title":"Built-in Serialization techniques - Java Code Geeks","og_description":"This article is part of our Academy Course titled Advanced Java. This course is designed to help you make the most effective use of Java. It discusses","og_url":"https:\/\/www.javacodegeeks.com\/2015\/09\/built-in-serialization-techniques.html","og_site_name":"Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2015-09-18T18:51:22+00:00","article_modified_time":"2023-12-06T10:17:00+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg","type":"image\/jpeg"}],"author":"Andrey Redko","twitter_card":"summary_large_image","twitter_creator":"@javacodegeeks","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Andrey Redko","Est. reading time":"9 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.javacodegeeks.com\/2015\/09\/built-in-serialization-techniques.html#article","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/2015\/09\/built-in-serialization-techniques.html"},"author":{"name":"Andrey Redko","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/771a6504862edc45322776832cbce413"},"headline":"Built-in Serialization techniques","datePublished":"2015-09-18T18:51:22+00:00","dateModified":"2023-12-06T10:17:00+00:00","mainEntityOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2015\/09\/built-in-serialization-techniques.html"},"wordCount":1965,"commentCount":0,"publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2015\/09\/built-in-serialization-techniques.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg","keywords":["Annotations","Concurrency","Generics","Reflection","Serialization"],"articleSection":["Core Java"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.javacodegeeks.com\/2015\/09\/built-in-serialization-techniques.html#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.javacodegeeks.com\/2015\/09\/built-in-serialization-techniques.html","url":"https:\/\/www.javacodegeeks.com\/2015\/09\/built-in-serialization-techniques.html","name":"Built-in Serialization techniques - Java Code Geeks","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2015\/09\/built-in-serialization-techniques.html#primaryimage"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2015\/09\/built-in-serialization-techniques.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg","datePublished":"2015-09-18T18:51:22+00:00","dateModified":"2023-12-06T10:17:00+00:00","description":"This article is part of our Academy Course titled Advanced Java. This course is designed to help you make the most effective use of Java. It discusses","breadcrumb":{"@id":"https:\/\/www.javacodegeeks.com\/2015\/09\/built-in-serialization-techniques.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.javacodegeeks.com\/2015\/09\/built-in-serialization-techniques.html"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/2015\/09\/built-in-serialization-techniques.html#primaryimage","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/www.javacodegeeks.com\/2015\/09\/built-in-serialization-techniques.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.javacodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Java","item":"https:\/\/www.javacodegeeks.com\/category\/java"},{"@type":"ListItem","position":3,"name":"Core Java","item":"https:\/\/www.javacodegeeks.com\/category\/java\/core-java"},{"@type":"ListItem","position":4,"name":"Built-in Serialization techniques"}]},{"@type":"WebSite","@id":"https:\/\/www.javacodegeeks.com\/#website","url":"https:\/\/www.javacodegeeks.com\/","name":"Java Code Geeks","description":"Java Developers Resource Center","publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.javacodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.javacodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/www.javacodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/javacodegeeks","https:\/\/x.com\/javacodegeeks"]},{"@type":"Person","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/771a6504862edc45322776832cbce413","name":"Andrey Redko","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/16419ce8394173028eddaeb992859862bab50cfcf74589fa9bb9a3dd8bb27518?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/16419ce8394173028eddaeb992859862bab50cfcf74589fa9bb9a3dd8bb27518?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/16419ce8394173028eddaeb992859862bab50cfcf74589fa9bb9a3dd8bb27518?s=96&d=mm&r=g","caption":"Andrey Redko"},"description":"Andriy is a well-grounded software developer with more then 12 years of practical experience using Java\/EE, C#\/.NET, C++, Groovy, Ruby, functional programming (Scala), databases (MySQL, PostgreSQL, Oracle) and NoSQL solutions (MongoDB, Redis).","sameAs":["http:\/\/aredko.blogspot.com\/","http:\/\/ca.linkedin.com\/in\/aredko"],"url":"https:\/\/www.javacodegeeks.com\/author\/andrey-redko"}]}},"_links":{"self":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/43714","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/users\/141"}],"replies":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=43714"}],"version-history":[{"count":0,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/43714\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media\/148"}],"wp:attachment":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=43714"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=43714"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=43714"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}