-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
57831ef
commit 7d81037
Showing
5 changed files
with
169 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
src/main/java/com/shimizukenta/jsonhub/JsonPathUnsupportedParseException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters