コード例 #1
0
  public ObjectMatch tryUnmarshall(SerializerState state, Class clazz, Object jso)
      throws UnmarshallException {
    // For some reason getClass can be String but getClasses will return an
    // empty array. This catches this.
    if (jso.getClass().equals(String.class)) {
      return ObjectMatch.OKAY;
    }
    Class classes[] = jso.getClass().getClasses();
    for (int i = 0; i < classes.length; i++) {
      if (classes[i].equals(String.class)) {
        state.setSerialized(jso, ObjectMatch.OKAY);
        return ObjectMatch.OKAY;
      }
    }

    state.setSerialized(jso, ObjectMatch.SIMILAR);
    return ObjectMatch.SIMILAR;
  }
コード例 #2
0
 public Object unmarshall(SerializerState state, Class clazz, Object jso)
     throws UnmarshallException {
   Object returnValue;
   String val = jso instanceof String ? (String) jso : jso.toString();
   if (clazz == char.class) {
     returnValue = new Character(val.charAt(0));
   } else if (clazz == byte[].class) {
     returnValue = val.getBytes();
   } else if (clazz == char[].class) {
     returnValue = val.toCharArray();
   } else {
     returnValue = val;
   }
   state.setSerialized(jso, returnValue);
   return returnValue;
 }
コード例 #3
0
 public Object unmarshall(SerializerState state, Class clazz, Object jso)
     throws UnmarshallException {
   state.setSerialized(jso, jso);
   return jso;
 }
コード例 #4
0
 public ObjectMatch tryUnmarshall(SerializerState state, Class clazz, Object jso)
     throws UnmarshallException {
   state.setSerialized(jso, ObjectMatch.OKAY);
   return ObjectMatch.OKAY;
 }
コード例 #5
0
 public Object unmarshall(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);
   }
   if (log.isDebugEnabled()) {
     log.debug("instantiating " + clazz.getName());
   }
   Object instance;
   try {
     instance = clazz.newInstance();
   } catch (InstantiationException e) {
     throw new UnmarshallException(
         "could not instantiate bean of type "
             + clazz.getName()
             + ", make sure it has a no argument "
             + "constructor and that it is not an interface or "
             + "abstract class",
         e);
   } catch (IllegalAccessException e) {
     throw new UnmarshallException("could not instantiate bean of type " + clazz.getName(), e);
   } catch (RuntimeException e) {
     throw new UnmarshallException("could not instantiate bean of type " + clazz.getName(), e);
   }
   state.setSerialized(o, instance);
   Object invokeArgs[] = new Object[1];
   Object fieldVal;
   Iterator i = jso.keys();
   while (i.hasNext()) {
     String field = (String) i.next();
     Method setMethod = (Method) bd.writableProps.get(field);
     if (setMethod != null) {
       try {
         Class param[] = setMethod.getParameterTypes();
         fieldVal = ser.unmarshall(state, param[0], jso.get(field));
       } catch (UnmarshallException e) {
         throw new UnmarshallException(
             "could not unmarshall field \"" + field + "\" of bean " + clazz.getName(), e);
       } catch (JSONException e) {
         throw new UnmarshallException(
             "could not unmarshall field \"" + field + "\" of bean " + clazz.getName(), e);
       }
       if (log.isDebugEnabled()) {
         log.debug("invoking " + setMethod.getName() + "(" + fieldVal + ")");
       }
       invokeArgs[0] = fieldVal;
       try {
         setMethod.invoke(instance, invokeArgs);
       } catch (Throwable e) {
         if (e instanceof InvocationTargetException) {
           e = ((InvocationTargetException) e).getTargetException();
         }
         throw new UnmarshallException(
             "bean "
                 + clazz.getName()
                 + "can't invoke "
                 + setMethod.getName()
                 + ": "
                 + e.getMessage(),
             e);
       }
     }
   }
   return instance;
 }
コード例 #6
0
  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;
    Iterator i = bd.writableProps.entrySet().iterator();
    while (i.hasNext()) {
      Map.Entry ent = (Map.Entry) i.next();
      String prop = (String) 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;
    i = jso.keys();
    while (i.hasNext()) {
      String field = (String) i.next();
      Method setMethod = (Method) 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;
  }