-
Notifications
You must be signed in to change notification settings - Fork 495
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
父类字段定义了忽略字段后,子类继承get方法忽略字段不生效,导致死循环 (#2914)
* 父类字段定义了忽略字段后,自定继承get方法忽略字段不生效,导致死循环 父类字段定义了忽略字段后,自定继承get方法忽略字段不生效,导致死循环 * 进一步处理继承get方法死循环问题 进一步处理继承get方法死循环问题
- Loading branch information
Showing
3 changed files
with
117 additions
and
2 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
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
114 changes: 114 additions & 0 deletions
114
core/src/test/java/com/alibaba/fastjson2/writer/InheritMethodWriterTest.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,114 @@ | ||
package com.alibaba.fastjson2.writer; | ||
|
||
import com.alibaba.fastjson2.JSON; | ||
import com.alibaba.fastjson2.JSONObject; | ||
import com.alibaba.fastjson2.annotation.JSONField; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.io.UnsupportedEncodingException; | ||
import java.net.URLEncoder; | ||
import java.util.Set; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
/** | ||
* 继承字段忽略 | ||
*/ | ||
public class InheritMethodWriterTest { | ||
@Test | ||
public void writer() { | ||
UrlEntity entity = new UrlEntity("https://www.baidu.com/web/user"); | ||
entity.setName("rose"); | ||
String text = JSON.toJSONString(entity); | ||
assertEquals(text, "{\"name\":\"rose\"}"); | ||
} | ||
|
||
@Test | ||
public void read() { | ||
String text = "{\"name\":\"rose\", \"url\":\"https://www.baidu.com/web/user\"}"; | ||
UrlEntity entity = JSON.parseObject(text, UrlEntity.class); | ||
System.out.println(entity); | ||
assertEquals(entity.getName(), "rose"); | ||
assertEquals(entity.getUrl(), "https://www.baidu.com/web/user?name=rose"); | ||
} | ||
public abstract static class AbstractJsonEntity { | ||
@JSONField(serialize = false) | ||
private String url; | ||
|
||
public AbstractJsonEntity(String url) { | ||
this.url = url; | ||
} | ||
|
||
public String getUrl() { | ||
return url; | ||
} | ||
|
||
public void setUrl(String url) { | ||
this.url = url; | ||
} | ||
} | ||
public class UrlEntity | ||
extends AbstractJsonEntity { | ||
private String name; | ||
|
||
public UrlEntity(String url) { | ||
super(url); | ||
} | ||
|
||
// @JSONField(serialize = false) | ||
@Override | ||
public String getUrl() { | ||
String text = JSON.toJSONString(this); | ||
JSONObject queryObj = JSONObject.parseObject(text); | ||
|
||
StringBuffer query = new StringBuffer(); | ||
Set<String> keys = queryObj.keySet(); | ||
for (String key : keys) { | ||
String value = queryObj.getString(key); | ||
if (StringUtils.isBlank(value)) { | ||
continue; | ||
} | ||
|
||
String encodeKey = ""; | ||
String encodeValue = ""; | ||
try { | ||
encodeKey = URLEncoder.encode(key, "UTF-8"); | ||
encodeValue = URLEncoder.encode(value, "UTF-8"); | ||
} catch (UnsupportedEncodingException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
if (StringUtils.isBlank(encodeValue)) { | ||
continue; | ||
} | ||
|
||
if (StringUtils.isBlank(query)) { | ||
query.append("?"); | ||
} else { | ||
query.append("&"); | ||
} | ||
query.append(encodeKey).append("=").append(encodeValue); | ||
} | ||
|
||
if (StringUtils.isNotBlank(query)) { | ||
return super.getUrl() + query; | ||
} | ||
|
||
return super.getUrl(); | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "UrlEntity [name=" + name + ", url=" + getUrl() + "]"; | ||
} | ||
} | ||
} |