/src/test/java/com/alibaba/json/bvt/path/BookExtractTest.java

https://github.com/alibaba/fastjson · Java · 103 lines · 81 code · 22 blank · 0 comment · 0 complexity · 88360a41aaaa1daa661b253eb4698d51 MD5 · raw file

  1. package com.alibaba.json.bvt.path;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.alibaba.fastjson.JSONPath;
  5. import com.alibaba.fastjson.parser.Feature;
  6. import com.alibaba.fastjson.util.IOUtils;
  7. import junit.framework.TestCase;
  8. import java.io.InputStream;
  9. import java.io.InputStreamReader;
  10. public class BookExtractTest extends TestCase {
  11. private String json;
  12. protected void setUp() throws Exception {
  13. InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("json/book.json");
  14. InputStreamReader reader = new InputStreamReader(is);
  15. json = IOUtils.readAll(reader);
  16. IOUtils.close(reader);
  17. }
  18. public void test_0() throws Exception {
  19. assertEquals(4, JSONPath.extract(json, "$..book.length()"));
  20. }
  21. public void test_1() throws Exception {
  22. assertEquals("[\"reference\",\"Nigel Rees\",\"Sayings of the Century\",8.95,\"fiction\",\"Evelyn Waugh\",\"Sword of Honour\",12.99,\"fiction\",\"Herman Melville\",\"Moby Dick\",\"0-553-21311-3\",8.99,\"fiction\",\"J. R. R. Tolkien\",\"The Lord of the Rings\",\"0-395-19395-8\",22.99,\"red\",19.95,10]"
  23. , JSON.toJSONString(JSONPath.extract(json, "$..*")));
  24. }
  25. public void test_2() throws Exception {
  26. assertEquals("[\"Nigel Rees\",\"Evelyn Waugh\",\"Herman Melville\",\"J. R. R. Tolkien\"]", JSON.toJSONString(JSONPath.extract(json, "$.store.book[*].author")));
  27. }
  28. public void test_3() throws Exception {
  29. assertEquals("[\"Nigel Rees\",\"Evelyn Waugh\",\"Herman Melville\",\"J. R. R. Tolkien\"]", JSON.toJSONString(JSONPath.extract(json, "$..author")));
  30. }
  31. public void test_4() throws Exception {
  32. assertEquals("[8.95,12.99,8.99,22.99,19.95]", JSON.toJSONString(JSONPath.extract(json, "$..price")));
  33. }
  34. public void test_5() throws Exception {
  35. assertEquals("[8.95,12.99,8.99,22.99]", JSON.toJSONString(JSONPath.extract(json, "$..book.price")));
  36. }
  37. public void test_6() throws Exception {
  38. assertEquals("[[{\"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}],{\"color\":\"red\",\"price\":19.95}]"
  39. , JSON.toJSONString(JSONPath.extract(json, "$.store.*")));
  40. }
  41. public void test_7() throws Exception {
  42. assertEquals("[8.95,12.99,8.99,22.99,19.95]"
  43. , JSON.toJSONString(JSONPath.extract(json, "$.store..price")));
  44. }
  45. public void test_8() throws Exception {
  46. assertEquals("{\"category\":\"fiction\",\"author\":\"Herman Melville\",\"title\":\"Moby Dick\",\"isbn\":\"0-553-21311-3\",\"price\":8.99}"
  47. , JSON.toJSONString(JSONPath.extract(json, "$..book[2]")));
  48. }
  49. public void test_9() throws Exception {
  50. assertEquals("{\"category\":\"fiction\",\"author\":\"J. R. R. Tolkien\",\"title\":\"The Lord of the Rings\",\"isbn\":\"0-395-19395-8\",\"price\":22.99}"
  51. , JSON.toJSONString(JSONPath.extract(json, "$..book[-1]")));
  52. }
  53. public void test_10() throws Exception {
  54. assertEquals("[{\"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}]"
  55. , JSON.toJSONString(JSONPath.extract(json, "$..book[0,1]")));
  56. }
  57. public void test_11() throws Exception {
  58. assertEquals("[{\"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}]"
  59. , JSON.toJSONString(JSONPath.extract(json, "$..book[?(@.isbn)]")));
  60. }
  61. public void test_12() throws Exception {
  62. assertEquals("[{\"category\":\"reference\",\"author\":\"Nigel Rees\",\"title\":\"Sayings of the Century\",\"price\":8.95},{\"category\":\"fiction\",\"author\":\"Herman Melville\",\"title\":\"Moby Dick\",\"isbn\":\"0-553-21311-3\",\"price\":8.99}]"
  63. , JSON.toJSONString(JSONPath.extract(json, "$.store.book[?(@.price < 10)]")));
  64. }
  65. public void test_13() throws Exception {
  66. assertEquals("[{\"category\":\"reference\",\"author\":\"Nigel Rees\",\"title\":\"Sayings of the Century\",\"price\":8.95},{\"category\":\"fiction\",\"author\":\"Herman Melville\",\"title\":\"Moby Dick\",\"isbn\":\"0-553-21311-3\",\"price\":8.99}]"
  67. , JSON.toJSONString(JSONPath.extract(json, "$..book[?(@.price <= $['expensive'])]")));
  68. }
  69. public void test_14() throws Exception {
  70. assertEquals("[{\"category\":\"reference\",\"author\":\"Nigel Rees\",\"title\":\"Sayings of the Century\",\"price\":8.95}]"
  71. , JSON.toJSONString(JSONPath.extract(json, "$..book[?(@.author =~ /.*REES/i)]")));
  72. }
  73. public void test_15() throws Exception {
  74. assertEquals("[{\"category\":\"reference\",\"author\":\"Nigel Rees\",\"title\":\"Sayings of the Century\",\"price\":8.95}]"
  75. , JSON.toJSONString(JSONPath.extract(json, "$..book[?(@.author =~ /.*REES/i)]")));
  76. }
  77. public void test_16() throws Exception {
  78. assertEquals("[{\"category\":\"fiction\",\"author\":\"Herman Melville\",\"title\":\"Moby Dick\",\"isbn\":\"0-553-21311-3\",\"price\":8.99}]"
  79. , JSON.toJSONString(JSONPath.extract(json, "$.store.book[?(@.price < 10 && @.category == 'fiction')]")));
  80. }
  81. }