Jackson with JSON: Unrecognized field, not marked as ignorable

You can use Jackson’s class-level annotation: import com.fasterxml.jackson.annotation.JsonIgnoreProperties @JsonIgnoreProperties class { … } It will ignore every property you haven’t defined in your POJO. Very useful when you are just looking for a couple of properties in the JSON and don’t want to write the whole mapping. More info at Jackson’s website. If you want to … Read more

When is the @JsonProperty property used and what is it used for?

Here’s a good example. I use it to rename the variable because the JSON is coming from a .Net environment where properties start with an upper-case letter. public class Parameter { @JsonProperty(“Name”) public String name; @JsonProperty(“Value”) public String value; } This correctly parses to/from the JSON: “Parameter”:{ “Name”:”Parameter-Name”, “Value”:”Parameter-Value” }

Convert a Map to a POJO

Well, you can achieve that with Jackson, too. (and it seems to be more comfortable since you were considering using jackson). Use ObjectMapper‘s convertValue method: final ObjectMapper mapper = new ObjectMapper(); // jackson’s objectmapper final MyPojo pojo = mapper.convertValue(map, MyPojo.class); No need to convert into JSON string or something else; direct conversion does much faster.

How to specify jackson to only use fields – preferably globally

You can configure individual ObjectMappers like this: ObjectMapper mapper = new ObjectMapper(); mapper.setVisibility(mapper.getSerializationConfig().getDefaultVisibilityChecker() .withFieldVisibility(JsonAutoDetect.Visibility.ANY) .withGetterVisibility(JsonAutoDetect.Visibility.NONE) .withSetterVisibility(JsonAutoDetect.Visibility.NONE) .withCreatorVisibility(JsonAutoDetect.Visibility.NONE)); If you want it set globally, I usually access a configured mapper through a wrapper class.

How do I use a custom Serializer with Jackson?

You can put @JsonSerialize(using = CustomDateSerializer.class) over any date field of object to be serialized. public class CustomDateSerializer extends SerializerBase<Date> { public CustomDateSerializer() { super(Date.class, true); } @Override public void serialize(Date value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException { SimpleDateFormat formatter = new SimpleDateFormat(“EEE MMM dd yyyy HH:mm:ss ‘GMT’ZZZ (z)”); String format = formatter.format(value); … Read more