diff options
author | jonmv <venstad@gmail.com> | 2024-01-23 18:40:11 +0100 |
---|---|---|
committer | jonmv <venstad@gmail.com> | 2024-01-24 11:10:31 +0100 |
commit | 185ff4a875557b6075fbb104bc4f62e0bbaec430 (patch) | |
tree | 7ed4e85a10049287e7a457dc6f25ce4564af194e /document | |
parent | c5a7723fb522a5aa2cff1a8662e1ddffc80c9de6 (diff) |
Simplify a bit
Diffstat (limited to 'document')
-rw-r--r-- | document/src/main/java/com/yahoo/document/json/TokenBuffer.java | 53 | ||||
-rw-r--r-- | document/src/main/java/com/yahoo/document/json/document/DocumentParser.java | 5 |
2 files changed, 19 insertions, 39 deletions
diff --git a/document/src/main/java/com/yahoo/document/json/TokenBuffer.java b/document/src/main/java/com/yahoo/document/json/TokenBuffer.java index 5a8310e5d30..52a2816334a 100644 --- a/document/src/main/java/com/yahoo/document/json/TokenBuffer.java +++ b/document/src/main/java/com/yahoo/document/json/TokenBuffer.java @@ -18,13 +18,11 @@ import java.util.function.Supplier; */ public class TokenBuffer { - private final Deque<Token> tokens; + private final Deque<Token> tokens = new ArrayDeque<>(); private int nesting = 0; - public TokenBuffer() { - this.tokens = new ArrayDeque<>(); - } + public TokenBuffer() { } /** Returns whether any tokens are available in this */ public boolean isEmpty() { return tokens.isEmpty(); } @@ -66,31 +64,25 @@ public class TokenBuffer { tokens.add(new Token(token, name, text)); } - public void bufferObject(JsonToken first, JsonParser tokens) { - bufferJsonStruct(first, tokens, JsonToken.START_OBJECT); + public void bufferObject(JsonParser parser) { + bufferJsonStruct(parser, JsonToken.START_OBJECT); } - private void bufferJsonStruct(JsonToken first, JsonParser tokens, JsonToken firstToken) { - int localNesting = 0; - JsonToken t = first; + private void bufferJsonStruct(JsonParser parser, JsonToken firstToken) { + JsonToken token = parser.currentToken(); + Preconditions.checkArgument(token == firstToken, + "Expected %s, got %s.", firstToken.name(), token); + if (isEmpty()) updateNesting(token); - Preconditions.checkArgument(first == firstToken, - "Expected %s, got %s.", firstToken.name(), t); - if (isEmpty()) { - updateNesting(t); + try { + for (int nesting = addFromParser(parser); nesting > 0; nesting += addFromParser(parser)) + parser.nextValue(); } - localNesting = storeAndPeekNesting(t, localNesting, tokens); - while (localNesting > 0) { - t = nextValue(tokens); - localNesting = storeAndPeekNesting(t, localNesting, tokens); + catch (IOException e) { + throw new IllegalArgumentException(e); } } - private int storeAndPeekNesting(JsonToken t, int nesting, JsonParser tokens) { - addFromParser(t, tokens); - return nesting + nestingOffset(t); - } - private int nestingOffset(JsonToken token) { if (token == null) return 0; if (token.isStructStart()) { @@ -102,20 +94,9 @@ public class TokenBuffer { } } - private void addFromParser(JsonToken t, JsonParser tokens) { - try { - add(t, tokens.getCurrentName(), tokens.getText()); - } catch (IOException e) { - throw new IllegalArgumentException(e); - } - } - - private JsonToken nextValue(JsonParser tokens) { - try { - return tokens.nextValue(); - } catch (IOException e) { - throw new IllegalArgumentException(e); - } + private int addFromParser(JsonParser tokens) throws IOException { + add(tokens.currentToken(), tokens.getCurrentName(), tokens.getText()); + return nestingOffset(tokens.currentToken()); } private void updateNesting(JsonToken token) { diff --git a/document/src/main/java/com/yahoo/document/json/document/DocumentParser.java b/document/src/main/java/com/yahoo/document/json/document/DocumentParser.java index 74656762fe1..e3b2253a912 100644 --- a/document/src/main/java/com/yahoo/document/json/document/DocumentParser.java +++ b/document/src/main/java/com/yahoo/document/json/document/DocumentParser.java @@ -113,10 +113,9 @@ public class DocumentParser { private void handleIdentLevelTwo(DocumentParseInfo documentParseInfo) { try { - JsonToken currentToken = parser.getCurrentToken(); // "fields" opens a dictionary and is therefore on level two which might be surprising. - if (currentToken == JsonToken.START_OBJECT && FIELDS.equals(parser.getCurrentName())) { - documentParseInfo.fieldsBuffer.bufferObject(currentToken, parser); + if (parser.currentToken() == JsonToken.START_OBJECT && FIELDS.equals(parser.getCurrentName())) { + documentParseInfo.fieldsBuffer.bufferObject(parser); processIndent(); } } catch (IOException e) { |