How to serialize using @Jsonview with nested objects

天大地大妈咪最大 提交于 2019-12-19 05:18:50

问题


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?


回答1:


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;
 }



回答2:


After struggling with the same problem, I came up to this solution:

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

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

Now when serializing an instance of class A using VerboseViewA.Complete.class, bEnitities will be included and serialized using a custom VerboseMinimalSerializer, overriding its JsonView:

public class VerboseMinimalSerializer extends JsonSerializer<Object>
{
    @Override
    public void serialize(Object object, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException
    {
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, false);
        mapper.setSerializationInclusion(Include.NON_NULL);
        mapper.setConfig(mapper.getSerializationConfig().withView(VerboseViewB.Minimal.class));

        jsonGenerator.setCodec(mapper);
        jsonGenerator.writeObject(object);
    }
}

Notice this custom serializer is using the view VerboseViewB.Minimal.class.




回答3:


you need to change your json view hierachy a little:

public class MinimalView { };
public class AComplete extends MinimalView { };
public class BComplete extends MinimalView { };

then you can annotate your classes like

class A{
    @JsonView(MinimalView.class)
    String field1;
    @JsonView(AComplete.class)
    String field2;
    @JsonView(AComplete.class)
    Collection<B> bEntities;
 }

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

when you serialize using the view AComplete, the serializer will take the AComplete and MinimalView properties because AComplete extends MinimalView.



来源:https://stackoverflow.com/questions/23260464/how-to-serialize-using-jsonview-with-nested-objects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!