private String writeError(Throwable error, String errorMsg) {
    StringBuffer errorBlock = new StringBuffer("\"error\":");
    errorBlock.append("\"");
    if (error != null) {
      JsonStringEncoder inst = JsonStringEncoder.getInstance();
      errorBlock.append(inst.quoteAsString("" + error.getClass() + ": "));
      errorBlock.append(inst.quoteAsString("" + error.getMessage()));
      String mode = Play.configuration.getProperty("application.mode");
      if (!"prod".equals(mode)) {
        inst = JsonStringEncoder.getInstance();
        errorBlock.append(inst.quoteAsString("" + ExceptionUtils.getStackTrace(error)));
      }
    }

    if (errorMsg != null) {
      errorBlock.append(errorMsg);
    }

    for (int i = 0; i < errors.size(); i++) {
      String err = errors.get(i);
      errorBlock.append(err);
      if (i < errors.size()) errorBlock.append(", ");
    }

    errorBlock.append("\"");
    return "" + errorBlock;
  }
 public static char[] jsonEncoding(String rawString) {
   return JsonStringEncoder.getInstance().quoteAsString(rawString);
 }
Example #3
0
 public static String quoteString(String s) {
   JsonStringEncoder encoder = new JsonStringEncoder();
   return new String(encoder.quoteAsUTF8(s));
 }