feat: 反序列化
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
package cn.celess.blog.enmu;
|
package cn.celess.blog.enmu;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonCreator;
|
||||||
import com.fasterxml.jackson.annotation.JsonValue;
|
import com.fasterxml.jackson.annotation.JsonValue;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
@@ -51,6 +52,16 @@ public enum UserAccountStatusEnum {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@JsonCreator
|
||||||
|
public static UserAccountStatusEnum get(Map<String, Object> map) {
|
||||||
|
for (UserAccountStatusEnum value : UserAccountStatusEnum.values()) {
|
||||||
|
if (value.code == (int) map.get("code") && value.desc.equals(map.get("desc"))) {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
@JsonValue
|
@JsonValue
|
||||||
public Map<String, Object> toJson() {
|
public Map<String, Object> toJson() {
|
||||||
Map<String, Object> map = new HashMap<>(2);
|
Map<String, Object> map = new HashMap<>(2);
|
||||||
|
|||||||
@@ -5,6 +5,8 @@ import com.fasterxml.jackson.core.JsonProcessingException;
|
|||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
public class UserAccountStatusEnumTest extends BaseTest {
|
public class UserAccountStatusEnumTest extends BaseTest {
|
||||||
@@ -18,7 +20,17 @@ public class UserAccountStatusEnumTest extends BaseTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void toJson() throws JsonProcessingException {
|
public void toJson() throws JsonProcessingException {
|
||||||
|
// 序列化
|
||||||
ObjectMapper objectMapper = new ObjectMapper();
|
ObjectMapper objectMapper = new ObjectMapper();
|
||||||
assertEquals("{\"code\":0,\"desc\":\"正常\"}", objectMapper.writeValueAsString(UserAccountStatusEnum.NORMAL));
|
assertEquals("{\"code\":0,\"desc\":\"正常\"}", objectMapper.writeValueAsString(UserAccountStatusEnum.NORMAL));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGet() throws IOException {
|
||||||
|
// 反序列化
|
||||||
|
ObjectMapper mapper = new ObjectMapper();
|
||||||
|
UserAccountStatusEnum userAccountStatusEnum = mapper.readValue(mapper.writeValueAsString(UserAccountStatusEnum.NORMAL), UserAccountStatusEnum.class);
|
||||||
|
assertEquals(UserAccountStatusEnum.NORMAL.getCode(), userAccountStatusEnum.getCode());
|
||||||
|
assertEquals(UserAccountStatusEnum.NORMAL.getDesc(), userAccountStatusEnum.getDesc());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user