Search in sources :

Example 1 with ObjectMatch

use of org.jabsorb.serializer.ObjectMatch in project wonder-slim by undur.

the class NSDictionarySerializer method tryUnmarshall.

public ObjectMatch tryUnmarshall(SerializerState state, Class clazz, Object o) throws UnmarshallException {
    JSONObject jso = (JSONObject) o;
    String java_class;
    try {
        java_class = jso.getString("javaClass");
    } catch (JSONException e) {
        throw new UnmarshallException("Could not read javaClass", e);
    }
    if (java_class == null) {
        throw new UnmarshallException("no type hint");
    }
    if (!(java_class.equals("com.webobjects.foundation.NSDictionary") || java_class.equals("com.webobjects.foundation.NSMutableDictionary"))) {
        throw new UnmarshallException("not an NSDictionary");
    }
    JSONObject jsondictionary;
    try {
        jsondictionary = jso.getJSONObject("nsdictionary");
    } catch (JSONException e) {
        throw new UnmarshallException("Could not read dictionary: " + e.getMessage(), e);
    }
    if (jsondictionary == null) {
        throw new UnmarshallException("nsdictionary missing");
    }
    ObjectMatch m = new ObjectMatch(-1);
    Iterator i = jsondictionary.keys();
    String key = null;
    state.setSerialized(o, m);
    try {
        while (i.hasNext()) {
            key = (String) i.next();
            m.setMismatch(ser.tryUnmarshall(state, null, jsondictionary.get(key)).max(m).getMismatch());
        }
    } catch (UnmarshallException e) {
        throw new UnmarshallException("key " + key + " " + e.getMessage(), e);
    } catch (JSONException e) {
        throw new UnmarshallException("key " + key + " " + e.getMessage(), e);
    }
    return m;
}
Also used : JSONObject(org.json.JSONObject) ObjectMatch(org.jabsorb.serializer.ObjectMatch) Iterator(java.util.Iterator) JSONException(org.json.JSONException) UnmarshallException(org.jabsorb.serializer.UnmarshallException)

Example 2 with ObjectMatch

use of org.jabsorb.serializer.ObjectMatch in project wonder-slim by undur.

the class NSSetSerializer method tryUnmarshall.

public ObjectMatch tryUnmarshall(SerializerState state, Class clazz, Object o) throws UnmarshallException {
    try {
        JSONObject jso = (JSONObject) o;
        String java_class = jso.getString("javaClass");
        if (java_class == null) {
            throw new UnmarshallException("no type hint");
        }
        if (!(java_class.equals("com.webobjects.foundation.NSSet") || java_class.equals("com.webobjects.foundation.NSMutableSet"))) {
            throw new UnmarshallException("not a Set");
        }
        JSONObject jsonset = jso.getJSONObject("set");
        if (jsonset == null) {
            throw new UnmarshallException("set missing");
        }
        ObjectMatch m = new ObjectMatch(-1);
        Iterator i = jsonset.keys();
        String key = null;
        try {
            while (i.hasNext()) {
                key = (String) i.next();
                m = ser.tryUnmarshall(state, null, jsonset.get(key)).max(m);
            }
        } catch (UnmarshallException e) {
            throw new UnmarshallException("key " + key + " " + e.getMessage());
        }
        return m;
    } catch (JSONException e) {
        throw new UnmarshallException("Failed to unmarshall NSSet.", e);
    }
}
Also used : JSONObject(org.json.JSONObject) ObjectMatch(org.jabsorb.serializer.ObjectMatch) Iterator(java.util.Iterator) JSONException(org.json.JSONException) UnmarshallException(org.jabsorb.serializer.UnmarshallException)

Example 3 with ObjectMatch

use of org.jabsorb.serializer.ObjectMatch in project wonder-slim by undur.

the class ERXBeanSerializer method tryUnmarshall.

