Last active
July 18, 2017 14:00
-
-
Save balysv/bae96686cbbb745d07b198927f1577f0 to your computer and use it in GitHub Desktop.
jackson + immutables.io deserialization issue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package test; | |
import static com.fasterxml.jackson.annotation.JsonCreator.Mode.DELEGATING; | |
import static org.junit.Assert.assertEquals; | |
import java.io.IOException; | |
import java.util.Arrays; | |
import java.util.List; | |
import org.junit.Test; | |
import com.fasterxml.jackson.annotation.JsonAutoDetect; | |
import com.fasterxml.jackson.annotation.JsonCreator; | |
import com.fasterxml.jackson.annotation.JsonIdentityInfo; | |
import com.fasterxml.jackson.annotation.ObjectIdGenerators; | |
import com.fasterxml.jackson.core.type.TypeReference; | |
import com.fasterxml.jackson.databind.ObjectMapper; | |
import com.fasterxml.jackson.databind.annotation.JsonDeserialize; | |
import com.fasterxml.jackson.databind.annotation.JsonSerialize; | |
public class T { | |
private ObjectMapper objectMapper = new ObjectMapper(); | |
@Test | |
public void test() throws IOException { | |
ImmutableTest test = new ImmutableTest(1, "test"); | |
List<ITest> input = Arrays.asList(test, test); | |
// [{"@id":1,"id":1,"name":"test"},1] | |
String json = objectMapper.writeValueAsString(input); | |
List<ITest> result = objectMapper.readValue(json, new TypeReference<List<ITest>>() {}); | |
// java.lang.AssertionError: | |
// Expected :[test.T$ImmutableTest@3644b1, test.T$ImmutableTest@3644b1] | |
// Actual :[test.T$ImmutableTest@3644b1, test.T$JsonTest@3b94d659] | |
assertEquals(input, result); | |
} | |
@JsonSerialize(as = ImmutableTest.class) | |
@JsonDeserialize(as = ImmutableTest.class) | |
@JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class) | |
public interface ITest { | |
Integer getId(); | |
String getName(); | |
} | |
public static class ImmutableTest implements ITest { | |
private final Integer id; | |
private final String name; | |
private ImmutableTest(Integer id, String name) { | |
this.id = id; | |
this.name = name; | |
} | |
@Override | |
public Integer getId() { | |
return id; | |
} | |
@Override | |
public String getName() { | |
return name; | |
} | |
@JsonCreator(mode = DELEGATING) | |
static ImmutableTest fromJson(JsonTest json) { | |
return new ImmutableTest(json.id, json.name); | |
} | |
@Override | |
public boolean equals(Object o) { | |
if (this == o) | |
return true; | |
if (o == null || getClass() != o.getClass()) | |
return false; | |
ImmutableTest test = (ImmutableTest) o; | |
return id.equals(test.id) && name.equals(test.name); | |
} | |
@Override | |
public int hashCode() { | |
int result = id.hashCode(); | |
result = 31 * result + name.hashCode(); | |
return result; | |
} | |
} | |
@JsonDeserialize | |
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.NONE) | |
public static class JsonTest implements ITest { | |
private String name; | |
private Integer id; | |
public void setId(Integer id) { | |
this.id = id; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
@Override | |
public String getName() { | |
throw new UnsupportedOperationException(); | |
} | |
@Override | |
public Integer getId() { | |
throw new UnsupportedOperationException(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment