如何更改JsonView的特定属性

时间:2015-10-22 23:56:59

标签: java serialization jackson json-view

嗨,我有很少的UI对象互相嵌入。示例#1:

class CarUI {
  public UserUI user;
  public UserUI agent;
  ....
  public UserUI getUser() {
    return user;
  }
  public UserUI getAgent() {
    return agent;
  }
}
class UserUI {
  @JsonView({Views.Public.class,Views.Private.class})
  public String name;
  @JsonView({Views.Private.class})
  public String phone;
  ...
}

正如您所看到的,如果我将UserUI序列化为Public,我将只显示名称,但对于私有我将公开所有字段。

这是我的问题:当我序列化对象CarUI时,我想只显示" user"的名称。以及"代理商"的名称和电话。这实际上意味着在getUser()的getter中我想将JsonView更改为Views.Public.class,而对于getAgent()我想将其更改为Views.Private.class

是否有办法在getter中覆盖JsonView,以便使用这个新的JsonView对返回的对象的所有层次结构进行序列化?

被修改 如下所示,您可以使用JsonView继承来解决我的示例#1。怎样更通用。以下是示例#2:

class CarUI {
  public UserUI user;
  public UserUI agent;
  ....
  public UserUI getUser() {
    return user;
  }
  public UserUI getAgent() {
    return agent;
  }
}
class UserUI {
  @JsonView({Views.Public.class,Views.Private.class})
  public String name;
  @JsonView({Views.Private.class})
  public String phone;
  @JsonView({Views.Public.class})
  public String avatarUrl;
  ...
}

在此示例中,Id喜欢为代理显示名称+电话,为用户显示名称+ avatarUrl。

2 个答案:

答案 0 :(得分:2)

@JsonView接受继承。 Views.Private字段扩展了Views.Public字段。这意味着Views.Private序列化也将查找Views.Public注释。

public Views {
    public Public {}
    public Private extends Public {}
}

class CarUI {
  public UserUI user;
  public UserUI agent;
  ....
  @JsonView(Views.Public.class)
  public UserUI getUser() {
    return user;
  }
  @JsonView(Views.Private.class)
  public UserUI getAgent() {
    return agent;
  }
}

class UserUI {
  @JsonView(Views.Public.class)
  public String name;
  @JsonView(Views.Private.class)
  public String phone;
  ...
}

例如#2

您可以引入多个类,如:

public Views {
    public Public {}
    public Private1 extends Public {}
    public Private2 extends Public {}
}

class CarUI {
  public UserUI user;
  public UserUI agent;
  ....
  public UserUI getUser() {
    return user;
  }
  public UserUI getAgent() {
    return agent;
  }
}
class UserUI {
  @JsonView(Views.Public.class)
  public String name;
  @JsonView({Views.Private1.class})
  public String phone;
  @JsonView({Views.Private2.class})
  public String avatarUrl;
  ...
}

或者您可以使用无视图属性并且不使用继承:

public Views {
    public Public {}
    public Private {}
}

class CarUI {
  public UserUI user;
  public UserUI agent;
  ....
  public UserUI getUser() {
    return user;
  }
  public UserUI getAgent() {
    return agent;
  }
}
class UserUI {
  // This one is view-less shows every time unless you specificy
  public String name;
  @JsonView({Views.Private.class})
  public String phone;
  @JsonView({Views.Public.class})
  public String avatarUrl;
  ...
}

...
// If you want to hide view-less properties, default is true 
objectMapper.configure(SerializationConfig.Feature.DEFAULT_VIEW_INCLUSION, false); 
...

答案 1 :(得分:1)

如果有人需要,只需找到更通用的案例。基本上你可以使用不同的JsonView在getter中转换你的属性:

  @JsonView({ Views.Detail.class})
  public JsonNode getAgent() throws IOException {
      ObjectMapper mapper = new ObjectMapper();
      String json = mapper.writerWithView(Views. Private.class).writeValueAsString(agent);
      return mapper.readTree(json);
  }