Skip to content

Commit

Permalink
Appendable in Printer
Browse files Browse the repository at this point in the history
  • Loading branch information
hamnis committed Sep 13, 2024
1 parent 30bbb45 commit 775a840
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 21 deletions.
10 changes: 10 additions & 0 deletions ast/src/main/java/net/hamnaberg/json/Json.java
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,16 @@ default String spaces4() {
default String pretty(PrettyPrinter p) {
return p.writeString(this);
}

/**
* Can emit directly to an Appendable without having to realize a String
*
* @param p the pretty printer
* @param appendable for instance a Writer instance
*/
default void writeTo(PrettyPrinter p, Appendable appendable) {
p.writeTo(this, appendable);
}
}

public sealed interface JScalarValue extends JValue permits JNull, JBoolean, JNumber, JString {
Expand Down
60 changes: 39 additions & 21 deletions ast/src/main/java/net/hamnaberg/json/PrettyPrinter.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package net.hamnaberg.json;


import java.io.IOException;
import java.util.Map;

public final class PrettyPrinter {
private final static int INDENT_LEVELS = 16;

private final int charsPerLevel;
private final boolean spaceafterColon;
private final boolean spaceAfterColon;
private final boolean dropNullKeys;

public PrettyPrinter(int charsPerLevel, boolean spaceAfterColon, boolean dropNullKeys) {
this.spaceafterColon = spaceAfterColon;
this.spaceAfterColon = spaceAfterColon;
this.charsPerLevel = charsPerLevel;
this.dropNullKeys = dropNullKeys;
}
Expand All @@ -33,38 +34,39 @@ public static PrettyPrinter spaces4() {
}

public PrettyPrinter dropNullKeys(boolean choice) {
return new PrettyPrinter(charsPerLevel, spaceafterColon, choice);
return new PrettyPrinter(charsPerLevel, spaceAfterColon, choice);
}

public String writeString(Json.JValue value) {
PrinterState state = new PrinterState();
StringBuilder sb = new StringBuilder();
writeTo(value, sb);
return sb.toString();
}

public void writeTo(Json.JValue value, Appendable appendable) {
PrinterState state = new PrinterState(appendable);
writeValue(value, state);
return state.toString();
}

private void writeValue(Json.JValue value, PrinterState state) {
value.foldUnit(new PrinterStateFolder(state, charsPerLevel));
}

public static class JsonWriteException extends RuntimeException {
public JsonWriteException(String message, Throwable cause) {
super(message, cause);
}
}

private class PrinterState {
private final Appendable appendable;
private int level = 0;
private final StringBuilder sb = new StringBuilder();

PrinterState append(String s) {
sb.append(s);
return this;
}

PrinterState append(char[] chars, int i, int length) {
sb.append(chars, i, length);
return this;
private PrinterState(Appendable appendable) {
this.appendable = appendable;
}

PrinterState append(boolean s) {
sb.append(s);
return this;
}

void levelUp() {
level++;
Expand All @@ -78,9 +80,25 @@ int getLevel() {
return level;
}

@Override
public String toString() {
return sb.toString();
PrinterState append(String s) {
try {
appendable.append(s);
} catch (IOException e) {
throw new JsonWriteException("Unable to append to writer", e);
}
return this;
}

void append(char[] chars, int i, int length) {
try {
appendable.append(new String(chars), i, length);
} catch (IOException e) {
throw new JsonWriteException("Unable to append to writer", e);
}
}

void append(boolean s) {
append(String.valueOf(s));
}
}

Expand Down Expand Up @@ -166,7 +184,7 @@ public void onObject(Json.JObject o) {

private void writeProperty(String name, Json.JValue value, PrinterState state) {
state.append(escape(name)).append(":");
if (spaceafterColon) {
if (spaceAfterColon) {
state.append(" ");
}
writeValue(value, state);
Expand Down

0 comments on commit 775a840

Please sign in to comment.