public ObjectMatch tryUnmarshall(SerializerState state, Class clazz, Object o) throws UnmarshallException {
    JSONObject jso = (JSONObject) o;
    BeanData bd;
    try {
        bd = getBeanData(clazz);
    } catch (IntrospectionException e) {
        throw new UnmarshallException(clazz.getName() + " is not a bean", e);
    }
    int match = 0;
    int mismatch = 0;
    for (Map.Entry<String, Method> ent : bd.writableProps.entrySet()) {
        String prop = ent.getKey();
        if (jso.has(prop)) {
            match++;
        } else {
            mismatch++;
        }
    }
    if (match == 0) {
        throw new UnmarshallException("bean has no matches");
    }
    // create a concrete ObjectMatch that is always returned in order to satisfy circular reference requirements
    ObjectMatch returnValue = new ObjectMatch(-1);
    state.setSerialized(o, returnValue);
    ObjectMatch m = null;
    ObjectMatch tmp;
    Iterator<String> i = jso.keys();
    while (i.hasNext()) {
        String field = i.next();
        Method setMethod = bd.writableProps.get(field);
        if (setMethod != null) {
            try {
                Class<?>[] param = setMethod.getParameterTypes();
                if (param.length != 1) {
                    throw new UnmarshallException("bean " + clazz.getName() + " method " + setMethod.getName() + " does not have one arg");
                }
                tmp = ser.tryUnmarshall(state, param[0], jso.get(field));
                if (tmp != null) {
                    if (m == null) {
                        m = tmp;
                    } else {
                        m = m.max(tmp);
                    }
                }
            } catch (UnmarshallException e) {
                throw new UnmarshallException("bean " + clazz.getName() + " " + e.getMessage(), e);
            } catch (JSONException e) {
                throw new UnmarshallException("bean " + clazz.getName() + " " + e.getMessage(), e);
            }
        } else {
            mismatch++;
        }
    }
    if (m != null) {
        returnValue.setMismatch(m.max(new ObjectMatch(mismatch)).getMismatch());
    } else {
        returnValue.setMismatch(mismatch);
    }
    return returnValue;
}
Also used : ObjectMatch(org.jabsorb.serializer.ObjectMatch) IntrospectionException(java.beans.IntrospectionException) JSONException(org.json.JSONException) Method(java.lang.reflect.Method) JSONObject(org.json.JSONObject) UnmarshallException(org.jabsorb.serializer.UnmarshallException) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with ObjectMatch

use of org.jabsorb.serializer.ObjectMatch in project wonder-slim by undur.

the class NSArraySerializer method tryUnmarshall.

// TODO: try unMarshall and unMarshall share 90% code. Put in into an
// intermediate function.
// TODO: Also cache the result somehow so that an unmarshall
// following a tryUnmarshall doesn't do the same work twice!
public ObjectMatch tryUnmarshall(SerializerState state, Class clazz, Object o) throws UnmarshallException {
    JSONObject jso = (JSONObject) o;
    String java_class;
    try {
        java_class = jso.getString("javaClass");
    } catch (JSONException e) {
        throw new UnmarshallException("Could not read javaClass", e);
    }
    if (java_class == null) {
        throw new UnmarshallException("no type hint");
    }
    Class klass;
    try {
        klass = Class.forName(java_class);
    } catch (ClassNotFoundException cnfe) {
        throw new UnmarshallException("Could not find class named: " + java_class);
    }
    if (!NSArray.class.isAssignableFrom(klass)) {
        throw new UnmarshallException("not an NSArray");
    }
    JSONArray jsonNSArray;
    try {
        jsonNSArray = jso.getJSONArray("nsarray");
    } catch (JSONException e) {
        throw new UnmarshallException("Could not read nsarray: " + e.getMessage(), e);
    }
    if (jsonNSArray == null) {
        throw new UnmarshallException("nsarray missing");
    }
    int i = 0;
    ObjectMatch m = new ObjectMatch(-1);
    state.setSerialized(o, m);
    try {
        for (; i < jsonNSArray.length(); i++) {
            m.setMismatch(ser.tryUnmarshall(state, null, jsonNSArray.get(i)).max(m).getMismatch());
        }
    } catch (UnmarshallException e) {
        throw new UnmarshallException("element " + i + " " + e.getMessage(), e);
    } catch (JSONException e) {
        throw new UnmarshallException("element " + i + " " + e.getMessage(), e);
    }
    return m;
}
Also used : JSONObject(org.json.JSONObject) NSArray(com.webobjects.foundation.NSArray) ObjectMatch(org.jabsorb.serializer.ObjectMatch) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) UnmarshallException(org.jabsorb.serializer.UnmarshallException)

Example 5 with ObjectMatch

use of org.jabsorb.serializer.ObjectMatch in project wonder-slim by undur.

the class JSONRPCBridge method resolveMethod.

