Skip to content

#7 Tests for JsonPath add operation. Fixed PathParser #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 81 additions & 23 deletions src/main/java/com/gravity9/jsonpatch/PathParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class PathParser {
* @return PathDetails containing path to parent, name of new node and boolean value if path contains filter or multi
* index notation
* @throws JsonPatchException when invalid path provided
*/
* */
public static PathDetails getParentPathAndNewNodeName(String path) throws JsonPatchException {
final String fullJsonPath = JsonPathParser.tmfStringToJsonPath(path);
final Path compiledPath = compilePath(fullJsonPath);
Expand Down Expand Up @@ -53,28 +53,28 @@ public static PathDetails getParentPathAndNewNodeName(String path) throws JsonPa

private static boolean isMultiIndexNotation(String path) {
String pathWithoutBracket = path
.replace("[", "")
.replace("]", "");
.replace("[", "")
.replace("]", "");
return !pathWithoutBracket.startsWith("'") && !pathWithoutBracket.matches("[0-9]+");
}

private static String getNewNodeName(String[] splitJsonPath) {
return splitJsonPath[splitJsonPath.length - 1]
.replace("'", "")
.replace("[", "")
.replace("]", "");
.replace("'", "")
.replace("[", "")
.replace("]", "");
}

/**
* Removes $ sign from the beginning of the path for easier processing - it will be added later on
* This method is called after PathCompiler.compile, so we are sure now, that the path is correct
* and bracket notation is used.
* We can now split JsonPath using positive lookahead regex (without removing separator).
*/
* */
private static String[] splitJsonPath(Path compiledPath) {
return compiledPath.toString()
.replace("$", "")
.split("(?=\\[)");
.replace("$", "")
.split("(?=\\[)");
}

private static Path compilePath(String fullJsonPath) throws JsonPatchException {
Expand All @@ -98,23 +98,81 @@ private static List<String> getFiltersOperations(String jsonPath) {
return new ArrayList<>();
}
List<String> filters = new ArrayList<>();
int openingBracketPosition = -1;
int counter = 0;
FilterParserState filterParserState = new FilterParserState();
for (int i = 0; i < jsonPath.length(); i++) {
if (jsonPath.charAt(i) == '[' && jsonPath.charAt(i + 1) == '?') {
if (openingBracketPosition == -1) {
openingBracketPosition = i;
}
counter++;
checkIfOpenBracket(jsonPath, i, filterParserState);
checkIfCloseBracket(jsonPath, i, filterParserState, filters);
}
return filters;
}

private static void checkIfCloseBracket(String jsonPath, int currentPosition, FilterParserState filterParserState, List<String> filters) {
if (jsonPath.charAt(currentPosition) == ']' && filterParserState.getNumberOfOpenInternalSquareBrackets() > 0) {
filterParserState.decreaseNumberOfOpenInternalSquareBrackets();
} else if (jsonPath.charAt(currentPosition) == ']' && filterParserState.getNumberOfOpenedFilters() > 0) {
filterParserState.decreaseNumberOfOpenedFilters();
if (filterParserState.noOpenFiltersAndBrackets()) {
filters.add(jsonPath.substring(filterParserState.getFilterOpeningPosition(), currentPosition + 1));
filterParserState.setFilterOpeningPosition(-1);
}
if (jsonPath.charAt(i) == ']' && counter > 0) {
counter--;
if (counter == 0) {
filters.add(jsonPath.substring(openingBracketPosition, i + 1));
openingBracketPosition = -1;
}
}
}

private static void checkIfOpenBracket(String jsonPath, int currentPosition, FilterParserState filterParserState) {
if (jsonPath.charAt(currentPosition) == '[' && jsonPath.charAt(currentPosition + 1) == '?') {
if (!filterParserState.wasOpeningFilterBracketFound()) {
filterParserState.setFilterOpeningPosition(currentPosition);
}
filterParserState.increaseNumberOfOpenedFilters();
} else if (jsonPath.charAt(currentPosition) == '[') {
filterParserState.increaseNumberOfOpenInternalSquareBrackets();
}
}

private static class FilterParserState {

private int filterOpeningPosition = -1;
private int numberOfOpenedFilters = 0; // filter starts with [?
private int numberOfOpenInternalSquareBrackets = 0; // only [, without ?

public boolean wasOpeningFilterBracketFound() {
return filterOpeningPosition != -1;
}

public void setFilterOpeningPosition(int position) {
this.filterOpeningPosition = position;
}

public void increaseNumberOfOpenedFilters() {
this.numberOfOpenedFilters++;
}

public void decreaseNumberOfOpenedFilters() {
this.numberOfOpenedFilters--;
}

public void increaseNumberOfOpenInternalSquareBrackets() {
this.numberOfOpenInternalSquareBrackets++;
}

public void decreaseNumberOfOpenInternalSquareBrackets() {
this.numberOfOpenInternalSquareBrackets--;
}

public int getNumberOfOpenedFilters() {
return numberOfOpenedFilters;
}

public int getNumberOfOpenInternalSquareBrackets() {
return numberOfOpenInternalSquareBrackets;
}

public int getFilterOpeningPosition() {
return filterOpeningPosition;
}

public boolean noOpenFiltersAndBrackets() {
return numberOfOpenedFilters == 0 && numberOfOpenInternalSquareBrackets == 0;
}
return filters;
}
}
Loading