diff --git a/README.md b/README.md index 822ede3..b734517 100644 --- a/README.md +++ b/README.md @@ -241,6 +241,14 @@ System.out.println(json); See also ["/src/examples/example07/CreateJsonStringByBuilder.java"](/src/examples/example07/CreateJsonStringByBuilder.java) +## JSONPath + +Supports + + + +See also ["/src/examples/example11/JsonPath.java"](/src/examples/example11/JsonPath.java) + ## JSONC Reading JSONC (JSON with comments) support. diff --git a/src/examples/example10/ReadJsoncFile.java b/src/examples/example10/ReadJsoncFile.java index 329dd88..115d214 100644 --- a/src/examples/example10/ReadJsoncFile.java +++ b/src/examples/example10/ReadJsoncFile.java @@ -8,7 +8,7 @@ import java.util.List; import com.shimizukenta.jsonhub.JsonHub; -import com.shimizukenta.jsonhub.impl.JsoncReaderImpl; +import com.shimizukenta.jsonhub.JsoncReader; public class ReadJsoncFile { @@ -45,7 +45,7 @@ public static void main(String[] args) { StandardOpenOption.CREATE); } - JsonHub jh = JsoncReaderImpl.readFile(path); + JsonHub jh = JsoncReader.readFile(path); System.out.println(jh.prettyPrint()); } diff --git a/src/examples/example11/JsonPath.java b/src/examples/example11/JsonPath.java new file mode 100644 index 0000000..743a822 --- /dev/null +++ b/src/examples/example11/JsonPath.java @@ -0,0 +1,74 @@ +package example11; + +import java.util.List; + +import com.shimizukenta.jsonhub.JsonHub; +import com.shimizukenta.jsonhub.JsonPathParseException; + +public class JsonPath { + + public JsonPath() { + /* Nothing */ + } + + public static void main(String[] args) { + + String json = "{ \"store\": { " + + " \"book\": [ " + + " { \"category\": \"reference\", " + + " \"author\": \"Nigel Rees\", " + + " \"title\": \"Sayings of the Century\"," + + " \"price\": 8.95 " + + " }, " + + " { \"category\": \"fiction\", " + + " \"author\": \"Evelyn Waugh\", " + + " \"title\": \"Sword of Honour\", " + + " \"price\": 12.99 " + + " }, " + + " { \"category\": \"fiction\", " + + " \"author\": \"Herman Melville\", " + + " \"title\": \"Moby Dick\", " + + " \"isbn\": \"0-553-21311-3\", " + + " \"price\": 8.99 " + + " }, " + + " { \"category\": \"fiction\", " + + " \"author\": \"J. R. R. Tolkien\", " + + " \"title\": \"The Lord of the Rings\", " + + " \"isbn\": \"0-395-19395-8\", " + + " \"price\": 22.99 " + + " } " + + " ], " + + " \"bicycle\": { " + + " \"color\": \"red\", " + + " \"price\": 19.95 " + + " } " + + " } " + + "} "; + + JsonHub jh = JsonHub.fromJson(json); + + tryJsonPath(jh, "$.store.book[*].author"); + tryJsonPath(jh, "$..author"); + tryJsonPath(jh, "$.store.*"); + tryJsonPath(jh, "$.store..price"); + tryJsonPath(jh, "$..book[2]"); + tryJsonPath(jh, "$..book[-1]"); + tryJsonPath(jh, "$..book[:2]"); + + } + + private static void tryJsonPath(JsonHub jh, String jsonPath) { + try { + System.out.println("JsonPath: \"" + jsonPath + "\""); + List ll = jh.jsonPath(jsonPath); + System.out.println("result length: " + ll.size()); + ll.forEach(x -> { + System.out.println(x.prettyPrint()); + }); + System.out.println(); + } + catch (JsonPathParseException e) { + e.printStackTrace(); + } + } +} diff --git a/src/main/java/com/shimizukenta/jsonhub/JsonPathUnsupportedParseException.java b/src/main/java/com/shimizukenta/jsonhub/JsonPathUnsupportedParseException.java new file mode 100644 index 0000000..0216e88 --- /dev/null +++ b/src/main/java/com/shimizukenta/jsonhub/JsonPathUnsupportedParseException.java @@ -0,0 +1,62 @@ +package com.shimizukenta.jsonhub; + +/** + * JsonPath unsupported parse Exception. + * + * @author kenta-shimizu + * + */ +public class JsonPathUnsupportedParseException extends JsonPathParseException { + + private static final long serialVersionUID = 7284639685947525518L; + + /** + * Constructor. + * + */ + public JsonPathUnsupportedParseException() { + super(); + } + + /** + * Constructor. + * + * @param message the message + */ + public JsonPathUnsupportedParseException(String message) { + super(message); + } + + /** + * Constructor. + * + * @param cause the cause + */ + public JsonPathUnsupportedParseException(Throwable cause) { + super(cause); + } + + /** + * Constructor. + * + * @param message the message + * @param cause the cause + */ + public JsonPathUnsupportedParseException(String message, Throwable cause) { + super(message, cause); + } + + /** + * Constructor. + * + * @param message the message + * @param cause the cause + * @param enableSuppression the enableSuppression + * @param writableStackTrace the writableStackTrace + */ + public JsonPathUnsupportedParseException(String message, Throwable cause, boolean enableSuppression, + boolean writableStackTrace) { + super(message, cause, enableSuppression, writableStackTrace); + } + +} diff --git a/src/main/java/com/shimizukenta/jsonhub/impl/JsonPathParser.java b/src/main/java/com/shimizukenta/jsonhub/impl/JsonPathParser.java index 5696674..0b7dcd1 100644 --- a/src/main/java/com/shimizukenta/jsonhub/impl/JsonPathParser.java +++ b/src/main/java/com/shimizukenta/jsonhub/impl/JsonPathParser.java @@ -9,6 +9,7 @@ import com.shimizukenta.jsonhub.JsonHub; import com.shimizukenta.jsonhub.JsonPathParseException; +import com.shimizukenta.jsonhub.JsonPathUnsupportedParseException; /** * This class is JsonPath parser. @@ -271,7 +272,7 @@ private void recursive(JsonHub jh, List ll) { } - //TODO + //HOOK //filter //script @@ -393,6 +394,19 @@ private static FindBracketGetterResult findBracketEnd(String jp, int pos, boolea final FindCharResult r = FindChars.nextIgnoreWhiteSpace(jp, pos); + if ( r.c == '*' ) { + final FindCharResult er = FindChars.nextIgnoreWhiteSpace(jp, r.pos + 1); + if ( er.c == ']' ) { + if ( recursive ) { + return new FindBracketGetterResult(recursiveWildcard, er.pos + 1); + } else { + return new FindBracketGetterResult(childWildcard, er.pos + 1); + } + } else { + throw new JsonPathParseException("Fount after '*'. position: " + er.pos); + } + } + if ( (r.c >= '0' && r.c <= '9') || r.c == '-' @@ -636,16 +650,20 @@ private static int findScriptEnd(String jp, int fromIndex) { private static JsonPathGetter parseFilterGetter(String filter) { - //TODO + //HOOK - return (a, b) -> a; + throw new JsonPathUnsupportedParseException("?()"); + +// return (a, b) -> a; } private static JsonPathGetter parseScriptGetter(String script) { - //TODO + //HOOK + + throw new JsonPathUnsupportedParseException("()"); - return (a, b) -> a; +// return (a, b) -> a; } }