Java object serialization and deserialization default format and JSON format use sample _java

Source: Internet
Author: User
Tags object serialization serialization

Default format

Copy Code code as follows:

public class MyClass implements serializable{
...}

Serialization:

Copy Code code as follows:

ObjectOutputStream output = new ObjectOutputStream (new FileOutputStream (OutputPath));
Output.writeobject (MyObject);

Deserialization:

Copy Code code as follows:

ObjectInputStream input = new ObjectInputStream (new FileInputStream (InputPath));
Return (MyClass) input.readobject ();

JSON format

Use the Jackson bag. Jackson is a highly efficient Java JSON package. See the website for documents and downloads.

Serialization of

Copy Code code as follows:

Objectmapper mapper = new Objectmapper ();
Mapper.writevalue (New File (OutputPath), myObject);

Deserialization:

Copy Code code as follows:

Return Mapper.readvalue (New File (OutputPath), myclass.class);

Complete test Code

Copy Code code as follows:

Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.ObjectInputStream;
Import Java.io.ObjectOutputStream;
Import java.io.Serializable;
Import java.util.ArrayList;

Import com.fasterxml.jackson.core.JsonGenerationException;
Import com.fasterxml.jackson.core.JsonParseException;
Import com.fasterxml.jackson.databind.JsonMappingException;
Import Com.fasterxml.jackson.databind.ObjectMapper;

public class Zoo implements Serializable {

Private static final long serialversionuid = 1L;
private static Objectmapper mapper = new Objectmapper ();

public static int maxanimalcount;
public arraylist<string> animals;

Public Zoo () {
Animals = new arraylist<string> ();
}

public static void Setmax (int max) {
Maxanimalcount = max;
}

/**
* Add An animal to animals Array.
* @param animalname
*/
public void Addanimal (String animalname) {
if (Animals.size () < Maxanimalcount)
Animals.add (Animalname);
}

@Override
Public String toString () {
Return "Zoo: \ n Animals:" + animals.tostring () +
"\ Maxanimalcount:" + maxanimalcount + "\ n";
}

/**
* Output standard serialization to file at LogPath.
* @param LogPath
*/
public void Serializetolog (String logPath) {
ObjectOutputStream output = null;
Try
{
Output = new ObjectOutputStream (
New FileOutputStream (LogPath));
Output.writeobject (this);
catch (Exception e) {
E.printstacktrace ();
finally {
try {
Output.close ();
catch (IOException e) {
E.printstacktrace ();
}
}
}

/**
* Output JSON serialization (using Jackson) to file at LogPath.
* @param LogPath
*/
public void Serializejsontolog (String logPath) {

try {
Mapper.writevalue (New File (LogPath), this);
catch (Jsongenerationexception e) {
E.printstacktrace ();
catch (Jsonmappingexception e) {
E.printstacktrace ();
catch (IOException e) {
E.printstacktrace ();
}
}

/**
* Standard Deserialize a Zoo instance from file at LogPath.
* @param LogPath
* @return Deserialized Zoo Instance
*/
public static Zoo Deserializefromlog (String logPath) {
ObjectInputStream input = null;
Try
{
Input =new ObjectInputStream (
New FileInputStream (LogPath));
Return (Zoo) input.readobject ();
catch (Exception e) {
E.printstacktrace ();
finally {
try {
Input.close ();
catch (IOException e) {
E.printstacktrace ();
}
}

return null;
}

/**
* JSON Deserialize a Zoo instance from file at LogPath.
* @param LogPath
* @return JSON Deserialized Zoo instance
*/
public static Zoo Deserializejsonfromlog (String logPath) {
try {
Return Mapper.readvalue (New File (LogPath), zoo.class);
catch (Jsonparseexception e) {
E.printstacktrace ();
catch (Jsonmappingexception e) {
E.printstacktrace ();
catch (IOException e) {
E.printstacktrace ();
}

return null;
}
}

Class Zooserializetest {
public static void Main (string[] args) {
Zoo zoo1 = new Zoo ();
Zoo.setmax (100);
Zoo1.addanimal ("Hamster");
Zoo1.addanimal ("sheep");

Zoo1.serializetolog ("Zoo1.log");

Zoo Zoo2 = new Zoo ();
Zoo.setmax (200);
Zoo2.addanimal ("Tiger");

Zoo2.serializetolog ("Zoo2.log");

Zoo.setmax (300);

Deserialization
Zoo1 = Zoo.deserializefromlog ("Zoo1.log");
Zoo2 = Zoo.deserializefromlog ("Zoo2.log");

System.out.println ("Zoo1: \ n" + zoo1);
System.out.println ("Zoo2: \ n" + Zoo2);

Serialize to JSON
Zoo1.serializejsontolog ("Zoo1.json");
Zoo1 = Zoo.deserializejsonfromlog ("Zoo1.json");

System.out.println ("zoo1 from JSON: \ n" + zoo1);
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.