I'm creating a REST server with Jersey/Java and I found a strange behavior.
I have a method on the server that returns an array of objects as Json
@GET
@Path("/files")
@Produces(MediaType.APPLICATION_JSON)
public Object getFiles() throws Exception{
DatabaseManager db = new DatabaseManager();
FileInfo[] result = db.getFiles();
return result;
}
The code is executed correctly and data is returned to the client (a jQuery ajax call). The problem is that the format of the returned data changes if the "result" array has one element or more than one.
Response with one element:
{"fileInfo":{"fileName":"weather.arff","id":"10"}}
Response with two elements:
{"fileInfo":[{"fileName":"weather.arff","id":"10"},{"fileName":"supermarket.arff","id":"11"}]}
As you can see, in the first scenario the value of the "fileInfo" property of the returned object is an object, and in the second case the value is an array. What am I doing wrong? Shouldn't the first case return something like this:
{"fileInfo":[{"fileName":"weather.arff","id":"10"}]}
i.e. an array with a single object inside?
I know that I can detect this on the client side, but it seems like a very ugly hack.
Thanks for your time.