0

I am using Java and Jackson library to parse a JSON file to a Java class. My problem is that I can't set a default value if the value is null or empty. Is it possible to do it? For now, I just write it in the setters section.

For example, I have a JSON file

{"CountryCode": "",
  "sourceName": "",
  "from": "type1",
  "to": "type2"}

And in my Java class I would like to set the value to some Java function if it is null or empty.

For example

public class ExClass{
    @JsonProperty("CountryCode")
    private String countryCode = "USA";
}

So, if the value is set, then it will be it. If it is not, then it should be USA.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Vova
  • 38
  • 9
  • see [Setting default values to null fields when mapping with Jackson](//stackoverflow.com/q/18805455) – Tom Apr 16 '22 at 14:43
  • @Tom I have tried but it is not working. – Vova Apr 16 '22 at 14:50
  • @Vova What do you mean? What did you try? And what were the results? Please [edit] your question to include more details. – Code-Apprentice Apr 16 '22 at 15:02
  • It doesn't work because country code is not null, but is empty string. Try to use @JsonSetter to implement the method that will handle null or empty string – mpdgr Apr 16 '22 at 15:03
  • @mpdgr could you desribe how to do this in right way? Thank you. – Vova Apr 16 '22 at 15:23
  • @Code-Apprentice i tried to set @JsonInclude(JsonInclude.Include.NON_EMPTY) for class. And result was that value were empty instead of default value. Also tried to set @JsonSetter(contentNulls = JsonSetter.Nulls.AS_EMPTY) but it just skips it and set the defualt value in any way. – Vova Apr 16 '22 at 15:28
  • I thought about exactly the same solution Rakib descibed below – mpdgr Apr 16 '22 at 15:34
  • @mpdgr so the only way its the defenite the all setters in the pojo class, corect? – Vova Apr 16 '22 at 16:12
  • @Vova You could also write your own JSON parser, Jackson allows you to do that and provides tools for doing so. But you don't really want that. – Tom Apr 16 '22 at 16:25
  • @mpdgr got you))) – Vova Apr 16 '22 at 16:42

1 Answers1

1

We can make the JSON setter like the below:

@JsonSetter("CountryCode")
public void setCountryCode (String countryCode) {
    if (!countryCode.equals(null) && !countryCode.equals("")) { 
        this.countryCode = countryCode; 
    }
}

I think it will serve your requirement. If not work, let us know the details.

Ref: reference

Rakib Hasan
  • 317
  • 1
  • 5
  • if there some posibility to set it in another way? – Vova Apr 16 '22 at 15:29
  • I honestly wonder why those two users chose to upvote an obviously incorrect answer which was taken from another answer, thus making this question a duplicate and although already said "I have tried but it is not working". – Tom Apr 16 '22 at 15:35
  • @Tom I upvoted because imo this is exactly the correct answer, can you pls clarify what is wrong with it? – mpdgr Apr 16 '22 at 15:50
  • @mpdgr It is correct to compare String with `!=`? – Tom Apr 16 '22 at 16:05
  • Yeah, i gues its better to use isEmpty method for Strings – Vova Apr 16 '22 at 16:15
  • @Tom yes, this should be .equals but otherwise the approach is correct isn't it? – mpdgr Apr 16 '22 at 16:18
  • @mpdgr All the other stuff got copied from somewhere else. There is only one thing that got added and that could warrant a reward, but that one thing is wrong. – Tom Apr 16 '22 at 16:24