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:com.nuvolect.securesuite.webserver.connector.base.ConnectorJsonCommand.java

License:Open Source License

protected InputStream getInputStream(@NonNull JsonElement jsonElement) {
    return new ByteArrayInputStream(jsonElement.toString().getBytes());
}

From source file:com.openyelp.client.JsonRpcInvoker.java

License:Apache License

private Object work(String handleName, JsonRpcClientTransport transport, Method method, Object[] args,
        String key) {/*from  w  w  w . java2s  .  c  om*/
    Gson gson = new Gson();
    String cachekey = handleName + method.getName() + getKey(args);
    String keyy = Utils.getMD5Str(cachekey);
    String responseData = null;
    if (cache != null) {
        responseData = cache.get(keyy);
        if (responseData == null) {
            responseData = c(handleName, transport, method, args, key, gson);
            cache.put(keyy, responseData);
        }
    } else {
        responseData = c(handleName, transport, method, args, key, gson);
    }
    if (responseData == null) {
        if (diskCache != null) {
            responseData = diskCache.get(keyy);
        }
    }
    if (responseData == null) {
        return null;
    }

    JsonParser parser = new JsonParser();
    JsonObject resp = (JsonObject) parser.parse(new StringReader(responseData));
    // int sid = resp.get("id").getAsInt();
    // if (id == sid) {
    // } else {
    // return null;
    // }
    JsonElement result = resp.get("result");
    JsonElement error = resp.get("error");
    /**
     * ?null
     */
    if (error != null && !error.isJsonNull()) {
        if (error.isJsonPrimitive()) {
            System.out.println("<<>>>>>" + error.getAsString());
        } else if (error.isJsonObject()) {
            JsonObject o = error.getAsJsonObject();
            Integer code = (o.has("code") ? o.get("code").getAsInt() : null);
            String message = (o.has("message") ? o.get("message").getAsString() : null);
            String data = (o.has("data") ? (o.get("data") instanceof JsonObject ? o.get("data").toString()
                    : o.get("data").getAsString()) : null);
            System.out.println(message + "<<>>>>>" + data);
        } else {
            System.out.println("<<>>>>>" + error.toString());
        }
        return null;
    }
    if (diskCache != null) {
        System.out.println("?");
        diskCache.put(keyy, responseData);
    }
    if (method.getReturnType() == void.class) {
        return null;
    }
    return gson.fromJson(result.toString(), method.getReturnType());
}

From source file:com.openyelp.server.JsonCacheRpcExecutor.java

License:Apache License

public Object[] getParameters(Method method, JsonArray params) {
    List<Object> list = new ArrayList<Object>();
    Gson gson = new Gson();
    Class<?>[] types = method.getParameterTypes();
    for (int i = 0; i < types.length; i++) {
        JsonElement p = params.get(i);
        Object o = gson.fromJson(p.toString(), types[i]);
        list.add(o);//from w  w  w  . ja v a  2s  .c o  m
    }
    return list.toArray();
}

From source file:com.orchestra.portale.profiler.FbProfiler.java

public List<String> getPoiStereotype() {
    if (access_token == null) {
        return null;
    }//  w  w w. ja  v a  2  s.c om

    try {

        HttpURLConnection connection = openConnection("/poi_stereotype", "GET");

        Map<String, String> params = new HashMap<String, String>();
        params.put("access_token", access_token);
        connection = addParameter(connection, params);

        String json_response = streamToString(connection.getInputStream());
        JsonParser parser = new JsonParser();
        JsonElement element = parser.parse(json_response);
        JsonObject j_object = (JsonObject) element;
        JsonArray j_poi_list = (JsonArray) j_object.get("poi_list");

        List<String> poi_list = new ArrayList<String>();

        for (JsonElement p : j_poi_list) {
            poi_list.add(p.toString().replace("\"", ""));
        }

        return poi_list;

    } catch (MalformedURLException ex) {
        ex.printStackTrace();
    } catch (IOException ioex) {
        ioex.printStackTrace();
    }

    return null;
}

From source file:com.photon.phresco.impl.HtmlApplicationProcessor.java

License:Apache License

private void writeJson(File sourceFolderLocation, List<JsonElement> compJsonElements, String environment,
        String type) throws PhrescoException {
    File jsonDir = new File(sourceFolderLocation + File.separator + "src/main/webapp/json");
    if (!jsonDir.exists()) {
        return;//from  ww w  .j a va2s  .com
    }

    if (CollectionUtils.isEmpty(compJsonElements)) {
        return;
    }

    File configFile = new File(getAppLevelConfigJson(sourceFolderLocation));

    JsonParser parser = new JsonParser();
    Gson gson = new GsonBuilder().setPrettyPrinting().create();
    JsonObject jsonObject = new JsonObject();
    JsonObject envObject = new JsonObject();
    if (!configFile.exists()) {
        jsonObject.addProperty(Constants.NAME, environment);
        StringBuilder sb = new StringBuilder();
        sb.append("{");
        for (JsonElement jsonElement : compJsonElements) {
            String jsonString = jsonElement.toString();
            sb.append(jsonString.substring(1, jsonString.length() - 1));
            sb.append(",");
        }
        String compConfig = sb.toString();
        compConfig = compConfig.substring(0, compConfig.length() - 1) + "}";
        jsonObject.add(type, parser.parse(compConfig));
        envObject.add(Constants.ENVIRONMENTS, parser.parse(Collections.singletonList(jsonObject).toString()));
    } else {
        FileReader reader = null;
        try {
            reader = new FileReader(configFile);
            Object obj = parser.parse(reader);
            envObject = (JsonObject) obj;
            JsonArray environments = (JsonArray) envObject.get(Constants.ENVIRONMENTS);
            jsonObject = getProductionEnv(environments, environment);
            JsonElement components = null;
            for (JsonElement compJsonElement : compJsonElements) {
                JsonObject allComponents = null;
                if (jsonObject == null) {
                    jsonObject = new JsonObject();
                    JsonElement jsonElement = envObject.get(Constants.ENVIRONMENTS);
                    String oldObj = jsonElement.toString().substring(1, jsonElement.toString().length() - 1)
                            .concat(",");
                    jsonObject.addProperty(Constants.NAME, environment);
                    jsonObject.add(type, compJsonElement);
                    String newObj = jsonObject.toString();
                    envObject.add(Constants.ENVIRONMENTS,
                            parser.parse(Collections.singletonList(oldObj + newObj).toString()));
                } else {
                    components = jsonObject.get(type);
                }
                if (components == null) {
                    jsonObject.add(type, compJsonElement);
                } else {
                    allComponents = components.getAsJsonObject();
                    Set<Entry<String, JsonElement>> entrySet = compJsonElement.getAsJsonObject().entrySet();
                    Entry<String, JsonElement> entry = entrySet.iterator().next();
                    String key = entry.getKey();
                    //                  if (allComponents.get(key) == null) {
                    allComponents.add(key, entry.getValue());
                    //                  }
                }
            }

        } catch (FileNotFoundException e) {
            throw new PhrescoException(e);
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    throw new PhrescoException(e);
                }
            }
        }
    }

    FileWriter writer = null;
    String json = gson.toJson(envObject);
    try {
        writer = new FileWriter(configFile);
        writer.write(json);
        writer.flush();
    } catch (IOException e) {
    } finally {
        try {
            if (writer != null) {
                writer.close();
            }
        } catch (IOException e) {
            throw new PhrescoException(e);
        }
    }
}

From source file:com.popdeem.sdk.uikit.fragment.PDUITagFriendsFragment.java

License:Open Source License

private void makeTaggableFriendsRequest() {
    Bundle parameters = new Bundle();
    parameters.putString("limit", "5000");
    GraphRequest taggableFriendsRequest = GraphRequest.newGraphPathRequest(AccessToken.getCurrentAccessToken(),
            "/me/taggable_friends", new GraphRequest.Callback() {
                @Override/*from  w  w  w  . j  av a  2  s.c o m*/
                public void onCompleted(GraphResponse graphResponse) {
                    FacebookRequestError error = graphResponse.getError();
                    if (error != null) {
                        //                            DialogUtils.showBasicDialog(ClaimActivity.this, error.getErrorUserTitle(), error.getErrorUserMessage(),
                        //                                    android.R.string.ok, 0, null, null);
                    } else {
                        JsonParser parser = new JsonParser();
                        JsonElement json = parser.parse(graphResponse.getJSONObject().toString());
                        if (json.getAsJsonObject().has("data")) {
                            Type type = new TypeToken<ArrayList<PDSocialMediaFriend>>() {
                            }.getType();

                            Gson gson = new GsonBuilder()
                                    .registerTypeAdapter(type, new PDSocialMediaFriendsDeserializer()).create();

                            ArrayList<PDSocialMediaFriend> friends = gson.fromJson(json, type);
                            updateTaggableFriends(friends);
                        }
                        PDLog.d(PDUITagFriendsFragment.class, "friends: " + json.toString());
                    }
                }
            });
    taggableFriendsRequest.setParameters(parameters);
    taggableFriendsRequest.executeAsync();
}

