Michael Rueegg
Michael Rueegg

Reputation: 765

SnakeYAML: Creating some objects differently

I have the following class structure in a program which I cannot change:

class Node {
    public String name;
}

class Nodes  {
    public List<Node> nodes;
}

class Master {
    public Nodes nodes;
}

I can use the following YAML to initialize it:

master:
  nodes:
    nodes:
      - name: test

Is there any possibility with SnakeYaml to leave out the first "nodes:" with some kind of custom object instantiation logic so that my customers can just use the following YAML?

master:
  nodes:
    - name: test

I tried with a custom constructor implementation, but didn't get this working:

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);
            }
        }
    }
}

At the end, this is what I like to get working:

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");
        }
    }
}

Any ideas? Thanks in advance.

Upvotes: 1

Views: 2235

Answers (1)

Michael Rueegg
Michael Rueegg

Reputation: 765

What I finally did to solve this issue is to manually transform the underlying map, dump it into a string and load it again with my DSL wrapper class:

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);

Probably not the most beautiful solution, but it works. What's still open is how to use this in the other direction (generating YAML for the DSL object).

Upvotes: 1

Related Questions