Skip to content
This repository has been archived by the owner on Oct 23, 2024. It is now read-only.

Commit

Permalink
bug fixed for JSONObject.toJavaObject. for issue #1611
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Dec 12, 2017
1 parent 3c3d1b0 commit f00d9b3
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -1179,9 +1179,11 @@ public Object createInstance(Map<String, Object> map, ParserConfig config) //
FieldInfo[] fieldInfoList = beanInfo.fields;
int size = fieldInfoList.length;
Object[] params = new Object[size];
Map<String, Integer> missFields = null;
for (int i = 0; i < size; ++i) {
FieldInfo fieldInfo = fieldInfoList[i];
Object param = map.get(fieldInfo.name);

if (param == null) {
Class<?> fieldClass = fieldInfo.fieldClass;
if (fieldClass == int.class) {
Expand All @@ -1201,10 +1203,29 @@ public Object createInstance(Map<String, Object> map, ParserConfig config) //
} else if (fieldClass == boolean.class) {
param = false;
}
if (missFields == null) {
missFields = new HashMap<String, Integer>();
}
missFields.put(fieldInfo.name, i);
}
params[i] = param;
}


if (missFields != null) {
for (Map.Entry<String, Object> entry : map.entrySet()) {
String key = entry.getKey();
Object value = entry.getValue();

FieldDeserializer fieldDeser = smartMatch(key);
if (fieldDeser != null) {
Integer index = missFields.get(fieldDeser.fieldInfo.name);
if (index != null) {
params[index] = value;
}
}
}
}

if (beanInfo.creatorConstructor != null) {
try {
object = beanInfo.creatorConstructor.newInstance(params);
Expand Down
28 changes: 28 additions & 0 deletions src/test/java/com/alibaba/json/bvt/issue_1600/Issue1611.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.alibaba.json.bvt.issue_1600;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import junit.framework.TestCase;

public class Issue1611 extends TestCase {
public void test_for_issue() throws Exception {
String pristineJson = "{\"data\":{\"lists\":[{\"Name\":\"Mark\"}]}}";
JSONArray list = JSON.parseObject(pristineJson).getJSONObject("data").getJSONArray("lists");
assertEquals(1, list.size());
for (int i = 0; i < list.size(); i++) {
JSONObject sss = list.getJSONObject(i);
Model model = sss.toJavaObject(Model.class);
assertEquals("Mark", model.name);
}

}

public static class Model {
private String name;

public Model(String name) {
this.name = name;
}
}
}

0 comments on commit f00d9b3

Please sign in to comment.