[JERSEY-380] Add application property to set the default JSON convention Created: 28/Sep/09  Updated: 11/Jan/16  Resolved: 01/Sep/15

Status: Resolved
Project: jersey
Component/s: core
Affects Version/s: 1.0.3
Fix Version/s: None

Type: Improvement Priority: Critical
Reporter: drampulla Assignee: Unassigned
Resolution: Complete Votes: 2
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Operating System: All
Platform: All


Issuezilla Id: 380

 Description   

When I attempt to serialize an array of one element as JSON, the serialization
is failing to surround my element with backets[]. This is causing problems
downstream since the expected result is a list.

A very simple example of this:

/**
This example produces a result of

{"data":"aaa"}

and it should have produced

{"data":["aaa"]}

*/
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@Path("test.ui")
public class Test {

@GET
@Produces(MediaType.APPLICATION_JSON)
public TestData getSessionState() {

TestData rc = new TestData();
rc.data = new String[]

{ "aaa" }

;
return rc;
}

@XmlRootElement(name="TestDAta")
public static class TestData

{ @XmlElement public String[] data; }

}



 Comments   
Comment by ido_ran [ 28/Sep/09 ]

I also run into this same issue.
I've change my serialization format to XML for now since I didn't find any result for it.

Comment by japod [ 29/Sep/09 ]

You can configure Jersey so that your single element array remains an array,
please see

https://jersey.dev.java.net/nonav/documentation/1.0.3/user-guide.html#d0e1137

for details.

You can either stick with the default mapped JSON notation, and explicitly
tell it, what arrays you use, or completely rely on the natural notation,
which should detect arrays automatically.

Does it solve your issue?

Comment by drampulla [ 29/Sep/09 ]

It seems strange that the default behavior is not "natural". Perhaps this
should be listed as an enhancement since it is very annoying to have to create a
new provider which returns a JAXBJSONContext for each message returned from my
actions. This means every action I perform needs to have a provider returning a
customized context. This generates a lot of excess code.

I do however want to thank you for your help on this. You really made my day.

Comment by sandoz [ 01/Oct/09 ]

The default is not natural because it would break backwards compatibility.

What we require is a property that could be declared in the web.xml or the
ResourceConfig to set the convention.

I have updated the title and changed the type to an enhancement.

Comment by Marek Potociar [ 01/Sep/15 ]

Jersey 1.x branch is not under active development anymore. Only security issues and issues reported by the customers of commercially supported products that ship with Jersey are fixed on this code base right now.
Please, move your code to Jersey 2.x and if you still see the issue there, feel free to file a bug against Jersey 2.x.

Generated at Thu Dec 08 12:26:33 UTC 2016 using JIRA 6.2.3#6260-sha1:63ef1d6dac3f4f4d7db4c1effd405ba38ccdc558.