@Override
    public JsonElement read(JsonReader in) throws IOException {
      // Either JsonArray or JsonObject
      JsonElement current;
      JsonToken peeked = in.peek();

      current = tryBeginNesting(in, peeked);
      if (current == null) {
        return readTerminal(in, peeked);
      }

      Deque<JsonElement> stack = new ArrayDeque<>();

      while (true) {
        while (in.hasNext()) {
          String name = null;
          // Name is only used for JSON object members
          if (current instanceof JsonObject) {
            name = in.nextName();
            if (!isValidString(name)) {
              throw new IOException("illegal characters in string");
            }
          }

          peeked = in.peek();
          JsonElement value = tryBeginNesting(in, peeked);
          boolean isNesting = value != null;

          if (value == null) {
            value = readTerminal(in, peeked);
          }

          if (current instanceof JsonArray) {
            ((JsonArray) current).add(value);
          } else {
            if (((JsonObject) current).has(name)) {
              throw new IOException("duplicate key: " + name);
            }
            ((JsonObject) current).add(name, value);
          }

          if (isNesting) {
            stack.addLast(current);
            if (stack.size() > RECURSION_LIMIT) {
               throw new IOException("too many recursions");
            }
            current = value;
          }
        }

        // End current element
        if (current instanceof JsonArray) {
          in.endArray();
        } else {
          in.endObject();
        }

        if (stack.isEmpty()) {
          return current;
        } else {
          // Continue with enclosing element
          current = stack.removeLast();
        }
      }
    }
Reads a {@link JsonElement} which cannot have any nested elements
java_src/src/main/java/com/google/crypto/tink/internal/JsonParser.java