Package org.json

Examples of org.json.JSONObject$Null


    jso.put(ATTRIBUTE_COORDINATES, coordinates);
    return jso;
  }

  private JSONObject fromPolygon(Polygon pg) {
    JSONObject jso = new JSONObject();
    JSONObject shell = fromLineString(pg.getExteriorRing());
    JSONArray holes = new JSONArray();
    for (int i = 0; i < pg.getNumInteriorRing(); i++) {
      holes.put(fromLineString(pg.getInteriorRingN(i)));
    }
    jso.put("shell", shell);
View Full Code Here


    putBasics(jso, pg);
    return jso;
  }

  private JSONObject fromMultiPolygon(MultiPolygon mp) {
    JSONObject jso = new JSONObject();
    JSONArray polys = new JSONArray();
    for (int i = 0; i < mp.getNumGeometries(); i++) {
      polys.put(fromPolygon((Polygon) mp.getGeometryN(i)));
    }
    jso.put("polygons", polys);
    putBasics(jso, mp);
    return jso;
  }
View Full Code Here

    putBasics(jso, mp);
    return jso;
  }

  private JSONObject fromMultiLineString(MultiLineString ml) {
    JSONObject jso = new JSONObject();
    JSONArray polys = new JSONArray();
    for (int i = 0; i < ml.getNumGeometries(); i++) {
      polys.put(fromLineString((LineString) ml.getGeometryN(i)));
    }
    jso.put("lineStrings", polys);
    putBasics(jso, ml);
    return jso;
  }
View Full Code Here

  public Class[] getJSONClasses() {
    return JSON_CLASSES;
  }

  public ObjectMatch tryUnmarshall(SerializerState state, Class clazz, Object o) throws UnmarshallException {
    JSONObject jso = (JSONObject) o;
    jso.getDouble("x");
    jso.getDouble("y");
    jso.getDouble("width");
    jso.getDouble("height");
    return ObjectMatch.OKAY;
  }
View Full Code Here

    jso.getDouble("height");
    return ObjectMatch.OKAY;
  }

  public Object unmarshall(SerializerState state, Class clazz, Object o) throws UnmarshallException {
    JSONObject jso = (JSONObject) o;
    jso.getString("javaClass");
    float x = (float) jso.getDouble("x");
    float y = (float) jso.getDouble("y");
    float width = (float) jso.getDouble("width");
    float height = (float) jso.getDouble("height");
    return new Rectangle(x, y, x + width, y + height);
  }
View Full Code Here

  }

  public Object marshall(SerializerState state, Object o) throws MarshallException {
    Rectangle rect = (Rectangle) o;

    JSONObject obj = new JSONObject();
    if (ser.getMarshallClassHints()) {
      obj.put("javaClass", o.getClass().getName());
    }
    obj.put("x", rect.getLeft());
    obj.put("y", rect.getBottom());
    obj.put("width", rect.getWidth());
    obj.put("height", rect.getHeight());
    return obj;
  }
