Exemplo n.º 1
0
  /**
   * @param obj
   * @return
   * @throws MapperException
   */
  @SuppressWarnings("unchecked")
  public JSonNode create(final Object obj) throws MapperException {
    try {

      if (obj == null) {
        return new JSonValue(null);
      }
      final Class<? extends Object> clazz = obj.getClass();
      TypeMapper<?> mapper;
      if (clazz.isPrimitive()) {
        if (clazz == boolean.class) {
          return new JSonValue((Boolean) obj);
        } else if (clazz == char.class) {
          return new JSonValue(0 + ((Character) obj).charValue());
        } else if (clazz == byte.class) {
          return new JSonValue(((Byte) obj).longValue());
        } else if (clazz == short.class) {
          return new JSonValue(((Short) obj).longValue());
        } else if (clazz == int.class) {
          return new JSonValue(((Integer) obj).longValue());
        } else if (clazz == long.class) {
          return new JSonValue(((Long) obj).longValue());
        } else if (clazz == float.class) {
          return new JSonValue(((Float) obj).doubleValue());
        } else if (clazz == double.class) {
          return new JSonValue(((Double) obj).doubleValue());
        }
      } else if (clazz.isEnum()) {
        return new JSonValue(obj + "");
      } else if (obj instanceof Boolean) {
        return new JSonValue(((Boolean) obj).booleanValue());
      } else if (obj instanceof Character) {
        return new JSonValue(0 + ((Character) obj).charValue());
      } else if (obj instanceof Byte) {
        return new JSonValue(((Byte) obj).longValue());
      } else if (obj instanceof Short) {
        return new JSonValue(((Short) obj).longValue());
      } else if (obj instanceof Integer) {
        return new JSonValue(((Integer) obj).longValue());
      } else if (obj instanceof Long) {
        return new JSonValue(((Long) obj).longValue());
      } else if (obj instanceof Float) {
        return new JSonValue(((Float) obj).doubleValue());
      } else if (obj instanceof Double) {
        return new JSonValue(((Double) obj).doubleValue());

      } else if (obj instanceof String) {
        return new JSonValue((String) obj);
      } else if (obj instanceof Map) {

        final JSonObject ret = new JSonObject();
        Entry<Object, Object> next;
        for (final Iterator<Entry<Object, Object>> it =
                ((Map<Object, Object>) obj).entrySet().iterator();
            it.hasNext(); ) {
          next = it.next();
          if (!(next.getKey() instanceof String)) {
            throw new MapperException(
                "Map keys have to be Strings: "
                    + clazz
                    + " Keyclass:"
                    + (next.getKey() == null ? "<null>" : next.getKey().getClass()));
          }
          ret.put(next.getKey().toString(), create(next.getValue()));
        }
        return ret;
      } else if (obj instanceof Collection) {
        final JSonArray ret = new JSonArray();
        for (final Object o : (Collection<?>) obj) {
          ret.add(create(o));
        }
        return ret;
      } else if (clazz.isArray()) {
        final JSonArray ret = new JSonArray();
        for (int i = 0; i < Array.getLength(obj); i++) {
          ret.add(create(Array.get(obj, i)));
        }
        return ret;
      } else if (obj instanceof Class) {
        return new JSonValue(((Class<?>) obj).getName());
      } else if ((mapper = typeMapper.get(clazz)) != null) {
        return mapper.map(obj);
      } else /* if (obj instanceof Storable) */ {
        final ClassCache cc = ClassCache.getClassCache(clazz);
        final JSonObject ret = new JSonObject();
        for (final Getter g : cc.getGetter()) {

          ret.put(g.getKey(), create(g.getValue(obj)));
        }
        return ret;
      }
    } catch (final IllegalArgumentException e) {
      e.printStackTrace();
    } catch (final IllegalAccessException e) {
      e.printStackTrace();
    } catch (final InvocationTargetException e) {
      e.printStackTrace();
    } catch (final SecurityException e) {

      e.printStackTrace();
    } catch (final NoSuchMethodException e) {

      e.printStackTrace();
    }

    return null;
  }