From source file:com.redhat.thermostat.gateway.service.wp.JVMParser.java

License:Open Source License

void parse(String contentResponse, String sourceURL, List<VM> result) throws Exception {

    JsonParser parser = new JsonParser();
    JsonObject json = (JsonObject) parser.parse(contentResponse);
    JsonElement response = json.get("response");

    JsonArray allData = response.getAsJsonArray();
    for (JsonElement entry : allData) {

        json = (JsonObject) parser.parse(entry.toString());
        if (!json.has("vmId") || !json.has("agentId")) {
            continue;
        }//from ww w  .  j  a  v a  2s .  c  o m

        String vmId = json.get("vmId").getAsString();
        String agentId = json.get("agentId").getAsString();

        result.add(new VM(vmId, agentId, sourceURL));
    }
}

From source file:com.replaymod.replaystudio.launcher.Launcher.java

License:MIT License

public void parseConfig(Studio studio, JsonObject root) {
    JsonArray instructions = root.getAsJsonArray("Instructions");
    for (JsonElement e : instructions) {
        JsonObject o = e.getAsJsonObject();
        Instruction instruction;/*from  w ww. j  a va 2 s .  com*/
        switch (o.get("Name").getAsString().toLowerCase()) {
        case "split":
            if (o.get("at").isJsonArray()) {
                List<Long> at = new ArrayList<>();
                Iterables.addAll(at,
                        Iterables.transform(o.getAsJsonArray("at"), (e1) -> timeStampToMillis(e1.toString())));
                instruction = new SplitInstruction(Longs.toArray(at));
            } else {
                instruction = new SplitInstruction(timeStampToMillis(o.get("at").toString()));
            }
            break;
        case "append":
            instruction = new AppendInstruction();
            break;
        case "squash":
            instruction = new SquashInstruction(studio);
            break;
        case "copy":
            instruction = new CopyInstruction();
            break;
        case "filter":
            Filter filter = studio.loadFilter(o.get("Filter").toString());
            instruction = new FilterInstruction(studio, filter, o.getAsJsonObject("Config"));
            break;
        default:
            System.out.println("Warning: Unrecognized instruction in json config: " + o.get("Name"));
            continue;
        }

        JsonElement inputs = o.get("Inputs");
        if (inputs.isJsonArray()) {
            for (JsonElement e1 : inputs.getAsJsonArray()) {
                instruction.getInputs().add(e1.getAsString());
            }
        } else {
            instruction.getInputs().add(inputs.getAsString());
        }

        JsonElement outputs = o.get("Outputs");
        if (outputs.isJsonArray()) {
            for (JsonElement e1 : outputs.getAsJsonArray()) {
                instruction.getOutputs().add(e1.getAsString());
            }
        } else {
            instruction.getOutputs().add(outputs.getAsString());
        }

        this.instructions.add(instruction);
    }

    // Get all inputs
    JsonObject inputs = root.getAsJsonObject("Inputs");
    for (Map.Entry<String, JsonElement> e : inputs.entrySet()) {
        this.inputs.put(e.getKey(), e.getValue().getAsString());
    }

    // Get all outputs
    JsonObject outputs = root.getAsJsonObject("Outputs");
    for (Map.Entry<String, JsonElement> e : outputs.entrySet()) {
        this.outputs.put(e.getKey(), e.getValue().getAsString());
    }

    // Calculate all pipes
    for (Instruction instruction : this.instructions) {
        pipes.addAll(instruction.getInputs());
        pipes.addAll(instruction.getOutputs());
    }
    pipes.removeAll(this.inputs.keySet());
    pipes.removeAll(this.outputs.keySet());
}

From source file:com.ryanantkowiak.jOptionsHouseAPI.IOhMsgRsp.java

License:Open Source License

public ErrorMap deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    return new ErrorMap(json.toString());
}

From source file:com.savoirtech.json.rules.impl.RegexMatchingRule.java

License:Apache License

private String getStringForComparison(JsonElement ele) {
    if (ele.isJsonPrimitive()) {
        return ele.getAsString();
    }/*  w  w  w.  j  a v  a2 s . co  m*/

    return ele.toString();
}