Carleslc / Simple-YAML

This Java API provides an easy-to-use way to store data and provide configurations using the YAML format.
https://carleslc.me/Simple-YAML
GNU General Public License v3.0
130 stars 38 forks source link

Question: Is it possible to return values from an existing config? #33

Closed Osiris-Team closed 4 years ago

Osiris-Team commented 4 years ago

Lets say I have a file called person.yml and I want to get the values for "name" and "last-name".

name: "John"
last-name: "Stamos"

Is there a way doing this with simple yaml?

portlek commented 4 years ago

there are getValues(boolean) and getMapValue(boolean) methods to get values of the each section.

Osiris-Team commented 4 years ago

@portlek Lets take it a step further and say that I don't know the path ("name" and "last-name") but I want to get a list or map containing all information.

portlek commented 4 years ago

as i said, you can get the values with getValues and getMapValues methods, you don't have to know these name or last-name section.

Osiris-Team commented 4 years ago

Ok great thx

portlek commented 4 years ago

when you use getValues(boolean) method it returns a map that looks like:

{
"name": "John",
"last-name": "Stamos"
}
Osiris-Team commented 4 years ago

@portlek Another question. Will this still work if we talk about a yml file that only exists in the memory?

portlek commented 4 years ago

Probably yes, the library is using MemorySection so it stores in the memory, and you will get these values from it.

Osiris-Team commented 4 years ago

@portlek Thanks for helping btw. But there is something more ^^ The map returns an object, but i need to know what object it is(List, String...). How do I do that?

portlek commented 4 years ago

just use instanceof which is a Java feature.

Carleslc commented 4 years ago

For single values you can use get(String path). If you know types you can use direct methods like getString or getInt. If you don't know the path of keys then use getValues or getMapValues.

If you know the path of keys but don't know the type of the value there are methods to check its type like isString or isInt.

Have a look to all available methods of ConfigurationSection here.

And yes, all changes are made in memory. File is only read on load and written on save. You can even have a YamlFile without any file too (entirely in memory).