Package com.github.ngoanh2n
Class YamlData<Model>
java.lang.Object
com.github.ngoanh2n.YamlData<Model>
Read Yaml file.
Static
Read Yaml file to
Read Yaml file to
Model class must be
Static
Read Yaml file to
Map
, List of Maps
.
- Read to Map
Map<String, Object> map = YamlData.toMapFromResource("file.json")
Map<String, Object> map = YamlData.toMapFromFile("src/test/resources/file.json")
- 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")
Read Yaml file to
Model
, List of Models
.Model class must be
public
and has setter
methods.
- Read to Model
-
Yaml: user.yml
Model: User.javausername: usr1 notes: - note1 - note2 companies: - name: Com1 address: Addr1 - name: Com2 address: Addr2
Model: Company.javapublic class User extends YamlData<User> { private String username; private List<String> notes; private List<Company> companies; ...GETTERS & SETTERS... }
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
Attachcom.github.ngoanh2n.YamlFrom
annotation forModel
.YamlFrom.resource()
@YamlFrom(resource = "user.yml") public class User extends YamlData<User> { ... }
YamlFrom.file()
@YamlFrom(file = "src/test/resources/user.yml") public class User extends YamlData<User> { ... }
com.github.ngoanh2n.YamlFrom
annotation by callingfromResource(String)
orfromFile(String)
method.
-
Yaml: user.yml
- Read to List of Models
-
Yaml: accounts.yml
Model: User.java- username: usr1 password: pwd1 - username: usr2 password: pwd2
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
Attachcom.github.ngoanh2n.YamlFrom
annotation forModel
.YamlFrom.resource()
@YamlFrom(resource = "users.yml") public class User extends YamlData<User> { ... }
YamlFrom.file()
@YamlFrom(file = "src/test/resources/users.yml") public class User extends YamlData<User> { ... }
com.github.ngoanh2n.YamlFrom
annotation by callingfromResource(String)
orfromFile(String)
method.
-
Yaml: accounts.yml
- GitHub: ngoanh2n/commons
- Maven: com.github.ngoanh2n:commons
- Since:
- 2019
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionSet the path name of Yaml file.fromResource
(String name) Set resource name to get the Yaml file.toMapFromFile
(String name) Read Yaml file asMap
.toMapFromFile
(String name, Charset cs) Read Yaml file asMap
.toMapFromResource
(String name) Read Yaml file asMap
.toMapFromResource
(String name, Charset cs) Read Yaml file asMap
.toMapsFromFile
(String name) toMapsFromFile
(String name, Charset cs) toMapsFromResource
(String name) toMapsFromResource
(String name, Charset cs) toModel()
Read Yaml file asYamlData
.Read Yaml file asYamlData
.toModels()
-
Constructor Details
-
YamlData
public YamlData()Construct a newYamlData
and get current Java Bean class.
-
-
Method Details
-
toMapFromFile
Read Yaml file asMap
.- 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
Read Yaml file asMap
.- Parameters:
name
- The name of file.
e.g. src/test/resources/com/foo/File.ymlcs
- ACharset
.- Returns:
Map
if the file exists.
RuntimeError
if the file doesn't exist or read multiple object as single object.
-
toMapFromResource
Read Yaml file asMap
.- 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
Read Yaml file asMap
.- Parameters:
name
- The name of resource.
e.g. com/foo/File.ymlcs
- ACharset
.- Returns:
Map
if the file exists.
RuntimeError
if the file doesn't exist or read multiple object as single object.
-
toMapsFromFile
- Parameters:
name
- The name of file.
e.g. com/foo/File.yml- Returns:
List
ofMap
if the file exists.
RuntimeError
otherwise.
-
toMapsFromFile
- Parameters:
name
- The name of file.
e.g. com/foo/File.ymlcs
- ACharset
.- Returns:
List
ofMap
if the file exists.
RuntimeError
otherwise.
-
toMapsFromResource
- Parameters:
name
- The name of resource.
e.g. com/foo/File.yml- Returns:
List
ofMap
if the file exists.
RuntimeError
otherwise.
-
toMapsFromResource
- Parameters:
name
- The name of resource.
e.g. com/foo/File.ymlcs
- ACharset
.- Returns:
List
ofMap
if the file exists.
RuntimeError
otherwise.
-
fromFile
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
Set resource name to get the Yaml file.- Parameters:
name
- The name of resource.
e.g. com/foo/File.yml- Returns:
- The
YamlData
.
-
toModel
Read Yaml file asYamlData
.- Returns:
YamlData
if the file exists.
ThrowsRuntimeError
if the file doesn't exist or read multiple object as single object.
-
toModel
Read Yaml file asYamlData
.- Parameters:
cs
- ACharset
.- Returns:
YamlData
if the file exists.
ThrowsRuntimeError
if the file doesn't exist or read multiple object as single object.
-
toModels
- Returns:
List
ofYamlData
if the file exists.
List
ofYamlData
with one element when trying to single object as multiple object. ThrowsRuntimeError
if the file doesn't exist.
-
toModels
-