List of usage examples for com.google.gson JsonParser JsonParser
@Deprecated
public JsonParser()
From source file:com.agateau.pixelwheels.stats.JsonGameStatsIO.java
License:Open Source License
@Override public void load() { Assert.check(mGameStats != null, "setGameStats() has not been called"); if (!mHandle.exists()) { return;/*from ww w . j a v a 2 s .com*/ } mGameStats.mTrackStats.clear(); String json = mHandle.readString("UTF-8"); JsonParser parser = new JsonParser(); JsonObject root = parser.parse(json).getAsJsonObject(); JsonObject trackStatsObject = root.getAsJsonObject("trackStats"); for (Map.Entry<String, JsonElement> kv : trackStatsObject.entrySet()) { String trackId = kv.getKey(); mGameStats.addTrack(trackId); loadTrackStats(mGameStats.getTrackStats(trackId), kv.getValue().getAsJsonObject()); } }
From source file:com.ai2020lab.aiutils.common.JsonUtils.java
License:Apache License
private static void init(ParserType type) { if (jsonParser == null) { jsonParser = new JsonParser(); }/*from w w w .j a v a 2s . co m*/ if (gson == null) { initGson(type); parserType = type; } else { if (parserType != type) { initGson(type); parserType = type; } } }
From source file:com.aliyun.odps.udf.utils.CounterUtils.java
License:Apache License
/** * JSON{@link Counters}/*from w ww. jav a 2 s. c o m*/ * {@link RuntimeException} * * @param json * JSON * @return {@link Counters} * @see #toJsonString(Counters) */ public static Counters createFromJsonString(String json) { JsonParser parser = new JsonParser(); JsonElement el = parser.parse(json); return createFromJson(el.getAsJsonObject()); }
From source file:com.amazonaws.apigatewaydemo.RequestRouter.java
License:Open Source License
/** * The main Lambda function handler. Receives the request as an input stream, parses the json and looks for the * "action" property to decide where to route the request. The "body" property of the incoming request is passed * to the DemoAction implementation as a request body. * * @param request The InputStream for the incoming event. This should contain an "action" and "body" properties. The * action property should contain the namespaced name of the class that should handle the invocation. * The class should implement the DemoAction interface. The body property should contain the full * request body for the action class. * @param response An OutputStream where the response returned by the action class is written * @param context The Lambda Context object * @throws BadRequestException This Exception is thrown whenever parameters are missing from the request or the action * class can't be found * @throws InternalErrorException This Exception is thrown when an internal error occurs, for example when the database * is not accessible *//*ww w . j a v a 2 s.c o m*/ public static void lambdaHandler(InputStream request, OutputStream response, Context context) throws BadRequestException, InternalErrorException { LambdaLogger logger = context.getLogger(); JsonParser parser = new JsonParser(); JsonObject inputObj; try { inputObj = parser.parse(IOUtils.toString(request)).getAsJsonObject(); } catch (IOException e) { logger.log("Error while reading request\n" + e.getMessage()); throw new InternalErrorException(e.getMessage()); } if (inputObj == null || inputObj.get("action") == null || inputObj.get("action").getAsString().trim().equals("")) { logger.log("Invald inputObj, could not find action parameter"); throw new BadRequestException("Could not find action value in request"); } String actionClass = inputObj.get("action").getAsString(); DemoAction action; try { action = DemoAction.class.cast(Class.forName(actionClass).newInstance()); } catch (final InstantiationException e) { logger.log("Error while instantiating action class\n" + e.getMessage()); throw new InternalErrorException(e.getMessage()); } catch (final IllegalAccessException e) { logger.log("Illegal access while instantiating action class\n" + e.getMessage()); throw new InternalErrorException(e.getMessage()); } catch (final ClassNotFoundException e) { logger.log("Action class could not be found\n" + e.getMessage()); throw new InternalErrorException(e.getMessage()); } if (action == null) { logger.log("Action class is null"); throw new BadRequestException("Invalid action class"); } JsonObject body = null; if (inputObj.get("body") != null) { body = inputObj.get("body").getAsJsonObject(); } String output = action.handle(body, context); try { IOUtils.write(output, response); } catch (final IOException e) { logger.log("Error while writing response\n" + e.getMessage()); throw new InternalErrorException(e.getMessage()); } }
From source file:com.amdocs.zusammen.utils.fileutils.json.JsonUtil.java
License:Apache License
public static boolean isValidJson(String json) { try {//from w w w .j a v a 2s . co m return new JsonParser().parse(json).isJsonObject(); } catch (JsonSyntaxException jse) { return false; } }
From source file:com.android.cloudfiles.cloudfilesforandroid.CloudFiles.java
License:Apache License
private void authentication(String userName, String apiKey) throws IOException { String json = generateToken(userName, apiKey); JsonParser jsonParser = new JsonParser(); JsonObject jo = (JsonObject) jsonParser.parse(json); setToken(jo.getAsJsonObject("access").getAsJsonObject("generateToken").get("id").getAsString()); JsonArray services = jo.getAsJsonObject("access").get("serviceCatalog").getAsJsonArray(); for (JsonElement jsonElement : services) { JsonObject jobj = jsonElement.getAsJsonObject(); if (jobj.get("name").getAsString().equals("cloudFiles")) { JsonArray endpoints = jobj.getAsJsonArray("endpoints"); for (JsonElement jsonElement2 : endpoints) { urls.put(jsonElement2.getAsJsonObject().get("region").getAsString(), jsonElement2.getAsJsonObject().get("publicURL").getAsString()); }/*from w w w . j a v a 2 s . com*/ } if (jobj.get("name").getAsString().equals("cloudFilesCDN")) { JsonArray endpoints = jobj.getAsJsonArray("endpoints"); for (JsonElement jsonElement2 : endpoints) { cdnUrls.put(jsonElement2.getAsJsonObject().get("region").getAsString(), jsonElement2.getAsJsonObject().get("publicURL").getAsString()); } } } }
From source file:com.android.tools.idea.editors.hierarchyview.HierarchyViewCaptureOptions.java
License:Apache License
public void parse(String json) { JsonObject obj = new JsonParser().parse(json).getAsJsonObject(); setVersion(obj.get(VERSION).getAsInt()); setTitle(obj.get(TITLE).getAsString()); }
From source file:com.android.tools.idea.gradle.structure.model.repositories.search.JCenterRepository.java
License:Apache License
SearchResult parse(@NotNull Reader response) { /*//from www. ja va 2s. c o m Sample response: [ { "name": "com.atlassian.guava:guava", "repo": "jcenter", "owner": "bintray", "desc": null, "system_ids": [ "com.atlassian.guava:guava" ], "versions": [ "15.0" ], "latest_version": "15.0" }, { "name": "com.atlassian.bundles:guava", "repo": "jcenter", "owner": "bintray", "desc": null, "system_ids": [ "com.atlassian.bundles:guava" ], "versions": [ "8.1", "8.0", "1.0-actually-8.1" ], "latest_version": "8.1" }, { "name": "io.janusproject.guava:guava", "repo": "jcenter", "owner": "bintray", "desc": null, "system_ids": [ "io.janusproject.guava:guava" ], "versions": [ "19.0.0", "17.0.2", "17.0" ], "latest_version": "19.0.0" }, { "name": "com.google.guava:guava", "repo": "jcenter", "owner": "bintray", "desc": "Guava is a suite of core and expanded libraries that include\n utility classes, google's collections, io classes, and much\n much more.\n\n Guava has two code dependencies - javax.annotation\n per the JSR-305 spec and javax.inject per the JSR-330 spec.", "system_ids": [ "com.google.guava:guava" ], "versions": [ "19.0", "19.0-rc3", "19.0-rc2", "19.0-rc1", "18.0", "18.0-rc2", "18.0-rc1", "11.0.2-atlassian-02", "17.0", "17.0-rc2", "17.0-rc1", "16.0.1", "16.0", "16.0-rc1", "15.0", "15.0-rc1", "14.0.1", "14.0", "14.0-rc3", "14.0-rc2", "14.0-rc1", "13.0.1", "13.0", "13.0-final", "13.0-rc2", "13.0-rc1", "12.0.1", "12.0", "12.0-rc2", "12.0-rc1", "11.0.2-atlassian-01", "11.0.2", "11.0.1", "11.0", "11.0-rc1", "10.0.1", "10.0", "10.0-rc3", "10.0-rc2", "10.0-rc1", "r09", "r08", "r07", "r06", "r05", "r03" ], "latest_version": "19.0" } ] */ JsonParser parser = new JsonParser(); JsonArray array = parser.parse(response).getAsJsonArray(); int totalFound = array.size(); List<FoundArtifact> artifacts = Lists.newArrayListWithExpectedSize(totalFound); for (int i = 0; i < totalFound; i++) { JsonObject root = array.get(i).getAsJsonObject(); String name = root.getAsJsonPrimitive("name").getAsString(); List<GradleVersion> availableVersions = Lists.newArrayList(); JsonArray versions = root.getAsJsonArray("versions"); versions.forEach(element -> { String version = element.getAsString(); availableVersions.add(GradleVersion.parse(version)); }); List<String> coordinate = Splitter.on(GRADLE_PATH_SEPARATOR).splitToList(name); assert coordinate.size() == 2; artifacts.add(new FoundArtifact(getName(), coordinate.get(0), coordinate.get(1), availableVersions)); } return new SearchResult(getName(), artifacts, totalFound); }
From source file:com.android.tools.lint.checks.AppLinksAutoVerifyDetector.java
License:Apache License
/** * Gets the Digital Asset Links JSON file on the website host. * * @param url The URL of the host on which JSON file will be fetched. *//* ww w.j a v a 2s .com*/ @NonNull private static HttpResult getJson(@NonNull String url) { try { URL urlObj = new URL(url); HttpURLConnection connection = (HttpURLConnection) urlObj.openConnection(); if (connection == null) { return new HttpResult(STATUS_HTTP_CONNECT_FAIL, null); } try { InputStream inputStream = connection.getInputStream(); if (inputStream == null) { return new HttpResult(connection.getResponseCode(), null); } try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, UTF_8))) { String line; StringBuilder response = new StringBuilder(); while ((line = reader.readLine()) != null) { response.append(line); response.append('\n'); } try { JsonElement jsonFile = new JsonParser().parse(response.toString()); return new HttpResult(connection.getResponseCode(), jsonFile); } catch (JsonSyntaxException e) { return new HttpResult(STATUS_WRONG_JSON_SYNTAX, null); } catch (RuntimeException e) { return new HttpResult(STATUS_JSON_PARSE_FAIL, null); } } } finally { connection.disconnect(); } } catch (MalformedURLException e) { return new HttpResult(STATUS_MALFORMED_URL, null); } catch (UnknownHostException e) { return new HttpResult(STATUS_UNKNOWN_HOST, null); } catch (FileNotFoundException e) { return new HttpResult(STATUS_NOT_FOUND, null); } catch (IOException e) { return new HttpResult(STATUS_HTTP_CONNECT_FAIL, null); } }
From source file:com.apcb.utils.utils.HtmlTemplateReader.java
public String process(Object dataInput) { sectionMatch = readJson(new JsonParser().parse(gson.toJson(dataInput)), "", new SectionMatch(dataInput.getClass().getSimpleName())); String result = processIterate(sectionMatch, sourcesFile.get("html")); result = clear(result);// ww w . jav a 2 s. co m return result; }