code: private static String toJson(Object x) {
      try {
        if (x == null || x instanceof String || x instanceof Number || x instanceof Character
            || x.getClass().isArray() || x instanceof Iterable<?>) {
          return new JSONObject().put("JSON", x).toString(2);
        } else if (x instanceof Map<?, ?>) {
          return (new JSONObject((Map<?, ?>) x)).toString(2);
        } else if (x instanceof JSONObject) {
          return ((JSONObject) x).toString(2);
        } else {
          return (new JSONObject(x)).toString(2);
        }
      } catch (Exception e) {
        throw new RuntimeException("x=" + x, e);
      }
    }
docs: Convert an object into its JSON representation.