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);
  }
Exemple #2
0
 /**
  * Searches a user by ID. If multiple realms are configured, each will be tried according to their
  * ordinal number configuration. Only the first found is returned.
  */
 public Json searchUserById(String id) {
   if (id == null || id.length() == 0) return Json.array();
   for (String providerName : orderedProviders()) {
     UserProvider P = provider(providerName);
     Json user = P.get(id);
     if (!user.isNull()) return user;
   }
   return Json.nil();
 }
  public Json make(Object anything) {
    if (anything == null) return Json.nil();
    else if (anything instanceof String) return f.string((String) anything);
    else if (anything instanceof Boolean) return f.bool((Boolean) anything);
    else if (anything instanceof Number) return f.number((Number) anything);
    else if (anything instanceof Json) return (Json) anything;
    else if (anything instanceof Performative) return f.make(anything.toString());

    return makeTyped(anything);
  }
 private Json makeProperty(Object value) {
   if (value == null) return Json.nil();
   Class<?> type = value.getClass();
   if (Number.class.isAssignableFrom(type)) return makeTyped(value);
   else return make(value);
 }