17

I'm using MappingJacksonJsonView to serialize to JSON a class, however, I'd like to be able to rename some of the fields from the default name based on the getter name.

This is because I've to output field names like "delete_url" and "delete_type" for jQuery file upload. I'm using @Jsonserialize annotation to hand pick the fields to serialize.

@JsonAutoDetect(getterVisibility = Visibility.NONE)
public interface Picture {

    @JsonSerialize
    String getName();

    @JsonSerialize
    String getDelete_url();

    ...

For instance, I'm forced to call a method getDelete_url(), while I'd like to call it getDeleteUrl(), but still output the key "delete_url" when serializing to JSON.

stivlo
  • 83,644
  • 31
  • 142
  • 199

2 Answers2

30

You should be able to qualify using @JsonProperty.

@JsonAutoDetect(getterVisibility = Visibility.NONE)
public interface Picture {

  @JsonSerialize
  @JsonProperty("name")
  String getName();

  @JsonSerialize
  @JsonProperty("delete_url")
  String getDeleteUrl();

  //...
stivlo
  • 83,644
  • 31
  • 142
  • 199
nicholas.hauschild
  • 42,483
  • 9
  • 127
  • 120
  • I am creating a web service using Spring MVC, which simply returns json reposne. This is not working in my case. It is taking name of the field only. – Badal May 15 '13 at 07:40
8

Have you tried using the @JsonProperty annotation?

"Defines name of the logical property, i.e. Json object field name to use for the property: if empty String (which is the default), will use name of the field that is annotated."

Juliano Alves
  • 2,006
  • 4
  • 35
  • 37