1

I am using @RestController in Spring and I have the following response:

{"name":"Khan","first":null,"last":null}

I want the null to be "" (empty string)

I do not want to do something ugly like initializing each property with an empty string like so:

@Transient private String first="";
@Transient private String last="";

I found a link that discusses how to implement this solution "for Jackson < 2.0)", but I am using Jackson libraries > 2.0 like:

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

Further, the solution in the link doesn't always work according to some others who tried it.

Is there an elegant way of doing a conversion from fields that have a null value to empty string? Preferably with annotations.

1
  • You can override the setFirst... public void setFirst(String first) { if(first == null) { this.first = ""; } else { this.first = first; } } Commented May 6, 2015 at 20:14

3 Answers 3

4

My previous answer was unacceptable due to it not including annotations and serializers. So referring to this link (which I found on google by searching "jackson 2.0 custom serializer"), I imagine this should work (assuming your class is Person since it's not specified):

public class PersonSerializer extends JsonSerializer<Person> {
@Override
public void serialize(Person value, JsonGenerator jgen, SerializerProvider provider) 
  throws IOException, JsonProcessingException {
        jgen.writeStartObject();
        jgen.writeStringField("name", value.name);
        if (value.first == null) {
            jgen.writeStringField("first", ""); 
        }else{
            jgen.writeStringField("first", value.first);
        }
        if(value.last == null){
            jgen.writeStringField("last", "");
        }else{
            jgen.writeStringField("last", value.last);
        }
        jgen.writeEndObject();
    }
}

And then on your person class

@JsonSerialize(using = PersonSerializer.class)
public class Person {
    //First/Last name etc
}

I think you should re-evaluate why you want a custom parser for this, if you continue reading the whole link at the top of this answer you'll see the use case for custom parsers and how it's different than yours. In his case he wants to return some data that's not directly related to that field, whereas you just wanted to have null treated as an empty string.

Sign up to request clarification or add additional context in comments.

2 Comments

Works like a charm! The only drawback is I have a large entity object and I have to write out multiple if statements. It would have been nice if there is some way to auto-discover if the values are null instead of doing it by hand.
Dave Lugg, I just stated it works well. It would be better I said if there was some way to detect null instead of manually writing out if statements. Read what I wrote first. Plus, I clicked up the answer because it was useful.
0

You can override the setFirst and setLast.

 public void setFirst(String first) { 
        if(first == null) { 
           this.first = ""; 
        } else {
           this.first = first;
        }
 }

Comments

-1

I think @Raphael Amoedo was on the right track, but I think the nature of your problem means that the solution would be to have it in the getFirst() and getLast() methods instead.

public string getFirst(){
   if (this.first == null) return "";
   return this.first;
}

public string getLast(){
   if (this.last == null) return "";
   return this.last;
}

This ensures that if the first and last fields are set to null by any other means (Serialization, perhaps) that they will still return an empty string instead of a null value.

2 Comments

This is not an elegant solution, it's hardcoding. I was looking for an annotation based solution or like one in the link I shared: stackoverflow.com/questions/12934045…, writing a custom Json serializer: public class NullSerializer extends JsonSerializer<Object>
I think a serializer would probably be overdoing it a little bit. This places the behavior inside the class inside the getter which is one of the first places one would look to find out "get" behavior.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.