Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java SnakeYAML:以不同的方式创建某些对象_Java_Yaml_Snakeyaml - Fatal编程技术网

Java SnakeYAML:以不同的方式创建某些对象

Java SnakeYAML:以不同的方式创建某些对象,java,yaml,snakeyaml,Java,Yaml,Snakeyaml,我无法更改程序中的以下类结构: class Node { public String name; } class Nodes { public List<Node> nodes; } class Master { public Nodes nodes; } SnakeYaml是否有可能省略第一个“节点”:使用某种自定义对象实例化逻辑,以便我的客户可以只使用以下YAML master: nodes: - name: test 我尝试使用自定

我无法更改程序中的以下类结构:

class Node {
    public String name;
}

class Nodes  {
    public List<Node> nodes;
}

class Master {
    public Nodes nodes;
}
SnakeYaml是否有可能省略第一个“节点”:使用某种自定义对象实例化逻辑,以便我的客户可以只使用以下YAML

master:
  nodes:
    - name: test
我尝试使用自定义构造函数实现,但没有成功:

class MyConstructor extends Constructor {
    MyConstructor() {
        yamlClassConstructors.put(NodeId.mapping, new NodesConstructor());
    }

    class NodesConstructor extends Constructor.ConstructMapping {
        @Override
        protected Object constructJavaBean2ndStep(MappingNode node, Object object) {
            Class type = node.getType();

            if (type.equals(Master.class)) {
                Nodes nodes = new Nodes();
                //FIXME: I don't want to construct the whole object tree here, I only want to fill the nodes
                nodes.nodes = new ArrayList<>();
                Master master = new Master();
                master.nodes = nodes;
                return master;
            } else {
                return super.constructJavaBean2ndStep(node, object);
            }
        }
    }
}

有什么想法吗?提前感谢。

我最终解决这个问题的方法是手动转换基础映射,将其转储为字符串,然后使用我的DSL包装类再次加载它:

Yaml yaml2 = new Yaml();
Map map = yaml2.loadAs("master:\n  nodes:\n    - name: mystage", Map.class);
Map master = (Map) map.get("master");
List nodes = (List) master.get("nodes");
Map newNodes = new HashMap();
newNodes.put("nodes", nodes);
master.put("nodes", newNodes);
String modifiedDsl = yaml.dump(map);
MyDsl myDsl2 = yaml2.loadAs(modifiedDsl, MyDsl.class);

也许不是最漂亮的解决方案,但它确实有效。还有一个问题是如何在另一个方向上使用它(为DSL对象生成YAML)。

为什么不预处理YAML文件并删除中间的
节点呢。这需要大约5行的python程序。谢谢。虽然这将是一种选择,但我正在寻找一种通过SnakeYAML本身实现的方法。此外,我还想从一个对象树生成这个YAML(没有中间
节点
)。我想一定有办法,我只是到目前为止找不到。。。
class MyDsl {
    public Master master;
}

public class SnakeYaml {
    public static void main(String[] args) throws IOException {
        // 1: this is working OK
        Yaml yaml = new Yaml();
        MyDsl myDsl = yaml.loadAs("master:\n  nodes:\n    nodes:\n      - name: mystage", MyDsl.class);
        if(!myDsl.master.nodes.nodes.get(0).name.equals("mystage")) {
            throw new AssertionError("Failed with nested nodes");
        }

        // 2: this is how I need it
        Yaml yaml2 = new Yaml(new MyConstructor());
        MyDsl myDsl2 = yaml2.loadAs("master:\n  nodes:\n    - name: mystage", MyDsl.class);
        if(!myDsl2.master.nodes.nodes.get(0).name.equals("mystage")) {
            throw new AssertionError("Failed with leaving out nodes");
        }
    }
}
Yaml yaml2 = new Yaml();
Map map = yaml2.loadAs("master:\n  nodes:\n    - name: mystage", Map.class);
Map master = (Map) map.get("master");
List nodes = (List) master.get("nodes");
Map newNodes = new HashMap();
newNodes.put("nodes", nodes);
master.put("nodes", newNodes);
String modifiedDsl = yaml.dump(map);
MyDsl myDsl2 = yaml2.loadAs(modifiedDsl, MyDsl.class);