Skip to content

Commit

Permalink
Preparations for Jackson 2.5
Browse files Browse the repository at this point in the history
Issue: SPR-12565
  • Loading branch information
jhoeller committed Dec 22, 2014
1 parent 7317457 commit c114c08
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
import com.fasterxml.jackson.databind.cfg.HandlerInstantiator;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import javafx.application.Application;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.FatalBeanException;
Expand Down Expand Up @@ -441,6 +440,7 @@ public <T extends ObjectMapper> T build() {
* settings. This can be applied to any number of {@code ObjectMappers}.
* @param objectMapper the ObjectMapper to configure
*/
@SuppressWarnings("deprecation")
public void configure(ObjectMapper objectMapper) {
Assert.notNull(objectMapper, "ObjectMapper must not be null");

Expand Down Expand Up @@ -495,13 +495,15 @@ public void configure(ObjectMapper objectMapper) {
objectMapper.setPropertyNamingStrategy(this.propertyNamingStrategy);
}
for (Class<?> target : this.mixIns.keySet()) {
// Deprecated as of Jackson 2.5, but just in favor of a fluent variant.
objectMapper.addMixInAnnotations(target, this.mixIns.get(target));
}
if (this.handlerInstantiator != null) {
objectMapper.setHandlerInstantiator(this.handlerInstantiator);
}
else if (this.applicationContext != null) {
objectMapper.setHandlerInstantiator(new SpringHandlerInstantiator(this.applicationContext.getAutowireCapableBeanFactory()));
objectMapper.setHandlerInstantiator(
new SpringHandlerInstantiator(this.applicationContext.getAutowireCapableBeanFactory()));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.ObjectCodec;
import com.fasterxml.jackson.databind.DatabindContext;
import com.fasterxml.jackson.databind.DeserializationConfig;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JavaType;
Expand All @@ -46,9 +47,6 @@
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
import com.fasterxml.jackson.databind.jsontype.impl.StdTypeResolverBuilder;
import com.fasterxml.jackson.databind.type.TypeFactory;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import org.junit.Before;
import org.junit.Test;

Expand All @@ -57,6 +55,8 @@
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.RootBeanDefinition;

import static org.junit.Assert.*;

/**
* Test class for {@link SpringHandlerInstantiatorTests}.
*
Expand All @@ -65,8 +65,10 @@
public class SpringHandlerInstantiatorTests {

private SpringHandlerInstantiator instantiator;

private ObjectMapper objectMapper;


@Before
public void setup() {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
Expand All @@ -78,6 +80,7 @@ public void setup() {
objectMapper = Jackson2ObjectMapperBuilder.json().handlerInstantiator(instantiator).build();
}


@Test
public void autowiredSerializer() throws JsonProcessingException {
User user = new User("bob");
Expand Down Expand Up @@ -113,6 +116,7 @@ public void applicationContextAwareTypeIdResolver() throws JsonProcessingExcepti
assertTrue(CustomTypeIdResolver.isAutowiredFiledInitialized);
}


public static class UserDeserializer extends JsonDeserializer<User> {

@Autowired
Expand All @@ -124,9 +128,9 @@ public User deserialize(JsonParser jsonParser, DeserializationContext deserializ
JsonNode node = oc.readTree(jsonParser);
return new User(this.capitalizer.capitalize(node.get("username").asText()));
}

}


public static class UserSerializer extends JsonSerializer<User> {

@Autowired
Expand All @@ -140,6 +144,7 @@ public void serialize(User user, JsonGenerator jsonGenerator, SerializerProvider
}
}


public static class UpperCaseKeyDeserializer extends KeyDeserializer {

@Autowired
Expand All @@ -151,6 +156,7 @@ public Object deserializeKey(String key, DeserializationContext context) throws
}
}


public static class CustomTypeResolverBuilder extends StdTypeResolverBuilder {

@Autowired
Expand All @@ -170,6 +176,7 @@ public TypeDeserializer buildTypeDeserializer(DeserializationConfig config, Java
}
}


public static class CustomTypeIdResolver implements TypeIdResolver {

@Autowired
Expand All @@ -178,7 +185,6 @@ public static class CustomTypeIdResolver implements TypeIdResolver {
public static boolean isAutowiredFiledInitialized = false;

public CustomTypeIdResolver() {

}

@Override
Expand All @@ -204,15 +210,20 @@ public String idFromValue(Object value) {

@Override
public void init(JavaType type) {

}

@Override
public String idFromBaseType() {
return null;
}

// New in Jackson 2.5
public JavaType typeFromId(DatabindContext context, String id) {
return null;
}
}


@JsonDeserialize(using = UserDeserializer.class)
@JsonSerialize(using = UserSerializer.class)
public static class User {
Expand All @@ -229,6 +240,7 @@ public User(String username) {
public String getUsername() { return this.username; }
}


public static class SecurityRegistry {

@JsonDeserialize(keyUsing = UpperCaseKeyDeserializer.class)
Expand All @@ -243,6 +255,7 @@ public Map<String, String> getCredentials() {
}
}


@JsonTypeInfo(use = JsonTypeInfo.Id.CUSTOM, property = "type")
@JsonTypeResolver(CustomTypeResolverBuilder.class)
@JsonTypeIdResolver(CustomTypeIdResolver.class)
Expand All @@ -255,18 +268,19 @@ public Group(String name) {
}

public Group() {

}

public String getType() {
return Group.class.getName();
}
}


public static class Capitalizer {

public String capitalize(String text) {
return text.toUpperCase();
}
}

}

0 comments on commit c114c08

Please sign in to comment.