Jackson && Lombok
Lombok
@Data
@Data
public class UserBean{
private Integer id;
private String userName;
}
@Getter @Setter @RequiredArgsConstructor @ToString @EqualsAndHashCode
@Getter/@Setter
@NonNull
public String getName(@NonNull Person p) {
return p.getName();
}
public String getName(@NonNull Person p) {
if (p == null) {
throw new NullPointerException("p");
}
return p.getName();
}
@ToString/@EqualsAndHashCode
@ToString(exclude={“param1”,“param2”})
@ToString(of={“param1”,“param2”})
@NoArgsConstructor/@RequiredArgsConstructor /@AllArgsConstructor
@NoArgsConstructor
@RequiredArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor
public class Shape {
private int x;
@NonNull
private int y;
@NonNull
private String name;
}
equals
public class Shape {
private int x;
@NonNull
private int y;
@NonNull
private String name;
}
public Shape(){}
protected Shape(int x,@NonNull double y,String name){
this.x = x;
this.y = y;
this.name = name;
}
public Shape (double y,String name){
return new Shape(y,name);
}
@Slf4j
use Slf4j in lombok, you don’t code below to test.
private final Logger logger = LoggerFactory.getLogger(XXX.class)
log.info(“xxxx{}”, key);
@Builder
public class User {
private String name;
private String password;
public void setName(String name) {
this.name = name;
}
public void setPassword(String password) {
this.password = password;
}
}
User user = new User();
user.setName("aa");
user.setPassword("1111");
now compared to Immutable Objects
public class User {
private final String name;
private final String password;
public User(String name, String password) {
this.name = name;
this.password = password;
}
}
need more than more constructor
public class UserBuilder {
private String name;
private String password;
public UserBuilder name(String name) {
this.name = name;
return this;
}
public UserBuilder password(String password) {
this.password = password;
return this;
}
public User build() {
return new User(name, password);
}
public static UserBuilder User() {
return new UserBuilder();
}
}
User user = UserBuilder.user()
.name("milo")
.password("123456")
.build();
lombok has tight coupling issues
@RequiredArgsConstructor
@RestController
public class MyController {
private final RoleAccessService roleAccessService;
private final QueueService queueService;
private final ManagementService managementService;
private final SecurityConfig securityConfig;
private final JsonAdapter jsonAdapter;
// REST methods
}
equals
@RestController
public class MyController {
private final RoleAccessService roleAccessService;
private final QueueService queueService;
private final ManagementService managementService;
private final SecurityConfig securityConfig;
private final JsonAdapter jsonAdapter;
@Autowired
public MyController(RoleAccessService roleAccessService,
QueueService queueService,
ManagementService managementService,
SecurityConfig securityConfig,
JsonAdapter jsonAdapter) {
this.roleAccessService = roleAccessService;
this.queueService = queueService;
this.managementService = managementService;
this.securityConfig = securityConfig;
this.jsonAdapter = jsonAdapter;
}
// REST methods
}
The lombok would make you forget your class becoming too big or it has two much responsibility
always remember to watch out the dependencies
JackSon
jackson-databind in
ObjectMapper
The simple readValue API of the ObjectMapper is a good entry point. We can use it to parse or deserialize JSON content into a Java object.
use the writeValue API to serialize any Java object as JSON output.
Java Object to JSON
ObjectMapper objectMapper = new ObjectMapper();
Person p = new Person("15", "female"); // private String age; private String sex;
objectMapper.writeValue(new File("target/p.json"), p); // {"age": "15", sex": "female"}
JSON to Java Object
String json = "{\"age"\" : \"15"\", \"sex"\" : \"female"\"}";
Person p = objectMapper.writeValue(json, Person.class);
Person p = objectMapper.writeValue(new URL("file:src/java/exe/json_p.json"), Person.class);
JSON to Object, List, Map
public class User {
private int id;
private String name;
// get, set
}
public class Client {
public static void main(String[] args) throws Exception {
ObjectMapper objectMapper = new ObjectMapper();
// User Object to json
User user1 = new User(1, "AA");
String json = objectMapper.writeValueAsString(user1);
// json to User Object
User user1 = new User(1, "AA");
User user2 = objectMapper.readValue(json, User.class);
// List<User> to json
List<User> uli = new ArrayList<>();
User user3 = new User(2, "BB");
uli.add(user3);
String ujson = objectMapper.writeValueAsString(uli);
// json to List<User>
List<User> ulist = objectMapper.readValue(json, new TypeReference<List<User>>() {});
// Map<Strng, User> to json
HashMap<String, User> umap = new HashMap<>();
User user4 = new User(4, "CC");
umap.put("CC", user4);
String mjson2 = objectMapper.writeValueAsString(umap);
// json to Map<Strng, User>
Map<Strng, User> umap1 = objectMapper.readValue(json, new TypeReference<HashMap<String, User>>() {});
}
public class Game<T> {
private Integer id;
private T source;
}
public class Item {
private String color;
private Integer price;
}
json
{
"id":001,
"source": {
"color" : "white",
"price" : 20
}
}
// json to class
public class Client {
public static <T> Game<T> convert(String json, Class<T> targetClass) throws Exception {
ObjectMapper objectMapper = new ObjectMapper();
Game<T> game = null;
JavaType clazzType = objectMapper.getTypeFactory().constructParametricType(Game.class, targetClass);
game = objectMapper.readValue(json, clazzType);
return game;
}
public static void main(String[] args) {
String json = "{\"id\": 001,\"source\": {\"color\": \"white\",\"price\": 20}}";
Game<Item> game1 = convert(json, Item.class);
}
}