Skip to content
Advertisement

Multiple Lines Snakeyaml

I wanted to know how to do this in yaml on java:

admins:
  test:
    id: 1234

I only managed to do it without “subfields”, like this:

address: Star City
id.test: 19
name: John
department: Medical

with this code:

        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("id","a");
        dataMap.put("name", "John");
        dataMap.put("address", "Star City");
        dataMap.put("department", "Medical");
        DumperOptions options = new DumperOptions();
        options.setIndent(2);
        options.setPrettyFlow(true);
        options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
        Yaml yaml = new Yaml(options);
        PrintWriter writer = null;
        try {
            writer = new PrintWriter(new File("./src/main/resources/admins.yml"));
            yaml.dump(dataMap, writer);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

Advertisement

Answer

If you want a nested YAML structure, create a nested data structure:

dataMap.put("admins", Map.of("test", Map.of("id", 1234)));

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement