Beispiel #1
0
  @SuppressWarnings("unchecked")
  private static JSONObject fromPairs(Pair pair) throws JSONException {
    JSONObject json = new JSONObject();

    if (pair.isEmpty()) return json;

    if (pair.isEmpty() == false) {
      Object first = pair.getFirst();

      if (first instanceof Pair) {
        Pair firstPair = (Pair) first;

        String key = firstPair.first.toString();

        Object value = firstPair.rest();

        if (value instanceof Pair) {
          Pair valuePair = (Pair) value;

          if (valuePair.first instanceof Pair) value = JSONHelper.fromPairs(valuePair);
          else value = JSONHelper.listFromPairs(valuePair);

          value = valuePair.toString();
        }

        json.put(key, value);
      }

      Object rest = pair.getRest();

      if (rest instanceof Pair) {
        Pair restPair = (Pair) rest;

        JSONObject restJson = JSONHelper.fromPairs(restPair);

        Iterator<String> keys = restJson.keys();

        while (keys.hasNext()) {
          String key = keys.next();

          json.put(key, restJson.get(key));
        }
      }
    }

    return json;
  }