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:org.eclipse.che.api.core.jsonrpc.impl.GsonJsonRpcComposer.java

License:Open Source License

private <T> T composeOne(Class<T> type, Object paramObject) {
    if (paramObject instanceof JsonElement) {
        JsonElement jsonElement = (JsonElement) paramObject;
        return DtoFactory.getInstance().createDtoFromJson(jsonElement.toString(), type);
    }/*from   w  w w  . jav  a 2 s .com*/

    return cast(paramObject);
}

From source file:org.eclipse.che.api.core.jsonrpc.impl.GsonJsonRpcUnmarshaller.java

License:Open Source License

private List<String> getArray(String message, boolean isArray) {
    if (!isArray) {
        return singletonList(message);
    }/*  w w w. j  a  v  a 2s  .  c om*/

    JsonArray jsonArray = jsonParser.parse(message).getAsJsonArray();
    int size = jsonArray.size();
    List<String> result = new ArrayList<>(size);
    for (int i = 0; i < size; i++) {
        JsonElement jsonElement = jsonArray.get(i);
        result.add(jsonElement.toString());
    }
    return result;
}

From source file:org.eclipse.che.api.core.jsonrpc.impl.RequestDispatcher.java

License:Open Source License

/**
 * Dispatches json rpc request received from endpoint identified by a high
 * level identifier and represented as a json object.
 *
 * @param endpointId/* w w w  .  j  a v a 2s .  c  om*/
 *         high level endpoint identifier
 * @param incomingJson
 *         json object
 */
public void dispatch(String endpointId, JsonObject incomingJson) {
    LOG.debug("Dispatching incoming request from: " + endpointId + ", json: " + incomingJson);

    final String id = incomingJson.get("id").getAsString();
    LOG.debug("Extracted request id: " + id);

    final String method = incomingJson.get("method").getAsString();
    LOG.debug("Extracted request method: " + method);

    final RequestHandler handler = handlers.get(method);
    if (handler == null) {
        LOG.error("Handler not found: " + method);
        // TODO make a centralized standard errors structure
        transmitter.transmit(endpointId, error(-32601, "Method not found: " + method));
        return;
    }

    final Class resultClass = handler.getResultClass();
    LOG.debug("Extracted request result class: " + resultClass);

    JsonElement result;
    if (incomingJson.has("params")) {
        final JsonObject params = incomingJson.get("params").getAsJsonObject();
        LOG.debug("Request is parametrized, processing parameters: " + params);

        final Class paramsClass = handler.getParamsClass();
        LOG.debug("Extracted request params class: " + paramsClass);
        result = response(endpointId, handler, params, paramsClass, resultClass);
    } else {
        LOG.debug("Request is not parametrized.");
        result = response(endpointId, handler, null, null, resultClass);
    }

    final JsonElement response = prepareResponse(id, result);
    LOG.debug("Generated response: " + response);

    transmitter.transmit(endpointId, response.toString());
}

From source file:org.eclipse.che.api.core.jsonrpc.JsonRpcUtils.java

License:Open Source License

static <T> T getAs(JsonElement element, Class<T> type) {
    if (type.equals(String.class)) {
        return cast(element.getAsString());
    } else if (type.equals(Double.class)) {
        return cast(element.getAsDouble());
    } else if (type.equals(Boolean.class)) {
        return cast(element.getAsBoolean());
    } else if (type.equals(Void.class)) {
        return null;
    } else {/* ww  w . j a  va2  s .co m*/
        return DtoFactory.getInstance().createDtoFromJson(element.toString(), type);
    }
}

From source file:org.eclipse.che.api.workspace.server.stack.StackMessageBodyAdapter.java

License:Open Source License

@Override
protected String adapt(String body) {
    if (!isOldFormat(body)) {
        return body;
    }/*from ww w.  ja v a 2s.c o m*/
    final JsonElement stackEl = new JsonParser().parse(body);
    if (!stackEl.isJsonObject()) {
        return body;
    }
    stackJsonAdapter.adaptModifying(stackEl.getAsJsonObject());
    return stackEl.toString();
}

From source file:org.eclipse.che.api.workspace.server.WorkspaceConfigMessageBodyAdapter.java

License:Open Source License

/** Adapts a string to a string instead of adapting streams */
protected String adapt(String body) {
    if (!isOldFormat(body)) {
        return body;
    }//  w w w  .ja  v  a2  s .  c  o  m
    final JsonParser parser = new JsonParser();
    final JsonElement rootEl = parser.parse(body);
    if (!rootEl.isJsonObject()) {
        return body;
    }
    final JsonObject workspaceConfObj = getWorkspaceConfigObj(rootEl.getAsJsonObject());
    if (workspaceConfObj == null) {
        return body;
    }
    configAdapter.adaptModifying(getWorkspaceConfigObj(rootEl.getAsJsonObject()));
    return rootEl.toString();
}

From source file:org.eclipse.che.selenium.core.client.keycloak.OsioKeycloakTestAuthServiceClient.java

License:Open Source License

