private Object navigatePath ( ) { int size = pathStack . size ( ) ; JSONObject innerMap = infoMap ; while ( size > 0 ) { Collection vals = innerMap . values ( ) ; if ( vals . size ( ) == 0 ) { return innerMap ; } else if ( vals . size ( ) != 1 ) { throw new IllegalStateException ( "Internal: More than one key found when extracting with path: " + vals ) ; } Object value = vals . iterator ( ) . next ( ) ; // End leaf, return it .... if ( size == 1 ) { return value ; } // Dive in deeper ... if ( ! ( value instanceof JSONObject ) ) { throw new IllegalStateException ( "Internal: Value within path extraction must be a Map, not " + value . getClass ( ) ) ; } innerMap = ( JSONObject ) value ; -- size ; } return innerMap ; }
Navigate to sub map or leaf value