diff --git a/src/main/java/dev/latvian/apps/webutils/json/JSON.java b/src/main/java/dev/latvian/apps/webutils/json/JSON.java index 7de3066..0a51368 100644 --- a/src/main/java/dev/latvian/apps/webutils/json/JSON.java +++ b/src/main/java/dev/latvian/apps/webutils/json/JSON.java @@ -183,10 +183,20 @@ public String writePretty(@Nullable Object value) { return builder.toString(); } + public void write(Path path, Object value, boolean pretty) throws IOException { + if (Files.notExists(path.getParent())) { + Files.createDirectories(path.getParent()); + } + + Files.writeString(path, pretty ? writePretty(value) : write(value)); + } + public void write(Writer writer, @Nullable Object value, int depth, boolean pretty) throws IOException { if (depth > 1000) { throw new IllegalStateException("JSON depth limit reached"); - } else if (value == null || value == NULL) { + } + + if (value == null || value == NULL) { writer.write("null"); } else if (value instanceof JSONSerializable) { write(writer, ((JSONSerializable) value).toJSON(), depth, pretty); @@ -207,13 +217,34 @@ public void write(Writer writer, @Nullable Object value, int depth, boolean pret writer.write(','); } + if (pretty) { + writer.write('\n'); + + for (int i = 0; i <= depth; i++) { + writer.write('\t'); + } + } + writer.write('"'); escape(writer, String.valueOf(entry.getKey())); writer.write('"'); writer.write(':'); + + if (pretty) { + writer.write(' '); + } + write(writer, entry.getValue(), depth + 1, pretty); } + if (pretty) { + writer.write('\n'); + + for (int i = 0; i < depth; i++) { + writer.write('\t'); + } + } + writer.write('}'); } else if (value instanceof Iterable itr) { boolean first = true; @@ -226,9 +257,25 @@ public void write(Writer writer, @Nullable Object value, int depth, boolean pret writer.write(','); } + if (pretty) { + writer.write('\n'); + + for (int i = 0; i <= depth; i++) { + writer.write('\t'); + } + } + write(writer, o, depth + 1, pretty); } + if (pretty) { + writer.write('\n'); + + for (int i = 0; i < depth; i++) { + writer.write('\t'); + } + } + writer.write(']'); } else if (value instanceof Object[] arr) { write(writer, Arrays.asList(arr), depth, pretty); diff --git a/src/main/java/dev/latvian/apps/webutils/net/MimeType.java b/src/main/java/dev/latvian/apps/webutils/net/MimeType.java index 73a5b45..46664e6 100644 --- a/src/main/java/dev/latvian/apps/webutils/net/MimeType.java +++ b/src/main/java/dev/latvian/apps/webutils/net/MimeType.java @@ -12,4 +12,7 @@ public interface MimeType { String XML_TEXT = "text/xml; charset=utf-8"; String SVG = "image/svg+xml; charset=utf-8"; String CSS = "text/css; charset=utf-8"; + String PDF = "application/pdf"; + String ZIP = "application/zip"; + String JAR = "application/java-archive"; } diff --git a/src/test/java/dev/latvian/apps/webutils/test/JSONTests.java b/src/test/java/dev/latvian/apps/webutils/test/JSONTests.java index 27f2293..b7ed807 100644 --- a/src/test/java/dev/latvian/apps/webutils/test/JSONTests.java +++ b/src/test/java/dev/latvian/apps/webutils/test/JSONTests.java @@ -43,6 +43,7 @@ public void readRemoteObject() throws Exception { Log.info(content); var json = JSON.DEFAULT.read(content).readObject(); Log.info(Ansi.ofObject(json)); + Log.info(JSON.DEFAULT.writePretty(json)); Log.info(json.asInt("total_launches")); Log.info(json.asDouble("hourly"));