Reputation: 902
I am using jackson library and I have came across a situation where I want to disable @JsonFormat annotation using objectmapper while serialization/deserialization.
My Api code is in 3rd party library so i can't remove/add any annotation, so objectMapper is the only choice.
Api class:
public class ApiClass {
@JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss'Z'")
private DateTime time;
}
My code:
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
objectMapper.configure(Feature.ALLOW_COMMENTS, true);
objectMapper.configure(MapperFeature.AUTO_DETECT_IS_GETTERS, true);
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
objectMapper.setSerializationInclusion(Include.NON_ABSENT);
objectMapper.registerModule(new JodaModule());
objectMapper.registerModule(new JavaTimeModule());
String str = " {\"time\": \"2012-05-01\"}";
ApiClass msg = objectMapper.readValue(str, ApiClass.class);
I want this conversion to happen successfully.
Currently I am getting: com.fasterxml.jackson.databind.JsonMappingException: Invalid format: "2012-05-01" is too short
Please help me here.
Thanks in advance
Upvotes: 3
Views: 3602
Reputation: 902
Below is the code that will disable JsonFormat specifically:
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
objectMapper.configure(Feature.ALLOW_COMMENTS, true);
objectMapper.configure(MapperFeature.AUTO_DETECT_IS_GETTERS, true);
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
objectMapper.setSerializationInclusion(Include.NON_ABSENT);
objectMapper.registerModule(new JodaModule());
objectMapper.registerModule(new JavaTimeModule());
objectMapper.setAnnotationIntrospector(new JacksonAnnotationIntrospector() {
@Override
protected <A extends Annotation> A _findAnnotation(final Annotated annotated,
final Class<A> annoClass) {
if (!annotated.hasAnnotation(JsonFormat.class)) { //since we need to disable JsonFormat annotation.
return super._findAnnotation(annotated, annoClass);
}
return null;
}
});
String str = " {\"time\": \"2012-05-01\"}";
ApiClass msg = objectMapper.readValue(str, ApiClass.class);
System.out.println(objectMapper.writeValueAsString(msg ));
In case we need to disable multiple annotations(JsonFormat, JsonUnWrapped) then:
replace:
if (!annotated.hasAnnotation(JsonFormat.class)) {
with:
if (!annotated.hasOneOf(new Class[] {JsonFormat.class, JsonUnwrapped.class})) {
Thanks all.
Upvotes: 7
Reputation: 902
There is one solution for the problem.
Please check: https://www.baeldung.com/jackson-annotations Point no. 9 Disable Jackson Annotation
objectMapper.disable(MapperFeature.USE_ANNOTATIONS);
ApiClass msg = objectMapper.readValue(str, ApiClass.class);
sout(msg.getTime()) //2012-05-01T00:00:00.000Z
But this will disable all annotations. I just want to disable @JsonFormat annotation. Please suggest.
Upvotes: 0
Reputation: 2814
Try using custom deserializer:
public class MyTest {
@Test
public void myTest() throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
objectMapper.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
objectMapper.configure(MapperFeature.AUTO_DETECT_IS_GETTERS, true);
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_ABSENT);
objectMapper.registerModule(new JavaTimeModule());
String str = " {\"time\": \"2012-05-01\"}";
SimpleModule module = new SimpleModule();
module.addDeserializer(DateTime.class, new DateTimeDeserializer());
objectMapper.registerModule(module);
ApiClass msg = objectMapper.readValue(str, ApiClass.class);
System.out.println(msg.getTime());
}
}
class ApiClass {
@JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss'Z'")
private DateTime time;
public DateTime getTime() {
return time;
}
public void setTime(DateTime time) {
this.time = time;
}
}
class DateTimeDeserializer extends StdDeserializer<DateTime> {
public DateTimeDeserializer() {
this(null);
}
public DateTimeDeserializer(Class<?> vc) {
super(vc);
}
@Override
public DateTime deserialize(JsonParser jp, DeserializationContext ctxt)
throws IOException {
JsonNode node = jp.getCodec().readTree(jp);
String time = node.asText();
return new DateTime(time);
}
}
Upvotes: 0
Reputation: 1
You can use SimpleDateFormat to convert string type date into date type like below
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
try {
Date parse = simpleDateFormat.parse(String date);
return parse;
} catch (ParseException e) {
e.printStackTrace();
}
Upvotes: -1
Reputation: 660
Try below code before using readValue
method
mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"));
Upvotes: 0