Java YAML 读取动态变化文件的最佳方式



我正在尝试找到在Java中读取YAML文件的最佳方法。我不确定哪个解析器是最好的,但目前我读取动态的解决方案(如果用户想向文件添加更多内容,下次程序加载时)是一系列 for 循环,在 ArrayLists 和哈希映射之间转换并解析原始字符串。

前任:

HashMap<Object, Object> perWaveHashMap = (HashMap)waveConfiguration.getList("waves").get(wave); //wave-1
        for(Object mobObject : perWaveHashMap.values()){
            ArrayList<Object> mobObjectArrayList = (ArrayList) mobObject;
            for(Object mob : mobObjectArrayList){
                HashMap<Object, Object> mobHashMap = (HashMap)mob;
                String mobNameString = null;
                for(Object mobName : mobHashMap.keySet()){
                    mobNameString = mobName.toString();
                }
                for(Object mobAttribute : mobHashMap.values()){
                    HashMap<Object, Object> attributesToGive = (HashMap)mobAttribute;
                    final ArrayList<Object> attributeList = new ArrayList<>();
                    for(Object attribute : attributesToGive.values()){
                        attributeList.add(attribute);
                    }
                    assert mobNameString != null;
                    final String finalMobNameString = mobNameString;
                    System.out.println("Final Mob Name String: " + finalMobNameString);
                    if(finalMobNameString.equalsIgnoreCase("vintr")){
                        Mobs.isSpecialBossWave =true;
                        new Vintr();
                    } else {
                        if (attributeList.size() == 1) {
                            Mobs.addMobs((int) attributeList.get(0), EntityType.valueOf(finalMobNameString.toUpperCase()));
                        } else if (attributeList.size() == 2) {
                            Mobs.addMobs((int) attributeList.get(0), EntityType.valueOf(finalMobNameString.toUpperCase()), (int) attributeList.get(1));
                        } else if (attributeList.size() == 3) {
                            Mobs.addMobs((int) attributeList.get(0), EntityType.valueOf(finalMobNameString.toUpperCase()), (int) attributeList.get(1), (boolean) attributeList.get(2));
                        } else if (attributeList.size() == 4) {
                            Mobs.addMobs((int) attributeList.get(0), EntityType.valueOf(finalMobNameString.toUpperCase()), (int) attributeList.get(1), (boolean) attributeList.get(2), (boolean) attributeList.get(3));
                        } else if (attributeList.size() == 5) {
                            Mobs.addMobs((int) attributeList.get(0), EntityType.valueOf(finalMobNameString.toUpperCase()), (int) attributeList.get(1), (boolean) attributeList.get(2), (boolean) attributeList.get(3), (boolean) attributeList.get(4));
                        } else if (attributeList.size() == 6) {
                            Mobs.addMobs((int) attributeList.get(0), EntityType.valueOf(finalMobNameString.toUpperCase()), (int) attributeList.get(1), (boolean) attributeList.get(2), (boolean) attributeList.get(3), (boolean) attributeList.get(4), (boolean) attributeList.get(5));
                        }
                    }
                }
            }
        }

基本上,YML 文件如下所示:

waves:
  - 1:
    - zombie:
        amount: 1
        modifier: 2
        boss: false
        beeline: false
  - 1:
    - spider:
        amount: 12
        modifier: 2
        boss: false
        beeline: false
        hunter: true
  -2 :
    - mobname:
        -options

我的代码现在的方式允许我添加任意数量的"波",以及我想要的每波中任意数量的生物。我发现我编写代码的方式非常丑陋且难以使用,并且正在寻找解决此问题的更好方法。

欢迎任何帮助!

谢谢

您可以使用 snakeyaml 将 yaml 文件读入 POJO。

File configFile = new File(configFilename);
FileInputStream fis = new FileInputStream(configFile);
Yaml yaml = new Yaml(new Constructor(Config.class));
Config config = yaml.loadAs(fis, Config.class);

您的 Config 类可以如下所示:

public class Config {
     List<Wave> waves;
     public List<Wave> getWaves() {
          return waves;
     }
}
// similarly define other classes for the Wave etc...

阅读文档:https://bitbucket.org/asomov/snakeyaml/wiki/Home

最新更新