Skip to content

Instantly share code, notes, and snippets.

@christophercurrie
Created September 21, 2012 04:26
Show Gist options
  • Save christophercurrie/3759710 to your computer and use it in GitHub Desktop.
Save christophercurrie/3759710 to your computer and use it in GitHub Desktop.
Key Serializer caching test
junit.framework.ComparisonFailure: null
Expected :{"map":{"Karl":1}}
Actual :{"map":{"Not Karl":1}}
<Click to see difference>
at TestKeySerializer.TestBoth(TestKeySerializer.java:42)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:76)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:195)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:63)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Process finished with exit code 255
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.google.common.collect.ImmutableMap;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.util.Map;
import static junit.framework.Assert.assertEquals;
public class TestKeySerializer {
private ObjectMapper mapper;
@Before
public void setUp()
{
mapper = new ObjectMapper();
}
@Test
public void TestNotKarl() throws IOException {
final String serialized = mapper.writeValueAsString(new NotKarlBean());
assertEquals("{\"map\":{\"Not Karl\":1}}", serialized);
}
@Test
public void TestKarl() throws IOException {
final String serialized = mapper.writeValueAsString(new KarlBean());
assertEquals("{\"map\":{\"Karl\":1}}", serialized);
}
@Test
public void TestBoth() throws IOException {
final String value1 = mapper.writeValueAsString(new NotKarlBean());
final String value2 = mapper.writeValueAsString(new KarlBean());
assertEquals("{\"map\":{\"Not Karl\":1}}", value1);
assertEquals("{\"map\":{\"Karl\":1}}", value2);
}
public static class KarlSerializer extends JsonSerializer<String>
{
@Override
public void serialize(String value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
jgen.writeFieldName("Karl");
}
}
public static class NotKarlBean
{
public Map<String,Integer> map = ImmutableMap.of("Not Karl", 1);
}
public static class KarlBean
{
@JsonSerialize(keyUsing = KarlSerializer.class)
public Map<String,Integer> map = ImmutableMap.of("Not Karl", 1);
}
}
@cowtowncoder
Copy link

Hmmh. Is it just me or is KarlBean bit buggy, wrt value contained?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment