
Let us take a look at how to achieve serialization and deserialization using Java Jackson Library, we will first convert a Java Object to JSON and save it as a file, then read the file and convert it into Java Object.
Step 1: Serialization
Pojo: Album.javaimport java.util.Arrays;
public class Album {
private String artistName;
private String albumName;
private int releaseYear;
private String[] songsList;
public Album(String artistName, String albumName, int releaseYear, String[] songsList) {
this.artistName = artistName;
this.albumName = albumName;
this.releaseYear = releaseYear;
this.songsList = songsList;
}
public String getArtistName() {
return artistName;
}
public void setArtistName(String artistName) {
this.artistName = artistName;
}
public String getAlbumName() {
return albumName;
}
public void setAlbumName(String albumName) {
this.albumName = albumName;
}
public int getReleaseYear() {
return releaseYear;
}
public void setReleaseYear(int releaseYear) {
this.releaseYear = releaseYear;
}
public String[] getSongsList() {
return songsList;
}
public void setSongsList(String[] songsList) {
this.songsList = songsList;
}
@Override
public String toString() {
return "Album{" +
"artistName='" + artistName + '\'' +
", albumName='" + albumName + '\'' +
", releaseYear=" + releaseYear +
", songsList=" + Arrays.toString(songsList) +
'}';
}
}
SerializationExampleJackson.java
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.File;
import java.io.IOException;
public class SerializationExampleJackson {
public static void main(String[] args) {
String[] songs = new String[9];
songs[0] = "Wanna Be Startin Somethin";
songs[1] = "Baby Be Mine";
songs[2] = "The Girl Is Mine";
songs[3] = "Thriller";
songs[4] = "Beat it";
songs[5] = "Billie Jean";
songs[6] = "Human Nature";
songs[7] = "P.Y.T. (Pretty Young Thing)";
songs[8] = "The Lady in My Life";
Album album = new Album("Michael Jackson","Thriller",1982,songs);
try {
String jsonFileName = "album.json";
File jsonFile = new File(jsonFileName);
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.writeValue(jsonFile, album);
System.out.println("Java Object saved as Json File: album.json");
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Output: Java Object saved as Json File: album.json
Step 2: Deserialization
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.File;
import java.io.IOException;
public class DeserializationExampleJackson {
public static void main(String[] args) {
try {
String jsonFileName = "album.json";
File jsonFile = new File(jsonFileName);
ObjectMapper objectMapper = new ObjectMapper();
Album album = objectMapper.readValue(jsonFile, Album.class);
System.out.println("Json read from album.json file..");
System.out.println(album.getAlbumName());
System.out.println(album.getArtistName());
System.out.println(album.getReleaseYear());
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Output:
Json read from album.json file..
Thriller
Michael Jackson
1982
-
Facing issues? Have Questions? Post them here! I am happy to answer!
More Posts related to Java,
- Convert Java Map Collection Object to JSON String using Jackson
- Java Stream flatmap() Examples
- [Fix] Instant java.time.temporal.UnsupportedTemporalTypeException: Unsupported unit: Years
- How to run Java Unit Test cases with Apache Maven?
- How to check if Java main thread is alive
- [Fix] java: incompatible types: incompatible parameter types in lambda expression error
- Parsing CSV file using Java code example (Comma Separated File)
- Unhandled exception type InterruptedException : Java Threads
- Native getClass() method from java.lang.Object Class Explained with examples.
- Java Jackson ObjectMapper Class with Examples
- Java 8 Streams map() with examples
- Java 8 - Convert List to Map Examples
- IntelliJ: Error: Could not find or load main class, java.lang.ClassNotFoundException
- Java Stream with Multiple Filters Example
- How to Clear StringJoiner in Java 8
- Spring 5 IoC Example with application Context XML (ClassPathXmlApplicationContext) and Gradle.
- How to get end of line (EOL) or new line character \r \n in Java
- Spring Boot CRUD Examples using JDBCTemplate
- Delete a File in Java with Examples
- Implementing Insertion Sort Algorithm in Java Program
- Java JDBC Batch Update Example with PreparedStatement
- Java JDBC Select Multiple Records from table as List using PreparedStatement
- [Hibernate] The method buildSessionFactory() from the type Configuration is deprecated
- How to fix Java HTTP java.net.UnknownHostException
- Java 8 Display time in 12 hour AM PM format
More Posts:
- Create Hidden File or Directory using Shell Command - Linux
- How to add border to Android TextView - Android
- How to submit website to dmoz directory - HowTos
- Create a Database Table using JDBC PreparedStatement - Java
- How To Remove Only Conditional Formatting in Excel - Microsoft
- Microsoft Office Excel - Couldnt Open the Workbook - The workbook cannot be opened. - Microsoft
- Python raise error with message example - Python
- [Fix] ValueError: substring not found in Python - Python