Jackson tips: using @JsonAnyGetter/@JsonAnySetter to create "dyna beans"
2016-05-19 11:44
585 查看
One relatively common "special" POJO is so-called dynamic bean ("dyna-bean"), which is sort of combination of regular bean and basic Java Map; with zero or more properties with known name, and extensible set of 'other' key/value pairs.
Here's what such a POJO might look like:
Since Jackson can serialize Bean as well as Maps, what is the problem? As presented, bean would not serialize and deserialize as expected, although it could be modified to just return Map of "other" properties, and deserialize them back. This would work, but
would result in an additional level of wrapping, so that secondary properties would be within a separate JSON Object.
But Jackson can actually be made to work with such POJOs: here is one way to do it:
And there we have it: serializes and deserializes nicely.
Share and enjoy...
Here's what such a POJO might look like:
public class DynaBean { // Two mandatory properties protected final int id; protected final String name; // and then "other" stuff: protected Map<String,Object> other = new HashMap<String,Object>(); public DynaBean(int id, String name) { this.id = id; this.name = name; } public int getId() { return id; } public String getName() { return name; } public Object get(String name) { return other.get(name); } public void set(String name, Object value) { other.put(name, value); } }
Since Jackson can serialize Bean as well as Maps, what is the problem? As presented, bean would not serialize and deserialize as expected, although it could be modified to just return Map of "other" properties, and deserialize them back. This would work, but
would result in an additional level of wrapping, so that secondary properties would be within a separate JSON Object.
But Jackson can actually be made to work with such POJOs: here is one way to do it:
public class DynaBean { // Two mandatory properties protected final int id; protected final String name; // and then "other" stuff: protected Map<String,Object> other = new HashMap<String,Object>(); // Could alternatively add setters, but since these are mandatory @JsonCreator public DynaBean(@JsonProperty("id") int id, @JsonProperty("name") String name) { this.id = id; this.name = name; } public int getId() { return id; } public String getName() { return name; } public Object get(String name) { return other.get(name); } // "any getter" needed for serialization @JsonAnyGetter public Map<String,Object> any() { return other; } @JsonAnySetter public void set(String name, Object value) { other.put(name, value); } }
And there we have it: serializes and deserializes nicely.
Share and enjoy...
相关文章推荐
- extjs-mvc开发模式
- JavaScript中解决多浏览器兼容性23个问题的快速解决方法
- 【转】图解javascript this指向什么?
- js获取不同浏览器盒子宽度高度
- 超好用的C# json转换器。类js动态使用!
- 判断数组是否包含某个元素的js函数实现方法
- 如何在QML中定义Javascript资源
- js常用
- 只有20行Javascript代码!手把手教你写一个页面模板引擎
- JSON真的比XML好很多吗?
- 使用Chrome调试JavaScript的断点设置和调试技巧
- oc调用js方法
- OC调用js(JavaScriptCore)
- phantomjs安装使用
- JS实现点击复制功能(ZeroClipboard)
- javascript 浏览器
- js splice()的用法
- js计算百分比
- ExtJs Column 显示文字内容过长 使用Tootip显示全部内容
- javascript面向对象程序设计高级特性经典教程(值得收藏)