@Override public int selectString(Options options) throws IOException { Object peeked = (stackSize != 0 ? stack[stackSize - 1] : null); if (!(peeked instanceof String)) { if (peeked == JSON_READER_CLOSED) { throw new IllegalStateException("JsonReader is closed"); } return -1; } String peekedString = (String) peeked; for (int i = 0, length = options.strings.length; i < length; i++) { if (options.strings[i].equals(peekedString)) { remove(); return i; } } return -1; }
@Override public String nextString() throws IOException { Object peeked = (stackSize != 0 ? stack[stackSize - 1] : null); if (peeked instanceof String) { remove(); return (String) peeked; } if (peeked instanceof Number) { remove(); return peeked.toString(); } if (peeked == JSON_READER_CLOSED) { throw new IllegalStateException("JsonReader is closed"); } throw typeMismatch(peeked, Token.STRING); }
@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 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 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 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 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 void skipValue() throws IOException { if (failOnUnknown) { throw new JsonDataException("Cannot skip unexpected " + peek() + " at " + getPath()); } // If this element is in an object clear out the key. if (stackSize > 1) { pathNames[stackSize - 2] = "null"; } Object skipped = stackSize != 0 ? stack[stackSize - 1] : null; if (skipped instanceof JsonIterator) { throw new JsonDataException("Expected a value but was " + peek() + " at path " + getPath()); } if (skipped instanceof Map.Entry) { // We're skipping a name. Promote the map entry's value. Map.Entry<?, ?> entry = (Map.Entry<?, ?>) stack[stackSize - 1]; stack[stackSize - 1] = entry.getValue(); } else if (stackSize > 0) { // We're skipping a value. remove(); } else { throw new JsonDataException("Expected a value but was " + peek() + " at path " + getPath()); } }
@Override public int selectString(Options options) throws IOException { Object peeked = (stackSize != 0 ? stack[stackSize - 1] : null); if (!(peeked instanceof String)) { if (peeked == JSON_READER_CLOSED) { throw new IllegalStateException("JsonReader is closed"); } return -1; } String peekedString = (String) peeked; for (int i = 0, length = options.strings.length; i < length; i++) { if (options.strings[i].equals(peekedString)) { remove(); return i; } } return -1; }
@Override public String nextString() throws IOException { Object peeked = (stackSize != 0 ? stack[stackSize - 1] : null); if (peeked instanceof String) { remove(); return (String) peeked; } if (peeked instanceof Number) { remove(); return peeked.toString(); } if (peeked == JSON_READER_CLOSED) { throw new IllegalStateException("JsonReader is closed"); } throw typeMismatch(peeked, Token.STRING); }
@Override public @Nullable <T> T nextNull() throws IOException { require(Void.class, Token.NULL); remove(); return null; }
@Override public boolean nextBoolean() throws IOException { Boolean peeked = require(Boolean.class, Token.BOOLEAN); remove(); return peeked; }
@Override public void skipValue() throws IOException { if (failOnUnknown) { throw new JsonDataException("Cannot skip unexpected " + peek() + " at " + getPath()); } // If this element is in an object clear out the key. if (stackSize > 1) { pathNames[stackSize - 2] = "null"; } Object skipped = stackSize != 0 ? stack[stackSize - 1] : null; if (skipped instanceof Map.Entry) { // We're skipping a name. Promote the map entry's value. Map.Entry<?, ?> entry = (Map.Entry<?, ?>) stack[stackSize - 1]; stack[stackSize - 1] = entry.getValue(); } else if (stackSize > 0) { // We're skipping a value. remove(); } }
@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 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 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 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; }