Implement opt/get methods for accumulated values
还没有人认领这个 Issue。
评估
- 难度
- 3/5
- 预计耗时
- 1-2 天
- 新手友好度
- 38/100
- Issue 类型
- 功能
- 描述清晰度
- 基本清楚
- 活跃度
- 停滞
- 技术栈
- java
调研方向
Start by reading JSONObject.accumulate, opt, and get, then inspect the JSONArray constructors and conversion behavior. Implement and verify the proposed optAccumulated and getAccumulated behavior for missing, null, scalar, array, collection, and existing JSONArray values, including the single- and multiple-element XML examples.
由索引模型根据 Issue 内容生成。
描述
Currently we have an accumulate method on JSONObject, however, we don't have an easy way to get back out a consistent value (i.e. JSONArray).
One area where this is most painful is in XML to JSON conversion where some XML documents may contain a single element, while other documents may contain many.
Example 1:
<Student>
<name>jack</name>
<subjects>
<class>english</class><grade>98</grade>
</subjects>
<subjects>
<class>english</class><grade>98</grade>
</subjects>
</Student>
Example 2:
<Student>
<name>jack</name>
<subjects>
<class>english</class><grade>98</grade>
</subjects>
</Student>
This currently leads to very messy code that has to check the type of value parsed:
void someFunction() {
JSONObject jo = XML.toJSONObject(someXmlSource);
JSONObject student = jo.getJSONObject("Student");
if ( student.optJSONArray("subjects") ==null ) {
processSubject(student.optJSONObject("subjects"));
} else {
processSubjects(student.optJSONArray("subjects"));
}
}
void processSubjects(JSONArray subjects) {
if (subjects == null || subjects.length() == 0) return;
for(int i=0; i<subjects.length; i++) {
processSubject(subjects.getJSONObject(i));
}
}
void processSubject(JSONObject subject) {
// some implementation
}
It would be nice if there was a convenience function in the JSONObject to get values that are accumulated:
// In JSONObject
/**
* Always returns a JSONArray, If the key doesn't exist or the value is `null`, the array will be empty.
* If the key holds a non-array/non-collection value, then it is placed in a JSONArray
* before returning.
* If the value is a JSONArray, then a copy is returned.
* If the value is directly convertible to a JSONArray (i.e. a JAVA Array or Collection), then it is
* converted to a JSONArray.
* @param key The key in the JSONObject to fetch
* @returns a JSONArray regardless of the value of the key.
*/
public JSONArray optAccumulated(String key) {
Object o = opt(key);
if (JSONObject.NULL.equals(o)) {
return new JSONArray();
}
if (o instanceof JSONArray) {
return new JSONArray(((JSONArray)o).myArrayList); // maybe create a copy constructor and/or extend putAll
}
if (o instanceof Collection || o.getClass().isArray()) {
return new JSONArray(o);
}
JSONArray ret = new JSONArray();
ret.put(o);
return ret;
}
/**
* Always returns a JSONArray, If the key value is `null`, the array will be empty.
* If the key holds a non-array/non-collection value, then it is placed in a JSONArray
* before returning.
* If the value is a JSONArray, then a copy is returned.
* If the value is directly convertible to a JSONArray (i.e. a JAVA Array or Collection), then it is
* converted to a JSONArray.
* @param key The key in the JSONObject to fetch
* @returns a JSONArray regardless of the value of the key.
* @throws JSONException Thrown when the key does not exist
*/
public JSONArray getAccumulated(String key) throws JSONException {
Object o = get(key);
if (JSONObject.NULL.equals(o)) {
return new JSONArray();
}
if (o instanceof JSONArray) {
return new JSONArray(((JSONArray)o).myArrayList); // maybe create a copy constructor and/or extend putAll
}
if (o instanceof Collection || o.getClass().isArray()) {
return new JSONArray(o);
}
JSONArray ret = new JSONArray();
ret.put(o);
return ret;
}
See also #550
- 主要语言
- 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
-
Fix before the next release
难度 2/5 1-3 小时 新手友好度 78/100
查看 stleary/JSON-java 的全部 Issue
相似的 Issue
-
area/plugin
难度 2/5 1-3 小时 新手友好度 75/100
kestra-io/plugin-kestra#190 ·
-
难度 2/5 1-3 小时 新手友好度 70/100
google-ai-edge/LiteRT-LM#3739 ·
-
bug
难度 2/5 1-3 小时 新手友好度 75/100
-
难度 2/5 1-3 小时 新手友好度 75/100
integra-team-red/meet-map#249 ·
-
[Studio][Bug] Cancelled create-user dialog keeps the password and admin switch for the next attempt 未关闭
难度 2/5 1-3 小时 新手友好度 75/100
apache/rocketmq-dashboard#5064 ·