/**
 * Resolve which method the caller is requesting <p/> If a method with the
 * requested number of arguments does not exist at all, null will be returned.
 * <p/> If the object or class (for static methods) being invoked contains
 * more than one overloaded methods that match the method key signature, find
 * the closest matching method to invoke according to the JSON arguments being
 * passed in.
 *
 * @param methodMap Map keyed by MethodKey objects and the values will be
 *          either a Method object, or an array of Method objects, if there is
 *          more than one possible method that can be invoked matching the
 *          MethodKey.
 * @param methodName method name being called.
 * @param arguments JSON arguments to the method, as a JSONArray.
 * @return the Method that most closely matches the call signature, or null if
 *         there is not a match.
 */
private Method resolveMethod(HashMap methodMap, String methodName, JSONArray arguments) {
    Method[] method;
    if (methodMap == null) {
        return null;
    }
    // first, match soley by the method name and number of arguments passed in
    // if there is a single match, return the single match
    // if there is no match at all, return null
    // if there are multiple matches, fall through to the second matching phase
    // below
    MethodKey mk = new MethodKey(methodName, arguments.length());
    Object o = methodMap.get(mk);
    if (o instanceof Method) {
        Method m = (Method) o;
        if (log.isDebugEnabled()) {
            log.debug("found method " + methodName + "(" + argSignature(m) + ")");
        }
        return m;
    } else if (o instanceof Method[]) {
        method = (Method[]) o;
    } else {
        return null;
    }
    // second matching phase: there were overloaded methods on the object
    // we are invoking so try and find the best match based on the types of
    // the arguments passed in.
    // try and unmarshall the arguments against each candidate method
    // to determine which one matches the best
    List candidate = new ArrayList();
    if (log.isDebugEnabled()) {
        log.debug("looking for method " + methodName + "(" + argSignature(arguments) + ")");
    }
    for (int i = 0; i < method.length; i++) {
        try {
            candidate.add(tryUnmarshallArgs(method[i], arguments));
            if (log.isDebugEnabled()) {
                log.debug("+++ possible match with method " + methodName + "(" + argSignature(method[i]) + ")");
            }
        } catch (Exception e) {
            if (log.isDebugEnabled()) {
                log.debug("xxx " + e.getMessage() + " in " + methodName + "(" + argSignature(method[i]) + ")");
            }
        }
    }
    // now search through all the candidates and find one which matches
    // the json arguments the closest
    MethodCandidate best = null;
    for (int i = 0; i < candidate.size(); i++) {
        MethodCandidate c = (MethodCandidate) candidate.get(i);
        if (best == null) {
            best = c;
            continue;
        }
        final ObjectMatch bestMatch = best.getMatch();
        final ObjectMatch cMatch = c.getMatch();
        if (bestMatch.getMismatch() > cMatch.getMismatch()) {
            best = c;
        } else if (bestMatch.getMismatch() == cMatch.getMismatch()) {
            best = betterSignature(best, c);
        }
    }
    if (best != null) {
        Method m = best.method;
        if (log.isDebugEnabled()) {
            log.debug("found method " + methodName + "(" + argSignature(m) + ")");
        }
        return m;
    }
    return null;
}
Also used : ObjectMatch(org.jabsorb.serializer.ObjectMatch) ArrayList(java.util.ArrayList) JSONObject(org.json.JSONObject) ArrayList(java.util.ArrayList) List(java.util.List) Method(java.lang.reflect.Method) JSONException(org.json.JSONException) NoSuchElementException(java.util.NoSuchElementException) UnmarshallException(org.jabsorb.serializer.UnmarshallException) InvocationTargetException(java.lang.reflect.InvocationTargetException) MarshallException(org.jabsorb.serializer.MarshallException) MethodKey(org.jabsorb.reflect.MethodKey)

Aggregations

ObjectMatch (org.jabsorb.serializer.ObjectMatch)5 UnmarshallException (org.jabsorb.serializer.UnmarshallException)5 JSONException (org.json.JSONException)5 JSONObject (org.json.JSONObject)5 Method (java.lang.reflect.Method)2 Iterator (java.util.Iterator)2 NSArray (com.webobjects.foundation.NSArray)1 IntrospectionException (java.beans.IntrospectionException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 NoSuchElementException (java.util.NoSuchElementException)1 MethodKey (org.jabsorb.reflect.MethodKey)1 MarshallException (org.jabsorb.serializer.MarshallException)1 JSONArray (org.json.JSONArray)1