Example usage for com.google.gson JsonObject toString

List of usage examples for com.google.gson JsonObject toString

Introduction

In this page you can find the example usage for com.google.gson JsonObject toString.

Prototype

@Override
public String toString() 

Source Link

Document

Returns a String representation of this element.

Usage

From source file:com.google.dart.server.internal.remote.ByteRequestSink.java

License:Open Source License

@Override
public void add(JsonObject request) {
    String text = request.toString();
    if (debugStream != null) {
        if (!text.contains("server.getVersion")) {
            debugStream.println(System.currentTimeMillis() + " => " + text);
        }//ww  w.  j  a v  a 2 s  .  c  o m
    }
    lineQueue.add(text);
}

From source file:com.google.gerrit.sshd.commands.QueryShell.java

License:Apache License

private void readEvalPrintLoop() {
    final StringBuilder buffer = new StringBuilder();
    boolean executed = false;
    for (;;) {/*from  w  w  w .j a  v a  2 s.c om*/
        if (outputFormat == OutputFormat.PRETTY) {
            print(buffer.length() == 0 || executed ? "gerrit> " : "     -> ");
        }
        String line = readLine();
        if (line == null) {
            return;
        }

        if (line.startsWith("\\")) {
            // Shell command, check the various cases we recognize
            //
            line = line.substring(1);
            if (line.equals("h") || line.equals("?")) {
                showHelp();

            } else if (line.equals("q")) {
                if (outputFormat == OutputFormat.PRETTY) {
                    println("Bye");
                }
                return;

            } else if (line.equals("r")) {
                buffer.setLength(0);
                executed = false;

            } else if (line.equals("p")) {
                println(buffer.toString());

            } else if (line.equals("g")) {
                if (buffer.length() > 0) {
                    executeStatement(buffer.toString());
                    executed = true;
                }

            } else if (line.equals("d")) {
                listTables();

            } else if (line.startsWith("d ")) {
                showTable(line.substring(2).trim());

            } else {
                final String msg = "'\\" + line + "' not supported";
                switch (outputFormat) {
                case JSON_SINGLE:
                case JSON: {
                    final JsonObject err = new JsonObject();
                    err.addProperty("type", "error");
                    err.addProperty("message", msg);
                    println(err.toString());
                    break;
                }
                case PRETTY:
                default:
                    println("ERROR: " + msg);
                    println("");
                    showHelp();
                    break;
                }
            }
            continue;
        }

        if (executed) {
            buffer.setLength(0);
            executed = false;
        }
        if (buffer.length() > 0) {
            buffer.append('\n');
        }
        buffer.append(line);

        if (buffer.length() > 0 && buffer.charAt(buffer.length() - 1) == ';') {
            executeStatement(buffer.toString());
            executed = true;
        }
    }
}

From source file:com.google.gerrit.sshd.commands.QueryShell.java

License:Apache License

private void executeStatement(final String sql) {
    final long start = TimeUtil.nowMs();
    final boolean hasResultSet;
    try {// w  w w  . ja va 2  s. c  o  m
        hasResultSet = statement.execute(sql);
    } catch (SQLException e) {
        error(e);
        return;
    }

    try {
        if (hasResultSet) {
            try (ResultSet rs = statement.getResultSet()) {
                showResultSet(rs, false, start);
            }

        } else {
            final int updateCount = statement.getUpdateCount();
            final long ms = TimeUtil.nowMs() - start;
            switch (outputFormat) {
            case JSON_SINGLE:
            case JSON: {
                final JsonObject tail = new JsonObject();
                tail.addProperty("type", "update-stats");
                tail.addProperty("rowCount", updateCount);
                tail.addProperty("runTimeMilliseconds", ms);
                println(tail.toString());
                break;
            }

            case PRETTY:
            default:
                println("UPDATE " + updateCount + "; " + ms + " ms");
                break;
            }
        }
    } catch (SQLException e) {
        error(e);
    }
}

From source file:com.google.gerrit.sshd.commands.QueryShell.java

License:Apache License

/**
 * Outputs a result set to stdout in Json format.
 *
 * @param rs ResultSet to show./*from ww  w.  j a va  2 s  . co m*/
 * @param alreadyOnRow true if rs is already on the first row. false
 *     otherwise.
 * @param start Timestamp in milliseconds when executing the statement
 *     started. This timestamp is used to compute statistics about the
 *     statement. If no statistics should be shown, set it to 0.
 * @param show Functions to map columns
 * @throws SQLException
 */
