0

I have a Java app working with CouchDB. For parsing JSON from it, I use lib: com.google.code.svenson v.1.3.8 But, I've got a problem. I receive JSON, and one property in it I want to exclude. Example:

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

I need to exclude "_attachments" field and all inners. But problem is, that property revision is dynamically. It changes all time. And my code doesn't work:

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)
Tunaki
  • 132,869
  • 46
  • 340
  • 423
Andry
  • 655
  • 2
  • 11
  • 22

1 Answers1

0

Yes, you can do it. All you need is to declare the List of Address as transient property in you business object.

Then add the following code to your jsonConfig:

JsonConfig jsonConfig = new JsonConfig();
jsonConfig.setIgnoreTransientFields(true);
Ren
  • 1,111
  • 5
  • 15
  • 24