@Override
protected KeycloakToken refreshRequest(KeycloakToken token) {
    KeycloakToken newToken = null;//  w  w w .jav  a 2s. c o m
    HttpURLConnection http = null;
    try {
        http = (HttpURLConnection) new URL(osioAuthEndpoint + "/api/token/refresh").openConnection();
        http.setRequestMethod(HttpMethod.POST);
        http.setAllowUserInteraction(false);
        http.setInstanceFollowRedirects(true);
        http.setRequestProperty("Content-Type", "application/json");
        http.setDoOutput(true);
        OutputStream output = http.getOutputStream();
        output.write(String.format(REFRESH_TOKEN_TEMPLATE, token.getRefreshToken()).getBytes(UTF_8));
        output.close();
        if (http.getResponseCode() != 200) {
            throw new RuntimeException(
                    "Cannot get access token using the KeyCloak REST API. Server response code: "
                            + osioAuthEndpoint + " " + http.getResponseCode()
                            + IoUtil.readStream(http.getErrorStream()));
        }

        // request was ok. Obtain token from inputStream. Response is in format
        // {"token":{"active_token":"<token>", ...}} Need to get rid of the root "token" element.

        final BufferedReader response = new BufferedReader(new InputStreamReader(http.getInputStream(), UTF_8));
        String responseString = IOUtils.toString(response);
        JsonParser parser = new JsonParser();
        JsonObject parse = parser.parse(responseString).getAsJsonObject();
        JsonElement jsonElement = parse.get("token");

        newToken = readerToKeycloakToken(new StringReader(jsonElement.toString()));

    } catch (IOException e) {
        LOG.error(e.getMessage(), e);
    } finally {
        if (http != null) {
            http.disconnect();
        }
    }

    return newToken;
}

From source file:org.edgexfoundry.device.virtual.handler.VirtualHandler.java

License:Apache License

private String parseArg(String arguments, ResourceOperation operation, PropertyValue value, String object) {
    // parse the argument string and get the "value" parameter
    JsonObject args;/*from  w  w  w .  jav a2  s . c o  m*/
    String val = null;
    JsonElement jElem = null;
    Boolean passed = true;

    // check for parameters from the command
    if (arguments != null) {
        args = new JsonParser().parse(arguments).getAsJsonObject();
        jElem = args.get(object);
    }

    // if the parameter is passed from the command, use it, otherwise treat parameter as the default
    if (jElem == null || jElem.toString().equals("null")) {
        val = operation.getParameter();
        passed = false;
    } else {
        val = jElem.toString().replace("\"", "");
    }

    // if no value is specified by argument or parameter, take the object default from the profile
    if (val == null) {
        val = value.getDefaultValue();
        passed = false;
    }

    // if a mapping translation has been specified in the profile, use it
    Map<String, String> mappings = operation.getMappings();
    if (mappings != null && mappings.containsKey(val)) {
        val = mappings.get(val);
        passed = false;
    }

    if (!value.mask().equals(BigInteger.ZERO) && passed) {
        val = transform.format(value, val);
    }

    return val;
}

From source file:org.edgexfoundry.handler.ProtocolHandler.java

License:Apache License

private String parseArg(String arguments, ResourceOperation operation, PropertyValue value, String object) {
    // parse the argument string and get the "value" parameter
    JsonObject args;//  w  w w.j  a  v a 2s  .c om
    String val = null;
    JsonElement jsonElem = null;
    Boolean passed = true;

    // check for parameters from the command
    if (arguments != null) {
        args = new JsonParser().parse(arguments).getAsJsonObject();
        jsonElem = args.get(object);
    }

    // if the parameter is passed from the command, use it, otherwise treat parameter
    // as the default
    if (jsonElem == null || jsonElem.toString().equals("null")) {
        val = operation.getParameter();
        passed = false;
    } else {
        val = jsonElem.toString().replace("\"", "");
    }

    // if no value is specified by argument or parameter, take the object default from the profile
    if (val == null) {
        val = value.getDefaultValue();
        passed = false;
    }

    // if a mapping translation has been specified in the profile, use it
    Map<String, String> mappings = operation.getMappings();
    if (mappings != null && mappings.containsKey(val)) {
        val = mappings.get(val);
        passed = false;
    }

    if (!value.mask().equals(BigInteger.ZERO) && passed) {
        val = transform.format(value, val);
    }

    return val;
}

From source file:org.esco.portlet.flashinfo.dao.impl.FlashInfoResourceGsonImpl.java

License:Apache License

private List<FlashInfo> getServiceInfos(String url) {
    if (log.isDebugEnabled()) {
        log.debug("Requesting Flash Infos on URL {}", url);
    }//ww w . j  a v  a2  s  .c om
    List<FlashInfo> flL = new ArrayList<>();
    JsonParser parser = new JsonParser();
    JsonElement element;
    try {
        element = parser.parse(url);
    } catch (JsonSyntaxException e) {
        log.error("JsonSyntaxException parsing JSON Element at  :" + url + " with message : " + e.getMessage());
        return flL;
    }

    if (element != null && element.isJsonArray()) {
        final JsonArray flashInfos = element.getAsJsonArray();
        for (JsonElement elm : flashInfos) {
            final JsonObject dataset = elm.getAsJsonObject();
            // getting waited attributes
            final JsonElement jeMediaUrl = dataset.get("mediaUrl");
            final JsonElement jeTitle = dataset.get("title");
            final JsonElement jeSummary = dataset.get("summary");
            final JsonElement jeLink = dataset.get("link");
            // testing if all goes well
            if (jeMediaUrl != null && jeTitle != null && jeSummary != null && jeLink != null) {
                final String mediaUrl = jeMediaUrl.getAsString();
                final String title = jeTitle.getAsString();
                final String summary = jeSummary.getAsString();
                final String link = jeLink.getAsString();
                final String alt = title;
                final FlashInfo info = new FlashInfo(mediaUrl, title, summary, link, alt);
                flL.add(info);
            } else {
                log.warn(
                        "Problems on reading json element, needed attributes aren't found, check json source ! \nJsonElement is {}",
                        elm.toString());
            }
        }
    }
    return flL;
}