Skip to content

Latest commit

 

History

History
66 lines (48 loc) · 1.23 KB

File metadata and controls

66 lines (48 loc) · 1.23 KB

YAMLParserJava

YAML decoder in Java.

Include in your project

  1. Puts src/HashTypeYaml.java in your project
  2. Sets any package to HashTypeYaml.java

Usage

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!"
  1. Create instance

    HashTypeYaml yaml = new HashTypeYaml();
  2. 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.

  3. 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.