1

I'm trying to use jcouchdb (https://code.google.com/p/jcouchdb/) for accessing my CouchDB instance from Java. I have some JSon documents that I'd like to parse into Java classes - with Svenson, used in jcouchdb, and then put those parsed objects into DB. I generate this JSON objects with AVRO (http://avro.apache.org) JSon Encoder, they seem to be ok, but apparently other parsers have problems with them.

My JSon strings look like this:

{
   "id":40,
   "event_id":"48764322212",
   "note":{
      "string":"ABC note"
   },
   "created_date":null,
   "event_category":null,
   "city":null,
   "address":null
}

Which seems valid JSON - validated with http://jsonformatter.curiousconcept.com/

However my Svenson object defined like this:

public class Note {

    Long id;
    String eventId;
    String note;
    String createdDate;
    String eventCategory;
    String city;
    String address;

    @JSONProperty()
    public Long getId() {

    @JSONProperty("event_id")
    public String getEventId() {

    @JSONProperty("note")
    public String getNote() {

    @JSONProperty("created_date")
    public String getCreatedDate() {

    @JSONProperty("event_category")
    public String getEventCategory() {

    @JSONProperty("city")
    public String getCity() {

    @JSONProperty("address")
    public String getAddress() {

}

(setters and getters' bodies intentionally removed)

The error when parsing is:

Cannot set property string on class java.lang.String

It seems that this JSON is parsed correctly (there is a difference in note field):

{
   "id":40,
   "event_case_id":"000-123123123",
   "event_msisdn":"48764322212",
   "note":"Planowana data portacji: 2011/01/27 11:42:49",
   "created_date":null,
   "event_category":null,
   "city":null,
   "address":null
}

How can I work this out? Perhaps there is another json library that would work for me?

Boundless
  • 2,444
  • 2
  • 25
  • 40
Marcin Cylke
  • 2,100
  • 2
  • 21
  • 40

1 Answers1

3

You declare note as a java.lang.String:

public String getNote()

but in the JSON you declare it as an Object with a property named "string":

"note":{
  "string":"ABC note"
}

You need to change the JSON or the Bean to match each other. For example, in the second functioning JSON, you declared the JSON note as a string. This is why it works.

Marcello Nuccio
  • 3,901
  • 2
  • 28
  • 28
  • Ok, this seemd quite obvious + the error message. Could you suggest how should I define my bean so the above JSON string would work? I can't modify Avro's generated json. – Marcin Cylke Mar 22 '11 at 08:01
  • 1
    I cannot test it right now, but I think you should use [type selection](http://code.google.com/p/svenson/wiki/TypeSelection) to map the object with the "string" property to a String. – Marcello Nuccio Mar 22 '11 at 08:34
  • Well, adding a simple: parser.addTypeHint(".string", String.class) didn't help - should I use different matching syntax? – Marcin Cylke Mar 22 '11 at 10:26