001: /*
002: * This file is part of PFIXCORE.
003: *
004: * PFIXCORE is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU Lesser General Public License as published by
006: * the Free Software Foundation; either version 2 of the License, or
007: * (at your option) any later version.
008: *
009: * PFIXCORE is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public License
015: * along with PFIXCORE; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: */
019:
020: package de.schlund.pfixcore.webservice.jsonws.deserializers;
021:
022: import java.lang.reflect.Array;
023: import java.lang.reflect.ParameterizedType;
024: import java.lang.reflect.Type;
025: import java.util.ArrayList;
026: import java.util.List;
027:
028: import de.schlund.pfixcore.webservice.json.JSONArray;
029: import de.schlund.pfixcore.webservice.jsonws.DeserializationContext;
030: import de.schlund.pfixcore.webservice.jsonws.DeserializationException;
031: import de.schlund.pfixcore.webservice.jsonws.Deserializer;
032:
033: public class ArrayDeserializer extends Deserializer {
034:
035: @Override
036: public boolean canDeserialize(DeserializationContext ctx,
037: Object jsonValue, Type targetType)
038: throws DeserializationException {
039: if (jsonValue instanceof JSONArray) {
040: Class<?> targetClass = null;
041: if (targetType instanceof Class)
042: targetClass = (Class<?>) targetType;
043: else if (targetType instanceof ParameterizedType) {
044: Type rawType = ((ParameterizedType) targetType)
045: .getRawType();
046: if (rawType instanceof Class)
047: targetClass = (Class<?>) rawType;
048: else
049: return false;
050: }
051: if (targetClass.isArray()
052: || List.class.isAssignableFrom(targetClass))
053: return true;
054: }
055: return false;
056: }
057:
058: @SuppressWarnings("unchecked")
059: @Override
060: public Object deserialize(DeserializationContext ctx,
061: Object jsonValue, Type targetType)
062: throws DeserializationException {
063:
064: if (jsonValue instanceof JSONArray) {
065:
066: JSONArray jsonArray = (JSONArray) jsonValue;
067:
068: Class<?> targetClass = null;
069: if (targetType instanceof Class)
070: targetClass = (Class<?>) targetType;
071: else if (targetType instanceof ParameterizedType) {
072: Type rawType = ((ParameterizedType) targetType)
073: .getRawType();
074: if (rawType instanceof Class)
075: targetClass = (Class<?>) rawType;
076: else
077: throw new DeserializationException(
078: "Type not supported: " + targetType);
079: }
080:
081: if (targetClass.isArray()) {
082:
083: Class<?> compType = targetClass.getComponentType();
084: Object arrayObj = Array.newInstance(compType, jsonArray
085: .size());
086: for (int i = 0; i < jsonArray.size(); i++) {
087: Object item = jsonArray.get(i);
088: Object obj = ctx.deserialize(item, compType);
089: Array.set(arrayObj, i, obj);
090: }
091: return arrayObj;
092:
093: } else if (List.class.isAssignableFrom(targetClass)) {
094:
095: Type argType = null;
096: if (targetType instanceof ParameterizedType) {
097: ParameterizedType paramType = (ParameterizedType) targetType;
098: Type[] argTypes = paramType
099: .getActualTypeArguments();
100: if (argTypes.length == 1) {
101: argType = argTypes[0];
102: } else
103: throw new DeserializationException(
104: "Type not supported: " + targetType);
105: } else
106: throw new DeserializationException(
107: "Deserialization of unparameterized List types isn't supported: "
108: + targetType);
109:
110: List list = null;
111: if (!targetClass.isInterface()) {
112: try {
113: list = (List) targetClass.newInstance();
114: } catch (Exception x) {
115: }
116: }
117: if (list == null) {
118: if (targetClass.isAssignableFrom(ArrayList.class)) {
119: list = new ArrayList<Object>();
120: } else
121: throw new DeserializationException(
122: "Can't create instance of class '"
123: + targetClass.getName() + "'.");
124: }
125:
126: for (int i = 0; i < jsonArray.size(); i++) {
127: Object item = jsonArray.get(i);
128: Object obj = ctx.deserialize(item, argType);
129: list.add(obj);
130: }
131: return list;
132:
133: } else
134: throw new DeserializationException(
135: "Type not supported: " + targetType);
136:
137: } else
138: throw new DeserializationException("Wrong type: "
139: + jsonValue.getClass().getName());
140:
141: }
142:
143: }
|