Example usage for com.google.gson JsonElement toString

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

Introduction

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

Prototype

@Override
public String toString() 

Source Link

Document

Returns a String representation of this element.

Usage

From source file:io.robusta.rra.representation.implementation.GsonRepresentation.java

License:Apache License

@Override
public List<String> getValues(String key) throws RepresentationException {
    List<String> list = new ArrayList<String>();
    for (JsonElement elt : asObject().get(key).getAsJsonArray()) {
        list.add(elt.toString());
    }/*from   ww w  .j  a  va2 s.c o  m*/
    return list;
}

From source file:io.robusta.rra.representation.implementation.GsonRepresentation.java

License:Apache License

/**
 * @param elt//from w w  w.  j a v a 2 s .c  o m
 * @return
 */
private boolean isString(JsonElement elt) {
    return elt.isJsonPrimitive()
            && (elt.toString().trim().startsWith("\"") || elt.toString().trim().startsWith("'"));
}

From source file:io.socket.IOConnection.java

License:Open Source License

/**
 * sends a JSON message from {@link SocketIO} to the {@link IOTransport}.
 * //from   w  w w  . jav a2s  .  c  o m
 * @param socket
 *            the socket
 * @param ack
 *            acknowledge package which can be called from the server
 * @param json
 *            the json
 */
public void send(SocketIO socket, IOAcknowledge ack, JsonElement json) {
    IOMessage message = new IOMessage(IOMessage.TYPE_JSON_MESSAGE, socket.getNamespace(), json.toString());
    synthesizeAck(message, ack);
    sendPlain(message.toString());
}

From source file:io.soliton.protobuf.json.JsonRpcClientHandler.java

License:Apache License

@Override
protected void channelRead0(ChannelHandlerContext channelHandlerContext, HttpResponse response)
        throws Exception {
    if (!(response instanceof HttpContent)) {
        clientLogger.logServerError(null, null, new Exception("Returned response has no content"));
        logger.severe("Returned response has no content");
        return;/*from  w  ww.j  a  v  a2s .  com*/
    }

    HttpContent content = (HttpContent) response;

    if (!response.headers().get(HttpHeaders.Names.CONTENT_TYPE).equals(JsonRpcProtocol.CONTENT_TYPE)) {
        logger.warning("Incorrect Content-Type: " + response.headers().get(HttpHeaders.Names.CONTENT_TYPE));
    }

    JsonElement root;
    try {
        ByteBufInputStream stream = new ByteBufInputStream(content.content());
        JsonReader reader = new JsonReader(new InputStreamReader(stream, Charsets.UTF_8));
        root = new JsonParser().parse(reader);
    } catch (JsonSyntaxException jsonException) {
        clientLogger.logServerError(null, null, jsonException);
        logger.warning("JSON response cannot be decoded");
        return;
    }
    if (!root.isJsonObject()) {
        logger.warning("JSON response is not a JSON object: " + root.toString());
        return;
    }

    JsonRpcResponse jsonRpcResponse = JsonRpcResponse.fromJson(root.getAsJsonObject());

    JsonElement requestId = jsonRpcResponse.id();
    if (requestId == null || !requestId.isJsonPrimitive()) {
        clientLogger.logServerError(null, null,
                new Exception("Received response identifier is not JSON primitive"));
        logger.warning("Received response identifier is not JSON primitive: " + requestId.toString());
        return;
    }

    JsonResponseFuture<? extends Message> future = inFlightRequests.remove(requestId.getAsLong());
    if (future == null) {
        clientLogger.logServerError(null, null, new Exception("Response received for unknown identifier"));
        logger.severe("Response received for unknown identifier: " + requestId.getAsLong());
        return;
    }

    clientLogger.logSuccess(future.method());
    future.setResponse(jsonRpcResponse);
}

From source file:io.thekraken.json2hive.HiveUtils.java

License:Apache License

public static String struct(String value, int nasted) {

    if (value == null)
        return " ";
    if (value.isEmpty())
        return " ";
    if (nasted < 0)
        nasted = 0;/*from   w  w  w  .j  a v a 2  s.  com*/
    JsonParser parser = new JsonParser();
    JsonElement jsonElement = parser.parse(value);
    JsonObject jsonObject = jsonElement.getAsJsonObject();
    String struct = "";
    COMMA = 0;
    for (Entry<String, JsonElement> entry : jsonObject.entrySet()) {
        String key = entry.getKey();
        JsonElement val = entry.getValue();
        // 3 Possibilities
        // 1. structures
        // 2. array
        // 3. primitive

        if (val.isJsonObject() && nasted >= 1) {
            if (COMMA > 0) {
                struct += ",";
            }
            COMMA = 0;
            struct += fieldWrapper(key) + ":" + STRUCT + "<" + struct(val.toString(), 1) + ">";
        }
        // Array
        else if (val.isJsonArray()) {
            if (COMMA > 0) {
                struct += ",";
            }
            COMMA++;

            struct += fieldWrapper(key) + ":" + ARRAY + "<" + array(val.toString()) + ">";
        } else { // normal field
            if (COMMA > 0) {
                struct += ",";
            }
            COMMA++;

            if (val.isJsonNull()) {
                struct += fieldWrapper(key) + ":" + STRING;
            } else {
                struct += fieldWrapper(key) + ":" + findType(val.toString());
            }
        }
    }
    return struct;
}

From source file:io.thekraken.json2hive.Json.java

License:Apache License

