public static void main(String[] args) throws IOException { // convert Java to writer JsonObject root = Json.object().add("message", "Hi").add( "place", Json.object().add("name", "World!") ); StringWriter writer = new StringWriter(); root.writeTo(writer); String json = writer.toString(); System.out.println(json); System.out.println(); // convert writer to Java JsonObject obj = Json.parse(json).asObject(); String message = obj.get("message").asString(); String name = obj.get("place").asObject().get("name").asString(); System.out.println(message + " " + name); } }
private List<String> toStringArray(JsonArray array) { List<String> result = new ArrayList<>(); for (JsonValue value : array) { result.add(value.asString()); } return result; }
JsonObject json = JsonObject.readFrom(FileUtils.readFileToString(configJson)); if (json.get("platform") != null) { platform = Platform.byDesc(json.get("platform").asString()); if (json.get("jdk") != null) { jdk = json.get("jdk").asString(); if (json.get("executable") != null) { executable = json.get("executable").asString(); if (json.get("classpath") != null) { classpath = toStringArray(json.get("classpath").asArray()); if (json.get("removelibs") != null) { removePlatformLibs = toStringArray(json.get("removelibs").asArray()); if (json.get("mainclass") != null) { mainClass = json.get("mainclass").asString(); if (json.get("vmargs") != null) { List<String> vmArgs = toStringArray(json.get("vmargs").asArray()); this.vmArgs = new ArrayList<>(); for (String vmArg : vmArgs) { if (json.get("minimizejre") != null) { minimizeJre = json.get("minimizejre").asString(); if (json.get("cachejre") != null) {
JsonArray reduceArray = minimizeJson.get("reduce").asArray(); for (JsonValue reduce : reduceArray) { String path = reduce.asObject().get("archive").asString(); File file = new File(output, path); JsonArray removeArray = reduce.asObject().get("paths").asArray(); for (JsonValue remove : removeArray) { File removeFile = new File(fileNoExt, remove.asString()); if (removeFile.exists()) { if (removeFile.isDirectory()) { JsonArray removeArray = minimizeJson.get("remove").asArray(); for (JsonValue remove : removeArray) { String platform = remove.asObject().get("platform").asString(); JsonArray removeFilesArray = remove.asObject().get("paths").asArray(); for (JsonValue removeFile : removeFilesArray) { removeFileWildcard(output, removeFile.asString(), config);
private static JsonArray extractMeasurements(JsonObject caliperResults) { JsonArray result = new JsonArray(); JsonArray measurements = caliperResults.get("run").asObject().get("measurements").asArray(); for (JsonValue measurement : measurements) { result.add(extractMeasurement(measurement.asObject())); } return result; }
private void readFalse() throws IOException { handler.startBoolean(); read(); readRequiredChar('a'); readRequiredChar('l'); readRequiredChar('s'); readRequiredChar('e'); handler.endBoolean(false); }
/** * Returns the JSON string for this value in its minimal form, without any additional whitespace. * The result is guaranteed to be a valid input for the method {@link Json#parse(String)} and to * create a value that is <em>equal</em> to this object. * * @return a JSON string that represents this value */ @Override public String toString() { return toString(WriterConfig.MINIMAL); }
private void readNull() throws IOException { handler.startNull(); read(); readRequiredChar('u'); readRequiredChar('l'); readRequiredChar('l'); handler.endNull(); }
private static JsonObject readMinimizeProfile(PackrConfig config) throws IOException { JsonObject json = null; if (new File(config.minimizeJre).exists()) { json = JsonObject.readFrom(FileUtils.readFileToString(new File(config.minimizeJre))); } else { InputStream in = Packr.class.getResourceAsStream("/minimize/" + config.minimizeJre); if (in != null) { json = JsonObject.readFrom(new InputStreamReader(in)); } } if (json == null && config.verbose) { System.out.println(" # No minimize profile '" + config.minimizeJre + "' found"); } return json; }
private void readString() throws IOException { handler.startString(); handler.endString(readStringInternal()); }
private ParseException error(String message) { return new ParseException(message, getLocation()); }
/** * Returns a JsonValue instance that represents the given string. * * @param string * the string to get a JSON representation for * @return a JSON value that represents the given string */ public static JsonValue value(String string) { return string == null ? NULL : new JsonString(string); }
/** * Print every value on a separate line. Use tabs (<code>\t</code>) for indentation. * * @return A PrettyPrint instance for wrapped mode with tab indentation */ public static PrettyPrint singleLine() { return new PrettyPrint(null); }
private List<File> toFileArray(JsonArray array) { List<File> result = new ArrayList<>(); for (JsonValue value : array) { result.add(new File(value.asString())); } return result; }
private void readTrue() throws IOException { handler.startBoolean(); read(); readRequiredChar('r'); readRequiredChar('u'); readRequiredChar('e'); handler.endBoolean(true); }