kaqqao
kaqqao

Reputation: 15439

Is there a way to use Jackson's SerializationFeature with annotations

I'm trying to use SerializationConfig.Feature.WRITE_CHAR_ARRAYS_AS_JSON_ARRAYS but I'm not configuring the mapper myself, relying on annotations exclusively and letting Spring's RestTemplate (de)serialize automatically. Is there a way to enable the aforementioned feature in this scenario (i.e. annotations only)?

NOTE: I'm using Jackson 1.x and can't upgrade due to other libs...

Upvotes: 3

Views: 1918

Answers (2)

StaxMan
StaxMan

Reputation: 116522

With JAX-RS (like DropWizard) you can actually annotated resource endpoints, using @JacksonFeatures

public class Resource {
      @Path("item")
      @GET
      @JacksonFeatures(serializationEnable={ SerializationFeature.WRAP_ROOT_VALUE })
      public Pojo getItem(String id) {
          ...
      }
}

I don't know if Spring exposes similar functionality, but it seems possible it does. And if not, it is something they should be able to add to allow per-endpoint setting/clearing of SerializationFeatures / DeserializationFeatures. So if it is not available, maybe file a feature request for Spring project?

Upvotes: 3

Jaiwo99
Jaiwo99

Reputation: 10017

Yes, it is possible.

checkout this link: http://jackson.codehaus.org/1.7.0/javadoc/org/codehaus/jackson/map/annotate/JsonSerialize.html

Example:

@JsonSerialize(using=MySerializer.class,
    as=MySubClass.class,
    include=JsonSerialize.Inclusion.NON_NULL,
    typing=JsonSerialize.Typing.STATIC
  )

Upvotes: 2

Related Questions