Skip to content

Commit

Permalink
修改节点为空时,转字符串的bug
Browse files Browse the repository at this point in the history
  • Loading branch information
wmz46 committed Mar 8, 2021
1 parent 882f4dc commit f4427cf
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<dependency>
<groupId>com.iceolive</groupId>
<artifactId>xpathmapper</artifactId>
<version>1.2.3</version>
<version>1.2.4</version>
</dependency>
```
## 二、快速开始
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</parent>
<groupId>com.iceolive</groupId>
<artifactId>xpathmapper</artifactId>
<version>1.2.3</version>
<version>1.2.4</version>
<description>xpathmapper is a XML processor (XML parser + XML generator) written in Java</description>
<licenses>
<license>
Expand Down
22 changes: 13 additions & 9 deletions src/main/java/com/iceolive/xpathmapper/JsonPathMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ public static <T> T parse(Object obj, Class<T> clazz) {

private static <T> T parse(Object obj, Class<T> clazz, String format) {
if (ReflectUtil.isBasicType(clazz)) {
return (T) StringUtil.parse(obj.toString(), format, clazz);
if (obj != null) {
return (T) StringUtil.parse(obj.toString(), format, clazz);
} else {
return null;
}
}
List<Field> fields = ReflectUtil.getAllFields(clazz);
T newObj = ReflectUtil.newInstance(clazz);
Expand All @@ -43,8 +47,8 @@ private static <T> T parse(Object obj, Class<T> clazz, String format) {
continue;
}
JSONField jsonField = field.getAnnotation(JSONField.class);
String dateFormat ="";
if(jsonField !=null && !StringUtil.isEmpty(jsonField.format())){
String dateFormat = "";
if (jsonField != null && !StringUtil.isEmpty(jsonField.format())) {
dateFormat = jsonField.format();
}
boolean isList = false;
Expand Down Expand Up @@ -113,9 +117,9 @@ private static <T> T parse(Object obj, Class<T> clazz, String format) {
ReflectUtil.setValue(newObj, field.getName(), values);

} else if (List.class.isAssignableFrom(currentObj.getClass())) {
Object values = ReflectUtil.newInstance(field, ((List)currentObj).size());
for (int i = 0; i < ((List)currentObj).size(); i++) {
Object item = parse(((List)currentObj).get(i), type, dateFormat);
Object values = ReflectUtil.newInstance(field, ((List) currentObj).size());
for (int i = 0; i < ((List) currentObj).size(); i++) {
Object item = parse(((List) currentObj).get(i), type, dateFormat);
((List) values).add(item);
}
ReflectUtil.setValue(newObj, field.getName(), values);
Expand Down Expand Up @@ -143,8 +147,8 @@ private static <T> T parse(Object obj, Class<T> clazz, String format) {
ReflectUtil.setValue(newObj, field.getName(), values);

} else if (Set.class.isAssignableFrom(currentObj.getClass())) {
Object values = ReflectUtil.newInstance(field,((Set)currentObj).size());
for (Object c : (Set)currentObj) {
Object values = ReflectUtil.newInstance(field, ((Set) currentObj).size());
for (Object c : (Set) currentObj) {
Object item = parse(c, type, dateFormat);
((Set) values).add(item);
}
Expand All @@ -164,5 +168,5 @@ private static <T> T parse(Object obj, Class<T> clazz, String format) {
}
return newObj;
}

}

0 comments on commit f4427cf

Please sign in to comment.