List of usage examples for com.google.gson JsonParser JsonParser
@Deprecated
public JsonParser()
From source file:com.ca.dvs.utilities.raml.JsonUtil.java
License:Open Source License
/** * Get a JSON representation of the Raml example, whether it is inline or included. * <p>//from w w w . j a va 2 s .co m * @param example the Raml example text * @param resourceDir a folder to search for imported Raml elements * @return Raml example data in JSON format */ public static String getJsonFromExample(String example, File resourceDir) { String json = example; // Example is not JSON. Is it a string containing the path to a file? File includeFile = null; includeFile = resourceDir == null ? new File(example.trim()) : new File(resourceDir, example.trim()); logger.debug(String.format("Potential include file: %s", includeFile.getAbsolutePath())); String includedContent = null; if (includeFile.canRead()) { // First check to see if example is actually an existing file path (RAML parser include bug workaround) Scanner scanner = null; try { scanner = new Scanner(includeFile); includedContent = scanner.useDelimiter("\\Z").next(); JsonParser parser = new JsonParser(); try { parser.parse(includedContent); json = includedContent; } catch (JsonSyntaxException jse) { String msg = String.format("Processing included example %s - %s\nContent follows...\n%s", includeFile.getPath(), jse.getMessage(), includedContent); throw new JsonSyntaxException(msg, jse.getCause()); } } catch (FileNotFoundException e1) { // If resolving the example content as a file lands us here, it's apparently not a file // validate the content by parsing as JSON try { JsonParser parser = new JsonParser(); parser.parse(example); // If we get this far without an exception, the example is truly JSON json = example; } catch (JsonSyntaxException e) { String msg = String.format("Processing example - %s\nContent follows...\n%s", e.getMessage(), example); throw new JsonSyntaxException(msg, e.getCause()); } } finally { if (null != scanner) { scanner.close(); } } } else { try { JsonParser parser = new JsonParser(); parser.parse(example); // If we get this far without an exception, the example is truly JSON json = example; } catch (JsonSyntaxException e) { String msg = String.format("Processing example - %s\nContent follows...\n%s", e.getMessage(), example); throw new JsonSyntaxException(msg, e.getCause()); } } return json; }
From source file:com.ca.dvs.utilities.raml.RamlUtil.java
License:Open Source License
/** * @param jsonString//from w ww. j av a2s. c o m * @return the appropriate JsonElement object from the parsed jsonString */ private static Object getJsonElementValue(String jsonString) { JsonParser jsonParser = new JsonParser(); JsonElement jsonElement = jsonParser.parse(jsonString); if (jsonElement.isJsonObject()) { return jsonElement.getAsJsonObject(); } else if (jsonElement.isJsonArray()) { return jsonElement.getAsJsonArray(); } else if (jsonElement.isJsonNull()) { return jsonElement.getAsJsonNull(); } else if (jsonElement.isJsonPrimitive()) { return jsonElement.getAsJsonPrimitive(); } else { return null; } }
From source file:com.caihan.scframe.utils.json.gson.GsonConvertUtils.java
License:Apache License
public static String formatJson(String json) { try {//from w w w .j av a 2 s . com JsonParser jp = new JsonParser(); JsonElement je = jp.parse(json); return new GsonBuilder().setPrettyPrinting() .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES) .registerTypeAdapter(Date.class, new DateTypeAdapter()).create().toJson(je); } catch (Exception e) { return json; } }
From source file:com.caihan.scframe.utils.json.gson.GsonConvertUtils.java
License:Apache License
public static String formatJson(Object src) { try {/*from w w w .j av a 2s .com*/ JsonParser jp = new JsonParser(); JsonElement je = jp.parse(toJson(src)); return new GsonBuilder().setPrettyPrinting() .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES) .registerTypeAdapter(Date.class, new DateTypeAdapter()).create().toJson(je); } catch (Exception e) { return e.getMessage(); } }
From source file:com.canoo.dolphin.impl.codec.OptimizedJsonCodec.java
License:Apache License
@Override public List<Command> decode(String transmitted) { Assert.requireNonNull(transmitted, "transmitted"); LOG.trace("Decoding message: {}", transmitted); try {//from ww w. j a v a2 s . c om final List<Command> commands = new ArrayList<>(); final JsonArray array = (JsonArray) new JsonParser().parse(transmitted); for (final JsonElement jsonElement : array) { final JsonObject command = (JsonObject) jsonElement; JsonPrimitive idPrimitive = command.getAsJsonPrimitive("id"); String id = null; if (idPrimitive != null) { id = idPrimitive.getAsString(); } LOG.trace("Decoding command: {}", id); CommandEncoder<?> encoder = null; if (id != null) { encoder = DECODERS.get(id); } if (encoder != null) { commands.add(encoder.decode(command)); } else { commands.addAll(fallBack.decode("[" + command.toString() + "]")); } } LOG.trace("Decoded command list with {} commands", commands.size()); return commands; } catch (ClassCastException | NullPointerException ex) { throw new JsonParseException("Illegal JSON detected", ex); } }
From source file:com.ccc.crest.core.client.CrestClient.java
License:Open Source License
public static String prettyPrintJson(String ugly) { Gson gson = new GsonBuilder().setPrettyPrinting().create(); JsonParser jp = new JsonParser(); JsonElement je = jp.parse(ugly);//from www . j a va 2s.c o m return gson.toJson(je); }
From source file:com.ccreanga.bitbucket.rest.client.http.BitBucketClient.java
License:Apache License
private Optional<JsonElement> getJsonElement(HttpResponse response, String responseString) { try {//from w ww . j ava 2 s.co m return responseString == null ? Optional.empty() : Optional.of(new JsonParser().parse(responseString)); } catch (JsonSyntaxException e) { throw createStashRestException(response, BitBucketException.toErrors("Failed to parse response: " + e.getMessage()), responseString); } }
From source file:com.chiwanpark.flume.plugins.handler.JSONHandler.java
License:Apache License
/** * {@inheritDoc}// w w w . j a v a 2 s.co m */ public JSONHandler(String charset) { super(charset); //UTF-8 is default for JSON. If no charset is specified, UTF-8 is to be assumed. if (charset == null) { LOG.debug("Charset is null, default charset of UTF-8 will be used."); this.charset = "UTF-8"; } else if (!(charset.equalsIgnoreCase("utf-8") || charset.equalsIgnoreCase("utf-16") || charset.equalsIgnoreCase("utf-32"))) { LOG.error("Unsupported character set in request {}. JSON handler supports UTF-8, " + "UTF-16 and UTF-32 only.", charset); throw new UnsupportedCharsetException("JSON handler supports UTF-8, UTF-16 and UTF-32 only."); } parser = new JsonParser(); gson = new Gson(); }
From source file:com.chiwanpark.flume.plugins.JSONHandler.java
License:Apache License
/** * {@inheritDoc}//from w w w. java 2 s .c o m */ public JSONHandler(String charset) { super(charset); //UTF-8 is default for JSON. If no charset is specified, UTF-8 is to //be assumed. if (charset == null) { LOG.debug("Charset is null, default charset of UTF-8 will be used."); charset = "UTF-8"; } else if (!(charset.equalsIgnoreCase("utf-8") || charset.equalsIgnoreCase("utf-16") || charset.equalsIgnoreCase("utf-32"))) { LOG.error("Unsupported character set in request {}. " + "JSON handler supports UTF-8, " + "UTF-16 and UTF-32 only.", charset); throw new UnsupportedCharsetException("JSON handler supports UTF-8, " + "UTF-16 and UTF-32 only."); } parser = new JsonParser(); }
From source file:com.cinchapi.concourse.server.plugin.PluginManager.java
License:Apache License
/** * Install the plugin bundle located within a zip file to the {@link #home} * directory./*w ww . j a v a2 s . com*/ * * @param bundle the path to the plugin bundle */ public void installBundle(String bundle) { String basename = com.google.common.io.Files.getNameWithoutExtension(bundle); String name = null; try { String manifest = ZipFiles.getEntryContent(bundle, basename + File.separator + MANIFEST_FILE); JsonObject json = (JsonObject) new JsonParser().parse(manifest); name = json.get("bundleName").getAsString(); ZipFiles.unzip(bundle, home); File src = new File(home + File.separator + basename); File dest = new File(home + File.separator + name); src.renameTo(dest); Logger.info("Installed the plugins in {} at {}", bundle, dest.getAbsolutePath()); activate(name); } catch (Exception e) { Throwable cause = null; if ((cause = e.getCause()) != null && cause instanceof ZipException) { throw new RuntimeException(bundle + " is not a valid plugin bundle"); } else { if (name != null) { // Likely indicates that there was a problem with // activation, so run uninstall path so things are not in a // weird state uninstallBundle(name); } throw e; // re-throw exception so CLI fails } } }