在jcouchdb中,我用来扩展BaseDocument,然后以一种透明的方式混合注释和未声明的字段。示例:
import org.jcouchdb.document.BaseDocument;
public class SiteDocument extends BaseDocument {
private String site;
@org.svenson.JSONProperty(value = "site", ignoreIfNull = true)
public String getSite() {
return site;
}
public void setSite(String name) {
site = name;
}
}然后使用它:
// Create a SiteDocument
SiteDocument site2 = new SiteDocument();
site2.setProperty("site", "http://www.starckoverflow.com/index.html");
// Set value using setSite
site2.setSite("www.stackoverflow.com");
// and using setProperty
site2.setProperty("description", "Questions & Answers");
db.createOrUpdateDocument(site2);在我使用通过注释定义的文档字段(site)和未定义的属性字段(description)的情况下,当我保存文档时,这两个字段都会被序列化。
这对我来说很方便,因为我可以处理半结构化文档。
当我尝试使用Ektorp做同样的事情时,我有使用注释的文档和使用HashMap的文档,但我找不到一种简单的方法来混合使用这两者(我已经尝试使用我自己的序列化程序,但对于我在jcouchdb中免费获得的东西,这似乎很有用)。我还试图注释一个HashMap字段,但随后被序列化为一个对象,并且我自动将这些字段保存在一个带有HashMap字段名称的对象中。
可以使用Ektorp (很容易/免费)做吗?
发布于 2012-05-21 14:45:05
这绝对是有可能的。您有两个选择:
使用@JsonAnySetter和@JsonAnyGetter将您的类基于org.ektorp.support.OpenCouchDbDocument
https://stackoverflow.com/questions/10672115
复制相似问题