users@jersey.java.net

Re: [Jersey] can i use jersey's json Unmarshaller?

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Wed, 10 Dec 2008 16:29:39 +0100

Hi,

The source code you sent looks like it is corrupted, namely the
readFrom method has the following:

                for (int i=0,size = arr.size(); i getActualClass(Type
genericType) {
         ParameterizedTypeImpl pti = (ParameterizedTypeImpl)
genericType;
         Type[] types = pti.getActualTypeArguments();
         ParameterizedTypeImpl type = (ParameterizedTypeImpl) types[1];
         Type[] types2 = type.getActualTypeArguments();
         Class type2 = (Class) types2[0];
         return type2;

So i cannot evaluate the code properly.

There is no existing functionality to return a Map instance for a JSON
entity. The closest supported Java type is JSONObject from the
jettison library.

Why are you passing methods in the request? The methods "add",
"delete", "update" can correspond to POST, DELETE and PUT methods in
HTTP.

For an example of implementing providers you can look at the following
sample:

   http://download.java.net/maven/2/com/sun/jersey/samples/entity-provider/1.0.1/entity-provider-1.0.1-project.zip

Paul.
On Dec 10, 2008, at 11:39 AM, beanor wrote:

> hi paul: I want Unmarshall a json string to Map param of Resource'
> method,now , I do it as follow:
> package com.jersey.sample.provider;
>
> import java.io.IOException;
> import java.io.InputStream;
> import java.io.InputStreamReader;
> import java.io.Reader;
> import java.lang.annotation.Annotation;
> import java.lang.reflect.Type;
> import java.util.ArrayList;
> import java.util.HashMap;
> import java.util.List;
> import java.util.Map;
>
> import javax.ws.rs.Consumes;
> import javax.ws.rs.WebApplicationException;
> import javax.ws.rs.core.MediaType;
> import javax.ws.rs.core.MultivaluedMap;
> import javax.ws.rs.ext.MessageBodyReader;
> import javax.ws.rs.ext.Provider;
>
> import net.sf.json.JSONArray;
> import net.sf.json.JSONObject;
> import sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl;
>
> import com.jersey.sample.Constant;
>
> /**
> *
> * @author Jack Dou
> * @since Dec 10, 2008
> */
> @Consumes("application/json")
> @Provider
> public class MapReader implements MessageBodyReader>> {
> private static String[] methods = { "add", "delete", "update" };
> private Map> returnMap = new HashMap>();
>
> public boolean isReadable(Class type, Type genericType,
> Annotation[] annotations, MediaType mediaType) {
> return type.isAssignableFrom(Map.class);
> }
>
> @SuppressWarnings("unchecked")
> public Map> readFrom(Class>> type,
> Type genericType, Annotation[] annotations, MediaType
> mediaType,
> MultivaluedMap httpHeaders, InputStream entityStream)
> throws IOException, WebApplicationException {
> String formData = readAsString(entityStream);
> JSONObject fromObj = JSONObject.fromObject(formData);
> Class cls = getActualClass(genericType);
> for (String method : methods) {
> boolean isHasTheMethod = fromObj.containsKey(method);
> if (isHasTheMethod) {
> JSONArray arr = fromObj.getJSONArray(method);
> List list = new ArrayList();
> for (int i=0,size = arr.size(); i
> getActualClass(Type genericType) {
> ParameterizedTypeImpl pti = (ParameterizedTypeImpl)
> genericType;
> Type[] types = pti.getActualTypeArguments();
> ParameterizedTypeImpl type = (ParameterizedTypeImpl) types[1];
> Type[] types2 = type.getActualTypeArguments();
> Class type2 = (Class) types2[0];
> return type2;
> }
>
> /**
> * 转化读取输入流中的数据为字符串型
> *
> * @param in 输入流
> * @return 输入流转化后的字符串
> * @throws IOException 输入流转化字符串失败,则报IO
> 异常
> */
> public final String readAsString(InputStream in) throws
> IOException {
> Reader reader = new InputStreamReader(in,Constant.CHARSET);
> StringBuilder sb = new StringBuilder();
> char[] c = new char[1024];
> int l;
> while ((l = reader.read(c)) != -1) {
> sb.append(c, 0, l);
> }
> return sb.toString();
> }
> }
> But I use json-lib.jar ,not jettison.jar, Can you gei me a demo for
> manual Unmarshall a json string to Map param using jersey default
> way? My English is poor,Sorry,Do you understand my meaning?
> View this message in context: can i use jersey's json Unmarshaller?
> Sent from the Jersey mailing list archive at Nabble.com.