Skip to content

Commit

Permalink
Add a key serializer unit test from #838
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Jun 25, 2015
1 parent dc53ca4 commit 80c1bbb
Showing 1 changed file with 65 additions and 3 deletions.
Expand Up @@ -3,10 +3,12 @@
import java.io.IOException;
import java.util.*;

import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.core.JsonGenerator;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.jsontype.TypeResolverBuilder;
import com.fasterxml.jackson.databind.module.SimpleModule;

public class TestKeySerializers extends BaseMapTest
Expand Down Expand Up @@ -40,7 +42,7 @@ enum ABC {
A, B, C
}

static class ABCSerializer extends JsonSerializer<ABC> {
static class ABCKeySerializer extends JsonSerializer<ABC> {
@Override
public void serialize(ABC value, JsonGenerator jgen,
SerializerProvider provider) throws IOException {
Expand All @@ -55,6 +57,26 @@ public ABCMapWrapper() {
}
}

static class BAR<T>{
T value;

public BAR(T value) {
this.value = value;
}

@JsonValue
public T getValue() {
return value;
}

@Override
public String toString() {
return this.getClass().getSimpleName()
+ ", value:" + value
;
}
}

/*
/**********************************************************
/* Unit tests
Expand Down Expand Up @@ -88,10 +110,50 @@ public void testCustomForEnum() throws IOException
{
final ObjectMapper mapper = new ObjectMapper();
SimpleModule mod = new SimpleModule("test");
mod.addKeySerializer(ABC.class, new ABCSerializer());
mod.addKeySerializer(ABC.class, new ABCKeySerializer());
mapper.registerModule(mod);

String json = mapper.writeValueAsString(new ABCMapWrapper());
assertEquals("{\"stuff\":{\"xxxB\":\"bar\"}}", json);
}

// [databind#838]
public void testUnWrappedMapWithDefaultType() throws Exception{
final ObjectMapper mapper = new ObjectMapper();
SimpleModule mod = new SimpleModule("test");
mod.addKeySerializer(ABC.class, new ABCKeySerializer());
mapper.registerModule(mod);

TypeResolverBuilder<?> typer = new ObjectMapper.DefaultTypeResolverBuilder(ObjectMapper.DefaultTyping.NON_FINAL);
typer = typer.init(JsonTypeInfo.Id.NAME, null);
typer = typer.inclusion(JsonTypeInfo.As.PROPERTY);
//typer = typer.typeProperty(TYPE_FIELD);
typer = typer.typeIdVisibility(true);
mapper.setDefaultTyping(typer);

Map<ABC,String> stuff = new HashMap<ABC,String>();
stuff.put(ABC.B, "bar");
String json = mapper.writerFor(new TypeReference<Map<ABC, String>>() {})
.writeValueAsString(stuff);
assertEquals("{\"@type\":\"HashMap\",\"xxxB\":\"bar\"}", json);
}

// [databind#838]
public void testUnWrappedMapWithKeySerializer() throws Exception{
SimpleModule mod = new SimpleModule("test");
mod.addKeySerializer(ABC.class, new ABCKeySerializer());
final ObjectMapper mapper = new ObjectMapper()
.registerModule(mod)
.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT)
.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.disable(SerializationFeature.WRITE_NULL_MAP_VALUES)
.setSerializationInclusion(JsonInclude.Include.NON_EMPTY)
;

Map<ABC,BAR<?>> stuff = new HashMap<ABC,BAR<?>>();
stuff.put(ABC.B, new BAR<String>("bar"));
String json = mapper.writerFor(new TypeReference<Map<ABC,BAR<?>>>() {})
.writeValueAsString(stuff);
assertEquals("{\"xxxB\":\"bar\"}", json);
}
}

0 comments on commit 80c1bbb

Please sign in to comment.