Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package openecho.json;
- import java.io.IOException;
- import java.util.Locale;
- import openecho.parser.ParserException;
- import org.junit.After;
- import org.junit.AfterClass;
- import org.junit.Before;
- import org.junit.BeforeClass;
- import org.junit.Test;
- /**
- *
- * @author jmarsden
- */
- public class JPathTest {
- JSONParser parser;
- public JPathTest() {
- }
- @BeforeClass
- public static void setUpClass() throws Exception {
- }
- @AfterClass
- public static void tearDownClass() throws Exception {
- }
- @Before
- public void setUp() {
- parser = new JSONParser();
- parser.setLocale(Locale.ENGLISH);
- }
- @After
- public void tearDown() {
- }
- @Test
- public void testGetMethod() throws IOException, ParserException {
- StringBuilder randomDoubleArray = new StringBuilder();
- randomDoubleArray.append("[" + Math.random());
- for(int i=0;i<10000;i++) {
- randomDoubleArray.append(",\t" + i);
- }
- randomDoubleArray.append("]");
- String jsonString =
- "{ \"store\": {\r\n"
- + " \"book\": [ \r\n"
- + " { \"category\": \"reference\",\r\n"
- + " \"author\": \"Nigel Rees\",\r\n"
- + " \"title\": \"Sayings of the Century\",\r\n"
- + " \"price\": 8.95\r\n"
- + " },\r\n"
- + " { \"category\": \"fiction\",\r\n"
- + " \"author\": \"Evelyn Waugh\",\r\n"
- + " \"title\": \"Sword of Honour\",\r\n"
- + " \"price\": 12.99\r\n"
- + " },\r\n"
- + " { \"category\": \"fiction\",\r\n"
- + " \"author\": \"Herman Melville\",\r\n"
- + " \"title\": \"Moby Dick\",\r\n"
- + " \"isbn\": \"0-553-21311-3\",\r\n"
- + " \"price\": 8.99,\r\n"
- + " \"sam\": \"Awesome\"\r\n"
- + " },\r\n"
- + " { \"category\": \"fiction\",\r\n"
- + " \"author\": \"J. R. R. Tolkien\",\r\n"
- + " \"title\": \"The Lord of the Rings\",\r\n"
- + " \"isbn\": \"0-395-19395-8\",\r\n"
- + " \"price\": 22.99\r\n"
- + " }\r\n"
- + " ],\r\n"
- + " \"bicycle\": {\r\n"
- + " \"color\": \"red\",\r\n"
- + " \"price\": 19.95\r\n"
- + " },\r\n"
- + " \"number\": " + randomDoubleArray.toString() + ""
- + " }\r\n"
- + "}";
- JSON json = JSON.parse(jsonString);
- System.out.println(json.getRoot().getValueType() + "(" + json.getRoot().nestedSize() + "): " + json.getRoot().toJSON());
- JPathParser jPathParser = new JPathParser();
- JPath path1 = jPathParser.parse("/store");
- path1.setRecordEvaluateTime(true);
- Value value = path1.evaluate(json);
- System.out.println(value.getValueType() + "(" + value.nestedSize() + "): " + value.toJSON() + " (in " + path1.getLastEvaluateTime() + " ms)");
- JPath path2 = jPathParser.parse("/store/book[2]");
- path2.setRecordEvaluateTime(true);
- value = path2.evaluate(json);
- System.out.println(value.getValueType() + "(" + value.nestedSize() + "): " + value.toJSON() + " (in " + path2.getLastEvaluateTime() + " ms)");
- JPath path3 = jPathParser.parse("/store/book[2]/title");
- path3.setRecordEvaluateTime(true);
- value = path3.evaluate(json);
- System.out.println(value.getValueType() + "(" + value.nestedSize() + "): " + value.toJSON() + " (in " + path3.getLastEvaluateTime() + " ms)");
- JPath path4 = jPathParser.parse("/store/book[$]/title");
- path4.setRecordEvaluateTime(true);
- value = path4.evaluate(json);
- System.out.println(value.getValueType() + "(" + value.nestedSize() + "): " + value.toJSON() + " (in " + path4.getLastEvaluateTime() + " ms)");
- JPath path5 = jPathParser.parse("/store/book[last()-3]/title");
- path5.setRecordEvaluateTime(true);
- value = path5.evaluate(json);
- System.out.println(value.getValueType() + "(" + value.nestedSize() + "): " + value.toJSON() + " (in " + path5.getLastEvaluateTime() + " ms)");
- JPath path6 = jPathParser.parse("/store/book[last()-1337]/title");
- try {
- value = path6.evaluate(json);
- } catch (Exception e) {
- System.out.println(e.toString());
- }
- JPath path7 = jPathParser.parse("/store/number[$-1337]");
- path7.setRecordEvaluateTime(true);
- value = path7.evaluate(json);
- System.out.println(value.getValueType() + "(" + value.nestedSize() + "): " + value.toJSON() + " (in " + path7.getLastEvaluateTime() + " ms)");
- }
- }
- /**
- Output
- OBJECT(10030): {"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,"sam":"Awesome"}, {"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},"number":[0.3682880742380762, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68,
- ..
- Lots more numbers
- ..
- , 9998, 9999]}}
- OBJECT(10029): {"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,"sam":"Awesome"}, {"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},"number":[0.3682880742380762, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68,..
- Lots more numbers
- ..
- 9996, 9997, 9998, 9999]} (in 640 ms)
- OBJECT(6): {"category":"fiction","author":"Herman Melville","title":"Moby Dick","isbn":"0-553-21311-3","price":8.99,"sam":"Awesome"} (in 163 ms)
- STRING(0): "Moby Dick" (in 23 ms)
- STRING(0): "The Lord of the Rings" (in 17 ms)
- STRING(0): "Sword of Honour" (in 13 ms)
- openecho.json.JPathRuntimeException: Component '2'. Predicate Index Out of Bounds. [-1333]
- NUMERIC(0): 8663 (in 16 ms)
- **/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement