Skip to content

Commit

Permalink
improved jsonpath support. fixed issue #735
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Jul 22, 2016
1 parent 0343ceb commit 53a5e09
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 6 deletions.
57 changes: 56 additions & 1 deletion src/main/java/com/alibaba/fastjson/JSONPath.java
Expand Up @@ -5,6 +5,7 @@
import java.math.BigDecimal; import java.math.BigDecimal;
import java.math.BigInteger; import java.math.BigInteger;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection; import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.IdentityHashMap; import java.util.IdentityHashMap;
Expand Down Expand Up @@ -936,6 +937,10 @@ Segement parseArrayAccess(boolean acceptBracket) {
} }


String text = path.substring(start, end); String text = path.substring(start, end);

if (text.indexOf("\\.") != -1) {
return new PropertySegement(text, false);
}


Segement segment = buildArraySegement(text); Segement segment = buildArraySegement(text);


Expand Down Expand Up @@ -2062,7 +2067,13 @@ protected Object getPropertyValue(final Object currentObject, final String prope


if (currentObject instanceof Map) { if (currentObject instanceof Map) {
Map map = (Map) currentObject; Map map = (Map) currentObject;
return map.get(propertyName); Object val = map.get(propertyName);

if (val == null && "size".equals(propertyName)) {
val = map.size();
}

return val;
} }


final Class<?> currentClass = currentObject.getClass(); final Class<?> currentClass = currentObject.getClass();
Expand All @@ -2078,6 +2089,10 @@ protected Object getPropertyValue(final Object currentObject, final String prope


if (currentObject instanceof List) { if (currentObject instanceof List) {
List list = (List) currentObject; List list = (List) currentObject;

if ("size".equals(propertyName)) {
return list.size();
}


List<Object> fieldValues = new JSONArray(list.size()); List<Object> fieldValues = new JSONArray(list.size());


Expand All @@ -2089,6 +2104,46 @@ protected Object getPropertyValue(final Object currentObject, final String prope


return fieldValues; return fieldValues;
} }

if (currentObject instanceof Enum) {
Enum e = (Enum) currentObject;
if ("name".equals(propertyName)) {
return e.name();
}

if ("ordinal".equals(propertyName)) {
return e.ordinal();
}
}

if (currentObject instanceof Calendar) {
Calendar e = (Calendar) currentObject;

if ("year".equals(propertyName)) {
return e.get(Calendar.YEAR);
}

if ("month".equals(propertyName)) {
return e.get(Calendar.MONTH);
}

if ("day".equals(propertyName)) {
return e.get(Calendar.DAY_OF_MONTH);
}

if ("hour".equals(propertyName)) {
return e.get(Calendar.HOUR_OF_DAY);
}

if ("minute".equals(propertyName)) {
return e.get(Calendar.MINUTE);
}

if ("second".equals(propertyName)) {
return e.get(Calendar.SECOND);
}
}

throw new JSONPathException("jsonpath error, path " + path + ", segement " + propertyName); throw new JSONPathException("jsonpath error, path " + path + ", segement " + propertyName);
} }


Expand Down
7 changes: 3 additions & 4 deletions src/test/java/com/alibaba/json/bvt/bug/Bug_for_issue_729.java
Expand Up @@ -5,6 +5,7 @@


import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONField; import com.alibaba.fastjson.annotation.JSONField;
import com.alibaba.json.bvt.bug.Bug_for_issue_729.Person;


import junit.framework.TestCase; import junit.framework.TestCase;


Expand All @@ -13,13 +14,11 @@ public class Bug_for_issue_729 extends TestCase {
public void test_for_issue() throws Exception { public void test_for_issue() throws Exception {
Person person = new Person(); Person person = new Person();
person.setName("bob"); person.setName("bob");
person.setStartTime(new Date()); person.startTime = new Date();


String result = JSON.toJSONString(person); String result = JSON.toJSONString(person);
System.out.println(result);
Person person2 = JSON.parseObject(result, Person.class); Person person2 = JSON.parseObject(result, Person.class);
System.out.println(person2.toString()); person2.toString();

} }


public static class Person implements Serializable { public static class Person implements Serializable {
Expand Down
17 changes: 17 additions & 0 deletions src/test/java/com/alibaba/json/bvt/path/JSONPath_4.java
@@ -0,0 +1,17 @@
package com.alibaba.json.bvt.path;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONPath;

import junit.framework.TestCase;

public class JSONPath_4 extends TestCase {

public void test_path() throws Exception {
String a = "{\"key\":\"value\",\"10.0.1.1\":\"haha\"}";
Object x = JSON.parse(a);
System.out.println(JSON.toJSONString(x));
JSONPath.set(x, "$.test", "abc");
Object o = JSONPath.eval(x, "$.10\\.0\\.1\\.1");
}
}
32 changes: 32 additions & 0 deletions src/test/java/com/alibaba/json/bvt/path/JSONPath_enum.java
@@ -0,0 +1,32 @@
package com.alibaba.json.bvt.path;

import org.junit.Assert;

import com.alibaba.fastjson.JSONPath;

import junit.framework.TestCase;

public class JSONPath_enum extends TestCase {

public void test_name() throws Exception {
Model model = new Model();
model.size = Size.Small;

Assert.assertEquals(Size.Small.name(), JSONPath.eval(model, "$.size.name"));
}

public void test_orginal() throws Exception {
Model model = new Model();
model.size = Size.Small;

Assert.assertEquals(Size.Small.ordinal(), JSONPath.eval(model, "$.size.ordinal"));
}

public static class Model {
public Size size;
}

public static enum Size {
Big, Median, Small
}
}
11 changes: 10 additions & 1 deletion src/test/java/com/alibaba/json/bvt/path/JSONPath_list_size.java
Expand Up @@ -10,7 +10,7 @@
import com.alibaba.fastjson.JSONPath; import com.alibaba.fastjson.JSONPath;


public class JSONPath_list_size extends TestCase { public class JSONPath_list_size extends TestCase {
public void test_list_map() throws Exception { public void test_list_size() throws Exception {
List list = new ArrayList(); List list = new ArrayList();
list.add(new Object()); list.add(new Object());
list.add(new Object()); list.add(new Object());
Expand All @@ -20,4 +20,13 @@ public void test_list_map() throws Exception {
Assert.assertEquals(list.size(), result.intValue()); Assert.assertEquals(list.size(), result.intValue());
} }


public void test_list_size2() throws Exception {
List list = new ArrayList();
list.add(new Object());
list.add(new Object());
list.add(new Object());
JSONPath path = new JSONPath("$.size");
Integer result = (Integer) path.eval(list);
Assert.assertEquals(list.size(), result.intValue());
}
} }
19 changes: 19 additions & 0 deletions src/test/java/com/alibaba/json/bvt/path/JSONPath_map_size.java
@@ -0,0 +1,19 @@
package com.alibaba.json.bvt.path;

import java.util.Collections;

import org.junit.Assert;

import com.alibaba.fastjson.JSONPath;

import junit.framework.TestCase;

public class JSONPath_map_size extends TestCase {
public void test_list_size() throws Exception {
Assert.assertEquals(0, JSONPath.eval(Collections.emptyMap(), "$.size"));
}

public void test_list_size1() throws Exception {
Assert.assertEquals(0, JSONPath.eval(Collections.emptyMap(), "$.size()"));
}
}

0 comments on commit 53a5e09

Please sign in to comment.