How to serialize using @Jsonview with nested objects

kiruba picture kiruba · Apr 24, 2014 · Viewed 6.9k times · Source

I have a class which holds a collection of another class.

class A{
 @JsonView(VerboseViewA.Minimal.class)
 String field1;
 @JsonView(VerboseViewA.Complete.class)
 String field2;
 @JsonView(VerboseViewA.Complete.class)
 Collection<B> bEntities;
}

class B{
   @JsonView(VerboseViewB.Minimal.class)
    String field2;
   @JsonView(VerboseViewB.Complete.class)
    String field3;
 }

When i serialize Class A using VerboseViewA.Complete, i want the collection bEntities to be serialized using VerboseViewB.Minimal.

Is there a way to achieve it?

Answer

kiruba picture kiruba · Apr 24, 2014

This solves my problem. I ain't sure if there is a better way to solve this.

 class A{
  @JsonView(VerboseViewA.Minimal.class)
  String field1;
  @JsonView(VerboseViewA.Complete.class)
  String field2;
  @JsonView(VerboseViewA.Complete.class)
  Collection<B> bEntities;
 }

 class B{
    @JsonView({VerboseViewA.Complete.class,VerboseViewB.Minimal.class})
    String field2;
    @JsonView(VerboseViewB.Complete.class)
    String field3;
 }