Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Jackson JSON Views without annotating original bean class

Is there any way that I can use Jackson JSON Views or something like it, without having to annotate the original bean class? I'm looking for some kind of runtime/dynamic configuration to let me do something similar.

My bean is an @Entity packaged in a JAR that may be shared by multiple projects. I'm trying to avoid touching and re-packaging the shared JAR because of UI changes in the consuming projects.

Ideally I'd like to do something like

jsonViewBuilder = createViewBuilder(View.class);
jsonViewBuilder.addProperty("property1");
jsonViewBuilder.addProperty("property2");

to replace

Bean {
  @JsonView(View.class)
  String property1;

  @JsonView(View.class)
  String property2;
}

Any ideas?

Underlying environment: Spring 3.0, Spring MVC and Glassfish 3.1.1.

like image 249
wrschneider Avatar asked Dec 12 '11 14:12

wrschneider


People also ask

What does @JsonProperty annotation do?

The @JsonProperty annotation is used to map property names with JSON keys during serialization and deserialization. By default, if you try to serialize a POJO, the generated JSON will have keys mapped to the fields of the POJO.

What is JsonView annotation?

The JsonView annotation can be used to include/exclude a property during the serialization and deserialization process dynamically. We need to configure an ObjectMapper class to include the type of view used for writing a JSON from Java object using the writerWithView() method.

What is the use of @JsonRootName?

The @JsonRootName annotation is used, if wrapping is enabled, to specify the name of the root wrapper to be used.

What is @JsonAlias?

The @JsonAlias annotation can define one or more alternate names for the attributes accepted during the deserialization, setting the JSON data to a Java object. But when serializing, i.e. getting JSON from a Java object, only the actual logical property name is used instead of the alias.


1 Answers

How about using the Mix-In feature?

http://wiki.fasterxml.com/JacksonMixInAnnotations

http://www.cowtowncoder.com/blog/archives/2009/08/entry_305.html


import org.codehaus.jackson.annotate.JsonAutoDetect.Visibility;
import org.codehaus.jackson.annotate.JsonMethod;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializationConfig;
import org.codehaus.jackson.map.annotate.JsonView;

public class JacksonFoo
{
  public static void main(String[] args) throws Exception
  {
    ObjectMapper mapper = new ObjectMapper().setVisibility(JsonMethod.FIELD, Visibility.ANY)
        .configure(SerializationConfig.Feature.DEFAULT_VIEW_INCLUSION, false);
    mapper.getSerializationConfig().addMixInAnnotations(Bar.class, BarMixIn.class);
    mapper.setSerializationConfig(mapper.getSerializationConfig().withView(Expose.class));

    System.out.println(mapper.writeValueAsString(new Bar()));
    // output: {"b":"B"}
  }
}

class Bar
{
  String a = "A";
  String b = "B";
}

abstract class BarMixIn
{
  @JsonView(Expose.class)
  String b;
}

// Used only as JsonView marker.  
// Could use any existing class, like Object, instead.  
class Expose {}
like image 64
Programmer Bruce Avatar answered Oct 04 '22 04:10

Programmer Bruce