Implement opt/get methods for accumulated values
Nadie ha tomado este issue todavía.
Evaluación
- Dificultad
- 3/5
- Tiempo estimado
- 1-2 días
- Aptitud para principiantes
- 38/100
- Tipo de issue
- Nueva funcionalidad
- Claridad
- Bastante claro
- Estado de actividad
- Estancado
- Stack tecnológico
- java
- Área
- backend-api-design
Línea de trabajo
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.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
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
- Lenguaje dominante
- Java
- Estrellas
- 4.7k
- Forks
- 2.6k
- Merge medio
- 6 d 20 h
- PR fusionados (30 d)
- 2
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de stleary/JSON-java
-
Fix before the next release
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
-
New JSONPointer tests needed Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 76/100
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 88/100
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 82/100
-
Fix before the next release
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
Todos los issues de stleary/JSON-java
Issues similares
-
executions.Query — startDate and timeRange filters are sent with inverted comparison operators Abiertoarea/plugin
Dificultad 2/5 1-3 horas Aptitud para principiantes 75/100
kestra-io/plugin-kestra#190 ·
-
litertlm-android AAR ships no consumer ProGuard rules → "mid == null" SIGABRT in minified apps Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 70/100
google-ai-edge/LiteRT-LM#3739 ·
-
Add canonical URLs and a sitemap Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 75/100
integra-team-red/meet-map#249 ·
-
[Studio][Bug] Cancelled create-user dialog keeps the password and admin switch for the next attempt Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 75/100
apache/rocketmq-dashboard#5064 ·
-
Consent portal: creating a duplicate Purpose shows a generic error instead of "already exists" Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 75/100
wso2/dpdp-accelerator#287 ·