private static Object wrapSingleton(Object o) {
   JsonNodeFactory jnf = JsonNodeFactory.instance;
   Object r;
   if (o == null || o instanceof Null) {
     r = makeResultObjectNode(jnf, jnf.nullNode());
   } else if (o instanceof Privacy || o instanceof CharSequence || o instanceof Character) {
     r = makeResultObjectNode(jnf, jnf.textNode(o.toString()));
   } else if (o instanceof Boolean) {
     r = makeResultObjectNode(jnf, jnf.booleanNode((Boolean) o));
   } else if (o instanceof Byte
       || o instanceof Short
       || o instanceof Integer
       || o instanceof Long) {
     r = makeResultObjectNode(jnf, jnf.numberNode(((Number) o).longValue()));
   } else if (o instanceof Float || o instanceof Double) {
     r = makeResultObjectNode(jnf, jnf.numberNode(((Number) o).doubleValue()));
   } else {
     r = o;
   }
   return r;
 }
Пример #2
0
  public static Object to(Object o, int type) {
    if (o == null) return null;

    switch (type) {
      case BOOLEAN:
        {
          if (o instanceof Boolean) return o;
          else if (o instanceof Number) return ((Number) o).longValue() != 0L;
          else if (o instanceof CharSequence || o instanceof Character)
            return Boolean.parseBoolean(o.toString());
          else throw new IllegalArgumentException();
        }

      case INT:
        {
          if (o instanceof Boolean) return ((Boolean) o) ? 1L : 0L;
          else if (o instanceof Number) return ((Number) o).longValue();
          else if (o instanceof CharSequence || o instanceof Character)
            return Long.parseLong(o.toString());
          else throw new IllegalArgumentException();
        }

      case FLOAT:
        {
          if (o instanceof Boolean) return ((Boolean) o) ? 1.0 : 0.0;
          else if (o instanceof Number) return ((Number) o).doubleValue();
          else if (o instanceof CharSequence || o instanceof Character)
            return Double.parseDouble(o.toString());
          else throw new IllegalArgumentException();
        }

      case STRING:
        return ObjectUtils.toString(o, "null");

      case JSON:
        {
          JsonNodeFactory jnf = JsonNodeFactory.instance;
          if (o instanceof Boolean) return jnf.booleanNode((Boolean) o);
          else if (o instanceof Number) return jnf.numberNode(((Number) o).longValue());
          else if (o instanceof CharSequence || o instanceof Character)
            try {
              return JsonHelper.parse(o.toString());
            } catch (Exception e) {
              return jnf.nullNode();
            }
          else if (o instanceof JsonNode) return o;
          else if (o instanceof Collection) {
            ArrayNode an = jnf.arrayNode();
            for (Object e : (Collection) o) {
              Object v = ValuesNewAccount.to(e, JSON);
              an.add(v instanceof JsonNode ? (JsonNode) v : jnf.nullNode());
            }
            return an;
          } else if (o instanceof Iterator) {
            ArrayNode an = jnf.arrayNode();
            Iterator iter = (Iterator) o;
            while (iter.hasNext()) {
              Object v = ValuesNewAccount.to(iter.next(), JSON);
              an.add(v instanceof JsonNode ? (JsonNode) v : jnf.nullNode());
            }
            return an;
          } else if (o instanceof Map) {
            ObjectNode on = jnf.objectNode();
            for (Object e0 : ((Map) o).entrySet()) {
              Map.Entry e = (Map.Entry) e0;
              Object v = ValuesNewAccount.to(e.getValue(), JSON);
              on.put(e.getKey().toString(), v instanceof JsonNode ? (JsonNode) v : jnf.nullNode());
            }
            return on;
          }
        }
        break;
    }
    return o;
  }