Technology & Design

Using Jackson To Write A Custom Jackson Serializer

JsonNullSerializer to send empty string instead of null value – Set them to “empty string”.

In this article, we’re going to look at a more advanced “use-case” of using Jackson to write a custom serializer.

Here are the steps (for Jackson < 2.0):

Step 1: Add Jackson libraries to your environment.

For Maven

<!– https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core –>

<dependency>

   <groupId>com.fasterxml.jackson.core</groupId>

   <artifactId>jackson-core</artifactId>

   <version>2.9.4</version>

</dependency>

For Gradle

// https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core
compile group: ‘com.fasterxml.jackson.core’, name: ‘jackson-core’, version: ‘2.9.4’

Step 2: Write your custom null serializer.

import java.io.IOException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

public class NullSerializer extends JsonSerializer<Object>
{

@Override
public void serialize(Object o, com.fasterxml.jackson.core.JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException,    com.fasterxml.jackson.core.JsonProcessingException { jsonGenerator.writeString(“”);

}
}

Step 3: Register this with Jackson object mapper.

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ser.DefaultSerializerProvider;
import com.XXX.NullSerializer;

public class CustomObjectMapper extends ObjectMapper
{
public CustomObjectMapper()
{
super();

DefaultSerializerProvider.Impl sp = new DefaultSerializerProvider.Impl();
sp.setNullValueSerializer(new NullSerializer());
this.setSerializerProvider(sp);
}
}

Step 4: Register this object mapper with Spring MVC by creating a bean in your Spring application class.

package com.sfl.is.mfn;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationConfig;
import com.XXX.CustomObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.actuate.autoconfigure.*;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.liquibase.LiquibaseProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import javax.annotation.PostConstruct;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.Collection;

@ComponentScan
@EnableAutoConfiguration(exclude = { MetricFilterAutoConfiguration.class, MetricRepositoryAutoConfiguration.class })
@EnableConfigurationProperties({ LiquibaseProperties.class, ApplicationProperties.class })

public class NullToEmptyApplication
{
private static final Logger log = LoggerFactory.getLogger(MindfullnessWebApiApp.class);
public NullToEmptyApplication()
{

}

public static void main(String[] args) throws UnknownHostException
{
SpringApplication app = new SpringApplication(NullToEmptyApplication.class);
}

@Bean
public ObjectMapper jacksonObjectMapper()
{
return new CustomObjectMapper();
}

@Bean
public SerializationConfig serializationConfig()
{
return jacksonObjectMapper()
.getSerializationConfig();
}
}

Published by
Yash Patel

Recent Posts

  • Data & Analytics

Why is Databricks Lakehouse the Ultimate Data Solution for Your Company?

Businesses are forced to make compromises because of this…

2 days ago
  • AI/ML

The First Step in Using AI & Automation for Productivity

When adopting AI and automation, taking the time to…

5 days ago
  • Data & Analytics

7 Steps to Become Data Strategist & Drive Revenue Growth with Databricks

Without a strategy built by experts, your business could…

5 days ago
  • AI/ML

Your AI & Automation Strategy is the Outcome of 2 Key Steps

Strategically implementing AI & Automation can drive significant value,…

1 week ago
  • Data & Analytics

4 Important Things Decision-Makers Should Know About Data Analytics

Data-driven businesses are not only 23 times more likely…

1 week ago
  • Data & Analytics

7 Key Strategies for Building a Profitable Data Ecosystem with Databricks

Our key strategies for building a profitable data ecosystem…

2 weeks ago