Serialize and Deserialize Enum with JSON-B / Yasson is performed in java using json-b or yasson serialization and deserialization. Java enum is a user defined data type that is serialize or de-serialize using the json-b as like any other variable. Json binding has recently been introduced to java as part of the JSR 367 specification. JSON binding api specifies the specifications for converting generic java objects to json format and converting json strings to generic java objects. In this article, we will see how to convert the Enum object to json string and how to convert the json string to a enum object.

Enum is a user defined data type that contains a collection of user defined values. The example for enum is weekdays. The 7 weekdays are unique values that will not fit into any of the common category like number, string, character etc. Here we will see json binding api with yasson implementation for an enum object.

The javax.json.bind dependency on the spring boot attaches the dependent jars to the spring boot project. Here we see the step-by-step process for building a spring boot application that converts a json string to a enum java object and converts a enum java object to a json string.

Step 1 – Create pom.xml with dependency

The first step is to build a spring boot application. The spring boot framework includes two simple “spring-boot-starter” and “spring-boot-starter-test” dependencies. To add json binding jars to the project, add “javax.json.bind-api” dependency to the pom.xml file. This is a specification api jar that enforces all json binding specifications.

Add the json binding jars to the spring boot program. The default json binding is org.eclipse.yasson. Add “org.eclipse.yasson” and “org.glassfish.javax.json” to the pom.xml file. The pom.xml file below includes all the dependency for json binding.


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns=""
		<relativePath /> <!-- lookup parent from repository -->
	<description>Spring Boot Project</description>










Step 2 – Create Enum and Java Bean Class

Build a java bean object that will be transformed to a json object in the spring boot framework. The Java Bean object will contain all private variables and public getter and setter methods. The java bean object must have a default constructor.

The java bean class contains a enum type variable. The getter and setter is created for the enum type variable. The example below shows a sample of the java bean model.

package com.yawintutor;

public enum Course {

package com.yawintutor;

public class Student {

	private int id;

	private String name;
	private Course course;

	public int getId() {
		return id;

	public void setId(int id) { = id;

	public String getName() {
		return name;

	public void setName(String name) { = name;

	public Course getCourse() {
		return course;

	public void setCourse(Course course) {
		this.course = course;


Step 3 – JSON Binding API code to convert Enum Java Bean Object

The json binding api supports to create a enum java bean object by taking input as a json string. The example below demonstrates how to build a enum java bean object and how to convert a json string to enum java bean object.

package com.yawintutor;

import javax.json.bind.Jsonb;
import javax.json.bind.JsonbBuilder;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

public class SpringBootJsonbSimpleApplication {

	public static void main(String[] args) {, args);
	public static void JavaObjecttoJSON() {
		Student student = new Student();

		Jsonb jsonb = JsonbBuilder.create();
		String jsonString = jsonb.toJson(student);
		System.out.println("Json format String : " + jsonString);

	public static void JSONToJavaObject() {
		String jsonString = "{\"course\":\"BCOM\",\"id\":100,\"name\":\"Yawin\"}";

		Jsonb jsonb = JsonbBuilder.create();
		Student st = jsonb.fromJson(jsonString, Student.class);
		System.out.println(st.getId() + " " + st.getName()+" "+st.getCourse().name());



  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 :: Spring Boot ::        (v2.2.5.RELEASE)

2020-03-18 20:56:14.438  INFO 43196 --- [           main] c.y.SpringBootJsonbSimpleApplication     : Starting SpringBootJsonbSimpleApplication on banl1691b9157 with PID 43196 (/Users/knatarajan2/STS/workspace/SpringBootJsonbSimple/target/classes started by knatarajan2 in /Users/knatarajan2/STS/workspace/SpringBootJsonbSimple)
2020-03-18 20:56:14.441  INFO 43196 --- [           main] c.y.SpringBootJsonbSimpleApplication     : No active profile set, falling back to default profiles: default
2020-03-18 20:56:14.878  INFO 43196 --- [           main] c.y.SpringBootJsonbSimpleApplication     : Started SpringBootJsonbSimpleApplication in 0.688 seconds (JVM running for 3.316)
Json format String : {"course":"BCOM","id":100,"name":"Yawin"}
100 Yawin BCOM

Useful other links

Leave a Reply