예제 #1
0
  public Json makeTyped(Object anything) {
    boolean isarray = anything.getClass().isArray();
    Class<?> type = isarray ? anything.getClass().getComponentType() : anything.getClass();
    JsonConverter converter = converterMap.get(type.getName());
    String typeName = shortNameMap.getY(type.getName());
    if (typeName == null) typeName = type.getName();
    if (isarray) {
      Json result = Json.array();
      Object[] A = (Object[]) anything;
      for (Object x : A) {
        if (x == null) result.add(Json.nil());
        else result.add(converter != null ? converter.to(x) : make(x));
      }
      return Json.object().set("javaArrayType", typeName).set("array", result);
    } else if (type.isEnum())
      return Json.object().set("java.lang.Enum", type.getName()).set("value", anything.toString());

    for (Class<?> abstractConv : converterFromAbstractMap.keySet())
      if (abstractConv.isAssignableFrom(type))
        return converterFromAbstractMap.get(abstractConv).to(anything);
    Json value = null;
    if (converter != null) value = converter.to(anything);
    else if (Collection.class.isAssignableFrom(type) || Map.class.isAssignableFrom(type))
      value = beanConverter.to(anything);
    else
      try {
        value = f.make(anything);
      } catch (Throwable t) {
        value = beanConverter.to(anything);
      }
    return Json.object().set("javaType", typeName).set("value", value);
  }
예제 #2
0
 @SuppressWarnings({"unchecked", "rawtypes"})
 public <T> T value(Json x) {
   if (x == null || x.isNull()) return null;
   else if (x.isPrimitive()) return (T) x.getValue();
   else if (x.isArray())
     // what to do here ... this is some sort of collection??
     return (T) x.getValue();
   else if (x.has("java.lang.Enum")) {
     try {
       return (T)
           Enum.valueOf(
               (Class<Enum>) Class.forName(x.at("java.lang.Enum").asString()),
               x.at("value").asString());
     } catch (Exception t) {
       throw new RuntimeException(t);
     }
   } else if (x.has("javaArrayType")) {
     String fullName = shortNameMap.getX(x.at("javaArrayType").asString());
     if (fullName == null) fullName = x.at("javaArrayType").asString();
     try {
       Class<?> cl = Class.forName(fullName);
       JsonConverter converter = converterMap.get(fullName);
       Object A = Array.newInstance(cl, x.at("array").asJsonList().size());
       for (int i = 0; i < x.at("array").asJsonList().size(); i++) {
         Json j = x.at("array").at(i);
         Array.set(A, i, j.isNull() ? null : converter != null ? converter.from(j) : value(j));
       }
       return (T) A;
     } catch (Exception ex) {
       throw new RuntimeException(ex);
     }
   } else if (x.has("javaType")) {
     String fullName = shortNameMap.getX(x.at("javaType").asString());
     if (fullName == null) fullName = x.at("javaType").asString();
     JsonConverter converter = converterMap.get(fullName);
     if (converter != null)
       return (T) converter.from(converter instanceof BeanJsonConverter ? x : x.at("value"));
     else return (T) beanConverter.from(x); // .at("value"));
   } else return (T) x.getValue();
 }