/** * Back up one character. This provides a sort of lookahead capability, * so that you can test for a digit or letter before attempting to parse * the next number or identifier. */ public void back() throws JSONException { if (this.usePrevious || this.index <= 0) { throw new JSONException("Stepping back two steps is not supported"); } this.index -= 1; this.character -= 1; this.usePrevious = true; this.eof = false; }
/** * Returns an exception containing the given message plus the current * position and the entire input string. */ public JSONException syntaxError(String message) { return new JSONException(message + this); }
String checkName(String name) throws JSONException { if (name == null) { throw new JSONException("Names must be non-null"); } return name; }
String checkName(String name) throws JSONException { if (name == null) { throw new JSONException("Names must be non-null"); } return name; }
/** * Returns an exception containing the given message plus the current position and the entire * input string. * * @param message The message we want to include. * @return An exception that we can throw. */ public JSONException syntaxError(String message) { return new JSONException(message + this); }
/** * Returns the value mapped by {@code name}. * * @throws JSONException if no such mapping exists. */ public Object get(String name) throws JSONException { Object result = nameValuePairs.get(name); if (result == null) { throw new JSONException("No value for " + name); } return result; }
public static JSONException typeMismatch(Object indexOrName, Object actual, String requiredType) throws JSONException { if (actual == null) { throw new JSONException("Value at " + indexOrName + " is null."); } else { throw new JSONException("Value " + actual + " at " + indexOrName + " of type " + actual.getClass().getName() + " cannot be converted to " + requiredType); } }
public static JSONException typeMismatch(Object actual, String requiredType) throws JSONException { if (actual == null) { throw new JSONException("Value is null."); } else { throw new JSONException("Value " + actual + " of type " + actual.getClass().getName() + " cannot be converted to " + requiredType); } } }
public static JSONException typeMismatch(Object actual, String requiredType) throws JSONException { if (actual == null) { throw new JSONException("Value is null."); } else { throw new JSONException("Value " + actual + " of type " + actual.getClass().getName() + " cannot be converted to " + requiredType); } } }
public static JSONException typeMismatch(Object indexOrName, Object actual, String requiredType) throws JSONException { if (actual == null) { throw new JSONException("Value at " + indexOrName + " is null."); } else { throw new JSONException("Value " + actual + " at " + indexOrName + " of type " + actual.getClass().getName() + " cannot be converted to " + requiredType); } }
/** * Returns the input if it is a JSON-permissible value; throws otherwise. */ static double checkDouble(double d) throws JSONException { if (Double.isInfinite(d) || Double.isNaN(d)) { throw new JSONException("Forbidden numeric value: " + d); } return d; }
/** * Returns the input if it is a JSON-permissible value; throws otherwise. */ static double checkDouble(double d) throws JSONException { if (Double.isInfinite(d) || Double.isNaN(d)) { throw new JSONException("Forbidden numeric value: " + d); } return d; }
/** * Returns the value on the top of the stack. */ private Scope peek() throws JSONException { if (stack.isEmpty()) { throw new JSONException("Nesting problem"); } return stack.get(stack.size() - 1); }
/** * Returns the value on the top of the stack. */ private Scope peek() throws JSONException { if (stack.isEmpty()) { throw new JSONException("Nesting problem"); } return stack.get(stack.size() - 1); }
/** * Make a JSONException to signal a syntax error. * * @param message The error message. * @return A JSONException object, suitable for throwing */ public JSONException syntaxError(String message) { return new JSONException(message + this.toString()); }
/** * Enters a new scope by appending any necessary whitespace and the given bracket. */ JSONStringer open(Scope empty, String openBracket) throws JSONException { if (stack.isEmpty() && out.length() > 0) { throw new JSONException("Nesting problem: multiple top-level roots"); } beforeValue(); stack.add(empty); out.append(openBracket); return this; }
/** * Encodes {@code value} to this stringer. * * @return this stringer. */ public JSONStringer value(boolean value) throws JSONException { if (stack.isEmpty()) { throw new JSONException("Nesting problem"); } beforeValue(); out.append(value); return this; }
/** * Encodes {@code value} to this stringer. * * @return this stringer. */ public JSONStringer value(long value) throws JSONException { if (stack.isEmpty()) { throw new JSONException("Nesting problem"); } beforeValue(); out.append(value); return this; }
/** * Enters a new scope by appending any necessary whitespace and the given * bracket. */ JSONStringer open(Scope empty, String openBracket) throws JSONException { if (stack.isEmpty() && out.length() > 0) { throw new JSONException("Nesting problem: multiple top-level roots"); } beforeValue(); stack.add(empty); out.append(openBracket); return this; }
@NonNull public String toJSON() throws JSONException { try { return StringFormat.JSON.toFormattedString(this, ImmutableSet.empty(), "", "", false); } catch (JSONException e) { throw e; } catch (Exception e) { throw new JSONException(e.getMessage()); } }