Bạn có chắc chắn muốn xóa bài viết này không ?
Bạn có chắc chắn muốn xóa bình luận này không ?
Sử dụng Jackson-databind mapping Object <-> Json và ứng dụng load file config
Sử dụng Jackson-databind mapping Object <-> Json và ứng dụng load file config
1. Mapping Object <-> Json
Jackson-databind Maven:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.6</version>
</dependency>
Ở đây mình sẽ lấy 1 ví dụ đơn giản về cách sử dụng jackson-databind để mapping qua lại giữa Object People và Json
People.class
class People {
private String name
private String fullName
private int age
People() {
super()
}
People(String name, int age, String fullName) {
this.name = name
this.age = age
this.fullName = fullName
}
String getName() {
return name
}
int getAge() {
return age
}
String getFullName() {
return fullName
}
@Override
String toString() {
return "Name: $name \nFull Name: $fullName\nAge: $age"
}
}
Khởi tạo objectMapper:
ObjectMapper objectMapper = new ObjectMapper().with {
configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
configure(JsonParser.Feature.ALLOW_COMMENTS,true)
setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE)
}
Ở đây mình có 3 config:
configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
Giá trị mặc định là true, 1 exception sẽ throw ra khi objectMapper không tìm thấy 1 trường trong object tương ứng với trường tìm thấy trong Json. (Tức yêu cầu mọi trường có trong Json phải tồn tại trong Object). Và giá trị false thì ngược lại, bạn có thể thêm thừa field trong Json cũng không vấn đề.
configure(JsonParser.Feature.ALLOW_COMMENTS,true)
Khi set giá trị bằng true, bạn có thể comment trong chuỗi Json, nó chấp nhận cú pháp comment trong Java hoặc C++.
setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE)
Quy ước naming strategy, ở đây mình chọn SNAKE_CASE tức là 1 trường trong object có tên fullName khi chuyển qua Json sẽ trở thành full_name, và ngược lại, giá trị của trường full_name trong Json sẽ được bind vào trường fullName trong object.
Chuyển từ Object -> Json:
People people = new People("Huan", 22, "Tat Huan")
String json = objectMapper.writeValueAsString(people)
println json
Output:
{
"name": "Huan",
"full_name": "Tat Huan",
"age": 22
}
Chuyển từ Json -> Object:
Input json file:
{
"name": "Huan",
"full_name": "Tat Huan",
"age": 22
}
File jsonFile = new File("src/main/resources/people.json")
People people = objectMapper.readValue(jsonFile, People.class)
println people.toString()
Output:
Name: Huan
Full Name: Tat Huan
Age: 22
Load config từ file:
config.json
{
"mongodb.uri":"mongodb://localhost:57017"
}
Appconfig:
class AppConfig {
@JsonProperty("mongodb.uri")
MongoDB mongoDB
static AppConfig newInstance(File appConfigFile) {
ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory()).with {
setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE)
configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
}
AppConfig appConfig = objectMapper.readValue(appConfigFile, AppConfig.class)
return appConfig
}
MongoDB getMongoDB() {
return mongoDB
}
}
Sử dụng Appconfig:
AppConfig appConfig = AppConfig.newInstance(new File("src/main/resources/config.json"))
println appConfig.getMongoDB().getUri()
Output:
Created new instance with uri: mongodb://localhost:57017
mongodb://localhost:57017
Code example: https://github.com/huantt/jackson-example
Write for sharing, write for remembering - Jack180715






