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

Commit

Permalink
bug fix toJavaObject, for issue #2516
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Jul 31, 2019
1 parent ee76c9d commit 7731562
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/main/java/com/alibaba/fastjson/JSON.java
Original file line number Diff line number Diff line change
Expand Up @@ -1107,6 +1107,10 @@ public static <T> T toJavaObject(JSON json, Class<T> clazz) {
* @since 1.2.9
*/
public <T> T toJavaObject(Class<T> clazz) {
if (clazz == JSONArray.class || clazz == JSON.class || clazz == Collection.class || clazz == List.class) {
return (T) this;
}

return TypeUtils.cast(this, clazz, ParserConfig.getGlobalInstance());
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/alibaba/fastjson/JSONObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@ protected void readStreamHeader() throws IOException, StreamCorruptedException {
}

public <T> T toJavaObject(Class<T> clazz) {
if (clazz == Map.class) {
if (clazz == Map.class || clazz == JSONObject.class || clazz == JSON.class) {
return (T) this;
}

Expand Down
23 changes: 23 additions & 0 deletions src/test/java/com/alibaba/json/bvt/issue_2500/Issue2516.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.alibaba.json.bvt.issue_2500;

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

import java.util.Collection;
import java.util.List;

public class Issue2516 extends TestCase
{
public void test_for_issue() throws Exception {
JSONObject jsonObject = new JSONObject();
jsonObject.toJavaObject(JSONObject.class);
jsonObject.toJavaObject(JSON.class);

new JSONArray().toJavaObject(JSON.class);
new JSONArray().toJavaObject(JSONArray.class);
new JSONArray().toJavaObject(Collection.class);
new JSONArray().toJavaObject(List.class);
}
}

0 comments on commit 7731562

Please sign in to comment.