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); } }
@Override public Object readFromString(String string) throws IOException { return Json.parse(string); }
@Override public Object readFromReader(Reader reader) throws IOException { return Json.parse(reader); }
/** * Reads a JSON value from the given string. * * @param text * the string that contains the JSON value * @return the JSON value that has been read * @throws ParseException * if the input is not valid JSON * @deprecated Use {@link Json#parse(String)} instead */ @Deprecated public static JsonValue readFrom(String text) { return Json.parse(text); }
/** * Reads a JSON value from the given reader. * <p> * Characters are read in chunks and buffered internally, therefore wrapping an existing reader in * an additional <code>BufferedReader</code> does <strong>not</strong> improve reading * performance. * </p> * * @param reader * the reader to read the JSON value from * @return the JSON value that has been read * @throws IOException * if an I/O error occurs in the reader * @throws ParseException * if the input is not valid JSON * @deprecated Use {@link Json#parse(Reader)} instead */ @Deprecated public static JsonValue readFrom(Reader reader) throws IOException { return Json.parse(reader); }
private void createJsonFile() throws IOException { JsonObject caliperJson = Json.parse(readFromFile(resultsFile)).asObject(); String resultsJson = new CaliperResultsPreprocessor(caliperJson).getResults().toString(); writeToFile(resultsJson, resultsFile); }
/** * Creates a JSON unflattener. * * @param jsonReader * the JSON reader * @throws IOException * if jsonReader cannot be read */ public JsonUnflattener(Reader jsonReader) throws IOException { root = Json.parse(jsonReader); }
/** * Creates a JSON flattener. * * @param json * the JSON string */ public JsonFlattener(String json) { source = Json.parse(json); }
/** * Creates a JSON flattener. * * @param jsonReader * the JSON reader * @throws IOException * if jsonReader cannot be read */ public JsonFlattener(Reader jsonReader) throws IOException { source = Json.parse(jsonReader); }
/** * Creates a JSON flattener. * * @param json * the JSON string */ public JsonFlattener(String json) { source = Json.parse(json); }
/** * Creates a JSON unflattener. * * @param json * the JSON string */ public JsonUnflattener(String json) { root = Json.parse(json); }
/** * Creates a JSON unflattener. * * @param jsonReader * the JSON reader * @throws IOException * if jsonReader cannot be read */ public JsonUnflattener(Reader jsonReader) throws IOException { root = Json.parse(jsonReader); }
public static Config fromJson(String filename) throws Exception { return new Config( Json.parse( new FileReader(filename) ).asObject() ); }
public static Config fromJson(String filename) throws Exception { return new Config( Json.parse( new FileReader(filename) ).asObject() ); }
private static com.eclipsesource.json.JsonValue tryToRead(final String json) { try { return Json.parse(json); } catch (final ParseException | StackOverflowError e) { throw JsonParseException.newBuilder() .message(MessageFormat.format("Failed to parse ''{0}''!", json)) .cause(e) .build(); } }
@SuppressWarnings({"unchecked", "rawtypes"}) public FractionListParser(InputStream fractionListJson, InputStream packageSpecStream) throws IOException { try (InputStreamReader reader = new InputStreamReader(fractionListJson)) { Json.parse(reader).asArray().forEach(entry -> { JsonObject fraction = entry.asObject(); FractionDescriptor fd = getFractionDescriptor(fraction); addDependencies(fraction, fd); }); } packageSpecs.putAll((Map) PropertiesUtil.loadProperties(packageSpecStream)); }
@SuppressWarnings({"unchecked", "rawtypes"}) public FractionListParser(InputStream fractionListJson) throws IOException { try (InputStreamReader reader = new InputStreamReader(fractionListJson)) { Json.parse(reader).asArray().forEach(entry -> { JsonObject fraction = entry.asObject(); FractionDescriptor fd = getFractionDescriptor(fraction); addDependencies(fraction, fd); }); } }
private static com.eclipsesource.json.JsonArray tryToReadMinimalJsonArrayFrom(final String jsonString) { try { final com.eclipsesource.json.JsonValue parsedJsonString = Json.parse(jsonString); return parsedJsonString.asArray(); } catch (final ParseException | UnsupportedOperationException | StackOverflowError e) { throw JsonParseException.newBuilder() .message("Failed to create JSON array from string!") .cause(e) .build(); } }
private static com.eclipsesource.json.JsonObject tryToReadJsonObjectFrom(final String jsonString) { try { final com.eclipsesource.json.JsonValue parsedJsonString = Json.parse(jsonString); return parsedJsonString.asObject(); } catch (final ParseException | UnsupportedOperationException | StackOverflowError e) { throw JsonParseException.newBuilder() .message("Failed to create JSON object from string!") .cause(e) .build(); } }
@Test public void testPreRequisites() throws Exception { JsonObject object = Json.parse("{\"name\":\"test\"}").asObject(); String name = object.get("name").asString(); assertEquals("test", name); }