/src/test/java/com/alibaba/json/bvt/path/extract/JSONPath_extract_2_book.java

https://github.com/alibaba/fastjson · Java · 128 lines · 102 code · 26 blank · 0 comment · 0 complexity · d5b040b19881c10dc193be36dfcec951 MD5 · raw file

  1. package com.alibaba.json.bvt.path.extract;
  2. import com.alibaba.fastjson.JSONPath;
  3. import com.alibaba.fastjson.util.IOUtils;
  4. import junit.framework.TestCase;
  5. import java.io.InputStream;
  6. import java.io.InputStreamReader;
  7. public class JSONPath_extract_2_book extends TestCase {
  8. public void test_0() throws Exception {
  9. assertEquals("[\"Nigel Rees\",\"Evelyn Waugh\",\"Herman Melville\",\"J. R. R. Tolkien\"]"
  10. , JSONPath.extract(json, "$.store.book.author")
  11. .toString());
  12. }
  13. public void test_1() throws Exception {
  14. assertEquals("[\"Nigel Rees\",\"Evelyn Waugh\",\"Herman Melville\",\"J. R. R. Tolkien\"]"
  15. , JSONPath.extract(json, "$.store.book[*].author")
  16. .toString());
  17. }
  18. public void test_2() throws Exception {
  19. assertNull(JSONPath.extract(json, "$.author"));
  20. }
  21. public void test_3() throws Exception {
  22. assertEquals("[\"Nigel Rees\",\"Evelyn Waugh\",\"Herman Melville\",\"J. R. R. Tolkien\"]"
  23. , JSONPath.extract(json, "$..author")
  24. .toString());
  25. }
  26. public void test_4() throws Exception {
  27. 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}]"
  28. , JSONPath.extract(json, "$.store.*")
  29. .toString());
  30. }
  31. public void test_5() throws Exception {
  32. assertEquals("$.store..price", "[8.95,12.99,8.99,22.99,19.95]"
  33. , JSONPath.extract(json, "$.store..price")
  34. .toString());
  35. }
  36. public void test_6() throws Exception {
  37. assertEquals("{\"category\":\"fiction\",\"author\":\"Herman Melville\",\"title\":\"Moby Dick\",\"isbn\":\"0-553-21311-3\",\"price\":8.99}"
  38. , JSONPath.extract(json, "$..book[2]")
  39. .toString());
  40. }
  41. public void test_7 () throws Exception {
  42. 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}]"
  43. , JSONPath.extract(json, "$..book[0,1]")
  44. .toString());
  45. }
  46. public void test_8() throws Exception {
  47. assertEquals("{\"category\":\"fiction\",\"author\":\"Herman Melville\",\"title\":\"Moby Dick\",\"isbn\":\"0-553-21311-3\",\"price\":8.99}"
  48. , JSONPath.extract(json, "$..book[-2]")
  49. .toString());
  50. }
  51. public void test_9() throws Exception {
  52. assertEquals("Nigel Rees"
  53. , JSONPath.extract(json, "$['store']['book'][0]['author']")
  54. .toString());
  55. assertEquals("Evelyn Waugh"
  56. , JSONPath.extract(json, "$['store']['book'][1]['author']")
  57. .toString());
  58. assertEquals("Herman Melville"
  59. , JSONPath.extract(json, "$['store']['book'][2]['author']")
  60. .toString());
  61. assertEquals("J. R. R. Tolkien"
  62. , JSONPath.extract(json, "$['store']['book'][3]['author']")
  63. .toString());
  64. }
  65. public void test_10() 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. , JSONPath.extract(json, "$.store.book[?(@.price < 10)]")
  68. .toString());
  69. }
  70. public void test_11() throws Exception {
  71. assertEquals("10"
  72. , JSONPath.extract(json, "$.expensive")
  73. .toString());
  74. }
  75. public void test_12() throws Exception {
  76. assertNull(JSONPath.extract(json, "$.store.book.doesnt_exist"));
  77. }
  78. public void test_13() throws Exception {
  79. assertEquals("J. R. R. Tolkien", JSONPath.extract(json, "$.store.book[3].author"));
  80. }
  81. public void test_14() throws Exception {
  82. 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}]"
  83. , JSONPath.extract(json, "$.store.book").toString());
  84. }
  85. public void test_15() throws Exception {
  86. assertEquals("{\"category\":\"reference\",\"author\":\"Nigel Rees\",\"title\":\"Sayings of the Century\",\"price\":8.95}"
  87. , JSONPath.extract(json, "$[\"store\"][\"book\"][0]").toString());
  88. }
  89. public void test_16() throws Exception {
  90. assertNull(JSONPath.extract(json, "$.store.object.inner_object.array[0].inner_array[0].x"));
  91. }
  92. public void test_17() throws Exception {
  93. assertEquals(4, JSONPath.extract(json, "$..book.length()"));
  94. }
  95. private static String json;
  96. static {
  97. InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("json/book.json");
  98. InputStreamReader reader = new InputStreamReader(is);
  99. json = IOUtils.readAll(reader);
  100. IOUtils.close(reader);
  101. }
  102. }