users@jersey.java.net

[Jersey] JSON Arrays

From: sarat.pediredla <sarat.pediredla_at_gmail.com>
Date: Sun, 21 Dec 2008 14:17:50 -0800 (PST)

Hi,

I know that in Jersey 1.0, to enable JSON elements to be forced to be
displayed as arrays, I can use the following,

props.put(JSONJAXBContext.JSON_ARRAYS, new
HashSet<String>(1){{add("user");}});

This might seem a newbie question but what do I do when I want multiple
beans added to the JSON_ARRAYS list? Is the following code right? It
definitely does not work as the JSON returned does not treat single elements
as arrays.

public class JsonContextResolver implements ContextResolver<JAXBContext> {

   private JAXBContext context;
   private Class[] types = {
           User.class,
           Role.class,
           Group.class
   };

   public JsonContextResolver() throws Exception {
       Map 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_ARRAYS, new
HashSet<String>(types.length){{
           add("users");
           add("roles");
           add("groups");
       }});
       this.context = new JSONJAXBContext(types, props);
   }

   public JAXBContext getContext(Class<?> objectType) {
       for(Class<?> x : types){
           if(x.equals(objectType)){
               return context;
           }
       }
       return null;
   }
-- 
View this message in context: http://n2.nabble.com/JSON-Arrays-tp1687952p1687952.html
Sent from the Jersey mailing list archive at Nabble.com.