Importing YAML files into Java

Feb 23, 2023Sellers Industries

There are many libraries to import a YAML into Java, but we will specifically be using the SnakeYAML library. We will be using Maven or Gradle to import Snake YAML and will then import each JSON object into a native Java Object. After importing the YAML file you do what you need to with the data.


What is YAML?

The YAML file format is used for storing serialized data in a human-readable form and is often used for writing configuration files. One popular example of a YAML in the real world is the Docker configuration file. YAML is so popular becuase of it's easy to read structure (which is white-space based) and it's ease of use when importing into programming languages.

# Example YAML File
name: Lara Croft
address:
    street: 38 Wrangler Court
    city: Huntsville
    state: AL
    zip: 35803
ipv4: 55.194.35.171
friends:
    - Alex Weiss
    - Jonah Maiava

Importing SnakeYAML

Maven

<dependencies>
    <!-- OTHER DEPENDENCIES... -->
    <dependency>
        <groupId>org.yaml</groupId>
        <artifactId>snakeyaml</artifactId>
        <version>2.0</version>
    </dependency>
</dependencies>

Gradle

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.yaml:snakeyaml:2.0'
}

Basic Usage

This example utilizes the basic implementation where the YAML file is loaded into a map of objects. You can then access this map and check each object's type and cast to the appropriate type.

// Acccess File
String      filename    = "user-lara-croft.yaml";
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(filename);
Map<String, Object> obj = (new Yaml()).load(inputStream);
System.out.println(obj); // {name=Lara Croft, address={street=38 Wrangler Court, city=Huntsville, state=AL, zip=35803}, ipv4=55.194.35.171, friends=[Alex Weiss, Jonah Maiava]}

// Access Name Data
String name = null;
if (rule.get("name") instanceof String) {
    name = rule.get("name");
}
System.out.println(name); // Lara Croft

// Access Friends Data
List<String> friends = new ArrayList<>();
if (obj.get("friends") instanceof List) {
    friends = (List<String>) obj.get("friends");
}
System.out.println(friends); // [Alex Weiss, Jonah Maiava]

While this is a perfect valid implementation when you need a more flexible design, there is a much safer and structured approach.


Class Usage

This is a basic example on how to load nested objects from a YAML file, but there is many additional options that can be enabled to help you import your YAML file. These options can be explored further in the Snake YAML Documentation.

// User Object
public class User {
    public String  name;
    public Address address;
    public String  ipv4;
    public ArrayList<String> friends;
}

// Address Object
public class Address {
    public String street;
    public String city;
    public String state;
    public int    zip;
}

// Loader
String      filename = "user-lara-croft.yaml";
InputStream stream   = new FileInputStream(filename);
User        user     = (new Yaml(new Constructor(User.class))).load(stream);
System.out.println(user.name);         // Lara Croft
System.out.println(user.ipv4);         // 55.194.35.171
System.out.println(user.friends);      // [Alex Weiss, Jonah Maiava]
System.out.println(user.address.city); // Huntsville
System.out.println(user.address.zip);  // 35803

As you can see, using Snake YAML we can easily import YAML files into Java. The YAML objects can be either manually or automatically converted into a Java class object. Don't forget to check out some of our other articles to see how to work with other data formats in other languages!

Related Articles

Sellers Industries Logo
Copyright (C) 2023 Sellers Industries, Inc. - All Rights Reserved

Exodus 35:31