Thursday, February 10, 2011

JAX-RS, Map<String,String> to JSON without the overhead?

Hi!

I'm using JAX-RS to create restful webservices in Java. I am getting to much overhead in the produced JSON.

Data class:

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Test {

    private Map<String,String> data;

    Test() {}

    public Test(Map<String,String> data) {
        this.data = data;
    }

    public Map<String, String> getData() {
        return data;
    }
}

Service:

@GET
@Path("/test")
@Produces("application/json; charset=UTF-8;")
public Test test() {
   Map<String,String> map = new HashMap<String,String>();
   map.put("foo", "bar");
   map.put("bingo", "bongo");
   return new Test(map);
}

Produces:

{"data":{"entry":[{"key":"foo","value":"bar"},{"key":"bingo","value":"bongo"}]}}

I would like it to produce:

{"data":{"foo":"bar","bingo":"bongo"}}

What is the simplest way to achive this? I am free to redifine my data class but I can't know in advance the keys or size of the map.

0 comments:

Post a Comment