Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

How to map POJO to several JSON presentation?

I am using Jackson.

I want something like code below

@JsonIgnorePropertiesStreamA({ "value2" })
@JsonIgnorePropertiesOtherWay({ "value3" })
public class Value {
  public int value;
  public int value2;
  public int value3;
}

How to do that with Jackson? or What other libraries could do that?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
345 views
Welcome To Ask or Share your Answers For Others

1 Answer

You use JSON Views

class Views {
    static class PublicView { }
    static class StreamA extends PublicView { }
    static class OtherWay extends PublicView { }
}

public class Value {
    @JsonView(Views.PublicView.class) public int value;
    @JsonView(Views.OtherWay.class) public int value2;
    @JsonView(Views.StreamA.class) public int value3;
}


String json = new ObjectMapper()
              .writerWithView(Views.OtherWay.class)
              .writeValueAsString(valueInstance);

Note that these are inclusive rather than exclusive; you create a view that includes the fields you want.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...