YAML decoder in Java.
- Puts
src/HashTypeYaml.java
in your project - Sets any package to HashTypeYaml.java
For example, decodes following YAML as test1.yml file.
test1:
id: 1234
country: "Japan"
code:
data:
users:
- Alice
- Bob
- Chris
title: "ABC Team"
description: "ABC Team is the best!"
-
Create instance
HashTypeYaml yaml = new HashTypeYaml();
-
Decode YAML and convert to Map object
try { Map<String, Object> map = yaml.decode(new FileInputStream("test1.yml")); } catch (IOException e) { // Error handling }
Or
Map<String, Object> map = yaml.decodeOrEmpty("test1.yml");
decodeOrEmpty
does not raise the exception and returns empty map if I/O error is occurred. -
Get value
// Get id. System.out.println(map.get("test1/id")); // => 1234 // Get title. System.out.println(map.get("test1/data/title")); // => ABC Team // Get users. List users = (List) map.get("test1/data/users"); System.out.println(users.get(1)); // => Bob
More info, see my sample code.