待转换的Java类如下:
/**
* 设备注册请求
*/
public class DeviceInfo implements java.io.Serializable {
private static final long serialVersionUID = -8829302684686651266L;
/**
* ID
*/
private Long id;
/**
* 设备UUID
*/
private String uuid;
/**
* 停车场ID
*/
private Long depotId;
/**
* 设备序列号
*/
private String deviceSerialNumber;
/**
* 进出方向
*/
private Direction direction;
/**
* 操作人ID
*/
private Long operatorId;
/**
* 操作人姓名
*/
private String operatorName;
/**
* 备注
*/
private String remark;
public DeviceInfo() {
super();
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUuid() {
return uuid;
}
public void setUuid(String uuid) {
this.uuid = uuid;
}
public Long getDepotId() {
return depotId;
}
public void setDepotId(Long depotId) {
this.depotId = depotId;
}
public String getDeviceSerialNumber() {
return deviceSerialNumber;
}
public void setDeviceSerialNumber(String deviceSerialNumber) {
this.deviceSerialNumber = deviceSerialNumber;
}
public Direction getDirection() {
return direction;
}
public void setDirection(Direction direction) {
this.direction = direction;
}
public Long getOperatorId() {
return operatorId;
}
public void setOperatorId(Long operatorId) {
this.operatorId = operatorId;
}
public String getOperatorName() {
return operatorName;
}
public void setOperatorName(String operatorName) {
this.operatorName = operatorName;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((depotId == null) ? 0 : depotId.hashCode());
result = prime * result + ((deviceSerialNumber == null) ? 0 : deviceSerialNumber.hashCode());
result = prime * result + ((direction == null) ? 0 : direction.hashCode());
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + ((operatorId == null) ? 0 : operatorId.hashCode());
result = prime * result + ((operatorName == null) ? 0 : operatorName.hashCode());
result = prime * result + ((remark == null) ? 0 : remark.hashCode());
result = prime * result + ((uuid == null) ? 0 : uuid.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
DeviceInfo other = (DeviceInfo) obj;
if (depotId == null) {
if (other.depotId != null)
return false;
} else if (!depotId.equals(other.depotId))
return false;
if (deviceSerialNumber == null) {
if (other.deviceSerialNumber != null)
return false;
} else if (!deviceSerialNumber.equals(other.deviceSerialNumber))
return false;
if (direction != other.direction)
return false;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
if (operatorId == null) {
if (other.operatorId != null)
return false;
} else if (!operatorId.equals(other.operatorId))
return false;
if (operatorName == null) {
if (other.operatorName != null)
return false;
} else if (!operatorName.equals(other.operatorName))
return false;
if (remark == null) {
if (other.remark != null)
return false;
} else if (!remark.equals(other.remark))
return false;
if (uuid == null) {
if (other.uuid != null)
return false;
} else if (!uuid.equals(other.uuid))
return false;
return true;
}
@Override
public String toString() {
return "DeviceInfo [id=" + id + ", uuid=" + uuid + ", depotId=" + depotId + ", deviceSerialNumber="
+ deviceSerialNumber + ", direction=" + direction + ", operatorId=" + operatorId + ", operatorName="
+ operatorName + ", remark=" + remark + "]";
}
}
其中Direction为如下枚举类型:
/**
* 出入方向
*/
public enum Direction {
IN, OUT;
}
测试类如下:
@Test
public void test() {
DeviceInfo info = new DeviceInfo();
info.setDepotId(15L);
info.setDeviceSerialNumber("1111");
info.setDirection(Direction.IN);
info.setId(222L);
info.setOperatorId(333L);
info.setRemark("备注信息");
info.setUuid(UUID.randomUUID().toString());
JSONObject jsonObj = new JSONObject(info);
System.out.println("JSONObject:" + jsonObj.toString());//使用JSONObject转JSON字符串
ObjectMapper mapper = new ObjectMapper();
try {
String str = mapper.writeValueAsString(info);
System.out.println("ObjectMapper:" + str);//使用ObjectMapper转JSON字符串
} catch (JsonProcessingException e) {
e.printStackTrace();
}
}
运行结果如下:
JSONObject:{"depotId":15,"remark":"备注信息","id":222,"operatorId":333,"uuid":"8e0463bb-af48-4f26-8f2e-ec53bccdf0bb","direction":{},"deviceSerialNumber":"1111"}
ObjectMapper:{"id":222,"uuid":"8e0463bb-af48-4f26-8f2e-ec53bccdf0bb","depotId":15,"deviceSerialNumber":"1111","direction":"IN","operatorId":333,"operatorName":null,"remark":"备注信息"}
可以看到两种方法对枚举类的处理结果不同。