From be15615f2c814cfea4a581536b441c7e0d6467a9 Mon Sep 17 00:00:00 2001 From: wenshao Date: Mon, 13 Mar 2017 15:42:55 +0800 Subject: [PATCH] bug fixed for jsonpath. issue #1030 --- .../java/com/alibaba/fastjson/JSONPath.java | 10 ++++++-- .../com/alibaba/json/bvt/bug/Issue1030.java | 24 +++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 src/test/java/com/alibaba/json/bvt/bug/Issue1030.java diff --git a/src/main/java/com/alibaba/fastjson/JSONPath.java b/src/main/java/com/alibaba/fastjson/JSONPath.java index f774da9795..48769dce94 100644 --- a/src/main/java/com/alibaba/fastjson/JSONPath.java +++ b/src/main/java/com/alibaba/fastjson/JSONPath.java @@ -78,7 +78,8 @@ public Object eval(Object rootObject) { Object currentObject = rootObject; for (int i = 0; i < segments.length; ++i) { - currentObject = segments[i].eval(this, rootObject, currentObject); + Segement segement = segments[i]; + currentObject = segement.eval(this, rootObject, currentObject); } return currentObject; } @@ -2155,7 +2156,12 @@ protected Object getPropertyValue(final Object currentObject, final String prope for (int i = 0; i < list.size(); ++i) { Object obj = list.get(i); Object itemValue = getPropertyValue(obj, propertyName, strictMode); - fieldValues.add(itemValue); + if (itemValue instanceof Collection) { + Collection collection = (Collection) itemValue; + fieldValues.addAll(collection); + } else { + fieldValues.add(itemValue); + } } return fieldValues; diff --git a/src/test/java/com/alibaba/json/bvt/bug/Issue1030.java b/src/test/java/com/alibaba/json/bvt/bug/Issue1030.java new file mode 100644 index 0000000000..4f329455e8 --- /dev/null +++ b/src/test/java/com/alibaba/json/bvt/bug/Issue1030.java @@ -0,0 +1,24 @@ +package com.alibaba.json.bvt.bug; + +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; +import com.alibaba.fastjson.JSONPath; +import junit.framework.TestCase; + +import java.util.List; + +/** + * Created by wenshao on 13/03/2017. + */ +public class Issue1030 extends TestCase { + public void test_for_issue() throws Exception { + String DOC = "{\"books\":[{\"pageWords\":[{\"num\":10},{\"num\":15}]},{\"pageWords\":[{\"num\":20}]}]}"; + + //fastjson包 + JSONObject result = JSONObject.parseObject(DOC); + + List array = (List) JSONPath.eval(result, "$.books[0:].pageWords[0:]"); + + assertEquals(3, array.size()); + } +}