use of io.openems.common.exceptions.NotImplementedException in project openems by OpenEMS.
the class ConfigUtils method writeConfigToFile.
protected static synchronized void writeConfigToFile(Path path, TreeMap<String, Config> configs) throws IOException {
// create JsonObject
JsonObject j = new JsonObject();
for (Entry<String, Config> entry : configs.entrySet()) {
// ignore configs that should not be stored
if (entry.getValue().isDoNotStore()) {
continue;
}
JsonObject jSub = new JsonObject();
// sort map by key to be able to write the json sorted
TreeMap<String, Object> sortedSub = new TreeMap<>();
for (Entry<String, Object> subEntry : entry.getValue().entrySet()) {
sortedSub.put(subEntry.getKey(), subEntry.getValue());
}
for (Entry<String, Object> subEntry : sortedSub.entrySet()) {
if (subEntry.getKey().equals("service.pid")) {
// ignore. It's already the key of the JsonObject
continue;
}
try {
jSub.add(subEntry.getKey(), JsonUtils.getAsJsonElement(subEntry.getValue()));
} catch (NotImplementedException e) {
Log.warn("Unable to store [" + entry.getKey() + "/" + subEntry.getKey() + "] value [" + subEntry.getValue() + "] in config: " + e.getMessage());
}
}
j.add(entry.getKey(), jSub);
}
// write to file
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String config = gson.toJson(j);
Files.write(path, config.getBytes(DEFAULT_CHARSET));
}
Aggregations