Class YamlData<Model>

java.lang.Object
com.github.ngoanh2n.YamlData<Model>

public abstract class YamlData<Model> extends Object
Read Yaml file.

Static
Read Yaml file to Map, List of Maps.
  1. Read to Map
    • Map<String, Object> map = YamlData.toMapFromResource("file.json")
    • Map<String, Object> map = YamlData.toMapFromFile("src/test/resources/file.json")
  2. Read to List of Maps
    • List<Map<String, Object>> maps = YamlData.toMapsFromResource("file.json")
    • List<Map<String, Object>> maps = YamlData.toMapsFromFile("src/test/resources/file.json")
Inheritance
Read Yaml file to Model, List of Models.
Model class must be public and has setter methods.
  1. Read to Model
    Yaml: user.yml
    
                          username: usr1
                          notes:
                              - note1
                              - note2
                          companies:
                              - name: Com1
                                address: Addr1
                              - name: Com2
                                address: Addr2
                      
    Model: User.java
    
                          public class User extends YamlData<User> {
                              private String username;
                              private List<String> notes;
                              private List<Company> companies;
    
                              ...GETTERS & SETTERS...
                          }
                      
    Model: Company.java
    
                          public class Company extends YamlData<Company> {
                              private String name;
                              private String address;
    
                              ...GETTERS & SETTERS...
                          }
                      
    • Without annotation
      • User user = new User().fromResource("user.yml").toModel()
      • User user = new User().fromFile("src/test/resources/user.yml").toModel()
    • With annotation
      Attach com.github.ngoanh2n.YamlFrom annotation for Model.
      • YamlFrom.resource()
        
                                       &#064;YamlFrom(resource = "user.yml")
                                       public class User extends YamlData<User> {
                                         ...
                                       }
                                   
      • YamlFrom.file()
        
                                       &#064;YamlFrom(file = "src/test/resources/user.yml")
                                       public class User extends YamlData<User> {
                                         ...
                                       }
                                   
      Overwrite value of com.github.ngoanh2n.YamlFrom annotation by calling fromResource(String) or fromFile(String) method.
  2. Read to List of Models
    Yaml: accounts.yml
    
                          - username: usr1
                            password: pwd1
                          - username: usr2
                            password: pwd2
                      
    Model: User.java
    
                          public class User extends YamlData<User> {
                              private String username;
                              private String password;
    
                              ...GETTERS & SETTERS...
                          }
                      
    • Without annotation
      • List<User> users = new User().fromResource("users.yml").toModels()
      • List<User> users = new User().fromFile("src/test/resources/users.yml").toModels()
    • With annotation
      Attach com.github.ngoanh2n.YamlFrom annotation for Model.
      • YamlFrom.resource()
        
                                       &#064;YamlFrom(resource = "users.yml")
                                       public class User extends YamlData<User> {
                                         ...
                                       }
                                   
      • YamlFrom.file()
        
                                       &#064;YamlFrom(file = "src/test/resources/users.yml")
                                       public class User extends YamlData<User> {
                                         ...
                                       }
                                   
      Overwrite value of com.github.ngoanh2n.YamlFrom annotation by calling fromResource(String) or fromFile(String) method.
Repository:
Since:
2019
  • Constructor Details

    • YamlData

      public YamlData()
      Construct a new YamlData and get current Java Bean class.
  • Method Details

    • toMapFromFile

      public static Map<String,Object> toMapFromFile(String name)
      Read Yaml file as Map.
      Parameters:
      name - The name of file.
      e.g. src/test/resources/com/foo/File.yml
      Returns:
      Map if the file exists.
      RuntimeError if the file doesn't exist or read multiple object as single object.
    • toMapFromFile

      public static Map<String,Object> toMapFromFile(String name, Charset cs)
      Read Yaml file as Map.
      Parameters:
      name - The name of file.
      e.g. src/test/resources/com/foo/File.yml
      cs - A Charset.
      Returns:
      Map if the file exists.
      RuntimeError if the file doesn't exist or read multiple object as single object.
    • toMapFromResource

      public static Map<String,Object> toMapFromResource(String name)
      Read Yaml file as Map.
      Parameters:
      name - The name of resource.
      e.g. com/foo/File.yml
      Returns:
      Map if the file exists.
      RuntimeError if the file doesn't exist or read multiple object as single object.
    • toMapFromResource

      public static Map<String,Object> toMapFromResource(String name, Charset cs)
      Read Yaml file as Map.
      Parameters:
      name - The name of resource.
      e.g. com/foo/File.yml
      cs - A Charset.
      Returns:
      Map if the file exists.
      RuntimeError if the file doesn't exist or read multiple object as single object.
    • toMapsFromFile

      public static List<Map<String,Object>> toMapsFromFile(String name)
      Read Yaml file as List of Map.
      Parameters:
      name - The name of file.
      e.g. com/foo/File.yml
      Returns:
      List of Map if the file exists.
      RuntimeError otherwise.
    • toMapsFromFile

      public static List<Map<String,Object>> toMapsFromFile(String name, Charset cs)
      Read Yaml file as List of Map.
      Parameters:
      name - The name of file.
      e.g. com/foo/File.yml
      cs - A Charset.
      Returns:
      List of Map if the file exists.
      RuntimeError otherwise.
    • toMapsFromResource

      public static List<Map<String,Object>> toMapsFromResource(String name)
      Read Yaml file as List of Map.
      Parameters:
      name - The name of resource.
      e.g. com/foo/File.yml
      Returns:
      List of Map if the file exists.
      RuntimeError otherwise.
    • toMapsFromResource

      public static List<Map<String,Object>> toMapsFromResource(String name, Charset cs)
      Read Yaml file as List of Map.
      Parameters:
      name - The name of resource.
      e.g. com/foo/File.yml
      cs - A Charset.
      Returns:
      List of Map if the file exists.
      RuntimeError otherwise.
    • fromFile

      public Model fromFile(String name)
      Set the path name of Yaml file.
      Parameters:
      name - The name of Yaml file.
      e.g. src/test/resources/com/foo/File.yml
      Returns:
      The YamlData.
    • fromResource

      public Model fromResource(String name)
      Set resource name to get the Yaml file.
      Parameters:
      name - The name of resource.
      e.g. com/foo/File.yml
      Returns:
      The YamlData.
    • toModel

      public Model toModel()
      Read Yaml file as YamlData.
      Returns:
      YamlData if the file exists.
      Throws RuntimeError if the file doesn't exist or read multiple object as single object.
    • toModel

      public Model toModel(Charset cs)
      Read Yaml file as YamlData.
      Parameters:
      cs - A Charset.
      Returns:
      YamlData if the file exists.
      Throws RuntimeError if the file doesn't exist or read multiple object as single object.
    • toModels

      public List<Model> toModels()
      Read Yaml file as List of YamlData.
      Returns:
      List of YamlData if the file exists.
      List of YamlData with one element when trying to single object as multiple object. Throws RuntimeError if the file doesn't exist.
    • toModels

      public List<Model> toModels(Charset cs)
      Read Yaml file as List of YamlData.
      Parameters:
      cs - A Charset.
      Returns:
      List of YamlData if the file exists.
      List of YamlData with one element when trying to single object as multiple object. Throws RuntimeError if the file doesn't exist.