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

Categories

0 votes
359 views
in Technique[技术] by (71.8m points)

java - How to map POJO to several JSON presentation

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

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

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...