org.svenson.JSONProperty以排除动态属性

时间:2012-12-12 08:53:08

标签: java json properties svenson

我有一个使用CouchDB的Java应用程序。为了从中解析JSON,我使用了lib: com.google.code.svenson v.1.3.8 但是,我遇到了问题。我收到JSON,其中包含一个我要排除的属性。例如:

{"groups":[], "norms":[], "unit":null, "_attachments":{
    "revision/1-b7c9e44edecb4c6509ce75d84d3aa06d":{
        "content_type":"application/json",
        "revpos":2,
        "digest":"md5-qPtWsDPF7vQANTQvQzmjrA==",
        "length":684,
        "stub":true
    }
}}

我需要排除“_ attachments”字段和所有内容。但问题是,属性修订版是动态的。它一直在变化。我的代码不起作用:

public class TestDTO {

    private String attachment;
    private String revision;
    private String contentType;
    private String revpos;
    private String digest;
    private String length;
    private String stub;

    @JSONProperty(ignore = true, value = "_attachments")
    public void setAttachment(String attachment) {
        this.attachment = attachment;
    }

    public String getRevision() {
        return revision;
    }

    @JSONProperty(ignore = true, value = "revision/")
    public void setRevision(String revision) {
        this.revision = revision;
    }
//other get/set methods 
}
org.svenson.JSONParseException: Cannot set property revision/1-d009693fd000f179495cb3f9087109b6 on class java.lang.String
    at org.svenson.JSONParser.parseObjectInto(JSONParser.java:589)
    at org.svenson.JSONParser.parseObjectInto(JSONParser.java:617)
    at org.svenson.JSONParser.parse(JSONParser.java:396)
    at org.svenson.JSONParser.parse(JSONParser.java:341)
    at com.foo.core.api.services.bar.BarObjectParser.parseToObject(BarObjectParser.java:47)
    at com.foo.core.api.services.bar.task.ChangesProcessorTask.persistObject(ChangesProcessorTask.java:78)
    at com.foo.core.api.services.bar.task.ChangesProcessorTask.processChange(ChangesProcessorTask.java:65)
    at com.foo.core.api.services.bar.task.ChangesProcessorTask.processChanges(ChangesProcessorTask.java:46)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.springframework.util.MethodInvoker.invoke(MethodInvoker.java:273)
    at org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean$MethodInvokingJob.executeInternal(MethodInvokingJobDetailFactoryBean.java:264)
    at org.springframework.scheduling.quartz.QuartzJobBean.execute(QuartzJobBean.java:86)
    at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
    at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:525)

1 个答案:

答案 0 :(得分:0)

是的,你可以做到。您所需要的只是将业务对象中的地址列表声明为瞬态属性。

然后将以下代码添加到jsonConfig:

JsonConfig jsonConfig = new JsonConfig();
jsonConfig.setIgnoreTransientFields(true);