List of usage examples for com.google.gson JsonObject has
public boolean has(String memberName)
From source file:com.google.api.ads.adwords.awalerting.util.JdbcUtil.java
License:Open Source License
/** * Create a JdbcTemplate according to the parameters. * * @param dbConfig the Json configuration for database connection * @param driverKey the key for JDBC driver class name * @param urlKey the key for JDBC url//from w w w . j av a2 s.co m * @param usernameKey the key for JDBC username * @param passwordKey the key JDBC password * @return a JdbcTemplate with the given parameters */ public static JdbcTemplate createJdbcTemplate(JsonObject dbConfig, String driverKey, String urlKey, String usernameKey, String passwordKey) { // Read the config values. String driver = DEFAULT_DB_DRIVER; if (dbConfig.has(driverKey)) { driver = dbConfig.get(driverKey).getAsString(); } Preconditions.checkArgument(dbConfig.has(urlKey), "Missing compulsory property: %s", urlKey); String url = dbConfig.get(urlKey).getAsString(); String username = null; if (dbConfig.has(usernameKey)) { username = dbConfig.get(usernameKey).getAsString(); } String password = null; if (dbConfig.has(passwordKey)) { password = dbConfig.get(passwordKey).getAsString(); } // Create the data source. DriverManagerDataSource dataSource = new DriverManagerDataSource(); dataSource.setDriverClassName(driver); dataSource.setUrl(url); dataSource.setUsername(username); dataSource.setPassword(password); // Create the JdbcTemplate with the data srouce. return new JdbcTemplate(dataSource); }
From source file:com.google.code.stackexchange.client.impl.StackExchangeApiJsonClient.java
License:Apache License
protected <T> PagedList<T> unmarshallList(Class<T> clazz, InputStream jsonContent) { try {/*from www. ja v a2 s. co m*/ JsonElement response = parser.parse(new InputStreamReader(jsonContent, UTF_8_CHAR_SET)); if (response.isJsonObject()) { JsonObject adaptee = response.getAsJsonObject(); PagedList<T> list = new PagedArrayList<T>(); if (adaptee.has("total")) { list.setTotal(adaptee.get("total").getAsLong()); } if (adaptee.has("page")) { list.setPage(adaptee.get("page").getAsInt()); } if (adaptee.has("pagesize")) { list.setPageSize(adaptee.get("pagesize").getAsInt()); } String placeHolder = LIST_PLACE_HOLDERS.get(clazz); if (adaptee.has(placeHolder)) { JsonArray elements = adaptee.get(placeHolder).getAsJsonArray(); if (elements != null) { Gson gson = getGsonBuilder().create(); for (JsonElement o : elements) { list.add(gson.fromJson(o, clazz)); } } } return list; } throw new StackExchangeApiException("Unknown content found in response:" + response.toString()); } catch (Exception e) { throw new StackExchangeApiException(e); } }
From source file:com.google.code.stackexchange.client.query.impl.BaseStackOverflowApiQuery.java
License:Apache License
/** * @param <A>// w ww. ja v a 2s . c om * @param clazz * @param adaptee * @return */ protected <A> PagedList<A> unmarshallList(Class<A> clazz, JsonObject adaptee) { PagedList<A> list = new PagedArrayList<A>(); if (adaptee.has("total")) { list.setTotal(adaptee.get("total").getAsLong()); } if (adaptee.has("page")) { list.setPage(adaptee.get("page").getAsInt()); } if (adaptee.has("pagesize")) { list.setPageSize(adaptee.get("pagesize").getAsInt()); } String placeHolder = LIST_PLACE_HOLDERS.get(clazz); if (adaptee.has(placeHolder)) { JsonArray elements = adaptee.get(placeHolder).getAsJsonArray(); if (elements != null) { Gson gson = getGsonBuilder().create(); for (JsonElement o : elements) { list.add(gson.fromJson(o, clazz)); } } } return list; }
From source file:com.google.dart.server.internal.remote.processor.FindElementReferencesProcessor.java
License:Open Source License
public void process(JsonObject resultObject, RequestError requestError) { if (resultObject != null) { try {//from w w w . j a v a 2 s . c o m String searchId = resultObject.has("id") ? resultObject.get("id").getAsString() : null; Element element = resultObject.has("element") ? Element.fromJson(resultObject.get("element").getAsJsonObject()) : null; consumer.computedElementReferences(searchId, element); } catch (Exception exception) { // catch any exceptions in the formatting of this response requestError = generateRequestError(exception); } } if (requestError != null) { consumer.onError(requestError); } }
From source file:com.google.dart.server.internal.remote.processor.GetRefactoringProcessor.java
License:Open Source License
public void process(String requestId, JsonObject resultObject, RequestError requestError) { if (resultObject != null) { try {//w w w .j a va 2s . c o m // problems List<RefactoringProblem> initialProblems = getRefactoringProblems(resultObject, "initialProblems"); List<RefactoringProblem> optionsProblems = getRefactoringProblems(resultObject, "optionsProblems"); List<RefactoringProblem> finalProblems = getRefactoringProblems(resultObject, "finalProblems"); // change SourceChange change = null; if (resultObject.has("change")) { change = SourceChange.fromJson(resultObject.get("change").getAsJsonObject()); } // potential edits List<String> potentialEdits = JsonUtilities .decodeStringList(resultObject.get("potentialEdits") != null ? resultObject.get("potentialEdits").getAsJsonArray() : null); // // Compute all refactoring-kind specific "Feedback" and put them into the feedback map // RefactoringFeedback feedback = null; if (resultObject.has("feedback")) { JsonObject feedbackObject = resultObject.get("feedback").getAsJsonObject(); String kind = requestToRefactoringKindMap.remove(requestId); if (RefactoringKind.EXTRACT_LOCAL_VARIABLE.equals(kind)) { feedback = ExtractLocalVariableFeedback.fromJson(feedbackObject); } else if (RefactoringKind.EXTRACT_METHOD.equals(kind)) { feedback = ExtractMethodFeedback.fromJson(feedbackObject); } else if (RefactoringKind.INLINE_LOCAL_VARIABLE.equals(kind)) { feedback = InlineLocalVariableFeedback.fromJson(feedbackObject); } else if (RefactoringKind.INLINE_METHOD.equals(kind)) { feedback = InlineMethodFeedback.fromJson(feedbackObject); } else if (RefactoringKind.RENAME.equals(kind)) { feedback = RenameFeedback.fromJson(feedbackObject); } } consumer.computedRefactorings(initialProblems, optionsProblems, finalProblems, feedback, change, potentialEdits); } catch (Exception exception) { // catch any exceptions in the formatting of this response requestError = generateRequestError(exception); } } if (requestError != null) { consumer.onError(requestError); } }
From source file:com.google.debugging.sourcemap.SourceMapConsumerV3.java
License:Apache License
/** * Parses the given contents containing a source map. */// w w w. ja v a2 s . com public void parse(JsonObject sourceMapRoot, SourceMapSupplier sectionSupplier) throws SourceMapParseException { try { // Check basic assertions about the format. int version = sourceMapRoot.get("version").getAsInt(); if (version != 3) { throw new SourceMapParseException("Unknown version: " + version); } if (sourceMapRoot.has("file") && sourceMapRoot.get("file").getAsString().isEmpty()) { throw new SourceMapParseException("File entry is empty"); } if (sourceMapRoot.has("sections")) { // Looks like a index map, try to parse it that way. parseMetaMap(sourceMapRoot, sectionSupplier); return; } lineCount = sourceMapRoot.has("lineCount") ? sourceMapRoot.get("lineCount").getAsInt() : -1; String lineMap = sourceMapRoot.get("mappings").getAsString(); sources = getJavaStringArray(sourceMapRoot.get("sources").getAsJsonArray()); names = getJavaStringArray(sourceMapRoot.get("names").getAsJsonArray()); if (lineCount >= 0) { lines = new ArrayList<>(lineCount); } else { lines = new ArrayList<>(); } if (sourceMapRoot.has("sourceRoot")) { sourceRoot = sourceMapRoot.get("sourceRoot").getAsString(); } for (Map.Entry<String, JsonElement> entry : sourceMapRoot.entrySet()) { if (entry.getKey().startsWith("x_")) { extensions.put(entry.getKey(), entry.getValue()); } } new MappingBuilder(lineMap).build(); } catch (JsonParseException ex) { throw new SourceMapParseException("JSON parse exception: " + ex); } }
From source file:com.google.debugging.sourcemap.SourceMapConsumerV3.java
License:Apache License
/** * @param sourceMapRoot//from w ww . j a va 2 s. co m * @throws SourceMapParseException */ private void parseMetaMap(JsonObject sourceMapRoot, SourceMapSupplier sectionSupplier) throws SourceMapParseException { if (sectionSupplier == null) { sectionSupplier = new DefaultSourceMapSupplier(); } try { // Check basic assertions about the format. int version = sourceMapRoot.get("version").getAsInt(); if (version != 3) { throw new SourceMapParseException("Unknown version: " + version); } String file = sourceMapRoot.get("file").getAsString(); if (file.isEmpty()) { throw new SourceMapParseException("File entry is missing or empty"); } if (sourceMapRoot.has("lineCount") || sourceMapRoot.has("mappings") || sourceMapRoot.has("sources") || sourceMapRoot.has("names")) { throw new SourceMapParseException("Invalid map format"); } SourceMapGeneratorV3 generator = new SourceMapGeneratorV3(); JsonArray sections = sourceMapRoot.get("sections").getAsJsonArray(); for (int i = 0, count = sections.size(); i < count; i++) { JsonObject section = sections.get(i).getAsJsonObject(); if (section.has("map") && section.has("url")) { throw new SourceMapParseException( "Invalid map format: section may not have both 'map' and 'url'"); } JsonObject offset = section.get("offset").getAsJsonObject(); int line = offset.get("line").getAsInt(); int column = offset.get("column").getAsInt(); String mapSectionContents; if (section.has("url")) { String url = section.get("url").getAsString(); mapSectionContents = sectionSupplier.getSourceMap(url); if (mapSectionContents == null) { throw new SourceMapParseException("Unable to retrieve: " + url); } } else if (section.has("map")) { mapSectionContents = section.get("map").toString(); } else { throw new SourceMapParseException( "Invalid map format: section must have either 'map' or 'url'"); } generator.mergeMapSection(line, column, mapSectionContents); } StringBuilder sb = new StringBuilder(); try { generator.appendTo(sb, file); } catch (IOException e) { // Can't happen. throw new RuntimeException(e); } parse(sb.toString()); } catch (IOException ex) { throw new SourceMapParseException("IO exception: " + ex); } catch (JsonParseException ex) { throw new SourceMapParseException("JSON parse exception: " + ex); } }
From source file:com.google.debugging.sourcemap.SourceMapObject.java
License:Apache License
/** * Construct a new {@link SourceMapObject} from the source JSON. *//*from w w w . j a va2 s.com*/ public SourceMapObject(String contents) throws SourceMapParseException { try { JsonObject sourceMapRoot = new Gson().fromJson(contents, JsonObject.class); version = sourceMapRoot.get("version").getAsInt(); file = getStringOrNull(sourceMapRoot, "file"); lineCount = sourceMapRoot.has("lineCount") ? sourceMapRoot.get("lineCount").getAsInt() : -1; mappings = getStringOrNull(sourceMapRoot, "mappings"); sourceRoot = getStringOrNull(sourceMapRoot, "sourceRoot"); if (sourceMapRoot.has("sections")) { ImmutableList.Builder<SourceMapSection> builder = ImmutableList.builder(); for (JsonElement each : sourceMapRoot.get("sections").getAsJsonArray()) { builder.add(buildSection(each.getAsJsonObject())); } sections = builder.build(); } else { sections = null; } sources = getJavaStringArray(sourceMapRoot.get("sources")); names = getJavaStringArray(sourceMapRoot.get("names")); Map<String, Object> extensions = new LinkedHashMap<>(); for (Map.Entry<String, JsonElement> entry : sourceMapRoot.entrySet()) { if (entry.getKey().startsWith("x_")) { extensions.put(entry.getKey(), entry.getValue()); } } this.extensions = Collections.unmodifiableMap(extensions); } catch (JsonParseException ex) { throw new SourceMapParseException("JSON parse exception: " + ex); } }
From source file:com.google.debugging.sourcemap.SourceMapObject.java
License:Apache License
private static SourceMapSection buildSection(JsonObject section) throws JsonParseException, SourceMapParseException { JsonObject offset = section.get("offset").getAsJsonObject(); int line = offset.get("line").getAsInt(); int column = offset.get("column").getAsInt(); if (section.has("map") && section.has("url")) { throw new SourceMapParseException("Invalid map format: section may not have both 'map' and 'url'"); } else if (section.has("url")) { return SourceMapSection.forURL(section.get("url").getAsString(), line, column); } else if (section.has("map")) { return SourceMapSection.forMap(section.get("map").toString(), line, column); }// w ww . j av a2 s .co m throw new SourceMapParseException("Invalid map format: section must have either 'map' or 'url'"); }
From source file:com.google.debugging.sourcemap.SourceMapObject.java
License:Apache License
private static String getStringOrNull(JsonObject object, String key) { return object.has(key) ? object.get(key).getAsString() : null; }