CurrentValueDeser4184Test.java
package com.fasterxml.jackson.databind.ser.filter;
import java.io.IOException;
import org.junit.jupiter.api.Test;
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.testutil.DatabindTestUtil;
import static org.junit.jupiter.api.Assertions.*;
// [databind#4184]
public class CurrentValueDeser4184Test extends DatabindTestUtil
{
static class User {
public Role role;
public UserType type;
}
static class Role {
public String name;
}
@JsonDeserialize(using = UserTypeDeserializer.class)
enum UserType {
ADMIN(1),
USER(2);
final int value;
UserType(int value) {
this.value = value;
}
public Integer getValue() {
return this.value;
}
public String getName() {
return this.name();
}
}
static class UserTypeDeserializer extends JsonDeserializer<UserType> {
@Override
public UserType deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
Object currentValue = p.getParsingContext().getParent().getCurrentValue();
if (null == currentValue) {
ctxt.reportInputMismatch(UserType.class, "No currentValue() available");
}
if (!(currentValue instanceof User)) {
ctxt.reportInputMismatch(UserType.class, "currentValue() of wrong type, not User but: "
+currentValue.getClass().getName());
}
JsonNode node = ctxt.readTree(p);
int value = node.path("value").asInt(-1);
switch (value) {
case 1:
return UserType.ADMIN;
case 2:
return UserType.USER;
}
throw new IllegalArgumentException("Bad value: "+value);
}
}
/*
/**********************************************************************
/* Test methods
/**********************************************************************
*/
private final ObjectMapper MAPPER = newJsonMapper();
// [databind#4184]
@Test
public void testCurrentValue4184FullPojo() throws Exception
{
String json = "{\"role\": {\"name\": \"Manager\"}, \"type\": {\"value\":1}}";
User user = MAPPER.readValue(json, User.class);
assertNotNull(user);
assertEquals(UserType.ADMIN, user.type);
}
// [databind#4184]
@Test
public void testCurrentValue4184EmptyPojo() throws Exception
{
String json = "{\"role\": {}, \"type\": {\"value\":1}}";
User user = MAPPER.readValue(json, User.class);
assertNotNull(user);
assertEquals(UserType.ADMIN, user.type);
}
}