Esempio n. 1
0
  @MayBeExcluded
  public static String objectToXml(Object obj, String _id) {
    String id = (_id != null ? " id=\"" + _id + "\"" : "");
    if (StrictWeb.jsTypeOf(obj) == "undefined") return "<null" + id + "/>";
    if (StrictWeb.jsTypeOf(obj) == "string")
      return "<s" + id + ">" + StrictWeb.toHTML((String) obj) + "</s>";
    if (StrictWeb.jsTypeOf(obj) == "boolean")
      return ((Boolean) obj) ? "<b" + id + ">1</b>" : "<b" + id + ">0</b>";
    if (StrictWeb.jsTypeOf(obj) == "number") return "<n" + id + ">" + obj + "</n>";
    if (StrictWeb.jsTypeOf(obj) == "object") {
      if (obj == null) return "<null" + id + "/>";
      if (StrictWeb.isEnum(obj)) return "<e" + id + ">" + obj.toString() + "</e>";
      if (StrictWeb.isInstanceOfDate(obj))
        return "<d" + id + ">" + StrictWeb.dateToStringSmart((Date) obj) + "</d>";
      if (StrictWeb.isInstanceOfArray(obj)) return arrayToXml((Object[]) obj, id);
      if (StrictWeb.isInstanceOfNode(obj)) {
        return "<form" + id + ">" + formToXml((Node) obj) + "</form>";
      }
      Map<String, String> map = (Map<String, String>) obj;
      String xml = "<o" + id + ">";
      for (String key : map.keySet()) {
        String val = map.get(key);
        if (StrictWeb.jsTypeOf(val) != "function") xml += objectToXml(val, key);
      }
      return xml + "</o>";
    }

    return "<" + StrictWeb.jsTypeOf(obj) + "/>";
  }
Esempio n. 2
0
  @MayBeExcluded
  public static String objectToJson(Object obj) {
    if (StrictWeb.jsTypeOf(obj) == "undefined") return "null";
    if (StrictWeb.jsTypeOf(obj) == "string") return '"' + StrictWeb.toJSON((String) obj) + '"';
    if (StrictWeb.jsTypeOf(obj) == "boolean") return ((Boolean) obj) ? "1" : "0";
    if (StrictWeb.jsTypeOf(obj) == "number") return obj.toString();
    if (StrictWeb.jsTypeOf(obj) == "object") {
      if (obj == null) return "null";
      if (StrictWeb.isEnum(obj)) return obj.toString();
      if (StrictWeb.isInstanceOfDate(obj)) return StrictWeb.dateToStringSmart((Date) obj);
      if (StrictWeb.isInstanceOfArray(obj)) return arrayToJson((Object[]) obj);
      if (StrictWeb.isInstanceOfNode(obj)) {
        String json = formToJson((Node) obj);
        if (json != null && json.length() > 0 && json.charAt(0) == '{') return "[" + json + "]";
        return "{" + json + "}";
      }
      Map<String, String> map = (Map<String, String>) obj;
      String json = "";
      for (String key : map.keySet()) {
        String val = map.get(key);
        if (StrictWeb.jsTypeOf(val) == "function") continue;
        if (json.length() > 0) json += ",";
        json += objectToJson(key) + ":" + objectToJson(val);
      }
      return "{" + json + "}";
    }

    return nativeJsThrow("Unknown object type: " + StrictWeb.jsTypeOf(obj));
  }