Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 7.11 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package openecho.json;
  6.  
  7. import java.io.IOException;
  8. import java.util.Locale;
  9. import openecho.parser.ParserException;
  10. import org.junit.After;
  11. import org.junit.AfterClass;
  12. import org.junit.Before;
  13. import org.junit.BeforeClass;
  14. import org.junit.Test;
  15.  
  16. /**
  17.  *
  18.  * @author jmarsden
  19.  */
  20. public class JPathTest {
  21.  
  22.     JSONParser parser;
  23.  
  24.     public JPathTest() {
  25.     }
  26.  
  27.     @BeforeClass
  28.     public static void setUpClass() throws Exception {
  29.     }
  30.  
  31.     @AfterClass
  32.     public static void tearDownClass() throws Exception {
  33.     }
  34.  
  35.     @Before
  36.     public void setUp() {
  37.         parser = new JSONParser();
  38.         parser.setLocale(Locale.ENGLISH);
  39.     }
  40.  
  41.     @After
  42.     public void tearDown() {
  43.     }
  44.  
  45.     @Test
  46.     public void testGetMethod() throws IOException, ParserException {
  47.         StringBuilder randomDoubleArray = new StringBuilder();
  48.         randomDoubleArray.append("[" + Math.random());
  49.         for(int i=0;i<10000;i++) {
  50.             randomDoubleArray.append(",\t" + i);
  51.         }
  52.         randomDoubleArray.append("]");
  53.  
  54.         String jsonString =
  55.                 "{ \"store\": {\r\n"
  56.                 + "    \"book\": [ \r\n"
  57.                 + "      { \"category\": \"reference\",\r\n"
  58.                 + "        \"author\": \"Nigel Rees\",\r\n"
  59.                 + "        \"title\": \"Sayings of the Century\",\r\n"
  60.                 + "        \"price\": 8.95\r\n"
  61.                 + "      },\r\n"
  62.                 + "      { \"category\": \"fiction\",\r\n"
  63.                 + "        \"author\": \"Evelyn Waugh\",\r\n"
  64.                 + "        \"title\": \"Sword of Honour\",\r\n"
  65.                 + "        \"price\": 12.99\r\n"
  66.                 + "      },\r\n"
  67.                 + "      { \"category\": \"fiction\",\r\n"
  68.                 + "        \"author\": \"Herman Melville\",\r\n"
  69.                 + "        \"title\": \"Moby Dick\",\r\n"
  70.                 + "        \"isbn\": \"0-553-21311-3\",\r\n"
  71.                 + "        \"price\": 8.99,\r\n"
  72.                 + "        \"sam\": \"Awesome\"\r\n"
  73.                 + "      },\r\n"
  74.                 + "      { \"category\": \"fiction\",\r\n"
  75.                 + "        \"author\": \"J. R. R. Tolkien\",\r\n"
  76.                 + "        \"title\": \"The Lord of the Rings\",\r\n"
  77.                 + "        \"isbn\": \"0-395-19395-8\",\r\n"
  78.                 + "        \"price\": 22.99\r\n"
  79.                 + "      }\r\n"
  80.                 + "    ],\r\n"
  81.                 + "    \"bicycle\": {\r\n"
  82.                 + "      \"color\": \"red\",\r\n"
  83.                 + "      \"price\": 19.95\r\n"
  84.                 + "    },\r\n"
  85.                 + "    \"number\": " + randomDoubleArray.toString() + ""
  86.                 + "  }\r\n"
  87.                 + "}";
  88.         JSON json = JSON.parse(jsonString);
  89.         System.out.println(json.getRoot().getValueType() + "(" + json.getRoot().nestedSize() + "): " + json.getRoot().toJSON());
  90.  
  91.         JPathParser jPathParser = new JPathParser();
  92.         JPath path1 = jPathParser.parse("/store");
  93.         path1.setRecordEvaluateTime(true);
  94.         Value value = path1.evaluate(json);
  95.         System.out.println(value.getValueType() + "(" + value.nestedSize() + "): " + value.toJSON() + " (in " + path1.getLastEvaluateTime() + " ms)");
  96.  
  97.         JPath path2 = jPathParser.parse("/store/book[2]");
  98.         path2.setRecordEvaluateTime(true);
  99.         value = path2.evaluate(json);
  100.         System.out.println(value.getValueType() + "(" + value.nestedSize() + "): " + value.toJSON() + " (in " + path2.getLastEvaluateTime() + " ms)");
  101.  
  102.         JPath path3 = jPathParser.parse("/store/book[2]/title");
  103.         path3.setRecordEvaluateTime(true);
  104.         value = path3.evaluate(json);
  105.         System.out.println(value.getValueType() + "(" + value.nestedSize() + "): " + value.toJSON() + " (in " + path3.getLastEvaluateTime() + " ms)");
  106.  
  107.         JPath path4 = jPathParser.parse("/store/book[$]/title");
  108.         path4.setRecordEvaluateTime(true);
  109.         value = path4.evaluate(json);
  110.         System.out.println(value.getValueType() + "(" + value.nestedSize() + "): " + value.toJSON() + " (in " + path4.getLastEvaluateTime() + " ms)");
  111.  
  112.         JPath path5 = jPathParser.parse("/store/book[last()-3]/title");
  113.         path5.setRecordEvaluateTime(true);
  114.         value = path5.evaluate(json);
  115.         System.out.println(value.getValueType() + "(" + value.nestedSize() + "): " + value.toJSON() + " (in " + path5.getLastEvaluateTime() + " ms)");
  116.  
  117.         JPath path6 = jPathParser.parse("/store/book[last()-1337]/title");
  118.         try {
  119.             value = path6.evaluate(json);
  120.         } catch (Exception e) {
  121.             System.out.println(e.toString());
  122.         }
  123.  
  124.         JPath path7 = jPathParser.parse("/store/number[$-1337]");
  125.         path7.setRecordEvaluateTime(true);
  126.         value = path7.evaluate(json);
  127.         System.out.println(value.getValueType() + "(" + value.nestedSize() + "): " + value.toJSON() + " (in " + path7.getLastEvaluateTime() + " ms)");
  128.  
  129.     }
  130. }
  131.  
  132.  
  133. /**
  134. Output
  135.  
  136. 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,
  137. ..
  138. Lots more numbers
  139. ..
  140. , 9998, 9999]}}
  141. 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,..
  142. Lots more numbers
  143. ..
  144.  9996, 9997, 9998, 9999]} (in 640 ms)
  145. OBJECT(6): {"category":"fiction","author":"Herman Melville","title":"Moby Dick","isbn":"0-553-21311-3","price":8.99,"sam":"Awesome"} (in 163 ms)
  146. STRING(0): "Moby Dick" (in 23 ms)
  147. STRING(0): "The Lord of the Rings" (in 17 ms)
  148. STRING(0): "Sword of Honour" (in 13 ms)
  149. openecho.json.JPathRuntimeException: Component '2'. Predicate Index Out of Bounds. [-1333]
  150. NUMERIC(0): 8663 (in 16 ms)
  151. **/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement