Search in sources :

Example 1 with JsonFormat

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

the class CustomDateTimeFormatIT method testCustomDateTimePatternWithDefaultTimezoneWhenFormatDateTimesConfigIsTrue.

/**
     * This tests the class generated when formatDateTimes config option is set to TRUE
     * The field should have @JsonFormat annotation with pattern defined in json schema and UTC timezone
     * It also tests the serialization and deserialization process
     * 
     * @throws Exception
     */
@Test
public void testCustomDateTimePatternWithDefaultTimezoneWhenFormatDateTimesConfigIsTrue() throws Exception {
    Field field = classWhenConfigIsTrue.getDeclaredField("customFormatDefaultTZ");
    JsonFormat annotation = field.getAnnotation(JsonFormat.class);
    assertThat(annotation, notNullValue());
    // Assert that the patterns match
    assertEquals("yyyy-MM-dd'T'HH:mm:ss", 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("customFormatDefaultTZ", "2016-11-06T00:00:00");
    Object pojo = objectMapper.treeToValue(node, classWhenConfigIsTrue);
    Method getter = new PropertyDescriptor("customFormatDefaultTZ", classWhenConfigIsTrue).getReadMethod();
    // Assert that the Date object in the deserialized class is as expected
    assertEquals(dateTimeFormatter.parse("2016-11-06T00:00:00").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", jsonVersion.get("customFormatDefaultTZ").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)

Example 2 with JsonFormat

use of com.fasterxml.jackson.annotation.JsonFormat in project typescript-generator by vojtechhabarta.

the class Jackson2Parser method parseEnumOrObjectEnum.

private DeclarationModel parseEnumOrObjectEnum(SourceType<Class<?>> sourceClass) {
    final JsonFormat jsonFormat = sourceClass.type.getAnnotation(JsonFormat.class);
    if (jsonFormat != null && jsonFormat.shape() == JsonFormat.Shape.OBJECT) {
        return parseBean(sourceClass);
    }
    final boolean isNumberBased = jsonFormat != null && (jsonFormat.shape() == JsonFormat.Shape.NUMBER || jsonFormat.shape() == JsonFormat.Shape.NUMBER_FLOAT || jsonFormat.shape() == JsonFormat.Shape.NUMBER_INT);
    final List<EnumMemberModel> enumMembers = new ArrayList<>();
    if (sourceClass.type.isEnum()) {
        final Class<?> enumClass = (Class<?>) sourceClass.type;
        try {
            Method valueMethod = null;
            final BeanInfo beanInfo = Introspector.getBeanInfo(enumClass);
            for (PropertyDescriptor propertyDescriptor : beanInfo.getPropertyDescriptors()) {
                final Method readMethod = propertyDescriptor.getReadMethod();
                if (readMethod.isAnnotationPresent(JsonValue.class)) {
                    valueMethod = readMethod;
                }
            }
            int index = 0;
            for (Field field : enumClass.getFields()) {
                if (field.isEnumConstant()) {
                    if (isNumberBased) {
                        final Number value = getNumberEnumValue(field, valueMethod, index++);
                        enumMembers.add(new EnumMemberModel(field.getName(), value, null));
                    } else {
                        final String value = getStringEnumValue(field, valueMethod);
                        enumMembers.add(new EnumMemberModel(field.getName(), value, null));
                    }
                }
            }
        } catch (Exception e) {
            System.out.println(String.format("Cannot get enum values for '%s' enum", enumClass.getName()));
            e.printStackTrace(System.out);
        }
    }
    return new EnumModel(sourceClass.type, isNumberBased ? EnumKind.NumberBased : EnumKind.StringBased, enumMembers, null);
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) BeanInfo(java.beans.BeanInfo) Method(java.lang.reflect.Method) EnumMemberModel(cz.habarta.typescript.generator.compiler.EnumMemberModel) Field(java.lang.reflect.Field) JsonFormat(com.fasterxml.jackson.annotation.JsonFormat)

Example 3 with JsonFormat

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

the class CustomDateTimeFormatIT method testCustomDateTimePatternWithCustomTimezoneWhenFormatDateTimesConfigIsFalse.

/**
     * This tests the class generated when formatDateTimes config option is set to FALSE
     * The field should have @JsonFormat annotation with pattern and timezone defined in json schema
     * It also tests the serialization and deserialization process
     * 
     * @throws Exception
     */
@Test
public void testCustomDateTimePatternWithCustomTimezoneWhenFormatDateTimesConfigIsFalse() throws Exception {
    Field field = classWhenConfigIsFalse.getDeclaredField("customFormatCustomTZ");
    JsonFormat annotation = field.getAnnotation(JsonFormat.class);
    assertThat(annotation, notNullValue());
    // Assert that the patterns match
    assertEquals("yyyy-MM-dd", annotation.pattern());
    // Assert that the timezones match
    assertEquals("PST", annotation.timezone());
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.setTimeZone(TimeZone.getTimeZone("PST"));
    ObjectNode node = objectMapper.createObjectNode();
    node.put("customFormatCustomTZ", "2016-11-06");
    Object pojo = objectMapper.treeToValue(node, classWhenConfigIsFalse);
    Method getter = new PropertyDescriptor("customFormatCustomTZ", classWhenConfigIsFalse).getReadMethod();
    // Assert that the Date object in the deserialized class is as expected
    assertEquals(dateFormatter.parse("2016-11-06").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-06", jsonVersion.get("customFormatCustomTZ").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)

Example 4 with JsonFormat

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

the class CustomDateTimeFormatIT method testCustomDateTimePatternWithCustomTimezoneWhenFormatDateTimesConfigIsTrue.

/**
     * This tests the class generated when formatDateTimes config option is set to TRUE
     * The field should have @JsonFormat annotation with pattern and timezone defined in json schema
     * It also tests the serialization and deserialization process
     * 
     * @throws Exception
     */
@Test
public void testCustomDateTimePatternWithCustomTimezoneWhenFormatDateTimesConfigIsTrue() throws Exception {
    Field field = classWhenConfigIsTrue.getDeclaredField("customFormatCustomTZ");
    JsonFormat annotation = field.getAnnotation(JsonFormat.class);
    assertThat(annotation, notNullValue());
    // Assert that the patterns match
    assertEquals("yyyy-MM-dd", annotation.pattern());
    // Assert that the timezones match
    assertEquals("PST", annotation.timezone());
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.setTimeZone(TimeZone.getTimeZone("PST"));
    ObjectNode node = objectMapper.createObjectNode();
    node.put("customFormatCustomTZ", "2016-11-06");
    Object pojo = objectMapper.treeToValue(node, classWhenConfigIsTrue);
    Method getter = new PropertyDescriptor("customFormatCustomTZ", classWhenConfigIsTrue).getReadMethod();
    // Assert that the Date object in the deserialized class is as expected
    assertEquals(dateFormatter.parse("2016-11-06").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-06", jsonVersion.get("customFormatCustomTZ").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)

Example 5 with JsonFormat

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

the class CustomDateTimeFormatIT method testCustomDateTimePatternWithDefaultTimezoneWhenFormatDateTimesConfigIsFalse.

/**
     * This tests the class generated when formatDateTimes config option is set to FALSE
     * The field should have @JsonFormat annotation with pattern defined in json schema and UTC timezone
     * It also tests the serialization and deserialization process
     * 
     * @throws Exception
     */
@Test
public void testCustomDateTimePatternWithDefaultTimezoneWhenFormatDateTimesConfigIsFalse() throws Exception {
    Field field = classWhenConfigIsFalse.getDeclaredField("customFormatDefaultTZ");
    JsonFormat annotation = field.getAnnotation(JsonFormat.class);
    assertThat(annotation, notNullValue());
    // Assert that the patterns match
    assertEquals("yyyy-MM-dd'T'HH:mm:ss", 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("customFormatDefaultTZ", "2016-11-06T00:00:00");
    Object pojo = objectMapper.treeToValue(node, classWhenConfigIsFalse);
    Method getter = new PropertyDescriptor("customFormatDefaultTZ", classWhenConfigIsFalse).getReadMethod();
    // Assert that the Date object in the deserialized class is as expected
    assertEquals(dateTimeFormatter.parse("2016-11-06T00:00:00").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", jsonVersion.get("customFormatDefaultTZ").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)7 Field (java.lang.reflect.Field)7 PropertyDescriptor (java.beans.PropertyDescriptor)6 Method (java.lang.reflect.Method)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 EnumMemberModel (cz.habarta.typescript.generator.compiler.EnumMemberModel)1 BeanInfo (java.beans.BeanInfo)1