In this post we will see how we can remove leading and trailing spaces from all the String fields in a Java object.
We are going to use Jackson to serialize and deserialize a Java object, and while deserializing we will use a custom implementation to trim all the String fields to Null.
Implementation:
Let's create a custom deserializer for String values in Jackson:
TrimStringToNullDeserializer.java
package com.blogspot.devnip.jackson;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import java.io.IOException;
import static java.util.Objects.isNull;
/**
* Deserializer that will trim all the leading and trailing spaces from a String value.
* If the final value us empty string ("") it will be returned as null.
*/
public class TrimStringToNullDeserializer extends JsonDeserializer {
@Override
public String deserialize(JsonParser jsonParser, DeserializationContext deserializationContext)
throws IOException {
String value = jsonParser.getValueAsString();
if (isNull(value)) {
return null;
}
value = value.trim();
if (value.length() == 0) {
value = null;
}
return value;
}
}
Now, let's hook it up to Jackson's ObjectMapper as shown below.
TrimStringToNullConfiguration.java
package com.blogspot.devnip.jackson;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import static java.util.Objects.isNull;
/**
* This class shows, how Jackson can be used to trim all the string values in a Java object.
*/
public class TrimStringToNullConfiguration {
private ObjectMapper objectMapper;
public Client trimToNull(Client inputClient) throws JsonProcessingException {
return getObjectMapper().readValue(getObjectMapper().writeValueAsString(inputClient), Client.class);
}
private ObjectMapper getObjectMapper() {
if (isNull(objectMapper)) {
objectMapper = new ObjectMapper();
SimpleModule module = new SimpleModule();
module.addDeserializer(String.class, new TrimStringToNullDeserializer());
objectMapper.registerModule(module);
}
return objectMapper;
}
}
That's it, you can call the `trimToNull()` function of above class to remove all the leading and trailing spaces from all the fields within a Java object.
Refer to below unit-test for how to use this method.
TrimStringToNullConfigurationTest.java
package com.blogspot.devnip.jackson;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
class TrimStringToNullConfigurationTest {
private TrimStringToNullConfiguration configuration = new TrimStringToNullConfiguration();
/**
* Demonstrates trimming of all string values in a Java object.
*
* @throws JsonProcessingException
*/
@Test
void test() throws JsonProcessingException {
Client testClient = new Client();
testClient.setName(" James Smith "); // String with spaces
testClient.setSocialId(null); // Null value
testClient.setAge(23); // Non string value
Address address = new Address();
address.setCity(" New York"); // Demonstrates nested fields
testClient.setOfficeAddress(address);
Client result = configuration.trimToNull(testClient);
Assertions.assertEquals("James Smith", result.getName());
Assertions.assertEquals("New York", result.getOfficeAddress().getCity());
}
}
All the posts in this blog have a working example. Please visit our Github repository
0 comments:
Post a Comment