@SuppressWarnings("unchecked")
    private static JSONArray getJsonArray(List<Object> messageList) {
      JSONArray jsonArr = new JSONArray();
      int index = 0;
      for (Object object : messageList) {
        if (object == null) {
          jsonArr.set(index++, JSONNull.getInstance());
        } else if (object instanceof Boolean) {
          jsonArr.set(index++, JSONBoolean.getInstance((Boolean) object));
        } else if (object instanceof Integer) {
          jsonArr.set(index++, new JSONNumber((Integer) object));
        } else if (object instanceof String) {
          jsonArr.set(index++, new JSONString((String) object));
        } else if (object instanceof List) {
          jsonArr.set(index++, getJsonArray((List<Object>) object));
        } else if (object instanceof Map) {
          jsonArr.set(index++, getJsonObject((Map<String, Object>) object));
        } else {
          throw new IllegalStateException("Invalid object encountered");
        }
      }
      return jsonArr;
    }