61

Example JSON (note that the string has trailing spaces):

{ "aNumber": 0, "aString": "string   " }

Ideally, the deserialised instance would have an aString property with a value of "string" (i.e. without trailing spaces). This seems like something that is probably supported but I can't find it (e.g. in DeserializationConfig.Feature).

We're using Spring MVC 3.x so a Spring-based solution would also be fine.

I tried configuring Spring's WebDataBinder based on a suggestion in a forum post but it does not seem to work when using a Jackson message converter:

@InitBinder
public void initBinder( WebDataBinder binder )
{
    binder.registerCustomEditor( String.class, new StringTrimmerEditor( " \t\r\n\f", true ) );
}
penfold
  • 1,523
  • 1
  • 14
  • 21
  • Are you 100% sure the spaces aren't in the actual value? Because I have never seen Jackson do this. Or are you saying that the class you pass to Jackson has these trailing spaces intentionally, and you want to set up Jackson to remove it for you? – matt b Jul 28 '11 at 00:31
  • 4
    @matt: I thought it was pretty clearly stated that the data has trailing spaces from the source and he wants to configure Jackson to remove the trailing spaces on deserialization. – Jim Garrison Jul 28 '11 at 00:37
  • That is correct, we have no valid reason to keep trailing (or leading) whitespace present in an incoming JSON message. – penfold Jul 28 '11 at 06:21

7 Answers7

36

Easy solution for Spring Boot users, just add that walv's SimpleModule extension to your application context:

package com.example;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.deser.std.StdScalarDeserializer;
import com.fasterxml.jackson.databind.module.SimpleModule;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class StringTrimModule extends SimpleModule {

    public StringTrimModule() {
        addDeserializer(String.class, new StdScalarDeserializer<String>(String.class) {
            @Override
            public String deserialize(JsonParser jsonParser, DeserializationContext ctx) throws IOException,
                    JsonProcessingException {
                return jsonParser.getValueAsString().trim();
            }
        });
    }
}

Another way to customize Jackson is to add beans of type com.fasterxml.jackson.databind.Module to your context. They will be registered with every bean of type ObjectMapper, providing a global mechanism for contributing custom modules when you add new features to your application.

http://docs.spring.io/spring-boot/docs/current/reference/html/howto-spring-mvc.html#howto-customize-the-jackson-objectmapper

if you are not using spring boot, you have to register the StringTrimModule yourself (you do not need to annotate it with @Component)

<bean class="org.springframework.http.converter.json.Jackson2Objec‌​tMapperFactoryBean">
    <property name="modulesToInstall" value="com.example.StringTrimModule"/>
</bean
Joram
  • 3,166
  • 1
  • 22
  • 29
Maciej Marczuk
  • 3,593
  • 29
  • 28
  • 1
    How can we skip this trimming process for some specific fields? (for ex. a password field) – Eagle_Eye Dec 07 '18 at 06:52
  • 4
    The standard String deserializer does a lot more than just calling jsonParser.getValueAsString(). Also, you don't need to create a Module and register it using @Component, you can just write a deserializer and add @JsonComponent to it: `@JsonComponent class TrimStringDeserializer extends StringDeserializer { @Override String deserialize(JsonParser jsonParser, DeserializationContext ctx) throws IOException { String text = super.deserialize(jsonParser, ctx) return text != null ? text.trim() : text } }` – eekboom Nov 22 '19 at 15:22
  • If I want to add trimming string to serialize as well, how can I add it using `addSerializer()` ? – Sandeepa Feb 28 '21 at 18:52
  • 1
    For future readers, I had to manually add it as a module on my objectMapper, otherwise it wouldn't work `objectMapper.registerModule(new StringTrimModule());` – fmbordignon Jun 27 '22 at 18:08
25

With a custom deserializer, you could do the following:

 <your bean>
 @JsonDeserialize(using=WhiteSpaceRemovalSerializer.class)
 public void setAString(String aString) {
    // body
 }

 <somewhere>
 public class WhiteSpaceRemovalDeserializer extends JsonDeserializer<String> {
     @Override
     public String deserialize(JsonParser jp, DeserializationContext ctxt) {
         // This is where you can deserialize your value the way you want.
         // Don't know if the following expression is correct, this is just an idea.
         return jp.getCurrentToken().asText().trim();
     }
 }

This solution does imply that this bean attribute will always be serialized this way, and you will have to annotate every attribute that you want to be deserialized this way.

kkurian
  • 3,844
  • 3
  • 30
  • 49
