Use char literals for array and object start/end

This commit is contained in:
Jake Wharton
2019-09-30 14:40:12 -04:00
parent 52aaf9cc84
commit ecb7083abf

View File

@@ -83,11 +83,11 @@ final class JsonUtf8Writer extends JsonWriter {
"Array cannot be used as a map key in JSON at path " + getPath()); "Array cannot be used as a map key in JSON at path " + getPath());
} }
writeDeferredName(); writeDeferredName();
return open(EMPTY_ARRAY, NONEMPTY_ARRAY, "["); return open(EMPTY_ARRAY, NONEMPTY_ARRAY, '[');
} }
@Override public JsonWriter endArray() throws IOException { @Override public JsonWriter endArray() throws IOException {
return close(EMPTY_ARRAY, NONEMPTY_ARRAY, "]"); return close(EMPTY_ARRAY, NONEMPTY_ARRAY, ']');
} }
@Override public JsonWriter beginObject() throws IOException { @Override public JsonWriter beginObject() throws IOException {
@@ -96,19 +96,19 @@ final class JsonUtf8Writer extends JsonWriter {
"Object cannot be used as a map key in JSON at path " + getPath()); "Object cannot be used as a map key in JSON at path " + getPath());
} }
writeDeferredName(); writeDeferredName();
return open(EMPTY_OBJECT, NONEMPTY_OBJECT, "{"); return open(EMPTY_OBJECT, NONEMPTY_OBJECT, '{');
} }
@Override public JsonWriter endObject() throws IOException { @Override public JsonWriter endObject() throws IOException {
promoteValueToName = false; promoteValueToName = false;
return close(EMPTY_OBJECT, NONEMPTY_OBJECT, "}"); return close(EMPTY_OBJECT, NONEMPTY_OBJECT, '}');
} }
/** /**
* Enters a new scope by appending any necessary whitespace and the given * Enters a new scope by appending any necessary whitespace and the given
* bracket. * bracket.
*/ */
private JsonWriter open(int empty, int nonempty, String openBracket) throws IOException { private JsonWriter open(int empty, int nonempty, char openBracket) throws IOException {
if (stackSize == flattenStackSize if (stackSize == flattenStackSize
&& (scopes[stackSize - 1] == empty || scopes[stackSize - 1] == nonempty)) { && (scopes[stackSize - 1] == empty || scopes[stackSize - 1] == nonempty)) {
// Cancel this open. Invert the flatten stack size until this is closed. // Cancel this open. Invert the flatten stack size until this is closed.
@@ -119,7 +119,7 @@ final class JsonUtf8Writer extends JsonWriter {
checkStack(); checkStack();
pushScope(empty); pushScope(empty);
pathIndices[stackSize - 1] = 0; pathIndices[stackSize - 1] = 0;
sink.writeUtf8(openBracket); sink.writeByte(openBracket);
return this; return this;
} }
@@ -127,7 +127,7 @@ final class JsonUtf8Writer extends JsonWriter {
* Closes the current scope by appending any necessary whitespace and the * Closes the current scope by appending any necessary whitespace and the
* given bracket. * given bracket.
*/ */
private JsonWriter close(int empty, int nonempty, String closeBracket) throws IOException { private JsonWriter close(int empty, int nonempty, char closeBracket) throws IOException {
int context = peekScope(); int context = peekScope();
if (context != nonempty && context != empty) { if (context != nonempty && context != empty) {
throw new IllegalStateException("Nesting problem."); throw new IllegalStateException("Nesting problem.");
@@ -147,7 +147,7 @@ final class JsonUtf8Writer extends JsonWriter {
if (context == nonempty) { if (context == nonempty) {
newline(); newline();
} }
sink.writeUtf8(closeBracket); sink.writeByte(closeBracket);
return this; return this;
} }