Skip to content

Commit

Permalink
2023-06-28T00:36
Browse files Browse the repository at this point in the history
  • Loading branch information
kenta-shimizu committed Jun 27, 2023
1 parent 57831ef commit 7d81037
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 7 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions src/examples/example10/ReadJsoncFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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());
}
Expand Down
74 changes: 74 additions & 0 deletions src/examples/example11/JsonPath.java
Original file line number Diff line number Diff line change
@@ -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<JsonHub> 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();
}
}
}
Original file line number Diff line number Diff line change
@@ -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);
}

}
28 changes: 23 additions & 5 deletions src/main/java/com/shimizukenta/jsonhub/impl/JsonPathParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import com.shimizukenta.jsonhub.JsonHub;
import com.shimizukenta.jsonhub.JsonPathParseException;
import com.shimizukenta.jsonhub.JsonPathUnsupportedParseException;

/**
* This class is JsonPath parser.
Expand Down Expand Up @@ -271,7 +272,7 @@ private void recursive(JsonHub jh, List<JsonHub> ll) {
}


//TODO
//HOOK
//filter
//script

Expand Down Expand Up @@ -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 == '-'
Expand Down Expand Up @@ -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;
}

}

0 comments on commit 7d81037

Please sign in to comment.