DCKing
  • 4,253
  • 2
  • 28
  • 43
  • Thanks, although I think _this.aString = aString.trim()_ is probably easier :-) Hopefully it will be a feature in a future version. – penfold Aug 13 '11 at 06:01
  • Although that is easier, the annotation ensures the trimming only happens when the bean is created by JSON deserialization :) – DCKing Aug 17 '11 at 12:58
  • 3
    @DCKing: Why not just register your custom deserializer globally via Module interface? I can't think of any bad consequences in a typical Spring app when Jackson is used only for RESTful web services, can you? – Artem Shafranov Jul 26 '13 at 14:59
  • 1
    @ArtemShafranov Note that if afterburner is used then registering a global deserializer won't work as afterburner optimizes those default deserializers and doesn't allow custom ones unless annotated with JsonDeserialize. – jontejj Apr 10 '15 at 08:50
  • Is that a good practice considering performance? What happens if the string field is _null_ I mean, if I start adding checkings for null or if I want to remove also white spaces from in between words, special characters, wouldn't that slow down the system? Performance-wise, shouldn't this convertion and checking be done in the front end? – skinny_jones Jun 10 '17 at 03:54
  • @skinny_jones I don't understand the motivation of your question. If basic input filtering is a performance concern in the first place, then I can't help but wonder how you write the other code in your application. No, this is a trivial operation and does not cost performance at all. – DCKing Jun 12 '17 at 05:40
  • @DCKing I was just really considering possible options. I'm starting my project, just want to make it right to avoid rework or refactoring... My first idea was to create a helper class with a _removeChars(object, regExp)_ method and in resource layer I would treat my input... something like that. Cause I'm using jpa entities and I'm not comfortable about decorating every string _set_ method of them and wasn't sure about the performance, that's why I asked. I think I will just give it a go. – skinny_jones Jun 12 '17 at 19:15
  • 1
    Couple things; `asText()` is giving me a "cannot find symbol" error. Everything else resolved to jackson.core, just not that method. Also, the link you originally posted for "custom deserializer" is broken. – Always Learning May 07 '18 at 21:33
  • 1
    @AlwaysLearning You can now just do `jp.getText()`. – Nitin Savant Nov 04 '21 at 16:22
19

I think it is better to extend default StringDeserializer as it already handles some specific cases (see here and here) that can be used by third party libraries. Below you can find configuration for Spring Boot. This is possible only with Jackson 2.9.0 and above as starting from 2.9.0 version StringDeserializer is not final anymore. If you have Jackson version below 2.9.0 you can still copy content of StringDeserializer to your code to handle above mentioned cases.

@JsonComponent
public class StringDeserializer extends com.fasterxml.jackson.databind.deser.std.StringDeserializer {

