Using Snakeyaml to read YAML configuration files

Source: Internet
Author: User

Many applications are beginning to use YAML as a program configuration file, about Yaml detailed introduction can refer to Yaml 1.2 (3rd Edition), this article uses Snakeyaml to parse Yaml. maven Dependencies

<dependency>
    <groupId>org.yaml</groupId>
    <artifactid>snakeyaml</artifactid >
    <version>1.17</version>
</dependency>
Sample sample.yml
version:1.0
released:2017-02-14

# Connection parameters
Connection:
    driverclass: Com.mysql.jdbc.Driver
    url:jdbc:mysql://localhost:3306/db
    username:root
    password:12345
    Poolsize:5

# Protocols
protocols:
   -HTTP
   -HTTPS

# developers
developers:
    Tom: Tom@gmail.com
    bob:bob@gmail.com
the corresponding JavaBean class

Connection.java

Package Com.bytebeats.code.yaml.model; /** * ${description} * * @author Ricky Fung * @date 2017-02-14 15:18 * * public class Connection {private String
    Driverclass;
    Private String URL;
    Private String username;
    private String password;

    private int poolsize;
    Public String Getdriverclass () {return driverclass;
    } public void Setdriverclass (String driverclass) {this.driverclass = Driverclass;
    Public String GetUrl () {return URL;
    public void SetUrl (String url) {this.url = URL;
    Public String GetUserName () {return username;
    } public void Setusername (String username) {this.username = username;
    Public String GetPassword () {return password;
    } public void SetPassword (String password) {this.password = password;
    public int getpoolsize () {return poolsize;
     } public void setpoolsize (int poolsize) {   This.poolsize = poolsize;
 }

}

Configuration.java

Package Com.bytebeats.code.yaml.model;
Import Java.util.Date;
Import java.util.List;

Import Java.util.Map;  /** * ${description} * * @author Ricky Fung * @date 2017-02-14 15:18 * * public class Configuration {private date
    Released;
    Private String version;
    Private Connection Connection;
    Private list<string> protocols;

    Private map<string, string> developers;
    Public Date getreleased () {return released;
    The public void Setreleased (Date released) {this.released = released;
    Public String GetVersion () {return version;
    public void Setversion (String version) {this.version = version;
    Public Connection getconnection () {return Connection;
    public void SetConnection (Connection Connection) {this.connection = Connection;
    Public list<string> Getprotocols () {return protocols; } public void Setprotocols (list<string> prOtocols) {this.protocols = protocols;
    Public map<string, String> getdevelopers () {return developers;
    public void Setdevelopers (map<string, string> developers) {this.developers = developers;
 }

}
parsing Yaml
Yaml Yaml = new Yaml ();
try {
    InputStream in = SnakeYAMLTest.class.getResourceAsStream ("/sample.yml");
    Configuration config = Yaml.loadas (in, configuration.class);
    System.out.println (config);
catch (Exception e) {
    e.printstacktrace ();
}

Serialization of

Configuration config = new Configuration ();
config.setreleased (New Date ());
Config.setversion ("1.0");

Connection Connection = new Connection ();
Connection.setdriverclass ("Com.mysql.jdbc.Driver");
Connection.seturl ("jdbc:mysql://localhost:3306/db");
Connection.setusername ("root");
Connection.setpassword ("root");
Connection.setpoolsize (ten);
Config.setconnection (connection);

Config.setprotocols (arrays.aslist ("http", "https"));
Config.setdevelopers (New hashmap<string, string> ());

Yaml Yaml = new Yaml ();
String output = yaml.dump (config);
SYSTEM.OUT.PRINTLN (output);
Source Code

Https://github.com/TiFG/daily-codelab/tree/master/yaml-sample

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.