Simple Java library for Map to POJO transformation 🧬
How to use. All you need is this to get the latest version:
<dependency>
<groupId>com.github.javaslang.map2pojo</groupId>
<artifactId>map2pojo</artifactId>
<version>1.1.2</version>
</dependency>
Transform a map to POJO:
POJO class:
public class SimplePojo {
private String firstName;
private BigDecimal height;
}
Transformation:
Map<String, Object> hashMap = new HashMap<String, Object>() {{
put("FIRST_NAME", "John");
put(" height_", "184.52");
}};
SimplePojo pojo = new Map2Pojo<>(SimplePojo.class).transform(hashMap);
Result:
SimplePojo(firstName=John, height=184.52)
Originally library supports string
, number
and date
value types and conversion.
Map2Pojo<TestPojoClass> testMap2Pojo = new Map2Pojo<>(
TestPojoClass.class,
new DefaultNormalization(),
new DefaultFillings()
);
TestPojoClass.class
‒ target POJO class
new DefaultNormalization()
‒ basic implementation of Function<String, String>
for keys normalization providing the correlation between keys in the map and field names in the POJO.
new DefaultFillings()
‒ basic implementation of Fillings
interface providing conversion of corresponding values from the original map to the POJO.
@Map2Pojo.OrderedFields
: provides the possibility for transformation basing on the order of keys / fields.
Retention: RUNTIME
.
Target: TYPE
.
Example:
POJO:
@Map2Pojo.OrderedFields
public static class OrderedTestPojo {
private String firstField;
private Date secondField;
private BigDecimal thirdField;
}
Original map:
new HashMap<String, Object>() {{
put("0", ...);
put("1", ...);
put("2", ...);
}};
@Map2Pojo.FormattedDate
: provides the possibility of from
/ to
date conversion basing on the specified format
value.
Retention: RUNTIME
.
Target: FIELD
.
Example:
POJO:
...
@Map2Pojo.FormattedDate("dd-MM-yyyy")
private Date annotatedTestDate;
@Map2Pojo.FormattedDate("dd-MM-yyyy")
private String dateAsString;
...
@Map2Pojo.FormattedDate("dd-MM-yyyy")
private LocalDate annotatedTestLocalDate;
@Map2Pojo.FormattedDate("yyyy-MM-dd-HH.mm.ss")
private LocalDateTime annotatedTestLocalDateTime;
...
Original map:
new HashMap<String, Object>() {{
...
put("Annotated Test Date", ...);
put("DATE_AS_STRING", ...);
...
}};
@Map2Pojo.Locale
: provides the possibility of number
value conversion basing on the locale
.
Retention: RUNTIME
.
Target: TYPE
, FIELD
.
Example:
POJO:
...
@Map2Pojo.Locale("de")
private BigDecimal rate;
...
@Map2Pojo.Locale("de")
public static class TestPojo {
...
}
Original map:
new HashMap<String, Object>() {{
...
put("RATE", "100,000000");
...
}});