users@jersey.java.net

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

From: beanor <beanor_at_gmail.com>
Date: Wed, 10 Dec 2008 02:39:11 -0800 (PST)

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: http://n2.nabble.com/can-i-use-jersey%27s-json-Unmarshaller--tp1638158p1638158.html
Sent from the Jersey mailing list archive at Nabble.com.