Skip to content

Commit

Permalink
Implemented pretty json
Browse files Browse the repository at this point in the history
  • Loading branch information
LatvianModder committed May 1, 2024
1 parent 2177949 commit 5bf6166
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
49 changes: 48 additions & 1 deletion src/main/java/dev/latvian/apps/webutils/json/JSON.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
Expand All @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/dev/latvian/apps/webutils/net/MimeType.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand Down

0 comments on commit 5bf6166

Please sign in to comment.