Parse a YAML file

前端 未结 3 807
半阙折子戏
半阙折子戏 2020-11-27 06:53

This is the first time I am working with YAML files, so the first think I looked at was to find any library that could help me to parse the file.

I have found two li

相关标签:
3条回答
  • 2020-11-27 07:10

    You could also use Jacksons YAML module.

    In order to use that, you'll need a few classes. The model classes which will carry the content of your file and the a class that takes care of reading the YAML file.

    The root model class could look like this:

    public class MyYamlFile {
        @JsonProperty
        private List<User> users;
        @JsonProperty
        private List<Group> groups;
    
        // getter methods ommitted
    }
    

    The User(*) class:

    public class User {
        @JsonProperty
        private List<String> name;
        @JsonProperty
        private List<GroupType> groups;
    
        // getter methods ommitted
    }
    

    The GroupType could be an Enum containing all possible group types:

    public enum GroupType {
        Premium, Mod, Default
    }
    

    Don't forget that the enum entries are case sensitive. So "premium" won't work. You can build all your model classes that way. Every sub entry should get an own model class.

    Now to the part where you can read that YAML file:

    public MyYamlFile readYaml(final File file) {
        final ObjectMapper mapper = new ObjectMapper(new YAMLFactory()); // jackson databind
        return mapper.readValue(file, MyYamlFile.class);
    }
    

    As you can see, this part is really neat, because you don't need much. The file instance contains your YAML file. You can create one like this:

    File file = new File("path/to/my/yaml/usersAndGroups.yaml");
    

    Instead of File the readValue method also supports InputStream, java.io.Reader, String (with the whole content), java.net.URL and byte array. You should find something that suits you.

    (*) You should consider changing the structure of your YAML file, because I don't think it is possible to use dynamic keys with Jackson (maybe someone knows more about that):

    users: 
        - name: user1
          groups:
            - Premium
        - name: user2
          groups:
            - Mod
        - name: user3
          groups:
            - Default
    groups:
        ....
    
    0 讨论(0)
  • 2020-11-27 07:11

    YamlBean is included to DMelt Java numeric computational environment (http://jwork.org/dmelt/). You can create Yaml files using jhplot.io.HFileYAML class which creates a key-value map and save as yaml file.

    0 讨论(0)
  • 2020-11-27 07:37

    I ended up using SnakeYaml and made some split strings to solve my issue.

    Loaded the yaml file to Object and then into a Map, then split the result from the Map into String[] and then in a for loop I read out the name from the String[]. I did the same with groups.

    I know that there is better solutions out there but this is good enough for this project.

    Thanks all for the replies.

    0 讨论(0)
提交回复
热议问题