Search in sources :

Example 6 with JsonFormat

use of com.fasterxml.jackson.annotation.JsonFormat in project jsonschema2pojo by joelittlejohn.

the class CustomDateTimeFormatIT method testDefaultWhenFormatDateTimesConfigIsTrue.

/**
     * This tests the class generated when formatDateTimes config option is set to TRUE
     * The field should have @JsonFormat annotation with iso8601 date time pattern and UTC timezone
     * It also tests the serialization and deserialization process
     * 
     * @throws Exception
     */
@Test
public void testDefaultWhenFormatDateTimesConfigIsTrue() throws Exception {
    Field field = classWhenConfigIsTrue.getDeclaredField("defaultFormat");
    JsonFormat annotation = field.getAnnotation(JsonFormat.class);
    assertThat(annotation, notNullValue());
    // Assert that the patterns match
    assertEquals("yyyy-MM-dd'T'HH:mm:ss.SSS", annotation.pattern());
    // Assert that the timezones match
    assertEquals("UTC", annotation.timezone());
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.setTimeZone(TimeZone.getTimeZone("UTC"));
    ObjectNode node = objectMapper.createObjectNode();
    node.put("defaultFormat", "2016-11-06T00:00:00.000");
    Object pojo = objectMapper.treeToValue(node, classWhenConfigIsTrue);
    Method getter = new PropertyDescriptor("defaultFormat", classWhenConfigIsTrue).getReadMethod();
    // Assert that the Date object in the deserialized class is as expected
    assertEquals(dateTimeMilliSecFormatter.parse("2016-11-06T00:00:00.000").toString(), getter.invoke(pojo).toString());
    JsonNode jsonVersion = objectMapper.valueToTree(pojo);
    // Assert that when the class is serialized, the date object is serialized as expected 
    assertEquals("2016-11-06T00:00:00.000", jsonVersion.get("defaultFormat").asText());
}
Also used : Field(java.lang.reflect.Field) JsonFormat(com.fasterxml.jackson.annotation.JsonFormat) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) PropertyDescriptor(java.beans.PropertyDescriptor) JsonNode(com.fasterxml.jackson.databind.JsonNode) Method(java.lang.reflect.Method) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Aggregations

JsonFormat (com.fasterxml.jackson.annotation.JsonFormat)6 Field (java.lang.reflect.Field)6 Test (org.junit.Test)6 JsonNode (com.fasterxml.jackson.databind.JsonNode)5 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)5 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)5 PropertyDescriptor (java.beans.PropertyDescriptor)5 Method (java.lang.reflect.Method)5