Return-Path: X-Original-To: apmail-asterixdb-notifications-archive@minotaur.apache.org Delivered-To: apmail-asterixdb-notifications-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id E94C9190B1 for ; Thu, 24 Mar 2016 23:52:27 +0000 (UTC) Received: (qmail 96498 invoked by uid 500); 24 Mar 2016 23:52:27 -0000 Delivered-To: apmail-asterixdb-notifications-archive@asterixdb.apache.org Received: (qmail 96464 invoked by uid 500); 24 Mar 2016 23:52:27 -0000 Mailing-List: contact notifications-help@asterixdb.incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@asterixdb.incubator.apache.org Delivered-To: mailing list notifications@asterixdb.incubator.apache.org Received: (qmail 96455 invoked by uid 99); 24 Mar 2016 23:52:27 -0000 Received: from pnap-us-west-generic-nat.apache.org (HELO spamd2-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 24 Mar 2016 23:52:27 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd2-us-west.apache.org (ASF Mail Server at spamd2-us-west.apache.org) with ESMTP id 77D3D1A142A for ; Thu, 24 Mar 2016 23:52:27 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd2-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: 0.919 X-Spam-Level: X-Spam-Status: No, score=0.919 tagged_above=-999 required=6.31 tests=[SPF_FAIL=0.919] autolearn=disabled Received: from mx2-lw-eu.apache.org ([10.40.0.8]) by localhost (spamd2-us-west.apache.org [10.40.0.9]) (amavisd-new, port 10024) with ESMTP id VGbSWag0Co4Z for ; Thu, 24 Mar 2016 23:52:25 +0000 (UTC) Received: from unhygienix.ics.uci.edu (unhygienix.ics.uci.edu [128.195.14.130]) by mx2-lw-eu.apache.org (ASF Mail Server at mx2-lw-eu.apache.org) with ESMTP id 4FF7B5F119 for ; Thu, 24 Mar 2016 23:52:24 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by unhygienix.ics.uci.edu (Postfix) with ESMTP id BF6202418D8; Thu, 24 Mar 2016 16:47:26 -0700 (PDT) Date: Thu, 24 Mar 2016 16:47:26 -0700 From: "Till Westmann (Code Review)" Message-ID: Reply-To: tillw@apache.org X-Gerrit-MessageType: newchange Subject: Change in asterixdb[master]: add parameter to QueryServiceServlet to indent JSON X-Gerrit-Change-Id: Id197d3ad6aa17c9a36bb3845bd3ca75a695ba6d9 X-Gerrit-ChangeURL: X-Gerrit-Commit: 5a90555c0e28a72b3519a4bb6d93a8b4b76560f9 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Content-Disposition: inline User-Agent: Gerrit/2.8.4 To: undisclosed-recipients:; Till Westmann has uploaded a new change for review. https://asterix-gerrit.ics.uci.edu/750 Change subject: add parameter to QueryServiceServlet to indent JSON ...................................................................... add parameter to QueryServiceServlet to indent JSON Change-Id: Id197d3ad6aa17c9a36bb3845bd3ca75a695ba6d9 --- M asterix-app/src/main/java/org/apache/asterix/api/common/SessionConfig.java M asterix-app/src/main/java/org/apache/asterix/api/http/servlet/JSONUtil.java M asterix-app/src/main/java/org/apache/asterix/api/http/servlet/QueryServiceServlet.java M asterix-app/src/main/java/org/apache/asterix/result/ResultUtils.java 4 files changed, 25 insertions(+), 7 deletions(-) git pull ssh://asterix-gerrit.ics.uci.edu:29418/asterixdb refs/changes/50/750/1 diff --git a/asterix-app/src/main/java/org/apache/asterix/api/common/SessionConfig.java b/asterix-app/src/main/java/org/apache/asterix/api/common/SessionConfig.java index 2ed67b2..7bfc55c 100644 --- a/asterix-app/src/main/java/org/apache/asterix/api/common/SessionConfig.java +++ b/asterix-app/src/main/java/org/apache/asterix/api/common/SessionConfig.java @@ -92,6 +92,11 @@ */ public static final String FORMAT_WRAPPER_ARRAY = "format-wrapper-array"; + /** + * Format flag: indent JSON results. + */ + public static final String INDENT_JSON = "indent-json"; + public interface ResultDecorator { PrintWriter print(PrintWriter pw); } diff --git a/asterix-app/src/main/java/org/apache/asterix/api/http/servlet/JSONUtil.java b/asterix-app/src/main/java/org/apache/asterix/api/http/servlet/JSONUtil.java index 56f349c..d69c3c1 100644 --- a/asterix-app/src/main/java/org/apache/asterix/api/http/servlet/JSONUtil.java +++ b/asterix-app/src/main/java/org/apache/asterix/api/http/servlet/JSONUtil.java @@ -26,13 +26,17 @@ public class JSONUtil { - static final String INDENT = " "; + static final String INDENT = "\t"; public static String indent(String str) { + return indent(str, 0); + } + + public static String indent(String str, int initialIndent) { try { - return append(new StringBuilder(), new JSONObject(str), 0).toString(); + return append(new StringBuilder(), new JSONObject(str), initialIndent).toString(); } catch (JSONException e) { - throw new IllegalArgumentException(e); + return str; } } @@ -43,7 +47,7 @@ return append(sb, (JSONArray) o, indent); } else if (o instanceof String) { return quote(sb, (String) o); - } else if (o instanceof Number || o instanceof Boolean) { + } else if (JSONObject.NULL.equals(o) || o instanceof Number || o instanceof Boolean) { return sb.append(String.valueOf(o)); } throw new UnsupportedOperationException(o.getClass().getSimpleName()); diff --git a/asterix-app/src/main/java/org/apache/asterix/api/http/servlet/QueryServiceServlet.java b/asterix-app/src/main/java/org/apache/asterix/api/http/servlet/QueryServiceServlet.java index c4d270b..4376b22 100644 --- a/asterix-app/src/main/java/org/apache/asterix/api/http/servlet/QueryServiceServlet.java +++ b/asterix-app/src/main/java/org/apache/asterix/api/http/servlet/QueryServiceServlet.java @@ -63,7 +63,8 @@ statement, format, // Asterix - header + header, + indent } public enum Header { @@ -159,14 +160,16 @@ SessionConfig.ResultDecorator resultPostfix = new SessionConfig.ResultDecorator() { @Override public PrintWriter print(PrintWriter pw) { - pw.print(",\n"); + pw.print("\t,\n"); return pw; } }; SessionConfig.OutputFormat format = getFormat(request); SessionConfig sessionConfig = new SessionConfig(resultWriter, format, resultPrefix, resultPostfix); - sessionConfig.set(SessionConfig.FORMAT_WRAPPER_ARRAY, (format == SessionConfig.OutputFormat.CLEAN_JSON)); + sessionConfig.set(SessionConfig.FORMAT_WRAPPER_ARRAY, format == SessionConfig.OutputFormat.CLEAN_JSON); + boolean indentJson = Boolean.parseBoolean(request.getParameter(Parameter.indent.name())); + sessionConfig.set(SessionConfig.INDENT_JSON, indentJson); if (format == SessionConfig.OutputFormat.CSV && ("present".equals(request.getParameter(Parameter.header.name())) || request.getHeader(Header.Accept.str()).contains("header=present"))) { diff --git a/asterix-app/src/main/java/org/apache/asterix/result/ResultUtils.java b/asterix-app/src/main/java/org/apache/asterix/result/ResultUtils.java index f1d20d0..2fe7f28 100644 --- a/asterix-app/src/main/java/org/apache/asterix/result/ResultUtils.java +++ b/asterix-app/src/main/java/org/apache/asterix/result/ResultUtils.java @@ -33,6 +33,7 @@ import org.apache.asterix.api.common.SessionConfig; import org.apache.asterix.api.common.SessionConfig.OutputFormat; import org.apache.asterix.api.http.servlet.APIServlet; +import org.apache.asterix.api.http.servlet.JSONUtil; import org.apache.asterix.om.types.ARecordType; import org.apache.http.ParseException; import org.apache.hyracks.algebricks.common.exceptions.AlgebricksException; @@ -135,6 +136,7 @@ break; } + final boolean indentJSON = conf.is(SessionConfig.INDENT_JSON); if (bytesRead > 0) { do { try { @@ -157,6 +159,10 @@ conf.out().print(", "); } notfirst = true; + if (indentJSON) { + // TODO(tillw): this is inefficient - do this during result generation + result = JSONUtil.indent(result, 2); + } conf.out().print(result); if (conf.fmt() == OutputFormat.CSV) { conf.out().print("\r\n"); -- To view, visit https://asterix-gerrit.ics.uci.edu/750 To unsubscribe, visit https://asterix-gerrit.ics.uci.edu/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Id197d3ad6aa17c9a36bb3845bd3ca75a695ba6d9 Gerrit-PatchSet: 1 Gerrit-Project: asterixdb Gerrit-Branch: master Gerrit-Owner: Till Westmann