Skip to content

Commit 0aca192

Browse files
author
Piotr Bugara
committed
#37 Renamed JsonPathParser method to be not TMF relevant.
Changed access modifier of JsonPathParser class and it's method to package scope.
1 parent 9decb19 commit 0aca192

File tree

8 files changed

+25
-19
lines changed

8 files changed

+25
-19
lines changed

src/main/java/com/gravity9/jsonpatch/CopyOperation.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public CopyOperation(@JsonProperty("from") final String from, @JsonProperty("pat
4949

5050
@Override
5151
public JsonNode applyInternal(final JsonNode node) throws JsonPatchException {
52-
final String jsonPath = JsonPathParser.tmfStringToJsonPath(from);
52+
final String jsonPath = JsonPathParser.parsePathToJsonPath(from);
5353
final JsonNode dupData = JsonPath.parse(node.deepCopy()).read(jsonPath);
5454
if (dupData == null) {
5555
throw new JsonPatchException(BUNDLE.getMessage("jsonPatch.noSuchPath"));

src/main/java/com/gravity9/jsonpatch/JsonPathParser.java

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
package com.gravity9.jsonpatch;
22

3-
public class JsonPathParser {
3+
class JsonPathParser {
44

55
private static final String ARRAY_ELEMENT_REGEX = "(?<=\\.)(\\d+)";
66

7-
public static String tmfStringToJsonPath(String path) throws JsonPatchException {
7+
/**
8+
* Method parses JsonPointer or JsonPath path to JsonPath syntax
9+
* @param path String containing JsonPath or JsonPointer expression
10+
* @return String containing JsonPath expression
11+
* @throws JsonPatchException throws when invalid JsonPointer expression provided
12+
*/
13+
static String parsePathToJsonPath(String path) throws JsonPatchException {
814
if (path.startsWith("$")) {
915
return path;
1016
} else if (path.contains("?")) {

src/main/java/com/gravity9/jsonpatch/MoveOperation.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public JsonNode applyInternal(final JsonNode node) throws JsonPatchException {
7373
if (from.equals(path)) {
7474
return node.deepCopy();
7575
}
76-
String jsonPath = JsonPathParser.tmfStringToJsonPath(from);
76+
String jsonPath = JsonPathParser.parsePathToJsonPath(from);
7777
final JsonNode movedNode = JsonPath.parse(node.deepCopy()).read(jsonPath, JsonNode.class);
7878
if (movedNode == null) {
7979
throw new JsonPatchException(BUNDLE.getMessage("jsonPatch.noSuchPath"));

src/main/java/com/gravity9/jsonpatch/PathParser.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class PathParser {
1919
* @throws JsonPatchException when invalid path provided
2020
* */
2121
public static PathDetails getParentPathAndNewNodeName(String path) throws JsonPatchException {
22-
final String fullJsonPath = JsonPathParser.tmfStringToJsonPath(path);
22+
final String fullJsonPath = JsonPathParser.parsePathToJsonPath(path);
2323
final Path compiledPath = compilePath(fullJsonPath);
2424
String[] splitJsonPath = splitJsonPath(compiledPath);
2525

src/main/java/com/gravity9/jsonpatch/RemoveOperation.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@
2222
import com.fasterxml.jackson.annotation.JsonCreator;
2323
import com.fasterxml.jackson.annotation.JsonProperty;
2424
import com.fasterxml.jackson.core.JsonGenerator;
25-
import com.fasterxml.jackson.core.JsonProcessingException;
2625
import com.fasterxml.jackson.databind.JsonNode;
2726
import com.fasterxml.jackson.databind.SerializerProvider;
2827
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
2928
import com.fasterxml.jackson.databind.node.MissingNode;
3029
import com.jayway.jsonpath.DocumentContext;
3130
import com.jayway.jsonpath.JsonPath;
31+
3232
import java.io.IOException;
3333

3434
/**
@@ -51,7 +51,7 @@ public JsonNode applyInternal(final JsonNode node) throws JsonPatchException {
5151
}
5252

5353
final DocumentContext nodeContext = JsonPath.parse(node.deepCopy());
54-
final String jsonPath = JsonPathParser.tmfStringToJsonPath(path);
54+
final String jsonPath = JsonPathParser.parsePathToJsonPath(path);
5555

5656
if (nodeContext.read(jsonPath) == null) {
5757
throw new JsonPatchException(BUNDLE.getMessage("jsonPatch.noSuchPath"));
@@ -62,16 +62,16 @@ public JsonNode applyInternal(final JsonNode node) throws JsonPatchException {
6262
}
6363

6464
@Override
65-
public void serialize(final JsonGenerator jgen, final SerializerProvider provider) throws IOException, JsonProcessingException {
65+
public void serialize(final JsonGenerator jgen, final SerializerProvider provider) throws IOException {
6666
jgen.writeStartObject();
6767
jgen.writeStringField("op", "remove");
68-
jgen.writeStringField("path", path.toString());
68+
jgen.writeStringField("path", path);
6969
jgen.writeEndObject();
7070
}
7171

7272
@Override
7373
public void serializeWithType(final JsonGenerator jgen, final SerializerProvider provider, final TypeSerializer typeSer)
74-
throws IOException, JsonProcessingException {
74+
throws IOException {
7575
serialize(jgen, provider);
7676
}
7777

src/main/java/com/gravity9/jsonpatch/ReplaceOperation.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public ReplaceOperation(@JsonProperty("path") final String path, @JsonProperty("
4343

4444
@Override
4545
public JsonNode applyInternal(final JsonNode node) throws JsonPatchException {
46-
final String jsonPath = JsonPathParser.tmfStringToJsonPath(path);
46+
final String jsonPath = JsonPathParser.parsePathToJsonPath(path);
4747
final DocumentContext nodeContext = JsonPath.parse(node.deepCopy());
4848
final JsonNode nodeAtPath = nodeContext.read(jsonPath);
4949
if (nodeAtPath == null) {

src/main/java/com/gravity9/jsonpatch/TestOperation.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public TestOperation(@JsonProperty("path") final String path, @JsonProperty("val
4949

5050
@Override
5151
public JsonNode applyInternal(final JsonNode node) throws JsonPatchException {
52-
final String jsonPath = JsonPathParser.tmfStringToJsonPath(path);
52+
final String jsonPath = JsonPathParser.parsePathToJsonPath(path);
5353
final JsonNode tested = JsonPath.parse(node.deepCopy()).read(jsonPath);
5454
if (tested == null) {
5555
throw new JsonPatchException(BUNDLE.getMessage("jsonPatch.noSuchPath"));

src/test/java/com/gravity9/jsonpatch/JsonPathParserTest.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -10,51 +10,51 @@ public class JsonPathParserTest {
1010
public void shouldConvertPointerToJsonPath() throws JsonPatchException {
1111
String jsonPointerWithQuery = "/productPrice/prodPriceAlteration";
1212
String expected = "$.productPrice.prodPriceAlteration";
13-
String result = JsonPathParser.tmfStringToJsonPath(jsonPointerWithQuery);
13+
String result = JsonPathParser.parsePathToJsonPath(jsonPointerWithQuery);
1414
assertEquals(result, expected);
1515
}
1616

1717
@Test
1818
public void shouldConvertPointerWithArrayToJsonPath() throws JsonPatchException {
1919
String jsonPointerWithQuery = "/productPrice/1/prodPriceAlteration";
2020
String expected = "$.productPrice.[1].prodPriceAlteration";
21-
String result = JsonPathParser.tmfStringToJsonPath(jsonPointerWithQuery);
21+
String result = JsonPathParser.parsePathToJsonPath(jsonPointerWithQuery);
2222
assertEquals(result, expected);
2323
}
2424

2525
@Test
2626
public void shouldConvertPointerWithArrayAtTheEndToJsonPath() throws JsonPatchException {
2727
String jsonPointerWithQuery = "/productPrice/prodPriceAlteration/1";
2828
String expected = "$.productPrice.prodPriceAlteration.[1]";
29-
String result = JsonPathParser.tmfStringToJsonPath(jsonPointerWithQuery);
29+
String result = JsonPathParser.parsePathToJsonPath(jsonPointerWithQuery);
3030
assertEquals(result, expected);
3131
}
3232

3333
@Test
3434
public void shouldConvertArrayPathToJsonPath() throws JsonPatchException {
3535
String jsonPointer = "/2/1/-";
3636
String expected = "$.[2].[1].-";
37-
String result = JsonPathParser.tmfStringToJsonPath(jsonPointer);
37+
String result = JsonPathParser.parsePathToJsonPath(jsonPointer);
3838
assertEquals(result, expected);
3939
}
4040

4141
@Test
4242
public void shouldLeaveJsonPathStatementsUntouched() throws JsonPatchException {
4343
String filterQuery = "$.arrayPath[?(@.innerArray[?(@.nestedVal=='as')] empty false)].innerArray[?(@.nestedVal=='df')].name";
4444
String expected = "$.arrayPath[?(@.innerArray[?(@.nestedVal=='as')] empty false)].innerArray[?(@.nestedVal=='df')].name";
45-
String result = JsonPathParser.tmfStringToJsonPath(filterQuery);
45+
String result = JsonPathParser.parsePathToJsonPath(filterQuery);
4646
assertEquals(result, expected);
4747
}
4848

4949
@Test(expectedExceptions = JsonPatchException.class, expectedExceptionsMessageRegExp = "Invalid path, `//` is not allowed in JsonPointer expressions.")
5050
public void shouldThrowExceptionWhenDoubleSlashesInJsonPointerPath() throws JsonPatchException {
5151
String filterQuery = "/characteristic/0//age";
52-
JsonPathParser.tmfStringToJsonPath(filterQuery);
52+
JsonPathParser.parsePathToJsonPath(filterQuery);
5353
}
5454

5555
@Test(expectedExceptions = JsonPatchException.class)
5656
public void shouldThrowExceptionWhenQuestionMarkInJsonPointerPath() throws JsonPatchException {
5757
String filterQuery = "/characteristic/0/age?";
58-
JsonPathParser.tmfStringToJsonPath(filterQuery);
58+
JsonPathParser.parsePathToJsonPath(filterQuery);
5959
}
6060
}

0 commit comments

Comments
 (0)