/** * Writes the JSON representation of this value to the given writer in its minimal form, without * any additional whitespace. * <p> * Writing performance can be improved by using a {@link java.io.BufferedWriter BufferedWriter}. * </p> * * @param writer the writer to write this value to * @throws IOException if an I/O error occurs in the writer */ public void writeTo(Writer writer) throws IOException { writeTo(writer, Stringify.PLAIN); }
/** * Writes the JSON representation of this value to the given writer in its minimal form, without * any additional whitespace. * <p> * Writing performance can be improved by using a {@link java.io.BufferedWriter BufferedWriter}. * </p> * * @param writer the writer to write this value to * @throws IOException if an I/O error occurs in the writer */ public void writeTo(Writer writer) throws IOException { writeTo(writer, Stringify.PLAIN); }
/** * Returns the JSON/Hjson string for this value using the given formatting. * * @param format controls the formatting * @return a JSON/Hjson string that represents this value */ public String toString(Stringify format) { StringWriter writer=new StringWriter(); try { writeTo(writer, format); } catch(IOException exception) { // StringWriter does not throw IOExceptions throw new RuntimeException(exception); } return writer.toString(); }
/** * Returns the Hjson string for this value using the given formatting. * * @param options options for the Hjson format * @return a Hjson string that represents this value */ public String toString(HjsonOptions options) { StringWriter writer=new StringWriter(); try { writeTo(writer, options); } catch(IOException exception) { // StringWriter does not throw IOExceptions throw new RuntimeException(exception); } return writer.toString(); } /**
/** * Returns the Hjson string for this value using the given formatting. * * @param options options for the Hjson format * @return a Hjson string that represents this value */ public String toString(HjsonOptions options) { StringWriter writer=new StringWriter(); try { writeTo(writer, options); } catch(IOException exception) { // StringWriter does not throw IOExceptions throw new RuntimeException(exception); } return writer.toString(); } /**
/** * Returns the JSON/Hjson string for this value using the given formatting. * * @param format controls the formatting * @return a JSON/Hjson string that represents this value */ public String toString(Stringify format) { StringWriter writer=new StringWriter(); try { writeTo(writer, format); } catch(IOException exception) { // StringWriter does not throw IOExceptions throw new RuntimeException(exception); } return writer.toString(); }
@Override public void toJson(final File out, final T object) { try (StringWriter writer = new StringWriter()) { this.objectMapper.writer(this.prettyPrinter).writeValue(writer, object); JsonValue.readHjson(writer.toString()).writeTo(new BufferedWriter(new FileWriter(out))); } catch (final Exception e) { throw new IllegalArgumentException(e); } }
@Override public void toJson(final Writer out, final T object) { try (StringWriter writer = new StringWriter()) { this.objectMapper.writer(this.prettyPrinter).writeValue(writer, object); JsonValue.readHjson(writer.toString()).writeTo(out, Stringify.FORMATTED); } catch (final Exception e) { throw new IllegalArgumentException(e); } }
@Override @SneakyThrows public void to(final Writer out, final T object) { try (val writer = new StringWriter()) { this.objectMapper.writer(this.prettyPrinter).writeValue(writer, object); if (isJsonFormat()) { val opt = this.prettyPrinter instanceof MinimalPrettyPrinter ? Stringify.PLAIN : Stringify.FORMATTED; JsonValue.readHjson(writer.toString()).writeTo(out, opt); } else { IOUtils.write(writer.toString(), out); } } }
@Override @SneakyThrows public void to(final File out, final T object) { try (val writer = new StringWriter()) { this.objectMapper.writer(this.prettyPrinter).writeValue(writer, object); if (isJsonFormat()) { try (val fileWriter = Files.newBufferedWriter(out.toPath(), StandardCharsets.UTF_8)) { val opt = this.prettyPrinter instanceof MinimalPrettyPrinter ? Stringify.PLAIN : Stringify.FORMATTED; JsonValue.readHjson(writer.toString()).writeTo(fileWriter, opt); fileWriter.flush(); } } else { FileUtils.write(out, writer.toString(), StandardCharsets.UTF_8); } } }