src/main/java/application/rest/v1/json/JSONPathParser.java
/**
     * Supports simple JSON path expressions (e.g. $.one.two.three, $['one']['two']['three'])
     * Returns a JSONPath object for a valid expression otherwise null if the expression is
     * invalid or not supported.
     */
Supports simple JSON path expressions
Returns a JSONPath object for a valid expression otherwise null if the expression is
invalid or not supported.
Supports simple JSON path expressions Returns a JSONPath object for a valid expression otherwise null if the expression is invalid or not supported .
public JSONPath parse(String path) {
        List<String> children = new ArrayList<>();
        int mark = 0;
        ParserState state = ParserState.START_EXPRESSION;
        final int length = path.length();
        for (int i = 0; i < length; ++i) {
            char c = path.charAt(i);
            switch (state) {
                case START_EXPRESSION:
                    if (path.charAt(0) == '$') {
                        state = ParserState.START_CHILD;
                    }
                    else {
                        return null;
                    }
                    break;
                case START_CHILD:
                    if (i < length - 1) {
                        if (c == '.') {
                            state = ParserState.CHILD_NAME_DOT_NOTATION;
                            mark = i + 1;
                        }
                        else if (c == '[') {
                            state = ParserState.START_CHILD_BRACKET_NOTATION;
                        }
                        else {
                            return null;
                        }
                    }
                    else {
                        return null;
                    }
                    break;
                case CHILD_NAME_DOT_NOTATION:
                    if (c == '.' || c == '[') {
                        if (i < length - 1) {
                            if (i > mark) {
                                children.add(path.substring(mark, i));
                            }
                            else {
                                return null;
                            }
                            if (c == '.') {
                                mark = i + 1;
                            }
                            else { 
                                state = ParserState.START_CHILD_BRACKET_NOTATION;
                            }
                        }
                        else {
                            return null;
                        }
                    }
                    else if (i == length - 1) {
                        children.add(path.substring(mark, i + 1));
                    }
                    break;
                case START_CHILD_BRACKET_NOTATION:
                    if (i < length - 1) {
                        if (c == '\'') {
                            state = ParserState.CHILD_NAME_BRACKET_NOTATION;
                            mark = i + 1;
                        }
                        else {
                            return null;
                        }
                    }
                    else {
                        return null;
                    }
                    break;
                case CHILD_NAME_BRACKET_NOTATION:
                    if (i < length - 1) {
                        if (c == '\'') {
                            if (i > mark) {
                                children.add(path.substring(mark, i));
                            }
                            else {
                                return null;
                            }
                            state = ParserState.END_CHILD_BRACKET_NOTATION;
                        }
                    }
                    else {
                        return null;
                    }
                    break;
                case END_CHILD_BRACKET_NOTATION:
                    if (c == ']') {
                        state = ParserState.START_CHILD;
                    }
                    else {
                        return null;
                    }
                    break;
            }  
        }
        return new JSONPath(children);
    }
public JSONPath parse ( String path ) { List < String > children = new ArrayList < > ( ) ; int mark = 0 ; ParserState state = ParserState . START_EXPRESSION ; final int length = path . length ( ) ; for ( int i = 0 ; i < length ; ++ i ) { char c = path . charAt ( i ) ; switch ( state ) { case START_EXPRESSION : if ( path . charAt ( 0 ) == '$' ) { state = ParserState . START_CHILD ; } else { return null ; } break ; case START_CHILD : if ( i < length - 1 ) { if ( c == '.' ) { state = ParserState . CHILD_NAME_DOT_NOTATION ; mark = i + 1 ; } else if ( c == '[' ) { state = ParserState . START_CHILD_BRACKET_NOTATION ; } else { return null ; } } else { return null ; } break ; case CHILD_NAME_DOT_NOTATION : if ( c == '.' || c == '[' ) { if ( i < length - 1 ) { if ( i > mark ) { children . add ( path . substring ( mark , i ) ) ; } else { return null ; } if ( c == '.' ) { mark = i + 1 ; } else { state = ParserState . START_CHILD_BRACKET_NOTATION ; } } else { return null ; } } else if ( i == length - 1 ) { children . add ( path . substring ( mark , i + 1 ) ) ; } break ; case START_CHILD_BRACKET_NOTATION : if ( i < length - 1 ) { if ( c == '\'' ) { state = ParserState . CHILD_NAME_BRACKET_NOTATION ; mark = i + 1 ; } else { return null ; } } else { return null ; } break ; case CHILD_NAME_BRACKET_NOTATION : if ( i < length - 1 ) { if ( c == '\'' ) { if ( i > mark ) { children . add ( path . substring ( mark , i ) ) ; } else { return null ; } state = ParserState . END_CHILD_BRACKET_NOTATION ; } } else { return null ; } break ; case END_CHILD_BRACKET_NOTATION : if ( c == ']' ) { state = ParserState . START_CHILD ; } else { return null ; } break ; } } return new JSONPath ( children ) ; }
Supports simple JSON path expressions (e.g.
Supports simple JSON path expressions ( e . g .
// State machine for walking the JSON path expression. // Reached end of child name. Add it to the list. // '[' // Reached end of child name. Add it to the list. // Reached end of child name. Add it to the list.