@Override public boolean nextBoolean() throws IOException { Boolean peeked = require(Boolean.class, Token.BOOLEAN); remove(); return peeked; }
@Override public @Nullable <T> T nextNull() throws IOException { require(Void.class, Token.NULL); remove(); return null; }
@Override public String nextName() throws IOException { Map.Entry<?, ?> peeked = require(Map.Entry.class, Token.NAME); // Swap the Map.Entry for its value on the stack and return its key. String result = stringKey(peeked); stack[stackSize - 1] = peeked.getValue(); pathNames[stackSize - 2] = result; return result; }
@Override public int selectName(Options options) throws IOException { Map.Entry<?, ?> peeked = require(Map.Entry.class, Token.NAME); String name = stringKey(peeked); for (int i = 0, length = options.strings.length; i < length; i++) { // Swap the Map.Entry for its value on the stack and return its key. if (options.strings[i].equals(name)) { stack[stackSize - 1] = peeked.getValue(); pathNames[stackSize - 2] = name; return i; } } return -1; }
@Override public void beginObject() throws IOException { Map<?, ?> peeked = require(Map.class, Token.BEGIN_OBJECT); JsonIterator iterator = new JsonIterator( Token.END_OBJECT, peeked.entrySet().toArray(new Object[peeked.size()]), 0); stack[stackSize - 1] = iterator; scopes[stackSize - 1] = JsonScope.EMPTY_OBJECT; // If the iterator isn't empty push its first value onto the stack. if (iterator.hasNext()) { push(iterator.next()); } }
@Override public void skipName() throws IOException { if (failOnUnknown) { throw new JsonDataException("Cannot skip unexpected " + peek() + " at " + getPath()); } Map.Entry<?, ?> peeked = require(Map.Entry.class, Token.NAME); // Swap the Map.Entry for its value on the stack. stack[stackSize - 1] = peeked.getValue(); pathNames[stackSize - 2] = "null"; }
@Override public void beginArray() throws IOException { List<?> peeked = require(List.class, Token.BEGIN_ARRAY); JsonIterator iterator = new JsonIterator( Token.END_ARRAY, peeked.toArray(new Object[peeked.size()]), 0); stack[stackSize - 1] = iterator; scopes[stackSize - 1] = JsonScope.EMPTY_ARRAY; pathIndices[stackSize - 1] = 0; // If the iterator isn't empty push its first value onto the stack. if (iterator.hasNext()) { push(iterator.next()); } }
@Override public void endArray() throws IOException { JsonIterator peeked = require(JsonIterator.class, Token.END_ARRAY); if (peeked.endToken != Token.END_ARRAY || peeked.hasNext()) { throw typeMismatch(peeked, Token.END_ARRAY); } remove(); }
@Override public void endObject() throws IOException { JsonIterator peeked = require(JsonIterator.class, Token.END_OBJECT); if (peeked.endToken != Token.END_OBJECT || peeked.hasNext()) { throw typeMismatch(peeked, Token.END_OBJECT); } pathNames[stackSize - 1] = null; remove(); }
@Override public long nextLong() throws IOException { Object peeked = require(Object.class, Token.NUMBER); long result; if (peeked instanceof Number) { result = ((Number) peeked).longValue(); } else if (peeked instanceof String) { try { result = Long.parseLong((String) peeked); } catch (NumberFormatException e) { try { BigDecimal asDecimal = new BigDecimal((String) peeked); result = asDecimal.longValueExact(); } catch (NumberFormatException e2) { throw typeMismatch(peeked, Token.NUMBER); } } } else { throw typeMismatch(peeked, Token.NUMBER); } remove(); return result; }
@Override public double nextDouble() throws IOException { Object peeked = require(Object.class, Token.NUMBER); double result; if (peeked instanceof Number) { result = ((Number) peeked).doubleValue(); } else if (peeked instanceof String) { try { result = Double.parseDouble((String) peeked); } catch (NumberFormatException e) { throw typeMismatch(peeked, Token.NUMBER); } } else { throw typeMismatch(peeked, Token.NUMBER); } if (!lenient && (Double.isNaN(result) || Double.isInfinite(result))) { throw new JsonEncodingException("JSON forbids NaN and infinities: " + result + " at path " + getPath()); } remove(); return result; }
@Override public int nextInt() throws IOException { Object peeked = require(Object.class, Token.NUMBER); int result; if (peeked instanceof Number) { result = ((Number) peeked).intValue(); } else if (peeked instanceof String) { try { result = Integer.parseInt((String) peeked); } catch (NumberFormatException e) { try { BigDecimal asDecimal = new BigDecimal((String) peeked); result = asDecimal.intValueExact(); } catch (NumberFormatException e2) { throw typeMismatch(peeked, Token.NUMBER); } } } else { throw typeMismatch(peeked, Token.NUMBER); } remove(); return result; }
@Override public boolean nextBoolean() throws IOException { Boolean peeked = require(Boolean.class, Token.BOOLEAN); remove(); return peeked; }
@Override public String nextName() throws IOException { Map.Entry<?, ?> peeked = require(Map.Entry.class, Token.NAME); // Swap the Map.Entry for its value on the stack and return its key. String result = stringKey(peeked); stack[stackSize - 1] = peeked.getValue(); pathNames[stackSize - 2] = result; return result; }
@Override public @Nullable <T> T nextNull() throws IOException { require(Void.class, Token.NULL); remove(); return null; }
@Override public void beginObject() throws IOException { Map<?, ?> peeked = require(Map.class, Token.BEGIN_OBJECT); JsonIterator iterator = new JsonIterator( Token.END_OBJECT, peeked.entrySet().toArray(new Object[peeked.size()]), 0); stack[stackSize - 1] = iterator; scopes[stackSize - 1] = JsonScope.EMPTY_OBJECT; // If the iterator isn't empty push its first value onto the stack. if (iterator.hasNext()) { push(iterator.next()); } }
@Override public void skipName() throws IOException { if (failOnUnknown) { throw new JsonDataException("Cannot skip unexpected " + peek() + " at " + getPath()); } Map.Entry<?, ?> peeked = require(Map.Entry.class, Token.NAME); // Swap the Map.Entry for its value on the stack. stack[stackSize - 1] = peeked.getValue(); pathNames[stackSize - 2] = "null"; }
@Override public void beginArray() throws IOException { List<?> peeked = require(List.class, Token.BEGIN_ARRAY); JsonIterator iterator = new JsonIterator( Token.END_ARRAY, peeked.toArray(new Object[peeked.size()]), 0); stack[stackSize - 1] = iterator; scopes[stackSize - 1] = JsonScope.EMPTY_ARRAY; pathIndices[stackSize - 1] = 0; // If the iterator isn't empty push its first value onto the stack. if (iterator.hasNext()) { push(iterator.next()); } }
@Override public void endArray() throws IOException { JsonIterator peeked = require(JsonIterator.class, Token.END_ARRAY); if (peeked.endToken != Token.END_ARRAY || peeked.hasNext()) { throw typeMismatch(peeked, Token.END_ARRAY); } remove(); }
@Override public void endObject() throws IOException { JsonIterator peeked = require(JsonIterator.class, Token.END_OBJECT); if (peeked.endToken != Token.END_OBJECT || peeked.hasNext()) { throw typeMismatch(peeked, Token.END_OBJECT); } pathNames[stackSize - 1] = null; remove(); }