All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.aiwiown.face.internal.parser.json.JsonConverter Maven / Gradle / Ivy

There is a newer version: 3.0.9
Show newest version
package com.aiwiown.face.internal.parser.json;

import com.aiwiown.face.*;
import com.aiwiown.face.internal.mapping.Converter;
import com.aiwiown.face.internal.mapping.Converters;
import com.aiwiown.face.internal.mapping.Reader;
import com.aiwiown.face.internal.util.json.ExceptionErrorListener;
import com.aiwiown.face.internal.util.json.JSONReader;
import com.aiwiown.face.internal.util.json.JSONValidatingReader;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * JSON格式转换器。
 *
 * @author carver.gu
 * @since 1.0, Apr 11, 2010
 */
public class JsonConverter implements Converter {

    @Override
    public  T toResponse(String rsp, Class clazz) throws ApiException {
        JSONReader reader = new JSONValidatingReader(new ExceptionErrorListener());
        Object rootObj = reader.read(rsp);
        if (rootObj instanceof Map) {
            Map rspJson = (Map) rootObj;
            return fromJson(rspJson, clazz);
        }
        return null;
    }

    /**
     * 把JSON格式的数据转换为对象。
     *
     * @param    泛型领域对象
     * @param json  JSON格式的数据
     * @param clazz 泛型领域类型
     * @return 领域对象
     * @throws ApiException
     */
    public  T fromJson(final Map json, Class clazz) throws ApiException {
        return Converters.convert(clazz, new Reader() {
            @Override
            public boolean hasReturnField(Object name) {
                return json.containsKey(name);
            }

            @Override
            public Object getPrimitiveObject(Object name) {
                return json.get(name);
            }

            @Override
            public Object getObject(Object name, Class type) throws ApiException {
                Object tmp = json.get(name);
                if (tmp instanceof Map) {
                    Map map = (Map) tmp;
                    return fromJson(map, type);
                } else {
                    return null;
                }
            }
            @Override
            public List getListObjects(Object listName, Object itemName, Class subType) throws ApiException {
                List listObjs = null;

                Object listTmp = json.get(listName);
                if (listTmp instanceof Map) {
                    Map jsonMap = (Map) listTmp;
                    Object itemTmp = jsonMap.get(itemName);
                    if (itemTmp == null && listName != null) {
                        String listNameStr = listName.toString();
                        itemTmp = jsonMap.get(listNameStr.substring(0, listNameStr.length() - 1));
                    }
                    if (itemTmp instanceof List) {
                        listObjs = getListObjectsInner(subType, itemTmp);
                    }
                } else if (listTmp instanceof List) {
                    listObjs = getListObjectsInner(subType, listTmp);
                }

                return listObjs;
            }

            private List getListObjectsInner(Class subType, Object itemTmp)
                    throws ApiException {
                List listObjs;
                listObjs = new ArrayList();
                List tmpList = (List) itemTmp;
                for (Object subTmp : tmpList) {
                    Object obj = null;
                    if (String.class.isAssignableFrom(subType)) {
                        obj = subTmp;
                    } else if (Long.class.isAssignableFrom(subType)) {
                        obj = subTmp;
                    } else if (Integer.class.isAssignableFrom(subType)) {
                        obj = subTmp;
                    } else if (Boolean.class.isAssignableFrom(subType)) {
                        obj = subTmp;
                    } else if (Date.class.isAssignableFrom(subType)) {
                        DateFormat format = new SimpleDateFormat(ApiConstants.DATE_TIME_FORMAT);
                        try {
                            obj = format.parse(String.valueOf(subTmp));
                        } catch (ParseException e) {
                            throw new ApiException(e);
                        }
                    } else if (subTmp instanceof Map) {// object
                        Map subMap = (Map) subTmp;
                        obj = fromJson(subMap, subType);
                    }

                    if (obj != null) {
                        listObjs.add(obj);
                    }
                }
                return listObjs;
            }

        });
    }

}