
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
-
Have Questions? Post them here!
More Posts related to Java,
- How to Get List of All Country Codes in Java Using Locale Class
- Unsupported major.minor version 52.0 in java
- Java - How to set custom thread name?
- Get the current timestamp in Java
- Java Spring Boot 3 Web Hello World with Gradle in IntelliJ
- [fix] NullPointerException Cannot Invoke findById because Repository is null - Java Spring
- java: unclosed string literal [Error]
- Convert Java Byte Array to String with code examples
- Error: Can not find the tag library descriptor for
- Java 8 - Convert List to Map Examples
- Java - Calculate time taken for the code to execute in milliseconds or nanoseconds
- Fix java.net.ProtocolException: Invalid HTTP method
- Java: Convert Stream to List
- Java equals method - Tutorial
- List of Java JDBC Database Driver Jars, Classes and URLs Details
- Read YAML file Java Jackson Library
- How to display Java Date Time timezone GMT/UTC offset using SimpleDateFormat
- List of Java Keywords
- Enable JSON Pretty Print in Java Jackson
- How to Word-Warp Console logs in IntelliJ
- Convert Map to List in Java 8 using Stream API
- Create a Directory using Java Code
- Ways to Convert Integer or int to Long in Java
- [Program] How to read three different values using Scanner in Java
- Java JDBC Example with Oracle Database Driver Connection
More Posts:
- How to add Business Users using Microsoft 365 Admin Center - Microsoft
- How to get current URL Location using Javascript HTML - JavaScript
- Android Parsing Data for android-L failed Unsupported major.minor version 51.0 Error - Android
- Python Program To Calculate Simple Interest (SimpleInterest.py) - Python
- [fix] fatal: pathspec index.html did not match any files error - Git
- How to share SharePoint site or document with all users in organization - SharePoint
- Java -Day of the week using Java 8 DayOfWeek Enum - Java
- [macOS] Change homepage Macbook Safari Browser - MacOS