class JsonResource extends ResourceFilter {

    static List<KeyPiece> splitKeyPieces(String key) {
        if (USE_JSONPATH_PATTERN.matcher(key).matches()) {
            List<KeyPiece> result = new ArrayList<KeyPiece>();
            boolean inQuotes = false;
            StringBuilder currentToken = new StringBuilder();
            // Disregard $ at the beginning - it's not really part of the key...
            StringCharacterIterator i = new StringCharacterIterator(key.substring(JSONPATH_ROOT.length()));
            boolean inSubscript = false;
            while (i.current() != StringCharacterIterator.DONE) {
                char c = i.current();
                if (c == '\'') {
                    inQuotes = !inQuotes;
                }
                if (!inQuotes && (c == '.' || c == '[' || c == ']')) {
                    if (currentToken.length() > 0) {
                        addToken(result, currentToken.toString(), inSubscript);
                        currentToken.setLength(0);
                        if (inSubscript) {
                            inSubscript = false;
                        }
                    }
                    if (c == '[') {
                        inSubscript = true; // Record that the next token had an
                                        // array subscript on it.
                    }
                } else {
                    currentToken.append(c);
                }
                i.next();
            }
            addToken(result, currentToken.toString(), inSubscript);

            return Collections.unmodifiableList(result);
        }
        // Otherwise, this is a plain JSON object label
        return Collections.singletonList(new KeyPiece(key, JsonToken.BEGIN_OBJECT));
    }

}