style(serialize-json): Make use of switch expressions

This commit is contained in:
Johannes Frohnmeyer 2024-04-17 10:44:09 +02:00
parent 5c1db8c339
commit ef874ede77
Signed by: Johannes
GPG Key ID: E76429612C2929F4
1 changed files with 98 additions and 123 deletions

View File

@ -211,37 +211,19 @@ public class JsonReader extends SerializeReader<IOException, JsonReader> impleme
p = doPeek();
}
switch (p) {
case PEEKED_BEGIN_OBJECT:
return Token.BEGIN_OBJECT;
case PEEKED_END_OBJECT:
return Token.END_OBJECT;
case PEEKED_BEGIN_ARRAY:
return Token.BEGIN_ARRAY;
case PEEKED_END_ARRAY:
return Token.END_ARRAY;
case PEEKED_SINGLE_QUOTED_NAME:
case PEEKED_DOUBLE_QUOTED_NAME:
case PEEKED_UNQUOTED_NAME:
return Token.NAME;
case PEEKED_TRUE:
case PEEKED_FALSE:
return Token.BOOLEAN;
case PEEKED_NULL:
return Token.NULL;
case PEEKED_SINGLE_QUOTED:
case PEEKED_DOUBLE_QUOTED:
case PEEKED_UNQUOTED:
case PEEKED_BUFFERED:
return Token.STRING;
case PEEKED_LONG:
case PEEKED_NUMBER:
return Token.NUMBER;
case PEEKED_EOF:
return Token.END_DOCUMENT;
default:
throw new AssertionError();
}
return switch (p) {
case PEEKED_BEGIN_OBJECT -> Token.BEGIN_OBJECT;
case PEEKED_END_OBJECT -> Token.END_OBJECT;
case PEEKED_BEGIN_ARRAY -> Token.BEGIN_ARRAY;
case PEEKED_END_ARRAY -> Token.END_ARRAY;
case PEEKED_SINGLE_QUOTED_NAME, PEEKED_DOUBLE_QUOTED_NAME, PEEKED_UNQUOTED_NAME -> Token.NAME;
case PEEKED_TRUE, PEEKED_FALSE -> Token.BOOLEAN;
case PEEKED_NULL -> Token.NULL;
case PEEKED_SINGLE_QUOTED, PEEKED_DOUBLE_QUOTED, PEEKED_UNQUOTED, PEEKED_BUFFERED -> Token.STRING;
case PEEKED_LONG, PEEKED_NUMBER -> Token.NUMBER;
case PEEKED_EOF -> Token.END_DOCUMENT;
default -> throw new AssertionError();
};
}
@SuppressWarnings("fallthrough")
@ -585,21 +567,20 @@ public class JsonReader extends SerializeReader<IOException, JsonReader> impleme
p = doPeek();
}
String result;
if (p == PEEKED_UNQUOTED_NAME) {
result = nextUnquotedValue();
} else if (p == PEEKED_SINGLE_QUOTED_NAME) {
result = nextQuotedValue('\'');
} else if (p == PEEKED_DOUBLE_QUOTED_NAME) {
result = nextQuotedValue('"');
} else {
// If we are in an array, allow reading an in inferred name once
if (!wroteName) {
if (stack[stackSize - 1] == JsonScope.EMPTY_ARRAY || stack[stackSize - 1] == JsonScope.NONEMPTY_ARRAY) {
wroteName = true;
return heuristics.guessArrayElementName(getPath());
switch (p) {
case PEEKED_UNQUOTED_NAME -> result = nextUnquotedValue();
case PEEKED_SINGLE_QUOTED_NAME -> result = nextQuotedValue('\'');
case PEEKED_DOUBLE_QUOTED_NAME -> result = nextQuotedValue('"');
default -> {
// If we are in an array, allow reading an in inferred name once
if (!wroteName) {
if (stack[stackSize - 1] == JsonScope.EMPTY_ARRAY || stack[stackSize - 1] == JsonScope.NONEMPTY_ARRAY) {
wroteName = true;
return heuristics.guessArrayElementName(getPath());
}
}
throw unexpectedTokenError("a name");
}
throw unexpectedTokenError("a name");
}
wroteName = true;
peeked = PEEKED_NONE;
@ -614,22 +595,20 @@ public class JsonReader extends SerializeReader<IOException, JsonReader> impleme
p = doPeek();
}
String result;
if (p == PEEKED_UNQUOTED) {
result = nextUnquotedValue();
} else if (p == PEEKED_SINGLE_QUOTED) {
result = nextQuotedValue('\'');
} else if (p == PEEKED_DOUBLE_QUOTED) {
result = nextQuotedValue('"');
} else if (p == PEEKED_BUFFERED) {
result = peekedString;
peekedString = null;
} else if (p == PEEKED_LONG) {
result = Long.toString(peekedLong);
} else if (p == PEEKED_NUMBER) {
result = new String(buffer, pos, peekedNumberLength);
pos += peekedNumberLength;
} else {
throw unexpectedTokenError("a string");
switch (p) {
case PEEKED_UNQUOTED -> result = nextUnquotedValue();
case PEEKED_SINGLE_QUOTED -> result = nextQuotedValue('\'');
case PEEKED_DOUBLE_QUOTED -> result = nextQuotedValue('"');
case PEEKED_BUFFERED -> {
result = peekedString;
peekedString = null;
}
case PEEKED_LONG -> result = Long.toString(peekedLong);
case PEEKED_NUMBER -> {
result = new String(buffer, pos, peekedNumberLength);
pos += peekedNumberLength;
}
default -> throw unexpectedTokenError("a string");
}
wroteName = false;
peeked = PEEKED_NONE;
@ -643,18 +622,21 @@ public class JsonReader extends SerializeReader<IOException, JsonReader> impleme
if (p == PEEKED_NONE) {
p = doPeek();
}
if (p == PEEKED_TRUE) {
wroteName = false;
peeked = PEEKED_NONE;
pathIndices[stackSize - 1]++;
return true;
} else if (p == PEEKED_FALSE) {
wroteName = false;
peeked = PEEKED_NONE;
pathIndices[stackSize - 1]++;
return false;
}
throw unexpectedTokenError("a boolean");
return switch (p) {
case PEEKED_TRUE -> {
wroteName = false;
peeked = PEEKED_NONE;
pathIndices[stackSize - 1]++;
yield true;
}
case PEEKED_FALSE -> {
wroteName = false;
peeked = PEEKED_NONE;
pathIndices[stackSize - 1]++;
yield false;
}
default -> throw unexpectedTokenError("a boolean");
};
}
@Override
@ -830,16 +812,18 @@ public class JsonReader extends SerializeReader<IOException, JsonReader> impleme
return (double) peekedLong;
}
if (p == PEEKED_NUMBER) {
peekedString = new String(buffer, pos, peekedNumberLength);
pos += peekedNumberLength;
} else if (p == PEEKED_SINGLE_QUOTED || p == PEEKED_DOUBLE_QUOTED) {
peekedString = nextQuotedValue(p == PEEKED_SINGLE_QUOTED ? '\'' : '"');
} else if (p == PEEKED_UNQUOTED) {
peekedString = nextUnquotedValue();
} else if (p != PEEKED_BUFFERED) {
throw unexpectedTokenError("a double");
}
peekedString = switch (p) {
case PEEKED_NUMBER -> {
String res = new String(buffer, pos, peekedNumberLength);
pos += peekedNumberLength;
yield res;
}
case PEEKED_SINGLE_QUOTED -> nextQuotedValue('\'');
case PEEKED_DOUBLE_QUOTED -> nextQuotedValue('"');
case PEEKED_UNQUOTED -> nextUnquotedValue();
case PEEKED_BUFFERED -> peekedString;
default -> throw unexpectedTokenError("a double");
};
peeked = PEEKED_BUFFERED;
LazilyParsedNumber result = new LazilyParsedNumber(peekedString); // don't catch this NumberFormatException.
@ -865,22 +849,22 @@ public class JsonReader extends SerializeReader<IOException, JsonReader> impleme
}
switch (p) {
case PEEKED_BEGIN_ARRAY:
case PEEKED_BEGIN_ARRAY -> {
push(JsonScope.EMPTY_ARRAY);
wroteName = false;
count++;
break;
case PEEKED_BEGIN_OBJECT:
}
case PEEKED_BEGIN_OBJECT -> {
push(JsonScope.EMPTY_OBJECT);
wroteName = false;
count++;
break;
case PEEKED_END_ARRAY:
}
case PEEKED_END_ARRAY -> {
wroteName = false;
stackSize--;
count--;
break;
case PEEKED_END_OBJECT:
}
case PEEKED_END_OBJECT -> {
// Only update when object end is explicitly skipped, otherwise stack is not updated
// anyways
if (count == 0) {
@ -890,53 +874,51 @@ public class JsonReader extends SerializeReader<IOException, JsonReader> impleme
wroteName = false;
stackSize--;
count--;
break;
case PEEKED_UNQUOTED:
}
case PEEKED_UNQUOTED -> {
wroteName = false;
skipUnquotedValue();
break;
case PEEKED_SINGLE_QUOTED:
}
case PEEKED_SINGLE_QUOTED -> {
wroteName = false;
skipQuotedValue('\'');
break;
case PEEKED_DOUBLE_QUOTED:
}
case PEEKED_DOUBLE_QUOTED -> {
wroteName = false;
skipQuotedValue('"');
break;
case PEEKED_UNQUOTED_NAME:
}
case PEEKED_UNQUOTED_NAME -> {
skipUnquotedValue();
wroteName = true;
// Only update when name is explicitly skipped, otherwise stack is not updated anyways
if (count == 0) {
pathNames[stackSize - 1] = "<skipped>";
}
break;
case PEEKED_SINGLE_QUOTED_NAME:
}
case PEEKED_SINGLE_QUOTED_NAME -> {
skipQuotedValue('\'');
wroteName = true;
// Only update when name is explicitly skipped, otherwise stack is not updated anyways
if (count == 0) {
pathNames[stackSize - 1] = "<skipped>";
}
break;
case PEEKED_DOUBLE_QUOTED_NAME:
}
case PEEKED_DOUBLE_QUOTED_NAME -> {
skipQuotedValue('"');
wroteName = true;
// Only update when name is explicitly skipped, otherwise stack is not updated anyways
if (count == 0) {
pathNames[stackSize - 1] = "<skipped>";
}
break;
case PEEKED_NUMBER:
}
case PEEKED_NUMBER -> {
wroteName = false;
pos += peekedNumberLength;
break;
case PEEKED_EOF:
throw new IllegalStateException("Attempt to skip led outside the document");
default:
// For all other tokens there is nothing to do; token has already been consumed from
// underlying reader
wroteName = false;
}
case PEEKED_EOF -> throw new IllegalStateException("Attempt to skip led outside the document");
// For all other tokens there is nothing to do; token has already been consumed from
// underlying reader
default -> wroteName = false;
}
peeked = PEEKED_NONE;
} while (count > 0);
@ -1327,29 +1309,22 @@ public class JsonReader extends SerializeReader<IOException, JsonReader> impleme
for (int i = 0; i < stackSize; i++) {
int scope = stack[i];
switch (scope) {
case JsonScope.EMPTY_ARRAY:
case JsonScope.NONEMPTY_ARRAY:
case JsonScope.EMPTY_ARRAY, JsonScope.NONEMPTY_ARRAY -> {
int pathIndex = pathIndices[i];
// If index is last path element it points to next array element; have to decrement
if (usePreviousPath && pathIndex > 0 && i == stackSize - 1) {
pathIndex--;
}
result.append('[').append(pathIndex).append(']');
break;
case JsonScope.EMPTY_OBJECT:
case JsonScope.DANGLING_NAME:
case JsonScope.NONEMPTY_OBJECT:
}
case JsonScope.EMPTY_OBJECT, JsonScope.DANGLING_NAME, JsonScope.NONEMPTY_OBJECT -> {
result.append('.');
if (pathNames[i] != null) {
result.append(pathNames[i]);
}
break;
case JsonScope.NONEMPTY_DOCUMENT:
case JsonScope.EMPTY_DOCUMENT:
case JsonScope.CLOSED:
break;
default:
throw new AssertionError("Unknown scope value: " + scope);
}
case JsonScope.NONEMPTY_DOCUMENT, JsonScope.EMPTY_DOCUMENT, JsonScope.CLOSED -> {}
default -> throw new AssertionError("Unknown scope value: " + scope);
}
}
return result.toString();