private void showResultSetJson(final ResultSet rs, boolean alreadyOnRow, long start, Function... show)
        throws SQLException {
    JsonArray collector = new JsonArray();
    final ResultSetMetaData meta = rs.getMetaData();
    final Function[] columnMap;
    if (show != null && 0 < show.length) {
        columnMap = show;

    } else {
        final int colCnt = meta.getColumnCount();
        columnMap = new Function[colCnt];
        for (int colId = 0; colId < colCnt; colId++) {
            final int p = colId + 1;
            final String name = meta.getColumnLabel(p);
            columnMap[colId] = new Identity(p, name);
        }
    }

    int rowCnt = 0;
    while (alreadyOnRow || rs.next()) {
        final JsonObject row = new JsonObject();
        final JsonObject cols = new JsonObject();
        for (Function function : columnMap) {
            String v = function.apply(rs);
            if (v == null) {
                continue;
            }
            cols.addProperty(function.name.toLowerCase(), v);
        }
        row.addProperty("type", "row");
        row.add("columns", cols);
        switch (outputFormat) {
        case JSON:
            println(row.toString());
            break;
        case JSON_SINGLE:
            collector.add(row);
            break;
        default:
            final JsonObject obj = new JsonObject();
            obj.addProperty("type", "error");
            obj.addProperty("message", "Unsupported Json variant");
            println(obj.toString());
            return;
        }
        alreadyOnRow = false;
        rowCnt++;
    }

    JsonObject tail = null;
    if (start != 0) {
        tail = new JsonObject();
        tail.addProperty("type", "query-stats");
        tail.addProperty("rowCount", rowCnt);
        final long ms = TimeUtil.nowMs() - start;
        tail.addProperty("runTimeMilliseconds", ms);
    }

    switch (outputFormat) {
    case JSON:
        if (tail != null) {
            println(tail.toString());
        }
        break;
    case JSON_SINGLE:
        if (tail != null) {
            collector.add(tail);
        }
        println(collector.toString());
        break;
    default:
        final JsonObject obj = new JsonObject();
        obj.addProperty("type", "error");
        obj.addProperty("message", "Unsupported Json variant");
        println(obj.toString());
    }
}

From source file:com.google.gerrit.sshd.commands.QueryShell.java

License:Apache License

private void warning(final String msg) {
    switch (outputFormat) {
    case JSON_SINGLE:
    case JSON: {//from  www.j a  va2s.  c o m
        final JsonObject obj = new JsonObject();
        obj.addProperty("type", "warning");
        obj.addProperty("message", msg);
        println(obj.toString());
        break;
    }

    case PRETTY:
    default:
        println("WARNING: " + msg);
        break;
    }
}

From source file:com.google.gerrit.sshd.commands.QueryShell.java

License:Apache License

private void error(final SQLException err) {
    switch (outputFormat) {
    case JSON_SINGLE:
    case JSON: {/* ww  w .j a  v a  2s.  c  o m*/
        final JsonObject obj = new JsonObject();
        obj.addProperty("type", "error");
        obj.addProperty("message", err.getMessage());
        println(obj.toString());
        break;
    }

    case PRETTY:
    default:
        println("ERROR: " + err.getMessage());
        break;
    }
}

From source file:com.google.identitytoolkit.RpcHelper.java

License:Open Source License

private JsonObject invokeGitkitApi(String method, JsonObject params, String accessToken)
        throws GitkitClientException, GitkitServerException {
    try {//from  w  w w.ja v  a 2  s  .  c  om
        Map<String, String> headers = Maps.newHashMap();
        if (accessToken != null) {
            headers.put("Authorization", "Bearer " + accessToken);
        }
        headers.put("Content-Type", "application/json");
        String response = httpSender.post(gitkitApiUrl + method, params.toString(), headers);
        return checkGitkitException(response);
    } catch (IOException e) {
        throw new GitkitServerException(e);
    }
}

From source file:com.google.samples.apps.iosched.sync.userdata.util.UserDataHelper.java

License:Open Source License

static public String toSessionsString(Set<String> sessionIds) {
    JsonArray array = new JsonArray();
    for (String sessionId : sessionIds) {
        array.add(new JsonPrimitive(sessionId));
    }//from w  w w.  j a v  a 2  s . co  m
    JsonObject obj = new JsonObject();
    obj.add(JSON_STARRED_SESSIONS_KEY, array);
    return obj.toString();
}

From source file:com.google.u2f.codec.ClientDataCodec.java

License:Open Source License

/** Computes ClientData.challenge */
public static String encodeClientData(String requestType, String serverChallengeBase64, String origin,
        JsonObject jsonChannelId) {//from  w w  w .  ja  va  2s  . co  m
    JsonObject browserData = new JsonObject();
    browserData.addProperty(JSON_PROPERTY_REQUEST_TYPE, requestType);
    browserData.addProperty(JSON_PROPERTY_SERVER_CHALLENGE_BASE64, serverChallengeBase64);
    browserData.add(JSON_PROPERTY_CHANNEL_ID, jsonChannelId);
    browserData.addProperty(JSON_PROPERTY_SERVER_ORIGIN, origin);
    return browserData.toString();
}