Upgraded to Jackson 1.9 (raising minimum to 1.8+) and 2.2

This commit is contained in:
Juergen Hoeller
2013-05-02 15:51:59 +02:00
parent 9f9b972f00
commit 767bd3f3f8
11 changed files with 164 additions and 188 deletions

View File

@@ -23,20 +23,7 @@ import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.junit.Before;
import org.junit.Test;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.ContextFactory;
import org.mozilla.javascript.ScriptableObject;
import org.springframework.http.MediaType;
import org.springframework.mock.web.test.MockHttpServletRequest;
import org.springframework.mock.web.test.MockHttpServletResponse;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.servlet.View;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.BeanProperty;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonSerializer;
@@ -44,10 +31,20 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.cfg.SerializerFactoryConfig;
import com.fasterxml.jackson.databind.ser.BasicSerializerFactory;
import com.fasterxml.jackson.databind.ser.BeanSerializerFactory;
import com.fasterxml.jackson.databind.ser.SerializerFactory;
import com.fasterxml.jackson.databind.ser.Serializers;
import org.junit.Before;
import org.junit.Test;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.ContextFactory;
import org.mozilla.javascript.ScriptableObject;
import org.springframework.http.MediaType;
import org.springframework.mock.web.test.MockHttpServletRequest;
import org.springframework.mock.web.test.MockHttpServletResponse;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.servlet.View;
import static org.junit.Assert.*;
import static org.mockito.BDDMockito.*;
@@ -69,6 +66,7 @@ public class MappingJackson2JsonViewTests {
private ScriptableObject jsScope;
@Before
public void setUp() {
request = new MockHttpServletRequest();
@@ -80,6 +78,7 @@ public class MappingJackson2JsonViewTests {
view = new MappingJackson2JsonView();
}
@Test
public void isExposePathVars() {
assertEquals("Must not expose path variables", false, view.isExposePathVariables());
@@ -87,7 +86,6 @@ public class MappingJackson2JsonViewTests {
@Test
public void renderSimpleMap() throws Exception {
Map<String, Object> model = new HashMap<String, Object>();
model.put("bindingResult", mock(BindingResult.class, "binding_result"));
model.put("foo", "bar");
@@ -110,12 +108,10 @@ public class MappingJackson2JsonViewTests {
@Test
public void renderWithSelectedContentType() throws Exception {
Map<String, Object> model = new HashMap<String, Object>();
model.put("foo", "bar");
view.render(model, request, response);
assertEquals("application/json", response.getContentType());
request.setAttribute(View.SELECTED_CONTENT_TYPE, new MediaType("application", "vnd.example-v2+xml"));
@@ -147,7 +143,6 @@ public class MappingJackson2JsonViewTests {
@Test
public void renderSimpleBean() throws Exception {
Object bean = new TestBeanSimple();
Map<String, Object> model = new HashMap<String, Object>();
model.put("bindingResult", mock(BindingResult.class, "binding_result"));
@@ -164,7 +159,6 @@ public class MappingJackson2JsonViewTests {
@Test
public void renderWithPrettyPrint() throws Exception {
ModelMap model = new ModelMap("foo", new TestBeanSimple());
view.setPrettyPrint(true);
@@ -178,14 +172,12 @@ public class MappingJackson2JsonViewTests {
@Test
public void renderSimpleBeanPrefixed() throws Exception {
view.setPrefixJson(true);
renderSimpleBean();
}
@Test
public void renderWithCustomSerializerLocatedByAnnotation() throws Exception {
Object bean = new TestBeanSimpleAnnotated();
Map<String, Object> model = new HashMap<String, Object>();
model.put("foo", bean);
@@ -200,7 +192,6 @@ public class MappingJackson2JsonViewTests {
@Test
public void renderWithCustomSerializerLocatedByFactory() throws Exception {
SerializerFactory factory = new DelegatingSerializerFactory(null);
ObjectMapper mapper = new ObjectMapper();
mapper.setSerializerFactory(factory);
@@ -314,11 +305,13 @@ public class MappingJackson2JsonViewTests {
}
}
@JsonSerialize(using=TestBeanSimpleSerializer.class)
public static class TestBeanSimpleAnnotated extends TestBeanSimple {
}
public static class TestChildBean {
private String value = "bar";
@@ -344,6 +337,7 @@ public class MappingJackson2JsonViewTests {
}
}
public static class TestBeanSimpleSerializer extends JsonSerializer<Object> {
@Override
@@ -355,32 +349,22 @@ public class MappingJackson2JsonViewTests {
}
}
public static class DelegatingSerializerFactory extends BasicSerializerFactory {
private SerializerFactory beanSerializer = BeanSerializerFactory.instance;
public static class DelegatingSerializerFactory extends BeanSerializerFactory {
protected DelegatingSerializerFactory(SerializerFactoryConfig config) {
super(config);
}
@Override
public JsonSerializer<Object> createSerializer(SerializerProvider prov, JavaType type, BeanProperty property) throws JsonMappingException {
public JsonSerializer<Object> createSerializer(SerializerProvider prov, JavaType type) throws JsonMappingException {
if (type.getRawClass() == TestBeanSimple.class) {
return new TestBeanSimpleSerializer();
}
else {
return beanSerializer.createSerializer(prov, type, property);
return super.createSerializer(prov, type);
}
}
@Override
public SerializerFactory withConfig(SerializerFactoryConfig config) {
return null;
}
@Override
protected Iterable<Serializers> customSerializers() {
return null;
}
}
}

View File

@@ -24,6 +24,8 @@ import java.util.Map;
import java.util.Set;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.map.BeanProperty;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.JsonSerializer;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializationConfig;
@@ -31,11 +33,13 @@ import org.codehaus.jackson.map.SerializerFactory;
import org.codehaus.jackson.map.SerializerProvider;
import org.codehaus.jackson.map.annotate.JsonSerialize;
import org.codehaus.jackson.map.ser.BeanSerializerFactory;
import org.codehaus.jackson.type.JavaType;
import org.junit.Before;
import org.junit.Test;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.ContextFactory;
import org.mozilla.javascript.ScriptableObject;
import org.springframework.mock.web.test.MockHttpServletRequest;
import org.springframework.mock.web.test.MockHttpServletResponse;
import org.springframework.ui.ModelMap;
@@ -60,6 +64,7 @@ public class MappingJacksonJsonViewTests {
private ScriptableObject jsScope;
@Before
public void setUp() {
request = new MockHttpServletRequest();
@@ -71,6 +76,7 @@ public class MappingJacksonJsonViewTests {
view = new MappingJacksonJsonView();
}
@Test
public void isExposePathVars() {
assertEquals("Must not expose path variables", false, view.isExposePathVariables());
@@ -78,7 +84,6 @@ public class MappingJacksonJsonViewTests {
@Test
public void renderSimpleMap() throws Exception {
Map<String, Object> model = new HashMap<String, Object>();
model.put("bindingResult", mock(BindingResult.class, "binding_result"));
model.put("foo", "bar");
@@ -122,7 +127,6 @@ public class MappingJacksonJsonViewTests {
@Test
public void renderSimpleBean() throws Exception {
Object bean = new TestBeanSimple();
Map<String, Object> model = new HashMap<String, Object>();
model.put("bindingResult", mock(BindingResult.class, "binding_result"));
@@ -139,7 +143,6 @@ public class MappingJacksonJsonViewTests {
@Test
public void renderWithPrettyPrint() throws Exception {
ModelMap model = new ModelMap("foo", new TestBeanSimple());
view.setPrettyPrint(true);
@@ -153,14 +156,12 @@ public class MappingJacksonJsonViewTests {
@Test
public void renderSimpleBeanPrefixed() throws Exception {
view.setPrefixJson(true);
renderSimpleBean();
}
@Test
public void renderWithCustomSerializerLocatedByAnnotation() throws Exception {
Object bean = new TestBeanSimpleAnnotated();
Map<String, Object> model = new HashMap<String, Object>();
model.put("foo", bean);
@@ -175,8 +176,7 @@ public class MappingJacksonJsonViewTests {
@Test
public void renderWithCustomSerializerLocatedByFactory() throws Exception {
SerializerFactory factory = new DelegatingSerializerFactory();
SerializerFactory factory = new DelegatingSerializerFactory(null);
ObjectMapper mapper = new ObjectMapper();
mapper.setSerializerFactory(factory);
view.setObjectMapper(mapper);
@@ -197,7 +197,6 @@ public class MappingJacksonJsonViewTests {
@Test
public void renderOnlyIncludedAttributes() throws Exception {
Set<String> attrs = new HashSet<String>();
attrs.add("foo");
attrs.add("baz");
@@ -288,11 +287,12 @@ public class MappingJacksonJsonViewTests {
}
}
@JsonSerialize(using=TestBeanSimpleSerializer.class)
public static class TestBeanSimpleAnnotated extends TestBeanSimple {
}
public static class TestChildBean {
private String value = "bar";
@@ -318,10 +318,11 @@ public class MappingJacksonJsonViewTests {
}
}
public static class TestBeanSimpleSerializer extends JsonSerializer<TestBeanSimple> {
public static class TestBeanSimpleSerializer extends JsonSerializer<Object> {
@Override
public void serialize(TestBeanSimple value, JsonGenerator jgen, SerializerProvider provider)
public void serialize(Object value, JsonGenerator jgen, SerializerProvider provider)
throws IOException {
jgen.writeStartObject();
@@ -332,19 +333,22 @@ public class MappingJacksonJsonViewTests {
}
}
public static class DelegatingSerializerFactory extends SerializerFactory {
private SerializerFactory delegate = BeanSerializerFactory.instance;
public static class DelegatingSerializerFactory extends BeanSerializerFactory {
public DelegatingSerializerFactory(Config config) {
super(config);
}
@Override
@SuppressWarnings("unchecked")
public <T> JsonSerializer<T> createSerializer(Class<T> type, SerializationConfig config) {
if (type == TestBeanSimple.class) {
return (JsonSerializer<T>) new TestBeanSimpleSerializer();
public JsonSerializer<Object> createSerializer(SerializationConfig config, JavaType baseType, BeanProperty property) throws JsonMappingException {
if (baseType.getRawClass() == TestBeanSimple.class) {
return new TestBeanSimpleSerializer();
}
else {
return delegate.createSerializer(type, config);
return super.createSerializer(config, baseType, property);
}
}
}
}