Question

I have a project that uses Spring MVC to create and handle multiple REST endpoints. I'm currently working on using Jackson to automatically handle the seralization/deserialization of JSON using the @RequestBody and @ResponseBody annotations.

I have gotten Jackson working, so I've got a starting point. My problem is that our old serialization was done manually and used Pascal casing instead of Camel casing ("MyVariable" instead of "myVariable"), and Jackson does Camel casing by default.

I know that I can manually change the name for a variable using @JsonProperty. That being said, I do not consider adding @JsonProperty to all of my variables to be a viable long-term solution.

Is there a way to make Jackson use Pascal casing when serializing and deserializing other than using the @JsonProperty annotation?

EDIT: It looks like there's not a clean way to do this externally. A couple people have suggested overriding different classes as a way to accomplish my goal. I'm open to suggestions on what I can override that will change the casing. At the moment I have made a custom ObjectMapper that sets some properties I want (namely Inclusion.NON_NULL). I haven't found any place yet that would let me change the casing behavior. Any thoughts?

Was it helpful?

Solution 2

I ended up solving the issue by overriding the (de)serializers. For those interested, here's how you can do it yourself:

Step 1. Extend BeanSerializerFactory.

Override the _constructWriter(SerializationConfig config, TypeBindings typeContext, PropertyBuilder pb, boolean staticTyping, String name, AnnotatedMember propertyMember) method. Inside that method, modify name in any way you see fit. To implement Pascal casing, I used this line: String formattedName = name.substring(0, 1).toUpperCase() + name.substring(1);. After modifying name, call super._constructWriter.

Step 2. Extend BeanDeserializationFactory.

Override the constructSettableProperty(DeserializationConfig config, BasicBeanDescription beanDesc, String name, AnnotatedMethod setter) method. Do the same thing with the name parameter that you did inside your custom BeanSerializerFactory.

Step 3. Create an ObjectMapper.

Set the serializer factory to be your custom bean serializer factory. Set the deserializer provider (I used this line: objectMapper.setDeserializerProvider(new StdDeserializerProvider(new CustomJacksonBeanDeserializerFactory()))).

That's it. The ObjectMapper you created will use your new naming scheme when serializing or deserializing JSON.

OTHER TIPS

See http://www.cowtowncoder.com/blog/archives/2011/03/entry_448.html If you can wait for 1.8 it will be included there.

For what it's worth, there is a Jira issue to support pluggable strategies; voting for it might help convince developers to add support. As I mentioned in the comment, it is possible to override internal behavior, but it is not a simple thing to do.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top