public void renderValue(
      Object aObj, JSONObject aObjectElement, JSONMarshall aMarshall, HashMap aPool)
      throws MarshallException {
    final JSONObject lElements = new JSONObject();
    aObjectElement.getValue().put(JSONMarshall.RNDR_ATTR_VALUE, lElements);

    try {
      Class lClass = aObj.getClass();
      PropertyDescriptor[] lPropDesc =
          Introspector.getBeanInfo(lClass, Introspector.USE_ALL_BEANINFO).getPropertyDescriptors();
      for (int i = 0; i < lPropDesc.length; i++) {
        Method lReader = lPropDesc[i].getReadMethod();
        Method lWriter = lPropDesc[i].getWriteMethod();
        String lPropName = lPropDesc[i].getName();

        // Only serialize if the property is READ-WRITE.
        if (lReader != null && lWriter != null) {
          lElements
              .getValue()
              .put(lPropName, aMarshall.marshallImpl(lReader.invoke(aObj, new Object[] {}), aPool));
        }
      }
    } catch (IntrospectionException e) {
      final String lMsg = "Error while introspecting JavaBean.";
      throw new MarshallException(lMsg);
    } catch (IllegalAccessException e) {
      final String lMsg = "Illegal access while trying to fetch a bean property (1).";
      throw new MarshallException(lMsg);
    } catch (InvocationTargetException e) {
      final String lMsg = "Illegal access while trying to fetch a bean property (2).";
      throw new MarshallException(lMsg);
    }
  }
Beispiel #2
0
  public void validate(JSONValue aValue) throws ValidationException {
    // Only for objects.
    if (!aValue.isObject())
      throw new ValidationException(String.format(PROP004, aValue.toString(), this.getName()));
    JSONObject lObj = (JSONObject) aValue;

    // First we check if required keys are there.
    for (PropRule aRequired : required) {
      if (!lObj.containsKey(aRequired.getKey()))
        throw new ValidationException(
            String.format(PROP002, aValue.toString(), aRequired.getKey(), this.getName()));
    }

    // Now we iterate over all keys in the object and lookup the spec.
    for (String lKey : lObj.getValue().keySet()) {
      if (!all.containsKey(lKey))
        throw new ValidationException(
            String.format(PROP003, aValue.toString(), lKey, this.getName()));
      PropRule lRule = all.get(lKey);
      try {
        lRule.getRule().validate(lObj.get(lKey));
      } catch (ValidationException e) {
        throw new ValidationException(
            String.format(PROP005, lKey, aValue.toString(), this.getName(), e.getMessage()), e);
      }
    }
  }
 public void renderValue(Object aObj, JSONObject aParent, JSONMarshall aMarshall, HashMap aPool)
     throws MarshallException {
   aParent.getValue().put(JSONMarshall.RNDR_ATTR_VALUE, new JSONString(aObj.toString()));
 }
  public Object parseValue(JSONObject aObjectElement, JSONMarshall aMarshall, HashMap aPool)
      throws MarshallException {
    JSONMarshall.requireStringAttribute(aObjectElement, JSONMarshall.RNDR_ATTR_CLASS);
    String lBeanClassName =
        ((JSONString) aObjectElement.get(JSONMarshall.RNDR_ATTR_CLASS)).getValue();

    String lId = null;
    try {
      JSONMarshall.requireStringAttribute(aObjectElement, JSONMarshall.RNDR_ATTR_ID);
      lId = ((JSONString) aObjectElement.get(JSONMarshall.RNDR_ATTR_ID)).getValue();
    } catch (Exception eIgnore) {
    }

    try {
      Class lBeanClass = Class.forName(lBeanClassName);
      Object lBean = null;

      lBean = lBeanClass.newInstance();
      if (lId != null) aPool.put(lId, lBean);

      JSONObject lProperties = (JSONObject) aObjectElement.get(JSONMarshall.RNDR_ATTR_VALUE);
      Iterator<String> lElIter = lProperties.getValue().keySet().iterator();

      while (lElIter.hasNext()) {
        // Fetch subelement information.
        String lPropname = lElIter.next();
        JSONObject lSubEl = (JSONObject) lProperties.get(lPropname);
        Object lProp = aMarshall.unmarshallImpl(lSubEl, aPool);

        // Put the property in the bean.
        boolean lFoundWriter = false;
        PropertyDescriptor[] lPropDesc =
            Introspector.getBeanInfo(lBeanClass, Introspector.USE_ALL_BEANINFO)
                .getPropertyDescriptors();
        for (int i = 0; i < lPropDesc.length; i++) {
          if (lPropDesc[i].getName().equals(lPropname)) {
            lFoundWriter = true;
            Method lWriter = lPropDesc[i].getWriteMethod();
            if (lWriter == null) {
              final String lMsg =
                  "Could not find a setter for prop: " + lPropname + " in class: " + lBeanClassName;
              throw new MarshallException(lMsg);
            }
            lWriter.invoke(lBean, new Object[] {lProp});
            break;
          }
        }

        if (!lFoundWriter) {
          final String lMsg =
              "Could not find a setter for prop: " + lPropname + " in class: " + lBeanClassName;
          throw new MarshallException(lMsg);
        }
      }

      return lBean;
    } catch (ClassNotFoundException e) {
      final String lMsg = "Could not find JavaBean class: " + lBeanClassName;
      throw new MarshallException(lMsg);
    } catch (IllegalAccessException e) {
      final String lMsg =
          "IllegalAccessException while trying to instantiate bean: " + lBeanClassName;
      throw new MarshallException(lMsg);
    } catch (InstantiationException e) {
      final String lMsg =
          "InstantiationException while trying to instantiate bean: " + lBeanClassName;
      throw new MarshallException(lMsg);
    } catch (IntrospectionException e) {
      final String lMsg = "IntrospectionException while trying to fill bean: " + lBeanClassName;
      throw new MarshallException(lMsg);
    } catch (InvocationTargetException e) {
      final String lMsg = "InvocationTargetException while trying to fill bean: " + lBeanClassName;
      throw new MarshallException(lMsg);
    }
  }