JSONObject.toString does not use an Enum's `toString()` but its `name()`
还没有人认领这个 Issue。
评估
调研方向
从 src/main/java/org/json/JSONObject.java 中 issue 标识为序列化路径的第 2656 行附近开始。运行提供的 TestEnumToString 复现,并检查 enum 字段是如何转换的。当 JSONObject.toString() 遵循被重写的 enum 字符串表示形式,且复现产生预期值时,即表示完成。
由索引模型根据 Issue 内容生成。
描述
When calling the toString method of a JSONObject, if a field of the object is an enum, the name method is called (when constructing the string) instead of the toString method. This is problematic as name() is not overidable (as it is decalred final) whereas toString is.
I have found where this is in the sources.
Furthermore, it is specified in the java doc of the name function :
Most programmers should use the toString() method in preference to this one, as the toString method may return a more user-friendly name
The fix should be really easy, I can do it if it helps you. Is this an issue not tracked ? I have not found corresponding issues.
Here's a quick way to reproduce this default.
public class TestEnumToString {
enum MyEnum {
V_1, V_2;
@Override
public String toString() {
switch (this) {
case V_1:
return "1.0";
case V_2:
return "2.0";
};
return "";
}
}
public static void main(String[] args) throws JSONException {
JSONObject json = new JSONObject();
json.put("v1", MyEnum.V_1);
json.put("v2", MyEnum.V_2);
System.out.println(json.toString());
}
}
Thanks in advance,
Arthur
- 主要语言
- Java
- 星标
- 4.7k
- 派生
- 2.6k
- 平均合并
- 6 天 20 小时
- 30 天内合并 PR
- 2
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
stleary/JSON-java 的其他 Issue
-
Fix before the next release
难度 2/5 1-3 小时 新手友好度 78/100
-
难度 2/5 1-3 小时 新手友好度 76/100
-
难度 2/5 1-3 小时 新手友好度 88/100
-
难度 2/5 1-3 小时 新手友好度 82/100
-
Active discussion
难度 5/5 一周以上 新手友好度 35/100
查看 stleary/JSON-java 的全部 Issue
相似的 Issue
-
bug
难度 1/5 1 小时以内 新手友好度 90/100
apache/cloudstack#14222 ·
-
难度 2/5 1-3 小时 新手友好度 88/100
-
1.0.0-alpha2 Type/Improvement
难度 2/5 1-3 小时 新手友好度 68/100
wso2/dpdp-accelerator#272 ·
-
难度 2/5 1-3 小时 新手友好度 82/100
infinispan/infinispan#18150 ·
-
area/frontend
难度 2/5 1-3 小时 新手友好度 65/100