Convert Java Properties to Object and Vise Versa
Sometimes we need to convert from Java Properties to a Java Object or vise versa. JavaPropsMapper from Jackson library class provides an easy way to do the conversion.
Properties to Object
Suppose we need to map database properties to DbConfig class, we can do it using readPropertiesAs
method.
@Data
static class DBConfig {
private List<String> hosts = new ArrayList<>();
private int port;
private String user;
private String password;
}
@Test
public void testConvertPropertiesToObject() throws IOException {
Properties properties = new Properties();
properties.setProperty("hosts[0]", "192.168.0.5");
properties.setProperty("hosts[1]", "192.168.0.6");
properties.setProperty("port", "3306");
properties.setProperty("user", "user1");
properties.setProperty("password", "password1");
JavaPropsMapper mapper = new JavaPropsMapper();
DBConfig config = mapper.readPropertiesAs(properties, DBConfig.class);
Assertions.assertEquals(3306, config.getPort());
Assertions.assertEquals(2, config.getHosts().size());
}
String Format Properties to Object
JavaPropsMapper also provide us a way to convert a string(in properties format) to a Java Object.
@Test
public void testConvertPropertiesFormatStringToObject() throws JsonProcessingException {
JavaPropsMapper mapper = new JavaPropsMapper();
String configString = """
hosts[0]=192.168.0.5
hosts[1]=192.168.0.6
port=3306
user=user1
password=password1
""";
DBConfig config = mapper.readValue(configString, DBConfig.class);
Assertions.assertEquals(3306, config.getPort());
Assertions.assertEquals(2, config.getHosts().size());
}
Read from Properties File and Convert as an Object
When you want to read properties from a file:
@Test
public void testConvertPropertiesFormatFileToObject() throws IOException {
JavaPropsMapper mapper = new JavaPropsMapper();
DBConfig config = mapper.readValue(new File("src/test/resources/db.properties"), DBConfig.class);
Assertions.assertEquals(3306, config.getPort());
Assertions.assertEquals(2, config.getHosts().size());
}
Convert an Object as Properties
The above cases show how to convert properties to a Java object, now let’s demonstrate how to convert the object as properties.
@Test
public void testConvertObjectToProperties() throws IOException {
DBConfig config = new DBConfig();
config.setPort(9999);
config.setHosts(Arrays.asList("192.168.0.5", "192.168.0.6"));
JavaPropsMapper mapper = new JavaPropsMapper();
Properties properties = mapper.writeValueAsProperties(config);
System.out.println(properties);
Assertions.assertEquals(9999, Integer.valueOf((String) properties.get("port")));
Assertions.assertEquals("192.168.0.6", properties.get("hosts.2"));
}
Convert an Object as Map<String, String>
Properties have a flat structure. JavaPropsMapper can help us convert the nested structures and collections as flat structure.
It may be a useful in some cases.
@Test
public void testConvertObjectToFlattenMap() throws IOException {
DBConfig config = new DBConfig();
config.setPort(9999);
config.setHosts(Arrays.asList("192.168.0.5", "192.168.0.6"));
Map<String, String> targetMap = Maps.newHashMap();
JavaPropsMapper mapper = new JavaPropsMapper();
mapper.writeValue(targetMap, config);
System.out.println(targetMap);
Assertions.assertEquals(9999, Integer.valueOf(targetMap.get("port")));
Assertions.assertEquals("192.168.0.6", targetMap.get("hosts.2"));
}