private String stringKey(Map.Entry<?, ?> entry) { Object name = entry.getKey(); if (name instanceof String) return (String) name; throw typeMismatch(name, Token.NAME); }
@Override public Token peek() throws IOException { if (stackSize == 0) return Token.END_DOCUMENT; // If the top of the stack is an iterator, take its first element and push it on the stack. Object peeked = stack[stackSize - 1]; if (peeked instanceof JsonIterator) return ((JsonIterator) peeked).endToken; if (peeked instanceof List) return Token.BEGIN_ARRAY; if (peeked instanceof Map) return Token.BEGIN_OBJECT; if (peeked instanceof Map.Entry) return Token.NAME; if (peeked instanceof String) return Token.STRING; if (peeked instanceof Boolean) return Token.BOOLEAN; if (peeked instanceof Number) return Token.NUMBER; if (peeked == null) return Token.NULL; if (peeked == JSON_READER_CLOSED) throw new IllegalStateException("JsonReader is closed"); throw typeMismatch(peeked, "a JSON value"); }
/** * Returns the top of the stack which is required to be a {@code type}. Throws if this reader is * closed, or if the type isn't what was expected. */ private @Nullable <T> T require(Class<T> type, Token expected) throws IOException { Object peeked = (stackSize != 0 ? stack[stackSize - 1] : null); if (type.isInstance(peeked)) { return type.cast(peeked); } if (peeked == null && expected == Token.NULL) { return null; } if (peeked == JSON_READER_CLOSED) { throw new IllegalStateException("JsonReader is closed"); } throw typeMismatch(peeked, expected); }
@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 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 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 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(); }
private String stringKey(Map.Entry<?, ?> entry) { Object name = entry.getKey(); if (name instanceof String) return (String) name; throw typeMismatch(name, Token.NAME); }
@Override public Token peek() throws IOException { if (stackSize == 0) return Token.END_DOCUMENT; // If the top of the stack is an iterator, take its first element and push it on the stack. Object peeked = stack[stackSize - 1]; if (peeked instanceof JsonIterator) return ((JsonIterator) peeked).endToken; if (peeked instanceof List) return Token.BEGIN_ARRAY; if (peeked instanceof Map) return Token.BEGIN_OBJECT; if (peeked instanceof Map.Entry) return Token.NAME; if (peeked instanceof String) return Token.STRING; if (peeked instanceof Boolean) return Token.BOOLEAN; if (peeked instanceof Number) return Token.NUMBER; if (peeked == null) return Token.NULL; if (peeked == JSON_READER_CLOSED) throw new IllegalStateException("JsonReader is closed"); throw typeMismatch(peeked, "a JSON value"); }
/** * Returns the top of the stack which is required to be a {@code type}. Throws if this reader is * closed, or if the type isn't what was expected. */ private @Nullable <T> T require(Class<T> type, Token expected) throws IOException { Object peeked = (stackSize != 0 ? stack[stackSize - 1] : null); if (type.isInstance(peeked)) { return type.cast(peeked); } if (peeked == null && expected == Token.NULL) { return null; } if (peeked == JSON_READER_CLOSED) { throw new IllegalStateException("JsonReader is closed"); } throw typeMismatch(peeked, expected); }
@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 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 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 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(); }