Skip to content

Commit

Permalink
Customize indent depth
Browse files Browse the repository at this point in the history
  • Loading branch information
bowbahdoe committed Nov 12, 2022
1 parent 0c6ff72 commit 0452434
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 29 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>dev.mccue</groupId>
<artifactId>json</artifactId>
<version>0.0.5</version>
<version>0.0.6</version>
<packaging>jar</packaging>

<properties>
Expand Down
35 changes: 16 additions & 19 deletions src/main/java/dev/mccue/json/Json.java
Original file line number Diff line number Diff line change
Expand Up @@ -278,32 +278,37 @@ record WriteOptions(
boolean escapeUnicode,
boolean escapeJavascriptSeparators,
boolean escapeSlash,
boolean indent
int indentation
) {
public WriteOptions {
if (indentation < 0) {
throw new IllegalArgumentException("indent must not be less than zero.");
}
}
public WriteOptions() {
this(true, true, true, false);
this(true, true, true, 0);
}

public WriteOptions withEscapeUnicode(boolean escapeUnicode) {
return new WriteOptions(escapeUnicode, escapeJavascriptSeparators, escapeSlash, indent);
return new WriteOptions(escapeUnicode, escapeJavascriptSeparators, escapeSlash, indentation);
}

public WriteOptions withEscapeJavascriptSeparators(boolean escapeJavascriptSeparators) {
return new WriteOptions(escapeUnicode, escapeJavascriptSeparators, escapeSlash, indent);
return new WriteOptions(escapeUnicode, escapeJavascriptSeparators, escapeSlash, indentation);
}

public WriteOptions withEscapeSlash(boolean escapeSlash) {
return new WriteOptions(escapeUnicode, escapeJavascriptSeparators, escapeSlash, indent);
return new WriteOptions(escapeUnicode, escapeJavascriptSeparators, escapeSlash, indentation);
}

public WriteOptions withIndent(boolean indent) {
return new WriteOptions(escapeUnicode, escapeJavascriptSeparators, escapeSlash, indent);
public WriteOptions withIndentation(int indentation) {
return new WriteOptions(escapeUnicode, escapeJavascriptSeparators, escapeSlash, indentation);
}
}

sealed interface EOFBehavior {
record ThrowException() implements EOFBehavior {}
record DefaultValue(Json json) implements EOFBehavior {}
enum EOFBehavior {
THROW_EXCEPTION,
RETURN_NULL
}

record ReadOptions(
Expand All @@ -315,7 +320,7 @@ record ReadOptions(
}

public ReadOptions() {
this(new EOFBehavior.ThrowException(), false);
this(EOFBehavior.THROW_EXCEPTION, false);
}

public ReadOptions withEOFBehavior(EOFBehavior eofBehavior) {
Expand All @@ -327,11 +332,3 @@ public ReadOptions withUseBigDecimals(boolean useBigDecimals) {
}
}
}








4 changes: 2 additions & 2 deletions src/main/java/dev/mccue/json/JsonReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -463,11 +463,11 @@ Json read(PushbackReader stream, boolean eofError, Json.ReadOptions options) thr
}
default -> {
if (c < 0) {
if (eofError || !(options.eofBehavior() instanceof Json.EOFBehavior.DefaultValue eofDefaultValue)) {
if (eofError || !(options.eofBehavior() == Json.EOFBehavior.RETURN_NULL)) {
throw new JsonReadException("JSON error (end-of-file)");
}
else {
return eofDefaultValue.json();
return null;
}
}
else {
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/dev/mccue/json/JsonWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,7 @@ private static void writeString(CharSequence s, Appendable out, OptionsWithInden

private static void writeIndent(Appendable out, OptionsWithIndentDepth options) throws IOException {
out.append('\n');
int i = options.indentDepth;
while (i > 0) {
out.append(' ');
i--;
}
out.append(" ".repeat(options.indentation() * options.indentDepth));
}

private static void writeObject(Json.Object m, Appendable out, OptionsWithIndentDepth options) throws IOException {
Expand Down Expand Up @@ -226,8 +222,12 @@ boolean escapeSlash() {
return options.escapeSlash();
}

int indentation() {
return options.indentation();
}

boolean indent() {
return options.indent();
return indentation() != 0;
}

OptionsWithIndentDepth incrementIndentDepth() {
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/json1k.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"results":[{"gender":"male","name":{"title":"mr","first":"morris","last":"lambert"},"location":{"street":"7239 hillcrest rd","city":"nowra","state":"australian capital territory","postcode":7541},"email":"[email protected]","login":{"username":"smallbird414","password":"carole","salt":"yO9OBSsk","md5":"658323a603522238fb32a86b82eafd55","sha1":"289f6e9a8ccd42b539e0c43283e788aeb8cd0f6e","sha256":"57bca99b2b4e78aa2171eda4db3f35e7631ca3b30f157bdc7ea089a855c66668"},"dob":"1950-07-13 09:18:34","registered":"2012-04-07 00:05:32","phone":"08-2274-7839","cell":"0452-558-702","id":{"name":"TFN","value":"740213762"},"picture":{"large":"https://randomuser.me/api/portraits/men/95.jpg","medium":"https://randomuser.me/api/portraits/med/men/95.jpg","thumbnail":"https://randomuser.me/api/portraits/thumb/men/95.jpg"},"nat":"AU"}],"info":{"seed":"fb0c2b3c7cedc7af","results":1,"page":1,"version":"1.1"}}
{"results":[{"gender":"male","name":{"title":"mr","first":"morris","last":"lambert"},"location":{"street":"7239 hillcrest rd","city":"nowra","state":"australian capital territory","postcode":7541},"email":"[email protected]","login":{"username":"smallbird414","password":"carole","salt":"yO9OBSsk","md5":"658323a603522238fb32a86b82eafd55","sha1":"289f6e9a8ccd42b539e0c43283e788aeb8cd0f6e","sha256":"57bca99b2b4e78aa2171eda4db3f35e7631ca3b30f157bdc7ea089a855c66668"},"dob":"1950-07-13 09:18:34","registered":"2012-04-07 00:05:32","phone":"08-2274-7839","cell":"0452-558-702","id":{"name":"TFN","value":"740213762"},"picture":{"large":"https://randomuser.me/api/portraits/men/95.jpg","medium":"https://randomuser.me/api/portraits/med/men/95.jpg","thumbnail":"https://randomuser.me/api/portraits/thumb/men/95.jpg"},"nat":"AU"}],"info":{"seed":"fb0c2b3c7cedc7af","results":1,"page":1,"version":"1.1"}}

0 comments on commit 0452434

Please sign in to comment.