0

So I have been across with snakeyaml. I know how to get their keys using java yaml.load(inputStream); That would return a hashmap of a String and an Object. Just for demonstration I have a yaml file with values:

player:
  randie:
    score: 4

When I use

File file = new File("test")
FileInputStream stream = new FileInputStream(file);

Map<String, Object> map = yaml.load(stream);

for (String str : map.keySet()) {
   System.out.println(map.get(str));
}

The output would be:

{randie={score=4}}

I came across other stackoverflow questions such as this

I wanted to get a value in the innermost "nested" value without using a list of hashmaps provided by the correct answer in the thread

thanks

BrokenEarth
  • 128
  • 1
  • 10

1 Answers1

0

You can create classes that represent the structure of the yaml file and then load it in the correct format.

public class Result {
    private Player player;

    public Player getPlayer() {
        return player;
    }

    public void setPlayer(Player player) {
        this.player = player;
    } 
}

public class Player {
    private Randie randie;

    public Randie getRandie() {
        return randie;
    }

    public void setRandie(Randie randie) {
        this.randie = randie;
    } 
}

public class Randie {
    private int score;

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    } 
}

and then load it using the loadAs function.

public class YamlDataInterpreter {

    public static void main(String[] args) {
        YamlDataInterpreter intepreter = new YamlDataInterpreter();
        intepreter.interpretYaml();
    }

    public void interpretYaml() {
        InputStream stream = this.getClass().getClassLoader()
            .getResourceAsStream("data.yml");

        Yaml yaml = new Yaml();
        Result res = yaml.loadAs(stream, Result.class);
        System.out.println(res.getPlayer().getRandie().getScore());
    }
}

https://github.com/KenavR/snakeyaml-example

KenavR
  • 3,769
  • 9
  • 37
  • 47
  • Thanks, though right now I am making a "SimpleYaml". I don't really need objects though! – BrokenEarth Aug 21 '18 at 23:02
  • Then you can look into Reflection https://stackoverflow.com/questions/2989560/how-to-get-the-fields-in-an-object-via-reflection https://www.oracle.com/technetwork/articles/java/javareflection-1536171.html or just read in the string and extract the value manually. – KenavR Aug 21 '18 at 23:27
  • Yep. Reflection is an api for accessing classes, fields, and methods in code. Though maybe you didn't understand what I said. I needed to get a value without making a class. Basically, read from a file. This will save memory space – BrokenEarth Aug 21 '18 at 23:33
  • I did understand it, but reflection isn't really something I would recommend for a beginner. If you already know of reflection, what do you need then? Someone implementing it with reflection? Are you working on a resource constraint system? If yes, why load a library in the first place? Why not just work with the string? – KenavR Aug 21 '18 at 23:41
  • ahh Thanks anyways. I just need to create a parser class, I guess. Again, thanks for help – BrokenEarth Aug 21 '18 at 23:43