private static HiveTable createHiveTable(String hiveTableName, String jsonObject) {
    HiveTable hive = new HiveTable(hiveTableName);

    JsonParser parser = new JsonParser();
    JsonElement jsonElement = parser.parse(jsonObject);
    JsonObject jsonObj = jsonElement.getAsJsonObject();

    for (Entry<String, JsonElement> entry : jsonObj.entrySet()) {
        String key = entry.getKey();
        JsonElement value = entry.getValue();

        /** Structure */
        if (value.isJsonObject()) {
            hive.addStructure(key, value.toString());
        }//from  w  w w . ja  v a 2 s . c  o  m
        /** Array */
        else if (value.isJsonArray()) {
            hive.addArray(key, value.toString());
        } else {
            /** primitive */
            if (value.isJsonNull()) {
                hive.AddUnknow(value.toString());
            } else {
                hive.addPrimitive(key, value.toString());
            }
        }
    }
    hive.close();
    return hive;
}

From source file:io.vertigo.ccc.console.JSonBeautifier.java

License:Apache License

private static void beautify(final StringBuilder sb, final JsonElement jsonElement, final int inOffset) {
    int offset = inOffset;
    if (jsonElement.isJsonArray()) {
        offset++;/*from  ww  w .  j  a  v a 2 s  .com*/
        for (Iterator<JsonElement> it = jsonElement.getAsJsonArray().iterator(); it.hasNext();) {
            //sb.append("- ");
            beautify(sb, it.next(), offset);
        }
        offset--;
    } else if (jsonElement.isJsonObject()) {
        for (Entry<String, JsonElement> entry : jsonElement.getAsJsonObject().entrySet()) {
            appendSpaces(sb, offset);
            sb.append(entry.getKey());
            sb.append(" : ");
            if (entry.getValue().isJsonPrimitive()) {
                sb.append(entry.getValue());
                appendCRLF(sb);
            } else {
                offset++;
                appendCRLF(sb);
                beautify(sb, entry.getValue(), offset);
                offset--;
            }
        }
    } else if (jsonElement.isJsonPrimitive()) {
        //               appendSpaces(sb, offset);
        String s = jsonElement.toString();
        if (s.startsWith("\"") && s.endsWith("\"")) {
            sb.append(s.substring(1, s.length() - 1));
        } else {
            sb.append(s);
        }
        appendCRLF(sb);
    } else {
        sb.append("???");
        //null
    }
}

From source file:io.vertigo.shell.util.JSonBeautifier.java

License:Apache License

private static void beautify(final StringBuilder sb, final JsonElement jsonElement, final int inOffset) {
    int offset = inOffset;
    if (jsonElement.isJsonArray()) {
        offset++;/*from  w  w w.jav  a  2 s .  c  o m*/
        for (final JsonElement jsonElement2 : jsonElement.getAsJsonArray()) {
            //sb.append("- ");
            beautify(sb, jsonElement2, offset);
        }
        offset--;
    } else if (jsonElement.isJsonObject()) {
        for (final Entry<String, JsonElement> entry : jsonElement.getAsJsonObject().entrySet()) {
            appendSpaces(sb, offset);
            sb.append(entry.getKey());
            sb.append(" : ");
            if (entry.getValue().isJsonPrimitive()) {
                sb.append(entry.getValue());
                appendCRLF(sb);
            } else {
                offset++;
                appendCRLF(sb);
                beautify(sb, entry.getValue(), offset);
                offset--;
            }
        }
    } else if (jsonElement.isJsonPrimitive()) {
        //               appendSpaces(sb, offset);
        final String s = jsonElement.toString();
        if (s.startsWith("\"") && s.endsWith("\"")) {
            sb.append(s.substring(1, s.length() - 1));
        } else {
            sb.append(s);
        }
        appendCRLF(sb);
    } else {
        sb.append("???");
        //null
    }
}

From source file:it.polimi.tower4clouds.model.ontology.Resource.java

License:Apache License

public static Set<Resource> fromJsonResources(String json) {
    Set<Resource> resources = new HashSet<Resource>();
    JsonArray jsonResources = jsonParser.parse(json).getAsJsonArray();
    for (JsonElement jsonElement : jsonResources) {
        resources.add(fromJsonResource(jsonElement.toString()));
    }//from w w w . j av a 2s. c o  m
    return resources;
}

From source file:jetbrick.web.mvc.result.GsonResultHandler.java

License:Open Source License

@Override
public void handle(RequestContext ctx, JsonElement result) throws IOException {
    HttpServletRequest request = ctx.getRequest();
    HttpServletResponse response = ctx.getResponse();

    if (result == null) {
        JsonObject json = new JsonObject();
        Enumeration<String> e = request.getAttributeNames();

        while (e.hasMoreElements()) {
            String name = e.nextElement();
            Object value = request.getAttribute(name);
            json.add(name, gson.toJsonTree(value));
        }/*from   w w w  . j  a v a  2 s. c  om*/

        for (Map.Entry<String, Object> entry : ctx.getModel().entrySet()) {
            String key = entry.getKey();
            Object value = entry.getValue();
            json.add(key, gson.toJsonTree(value));
        }

        result = json;
    }

    String characterEncoding = request.getCharacterEncoding();
    response.setCharacterEncoding(characterEncoding);

    String mimetype = MimetypeUtils.getJSON(request);
    response.setContentType(mimetype + "; charset=" + characterEncoding);

    // jsonp callback
    String callback = ctx.getParameter("callback");

    PrintWriter out = response.getWriter();
    if (callback != null) {
        out.write(callback + '(' + result.toString() + ')');
    } else {
        out.write(result.toString());
    }
    out.flush();
}