Table of Contents
Add dependency to build.gradle
.
implementation("com.github.ngoanh2n:commons:1.7.0")
Add dependency to pom.xml
.
<dependency>
<groupId>com.github.ngoanh2n</groupId>
<artifactId>commons</artifactId>
<version>1.7.0</version>
</dependency>
├── build
├── out
│ ├── production
│ | └── resources
│ | └── log4j.properties
│ └── test
│ └── resources
│ ├── file.json
│ ├── user.yml
│ └── users.yml
├── src
│ ├── main
│ | └── resources
│ | └── log4j.properties
│ └── test
│ └── resources
│ ├── file.json
│ ├── user.yml
│ └── users.yml
Find and read Java resources.
File file = Resources.getFile("file.json")
Path path = Resources.getPath("file.json")
String content = Resources.getContent("file.json")
InputStream is = Resources.getInputStream("file.json")
ngoanh2n.findResourceOnClasspath
Indicate to find the resource file on classpath (Default to true).true
: Get the resource on the classpath{project}/out/test/resources/
{project}/out/production/resources/
false
: Get the resource in root location{project}/src/test/resources/
{project}/src/main/resources/
Reads Yaml file to Map
, List of Map
.
- Read to
Map
Map<String, Object> map = YamlData.toMapFromResource("user.yml")
Map<String, Object> map = YamlData.toMapFromFile("src/test/resources/user.yml")
- Read to
List of Maps
List<Map<String, Object>> maps = YamlData.toMapsFromResource("user.yml")
List<Map<String, Object>> maps = YamlData.toMapsFromFile("src/test/resources/user.yml")
Read Yaml file to Model
, List of Models
.
Model class must be public
and has setter
methods.
- Read to
Model
# Yaml: user.yml username: usr1 notes: - note1 - note2 companies: - name: Com1 address: Addr1 - name: Com2 address: Addr3
// 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
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.
- Without annotation
- Read to
List of Models
# Yaml File: users.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
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.
- Without annotation