-
Notifications
You must be signed in to change notification settings - Fork 453
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TypeReference support null argument, for issue #1267
- Loading branch information
Showing
4 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
core/src/test/java/com/alibaba/fastjson2/TypeReferenceTest4.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.alibaba.fastjson2; | ||
|
||
import lombok.Data; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
public class TypeReferenceTest4 { | ||
@Test | ||
public void test() { | ||
ExtInfoBean extInfoBean = new ExtInfoBean(); | ||
extInfoBean.setInfoId("id"); | ||
extInfoBean.setInfoName(ExtInfoBean.class.getName()); | ||
extInfoBean.setInfoData(null); | ||
|
||
String json = JSON.toJSONString(extInfoBean); | ||
assertEquals("{\"infoId\":\"id\",\"infoName\":\"com.alibaba.fastjson2.TypeReferenceTest4$ExtInfoBean\"}", json); | ||
|
||
ExtInfoBean result = testTypeReference(json, null); | ||
assertEquals(json, JSON.toJSONString(result)); | ||
} | ||
|
||
private static <T> ExtInfoBean<T> testTypeReference(String extJson, Class<T> clz) { | ||
ExtInfoBean extInfoBean = JSON.parseObject(extJson, | ||
new TypeReference<ExtInfoBean<T>>(clz) { | ||
}); | ||
|
||
return extInfoBean; | ||
} | ||
|
||
/** | ||
* @author weizhiyu | ||
* @since 2022/07/06 2:35 PM | ||
*/ | ||
@Data | ||
public static class ExtInfoBean<T> { | ||
private String infoId; | ||
private String infoName; | ||
private T infoData; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
fastjson1-compatible/src/test/java/com/alibaba/fastjson/TypeReferenceTest4.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.alibaba.fastjson; | ||
|
||
import lombok.Data; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
public class TypeReferenceTest4 { | ||
@Test | ||
public void test() { | ||
ExtInfoBean extInfoBean = new ExtInfoBean(); | ||
extInfoBean.setInfoId("id"); | ||
extInfoBean.setInfoName(ExtInfoBean.class.getName()); | ||
extInfoBean.setInfoData(null); | ||
|
||
String json = com.alibaba.fastjson2.JSON.toJSONString(extInfoBean); | ||
assertEquals("{\"infoId\":\"id\",\"infoName\":\"com.alibaba.fastjson.TypeReferenceTest4$ExtInfoBean\"}", json); | ||
|
||
ExtInfoBean result = testTypeReference(json, null); | ||
assertEquals(json, com.alibaba.fastjson2.JSON.toJSONString(result)); | ||
} | ||
|
||
private static <T> ExtInfoBean<T> testTypeReference(String extJson, Class<T> clz) { | ||
ExtInfoBean extInfoBean = JSON.parseObject(extJson, | ||
new TypeReference<ExtInfoBean<T>>(clz) { | ||
}); | ||
|
||
return extInfoBean; | ||
} | ||
|
||
/** | ||
* @author weizhiyu | ||
* @since 2022/07/06 2:35 PM | ||
*/ | ||
@Data | ||
public static class ExtInfoBean<T> { | ||
private String infoId; | ||
private String infoName; | ||
private T infoData; | ||
} | ||
} |