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());
}
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);
}
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());
}
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());
}
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());
}
Aggregations