我们想知道是否可以将YAML配置对象扁平化?例如,我们的配置文件如下所示
vertx:
verticle:
instance: 1
metrics:
options:
enabled: true我们只想通过一个操作访问我们的配置值,例如:
config.getInteger("vertx.verticle.instance")而不是必须这样做:
config.getJsonObject("vertx").getJsonObject("verticle").getInteger("integer")谢谢。
发布于 2020-05-12 12:35:28
来自RFC6901的Vert.x supports Json指针。你可以这样做:
JsonPointer pointer = JsonPointer.from("/vertx/verticle/instance");
Integer instance = (Integer) pointer.queryJson(config);发布于 2020-05-11 23:28:33
虽然API不直接支持它,但它看起来很容易自己实现:
public class FlatConfig {
private final JsonObject root;
public FlatConfig(JsonObject root) {
this.root = root;
}
private JsonObject walk(String[] path) {
JsonObject cur = root;
// skip last element since it contains the value
for (int i = 0; i < path.length - 2; i++) {
cur = cur.getJsonObject(path[i]);
}
return cur;
}
public Integer getInteger(String path) {
final String[] splitPath = path.split(".");
return walk(splitPath).getInteger(splitPath[splitPath.length - 1]);
}
}您可以根据需要添加用于检索其他类型的其他方法。
https://stackoverflow.com/questions/61731904
复制相似问题