    @Override
    public String deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
        String value = super.deserialize(p, ctxt);
        return value != null ? value.trim() : null;
    }
}
Eugene Maysyuk
  • 2,977
  • 25
  • 24
  • BTW, StringUtils#trimWhitespace has shown slow benchmarks comparing to java.lang.String#trim. The java.lang.String#trim was ~10x faster in this case. – Eugene Maysyuk Mar 26 '20 at 21:53
  • 2
    Good answer, `trim()` can be replaced by Java 11 [`strip()`](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/String.html#strip()) which covers all the possible flavors of whitespace according to `Character.isWhitespace()`. – Alexander Ivanchenko Dec 31 '22 at 18:20
17

The problem of annotation @JsonDeserialize is that you must always remember to put it on the setter. To make it globally "once and forever" with Spring MVC, I did next steps:

pom.xml:

<dependency>
   <groupId>com.fasterxml.jackson.core</groupId>
   <artifactId>jackson-databind</artifactId>
   <version>2.3.3</version>
</dependency>

Create custom ObjectMapper:

package com.mycompany;

    import java.io.IOException;
    import org.apache.commons.lang3.StringUtils;
    import com.fasterxml.jackson.core.JsonParser;
    import com.fasterxml.jackson.core.JsonProcessingException;
    import com.fasterxml.jackson.databind.DeserializationContext;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import com.fasterxml.jackson.databind.deser.std.StdScalarDeserializer;
    import com.fasterxml.jackson.databind.module.SimpleModule;

    public class MyObjectMapper extends ObjectMapper {

        public MyObjectMapper() {
            registerModule(new MyModule());
        }
    }

    class MyModule extends SimpleModule {

        public MyModule() {
            addDeserializer(String.class, new StdScalarDeserializer<String>  (String.class) {
                @Override
                public String deserialize(JsonParser jp, DeserializationContext  ctxt) throws IOException,
                    JsonProcessingException {
                    return StringUtils.trim(jp.getValueAsString());
                }
            });
        }
    }

Update Spring's servlet-context.xml:

<bean id="objectMapper" class="com.mycompany.MyObjectMapper" />

    <mvc:annotation-driven>
        <mvc:message-converters>
            <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                <property name="objectMapper" ref="objectMapper" />
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>
kryger
  • 12,906
  • 8
  • 44
  • 65
walv
  • 2,680
  • 3
  • 31
  • 36
  • This seems a nice global solution that was easy to add my Spring Boot application already using a custom ObjectMapper: I just defined and registered "StringTrimmerModule" along these lines. (Although I preferred plain old `String.trim()` for non-null values instead of Commons StringUtils.) – Jonik Feb 19 '15 at 12:53
  • Note that if [afterburner](https://github.com/FasterXML/jackson-module-afterburner) is used then this won't work as afterburner optimizes those default deserializers. If that's the case then I guess you're stuck with @JsonDeserialize? – jontejj Apr 10 '15 at 08:14
  • What is the impact of this solution on JSON attributes that are not strings - like numbers or booleans – Wand Maker Aug 22 '16 at 10:13
  • @WandMaker, other types will be ignored because this deserialized is overwritten for String.class only. – walv Aug 25 '16 at 17:31
7

For Spring Boot, we just have to create a custom deserializer as documented in the manual.

The following is my Groovy code but feel free to adapt it to work in Java.

import com.fasterxml.jackson.core.JsonParser
import com.fasterxml.jackson.databind.DeserializationContext
import com.fasterxml.jackson.databind.JsonDeserializer
import org.springframework.boot.jackson.JsonComponent

import static com.fasterxml.jackson.core.JsonToken.VALUE_STRING

@JsonComponent
class TrimmingJsonDeserializer extends JsonDeserializer<String> {

    @Override
    String deserialize(JsonParser parser, DeserializationContext context) {
        parser.hasToken(VALUE_STRING) ? parser.text?.trim() : null
    }
}
adarshr
  • 61,315
  • 23
  • 138
  • 167
  • My observation is that Jackson uses this deserializer for all strings. If the request class contains strings for boolean, etc, then having the parser.hasToken(VALUE_STRING) will create bugs. If you have boolean, uuid, int, etc in your json request, you will get null values from JACKSON. My solution was to simply use parser.getText.trim() and it works fine on my machine :) – Bence Mar 22 '20 at 10:06
6

com.fasterxml.jackson.dataformat

pom.xml

   <dependency>
      <groupId>com.fasterxml.jackson.dataformat</groupId>
      <artifactId>jackson-dataformat-csv</artifactId>
      <version>2.5.3</version>
    </dependency>

CsvUtil.java

     CsvSchema bootstrapSchema = CsvSchema.emptySchema().withHeader().sortedBy();
     CsvMapper mapper = new CsvMapper();
     mapper.enable(CsvParser.Feature.TRIM_SPACES);
     InputStream inputStream = ResourceUtils.getURL(fileName).openStream();
     MappingIterator<T> readValues =
          mapper.readerFor(type).with(bootstrapSchema).readValues(inputStream);
vanduc1102
  • 5,769
  • 1
  • 46
  • 43
  • It's ok if you want to trim all Strings, but using custom JsonDeserializer is more flexible. – Andrey Jun 25 '21 at 06:59
1

I propose you the following:

First, create a module to trim and put it into a class:

import java.io.IOException;

import org.springframework.stereotype.Component;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.deser.std.StdScalarDeserializer;
import com.fasterxml.jackson.databind.module.SimpleModule;

@Component
public class StringTrimModule extends SimpleModule {

    public StringTrimModule() {
        addDeserializer(String.class, new StdScalarDeserializer<String>(String.class) {
            @Override
            public String deserialize(JsonParser jsonParser, DeserializationContext ctx) throws IOException {
                return jsonParser.getValueAsString().trim();
            }
        });
    }
}

Then, create a class to configure jackson and add the module:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * Class used to configure Jackson
 */
@Configuration
public class JacksonConfiguration {

    @Bean
    public ObjectMapper objectMapper() {
        ObjectMapper mapper = new ObjectMapper();
        mapper.registerModule(new StringTrimModule());
        return mapper;
    }
}

That's it.