Package org.jabsorb.serializer

Examples of org.jabsorb.serializer.UnmarshallException


  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);
    }

  }
View Full Code Here


  public Object unmarshall(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");
      }
      NSMutableSet abset = null;
      if (java_class.equals("com.webobjects.foundation.NSSet") || java_class.equals("com.webobjects.foundation.NSMutableSet")) {
        abset = new NSMutableSet();
      }
      else {
        throw new UnmarshallException("not a Set");
      }
      JSONObject jsonset = jso.getJSONObject("set");

      if (jsonset == null) {
        throw new UnmarshallException("set missing");
      }

      Iterator i = jsonset.keys();
      String key = null;

      try {
        while (i.hasNext()) {
          key = (String) i.next();
          Object setElement = jsonset.get(key);
          Object unmarshalledObject = ser.unmarshall(state, null, setElement);
          abset.addObject(unmarshalledObject);
        }
      }
      catch (UnmarshallException e) {
        throw new UnmarshallException("key " + i + e.getMessage());
      }
      return abset;
    }
    catch (JSONException e) {
      throw new UnmarshallException("Failed to unmarshall NSSet.", e);
    }
  }
View Full Code Here

    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++;
      }
View Full Code Here

    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<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();
          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;
  }
View Full Code Here

  public Object unmarshall(SerializerState state, Class clazz, Object o) throws UnmarshallException {
    try {
      JSONObject jso = (JSONObject) o;
      Object object = jso.get("value");
      if (object == null) {
        throw new UnmarshallException("ERXConstant missing");
      }
      String javaClassName = jso.getString("javaClass");
      ERXConstant.Constant constant = ERXConstant.constantForClassNamed(object, javaClassName);
      state.setSerialized(o, constant);
      return constant;
    }
    catch (Exception e) {
      throw new UnmarshallException("Failed to unmarshall ERXConstant.", e);
    }
  }
View Full Code Here

    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;
  }
View Full Code Here

    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");
    }
    NSMutableArray al = new NSMutableArray();
    boolean immutableClone = true;
   
    Class klass;
    try {
      klass = Class.forName(java_class);
    } catch (ClassNotFoundException cnfe) {
      throw new UnmarshallException("Could not find class named: " + java_class);
    }

    if (NSMutableArray.class.isAssignableFrom(klass)){
      immutableClone = false;
    } else 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;
    try {
      for (; i < jsonNSArray.length(); i++) {
        Object obj = ser.unmarshall(state, null, jsonNSArray.get(i));
        if (obj != null) {
          al.addObject(obj);
        }
        else {
          al.addObject(NSKeyValueCoding.NullValue);
        }
      }
      NSArray finalArray = al;
      if (immutableClone) {
        finalArray = al.immutableClone();
      }
      state.setSerialized(o, finalArray);
      return finalArray;
    }
    catch (UnmarshallException e) {
      throw new UnmarshallException("element " + i + " " + e.getMessage(), e);
    }
    catch (JSONException e) {
      throw new UnmarshallException("element " + i + " " + e.getMessage(), e);
    }
  }
View Full Code Here

      Object obj = beanSerializer.unmarshall(state, javaClass, jso);
      state.setSerialized(o, obj);
      return obj;
    }
    catch (Exception e) {
      throw new UnmarshallException("Failed to unmarshall EO.", e);
    }
  }
View Full Code Here

  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.NSTimestamp"))) {
        throw new UnmarshallException("not a NSTimestamp");
      }
      long time = jso.getLong("time");
      String tz = jso.getString("tz");
      return ObjectMatch.OKAY;
    }
    catch (JSONException e) {
      throw new UnmarshallException("Failed to unmarshall NSTimestamp.", e);
    }
  }
View Full Code Here

      if (jso.has("javaClass")) {
        try {
          clazz = Class.forName(jso.getString("javaClass"));
        }
        catch (ClassNotFoundException cnfe) {
          throw new UnmarshallException(cnfe.getMessage());
        }
      }
      if (NSTimestamp.class.equals(clazz)) {
        NSTimestamp timestamp = new NSTimestamp(time, NSTimeZone.getTimeZone(tz));
        state.setSerialized(o, timestamp);
        return timestamp;
      }
      throw new UnmarshallException("invalid class " + clazz);
    }
    catch (JSONException e) {
      throw new UnmarshallException("Failed to unmarshall NSTimestamp.", e);
    }
  }
View Full Code Here

TOP

Related Classes of org.jabsorb.serializer.UnmarshallException

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.