Created
December 17, 2022 17:11
-
-
Save rnkoaa/36b7ded8c158caef959fe53b4c3e9551 to your computer and use it in GitHub Desktop.
Convert a Yaml File to a Java Properties of Map<String, Object>
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void main(String[] args) { | |
try (var inputStream = Main.class.getClassLoader().getResourceAsStream("application.yml")) { | |
var yamlToProperties = new YamlToProperties(inputStream); | |
Map<String, Object> stringObjectMap = yamlToProperties.asProperties(); | |
stringObjectMap.forEach((key, value) -> { | |
System.out.println("Key: " + key + ", Value: " + value); | |
}); | |
} catch (IOException e) { | |
System.out.println(e.getMessage()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.InputStream; | |
import java.util.Arrays; | |
import java.util.Map; | |
import java.util.Objects; | |
import java.util.TreeMap; | |
import java.util.stream.Collectors; | |
import org.yaml.snakeyaml.Yaml; | |
public class YamlToProperties { | |
record Pair(String key, String value) {} | |
static final Yaml yaml = new Yaml(); | |
TreeMap<String, Map<String, Object>> config; | |
@SuppressWarnings({"raw_types", "unchecked"}) | |
public YamlToProperties(String content) { | |
this.config = (TreeMap<String, Map<String, Object>>) yaml.loadAs(content, TreeMap.class); | |
} | |
@SuppressWarnings({"raw_types", "unchecked"}) | |
public YamlToProperties(InputStream inputStream) { | |
this.config = (TreeMap<String, Map<String, Object>>) yaml.loadAs(inputStream, TreeMap.class); | |
} | |
@Override | |
public String toString() { | |
return toProperties(config); | |
} | |
public Map<String, Object> asProperties() { | |
return toPropertyMap(config); | |
} | |
private static String toProperties(final TreeMap<String, Map<String, Object>> config) { | |
StringBuilder sb = new StringBuilder(); | |
for (final String key : config.keySet()) { | |
sb.append(toString(key, config.get(key))); | |
} | |
return sb.toString(); | |
} | |
private static Map<String, Object> toPropertyMap(final TreeMap<String, Map<String, Object>> config) { | |
return config.keySet().stream() | |
.map(it -> toString(it, config.get(it))) | |
.filter(it -> !it.isEmpty()) | |
.flatMap(it -> { | |
String[] lines = it.split("\n"); | |
return Arrays.stream(lines) | |
.map(line -> { | |
String[] propertyParts = line.split("="); | |
if (propertyParts.length == 2) { | |
return new Pair(propertyParts[0], propertyParts[1]); | |
} | |
return null; | |
}); | |
}) | |
.filter(Objects::nonNull) | |
.collect(Collectors.toMap(Pair::key, Pair::value)); | |
} | |
@SuppressWarnings("unchecked") | |
private static String toString(final String key, final Object o) { | |
StringBuilder sb = new StringBuilder(); | |
if (o instanceof Map) { | |
Map<String, Object> map = (Map<String, Object>) o; | |
for (final String mapKey : map.keySet()) { | |
if (map.get(mapKey) instanceof Map) { | |
sb.append(toString(String.format("%s.%s", key, mapKey), map.get(mapKey))); | |
} else { | |
sb.append(String.format("%s.%s=%s%n", key, mapKey, | |
(null == map.get(mapKey)) ? null : map.get(mapKey).toString())); | |
} | |
} | |
} else { | |
sb.append(String.format("%s=%s%n", key, o)); | |
} | |
return sb.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment