Example #1
0
 /**
  * Make a prettyprinted JSON text of this JSONArray. Warning: This method assumes that the data
  * structure is acyclical.
  *
  * @param indentFactor The number of spaces to add to each level of indentation.
  * @param indent The indention of the top level.
  * @return a printable, displayable, transmittable representation of the array.
  * @throws JSONException
  */
 String toString(int indentFactor, int indent) throws JSONException {
   int len = length();
   if (len == 0) {
     return "[]";
   }
   int i;
   StringBuffer sb = new StringBuffer("[");
   if (len == 1) {
     sb.append(JSONObject.valueToString(this.myArrayList.get(0), indentFactor, indent));
   } else {
     int newindent = indent + indentFactor;
     sb.append('\n');
     for (i = 0; i < len; i += 1) {
       if (i > 0) {
         sb.append(",\n");
       }
       for (int j = 0; j < newindent; j += 1) {
         sb.append(' ');
       }
       sb.append(JSONObject.valueToString(this.myArrayList.get(i), indentFactor, newindent));
     }
     sb.append('\n');
     for (i = 0; i < indent; i += 1) {
       sb.append(' ');
     }
   }
   sb.append(']');
   return sb.toString();
 }
Example #2
0
  /**
   * Write the contents of the JSONObject as JSON text to a writer. For compactness, no whitespace
   * is added.
   *
   * <p>Warning: This method assumes that the data structure is acyclical.
   *
   * @return The writer.
   * @throws JSONException
   */
  public Writer write(Writer writer) throws JSONException {
    try {
      boolean commanate = false;
      final Iterator keys = this.keys();
      writer.write('{');

      while (keys.hasNext()) {
        if (commanate) {
          writer.write(',');
        }
        final Object key = keys.next();
        writer.write(JSONObject.quote(key.toString()));
        writer.write(':');
        final Object value = this.map.get(key);
        if (value instanceof JSONObject) {
          ((JSONObject) value).write(writer);
        } else if (value instanceof JSONArray) {
          ((JSONArray) value).write(writer);
        } else {
          writer.write(JSONObject.valueToString(value));
        }
        commanate = true;
      }
      writer.write('}');
      return writer;
    } catch (final IOException exception) {
      throw new JSONException(exception);
    }
  }
Example #3
0
  /**
   * Write the contents of the JSONArray as JSON text to a writer. For compactness, no whitespace is
   * added.
   *
   * <p>Warning: This method assumes that the data structure is acyclical.
   *
   * @return The writer.
   * @throws JSONException
   */
  public Writer write(Writer writer) throws JSONException {
    try {
      boolean b = false;
      int len = length();

      writer.write('[');

      for (int i = 0; i < len; i += 1) {
        if (b) {
          writer.write(',');
        }
        Object v = this.myArrayList.get(i);
        if (v instanceof JSONObject) {
          ((JSONObject) v).write(writer);
        } else if (v instanceof JSONArray) {
          ((JSONArray) v).write(writer);
        } else {
          writer.write(JSONObject.valueToString(v));
        }
        b = true;
      }
      writer.write(']');
      return writer;
    } catch (IOException e) {
      throw new JSONException(e);
    }
  }
Example #4
0
  /**
   * Make a string from the contents of this JSONArray. The <code>separator</code> string is
   * inserted between each element. Warning: This method assumes that the data structure is
   * acyclical.
   *
   * @param separator A string that will be inserted between the elements.
   * @return a string.
   * @throws JSONException If the array contains an invalid number.
   */
  public String join(String separator) throws JSONException {
    int len = this.length();
    StringBuffer sb = new StringBuffer();

    for (int i = 0; i < len; i += 1) {
      if (i > 0) {
        sb.append(separator);
      }
      sb.append(JSONObject.valueToString(this.myArrayList.get(i)));
    }
    return sb.toString();
  }
Example #5
0
 /**
  * Make a prettyprinted JSON text of this JSONObject.
  *
  * <p>Warning: This method assumes that the data structure is acyclical.
  *
  * @param indentFactor The number of spaces to add to each level of indentation.
  * @param indent The indentation of the top level.
  * @return a printable, displayable, transmittable representation of the object, beginning with
  *     <code>{</code>&nbsp;<small>(left brace)</small> and ending with <code>}</code>
  *     &nbsp;<small>(right brace)</small>.
  * @throws JSONException If the object contains an invalid number.
  */
 String toString(int indentFactor, int indent) throws JSONException {
   int i;
   final int length = this.length();
   if (length == 0) {
     return "{}";
   }
   final Iterator keys = this.keys();
   final int newindent = indent + indentFactor;
   Object object;
   final StringBuffer sb = new StringBuffer("{");
   if (length == 1) {
     object = keys.next();
     sb.append(JSONObject.quote(object.toString()));
     sb.append(": ");
     sb.append(JSONObject.valueToString(this.map.get(object), indentFactor, indent));
   } else {
     while (keys.hasNext()) {
       object = keys.next();
       if (sb.length() > 1) {
         sb.append(",\n");
       } else {
         sb.append('\n');
       }
       for (i = 0; i < newindent; i += 1) {
         sb.append(' ');
       }
       sb.append(JSONObject.quote(object.toString()));
       sb.append(": ");
       sb.append(JSONObject.valueToString(this.map.get(object), indentFactor, newindent));
     }
     if (sb.length() > 1) {
       sb.append('\n');
       for (i = 0; i < indent; i += 1) {
         sb.append(' ');
       }
     }
   }
   sb.append('}');
   return sb.toString();
 }
Example #6
0
  /**
   * Append the string representation of the filter to the provided buffer.
   *
   * @param builder The buffer to which the string representation of the filter is to be appended.
   */
  public void toString(final StringBuilder builder) {
    switch (filterType) {
      case AND:
      case OR:
        builder.append('(');

        for (int i = 0; i < filterComponents.size(); i++) {
          if (i != 0) {
            builder.append(' ');
            builder.append(filterType);
            builder.append(' ');
          }

          builder.append(filterComponents.get(i));
        }

        builder.append(')');
        break;

      case EQUALITY:
      case CONTAINS:
      case STARTS_WITH:
      case GREATER_THAN:
      case GREATER_OR_EQUAL:
      case LESS_THAN:
      case LESS_OR_EQUAL:
        builder.append(filterAttribute);
        builder.append(' ');
        builder.append(filterType);
        builder.append(' ');

        if (quoteFilterValue) {
          try {
            builder.append(JSONObject.valueToString(filterValue));
          } catch (JSONException e) {
            Debug.debugException(e);
          }
        } else {
          builder.append(filterValue);
        }
        break;

      case PRESENCE:
        builder.append(filterAttribute);
        builder.append(' ');
        builder.append(filterType);
        break;
    }
  }
Example #7
0
  @RequestMapping("/userInfo")
  public String userInfo(HttpServletRequest request, Model model) {

    String code = request.getParameter("code");

    AccessTokenDTO accessTokenDTO = WeChatUtils.getLoginAccessTokenDTO(code);

    if (accessTokenDTO != null) {
      UserInfoDTO userInfoDTO =
          WeChatUtils.getUserInfoDTO(accessTokenDTO.getOpenid(), accessTokenDTO.getAccess_token());
      model.addAttribute("userInfoDTO", userInfoDTO);
      System.out.println(JSONObject.valueToString(userInfoDTO));
    }

    return "/user_info";
  }
Example #8
0
  /**
   * Make a JSON text of this JSONObject. For compactness, no whitespace is added. If this would not
   * result in a syntactically correct JSON text, then null will be returned instead.
   *
   * <p>Warning: This method assumes that the data structure is acyclical.
   *
   * @return a printable, displayable, portable, transmittable representation of the object,
   *     beginning with <code>{</code>&nbsp;<small>(left brace)</small> and ending with <code>}
   *     </code>&nbsp;<small>(right brace)</small>.
   */
  @Override
  public String toString() {
    try {
      final Iterator keys = this.keys();
      final StringBuffer sb = new StringBuffer("{");

      while (keys.hasNext()) {
        if (sb.length() > 1) {
          sb.append(',');
        }
        final Object o = keys.next();
        sb.append(JSONObject.quote(o.toString()));
        sb.append(':');
        sb.append(JSONObject.valueToString(this.map.get(o)));
      }
      sb.append('}');
      return sb.toString();
    } catch (final Exception e) {
      return null;
    }
  }
 /**
  * Append an object value.
  *
  * @param o The object to append. It can be null, or a Boolean, Number, String, JSONObject, or
  *     JSONArray, or an object with a toJSONString() method.
  * @return this
  * @throws JSONException If the value is out of sequence.
  */
 public JSONWriter value(Object o) throws JSONException {
   return this.append(JSONObject.valueToString(o));
 }