users@jersey.java.net

Re: [Jersey] Can i take out the json document prefix ?

From: Jakub Podlesak <Jakub.Podlesak_at_Sun.COM>
Date: Mon, 05 Jan 2009 14:08:28 +0100

Hi Jack,

please see inline...

On Wed, Dec 24, 2008 at 05:57:51PM -0800, beanor wrote:
>
> hi
>
> We have a Person class like this:
>
> @XmlAccessorType(XmlAccessType.FIELD)
> @XmlRootElement(name = "person")
> public class Person {
> private String name;
> private String sex;
> private String age;
> private int mark;
> private Set<Child> childSet = new HashSet<Child>();
> public String getName() {
> return name;
> }
> public void setName(String name) {
> this.name = name;
> }
> public String getSex() {
> return sex;
> }
> public void setSex(String sex) {
> this.sex = sex;
> }
> public String getAge() {
> return age;
> }
> public void setAge(String age) {
> this.age = age;
> }
> public Set<Child> getChildSet() {
> return childSet;
> }
> public void setChildSet(Set<Child> childSet) {
> this.childSet = childSet;
> }
> public int getMark() {
> return mark;
> }
> public void setMark(int mark) {
> this.mark = mark;
> }
> }
>
> And Provider like this:
>
> @Provider
> public final class JAXBContextResolver implements
> ContextResolver<JAXBContext> {
>
> Map<String, Object> props = new HashMap<String, Object>();
> private final JAXBContext context;
> private final Class<?>[] classes = {Person.class};
> public JAXBContextResolver() throws Exception {
> Map<String, Object> props = new HashMap<String, Object>();
> props.put(JSONJAXBContext.JSON_NOTATION,
> JSONJAXBContext.JSONNotation.MAPPED);
> props.put(JSONJAXBContext.JSON_ROOT_UNWRAPPING, Boolean.TRUE);
> props.put(JSONJAXBContext.JSON_NON_STRINGS, new
> HashSet<String>(1){{add("mark");}});
> context = new JSONJAXBContext(classes, props);
> }
>
> public JAXBContext getContext(Class<?> objectType) {
> return context;
> }
> }
>
> Our Resource class is :
>
> @Path("person")
> public class PersonResource {
> @GET
> @Path("list")
> @Produces("application/json")
> public List<Person> getPersons() {
> List<Person> list = new ArrayList<Person>();
> for (int i=0; i<5; i++) {
> Person p = new Person();
> p.setName("redhacker" +1);
> p.setAge("22");
> p.setSex("1");
> p.setMark(98);
> list.add(p);
> }
> return list;
> }
> }
>
> We have got a result is:
>
> {"person":[{"name":"redhacker1","sex":"1","age":"22","mark":"98"},{"name":"redhacker1","sex":"1","age":"22","mark":"98"},{"name":"redhacker1","sex":"1","age":"22","mark":"98"},{"name":"redhacker1","sex":"1","age":"22","mark":"98"},{"name":"redhacker1","sex":"1","age":"22","mark":"98"}]}
>
> But we hope to get the result like this:
>
> [{"name":"redhacker1","sex":"1","age":"22","mark":"98"},{"name":"redhacker1","sex":"1","age":"22","mark":"98"},{"name":"redhacker1","sex":"1","age":"22","mark":"98"},{"name":"redhacker1","sex":"1","age":"22","mark":"98"},{"name":"redhacker1","sex":"1","age":"22","mark":"98"}]
>
> NO have a prefix(person), can we realize this?


Unfortunatelly not at the moment. I want to introduce, what you are requesting here with improved
JAXB/JSON support. It should come into the next Jersey release at the end of this month.

Thanks for your patience,

~Jakub
>
> Any helps?
>
> Thanks and Merry Christmas!
>
> Jack.
>
> --
> View this message in context: http://n2.nabble.com/Can-i-take-out-the-json-document-prefix---tp1799407p1799407.html
> Sent from the Jersey mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>