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!
Author Info:
Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.
Follow him on: X
You can also reach out to him via e-mail: rakesh@code2care.org
More Posts related to Java,
- Get the current timestamp in Java
- Java Stream with Multiple Filters Example
- Java SE JDBC with Prepared Statement Parameterized Select Example
- Fix: UnsupportedClassVersionError: Unsupported major.minor version 63.0
- [Fix] Java Exception with Lambda - Cannot invoke because object is null
- 7 deadly java.lang.OutOfMemoryError in Java Programming
- How to Calculate the SHA Hash Value of a File in Java
- Java JDBC Connection with Database using SSL (https) URL
- How to Add/Subtract Days to the Current Date in Java
- Create Nested Directories using Java Code
- Spring Boot: JDBCTemplate BatchUpdate Update Query Example
- What is CA FE BA BE 00 00 00 3D in Java Class Bytecode
- Save Java Object as JSON file using Jackson Library
- Adding Custom ASCII Text Banner in Spring Boot Application
- [Fix] Java: Type argument cannot be of primitive type generics
- List of New Features in Java 11 (JEPs)
- Java: How to Add two Maps with example
- Java JDBC Transition Management using PreparedStatement Examples
- Understanding and Handling NullPointerException in Java: Tips and Tricks for Effective Debugging
- Steps of working with Stored Procedures using JDBCTemplate Spring Boot
- Java 8 java.util.Function and BiFunction Examples
- The Motivation Behind Generics in Java Programming
- Get Current Local Date and Time using Java 8 DateTime API
- Java: Convert Char to ASCII
- Deep Dive: Why avoid java.util.Date and Calendar Classes
More Posts:
- [Java] How to throws Exception using Functional Interface and Lambda code - Java
- How to get the Execution Time of A Python Program - Python
- List of PowerShell Function Commands for Mac - Powershell
- How to Undo-Revert Sent Email in Google Gmail - Google
- What is the Difference Between $SHELL and $0 Environment Variables? - Linux
- Remove Html head and body tags from ckeditor source - Html
- How to send email from JavaScript HTML using mailto - JavaScript
- SharePoint error 2130575251 - The security validation for this page is invalid and might be corrupted - SharePoint