View Full Code Here

    }
    return bd;
  }

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

    int match = 0, 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");
    }

    ObjectMatch m = null, tmp;
    i = jso.keys();
    while (i.hasNext()) {
      String field = (String) 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 (m == null) {
            m = tmp;
          } else {
            m = m.max(tmp);
          }
View Full Code Here

    }
    return m.max(new ObjectMatch(mismatch));
  }

  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");
    }
    log.debug("instantiating {}", clazz.getName());
    Object instance;
    try {
      String beanName = beanNameSimplifier.simplify(clazz.getName());
      if (applicationContext.containsBean(beanName)) {
        instance = applicationContext.getBean(beanName);
      } else {
        log.debug("instantiating " + clazz.getName());
        instance = clazz.newInstance();
      }
    } catch (Exception e) {
      log.error(e.getMessage(), e);
      throw new UnmarshallException("can't instantiate bean " + clazz.getName() + ": " + e.getMessage());
    }
    Object[] invokeArgs = new Object[1];
    Object fieldVal;
    Iterator i = jso.keys();
    while (i.hasNext()) {
      String field = (String) i.next();
      Method setMethod = bd.writableProps.get(field);
      if (setMethod != null) {
        try {
          Class param = setMethod.getParameterTypes()[0];
          if (instance instanceof PrimitiveAttribute && "value".equals(field)) {
            log.debug("replace 'value' type for PrimitiveAttribute");
            switch(((PrimitiveAttribute) instance).getType()) {
              case BOOLEAN:
                param = Boolean.class;
                break;
              case DATE:
                param = Date.class;
                break;
              case DOUBLE:
                param = Double.class;
                break;
              case FLOAT:
                param = Float.class;
                break;
              case INTEGER:
                param = Integer.class;
                break;
              case LONG:
                param = Long.class;
                break;
              case SHORT:
                param = Short.class;
                break;
              case CURRENCY:
              case IMGURL:
              case URL:
              case STRING:
                param = String.class;
                break;
              default:
                throw new UnmarshallException("Unknown type of PrimitiveAttribute " +
                    ((PrimitiveAttribute) instance).getType());
            }
          }
          fieldVal = ser.unmarshall(state, param, jso.get(field));
        } catch (UnmarshallException e) {
          throw new UnmarshallException("bean " + clazz.getName() + " " + e.getMessage());
        }
        log.debug("invoking {}({})", setMethod.getName(), fieldVal);
        invokeArgs[0] = fieldVal;
View Full Code Here

      bd = getBeanData(o.getClass());
    } catch (IntrospectionException e) {
      throw new MarshallException(o.getClass().getName() + " is not a bean");
    }

    JSONObject val = new JSONObject();
    if (ser.getMarshallClassHints()) {
      val.put("javaClass", o.getClass().getName());
    }
    Iterator i = bd.readableProps.entrySet().iterator();
    Object[] args = new Object[0];
    Object result;
    while (i.hasNext()) {
      Map.Entry ent = (Map.Entry) i.next();
      String prop = (String) ent.getKey();
      Method getMethod = (Method) ent.getValue();
      log.debug("invoking {}()", getMethod.getName());
      try {
        result = getMethod.invoke(o, args);
      } catch (Throwable e) {
        if (e instanceof InvocationTargetException) {
          e = ((InvocationTargetException) e).getTargetException();
        }
        throw new MarshallException("bean " + o.getClass().getName() + " can't invoke "
            + getMethod.getName() + ": " + e.getMessage());
      }
      try {
        if (result != null || ser.getMarshallNullAttributes()) {

          val.put(prop, ser.marshall(state, result));
        }
      } catch (MarshallException e) {
        throw new MarshallException("bean " + o.getClass().getName() + " " + e.getMessage());
      }
    }
View Full Code Here

  // UNMARSHALL - attempt: client-to-server deserialization attempt
  // -------------------------------------------------------------------------

  @SuppressWarnings("unchecked")
  public ObjectMatch tryUnmarshall(SerializerState state, Class clazz, Object o) throws UnmarshallException {
    JSONObject jso = (JSONObject) o;
    String type = jso.getString(ATTRIBUTE_TYPE);
    if (type == null) {
      throw new UnmarshallException("no type hint");
    }
    int srid = jso.getInt(ATTRIBUTE_SRID);
    if (srid <= 0) {
      throw new UnmarshallException("no srid");
    }
    int precision = jso.getInt(ATTRIBUTE_PRECISION);
    if (precision <= 0) {
      throw new UnmarshallException("no precision");
    }
    if (!(type.equals(Geometry.POINT) || type.equals(Geometry.LINE_STRING) || type.equals(Geometry.POLYGON)
        || type.equals(Geometry.LINEAR_RING) || type.equals(Geometry.MULTI_LINE_STRING) ||
        type.equals(Geometry.MULTI_POLYGON))) {
      throw new UnmarshallException(type + " is not a supported geometry");
    }
    JSONArray coordinates = jso.getJSONArray(ATTRIBUTE_COORDINATES);
    if (coordinates == null) {
      throw new UnmarshallException("coordinates missing");
    }
    return ObjectMatch.OKAY;
  }
View Full Code Here

TOP

Related Classes of org.json.JSONObject$Null

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.