Skip to content

Make JSONArray and JSONObject generic and add a getAsObject() and getAsArray() methods to JSONObject class #215

Description

@SuperPat45

Can you make JSONArray generic:, so we can write:
JSONArray<JSONObject> array = new JSONArray<JSONObject>();

JSONObject can also be generic for the value:
JSONObject<String> object = new JSONObject<String>();

To remove the need of castings when getting an object or an array, add, in the JSONObject class, two methods getAsObject() and getAsArray() methods:

function <T> JSONObject<T> getAsObject(String key) {
}
function <T> JSONArray<T> getAsArray(String key) {
}

If the map or the collection is not a JSONObject or JSONArray, the method will convert it.

This can be used easily without cast and throw an Exception if the value is not a map:
JSONObject<String> object = jsonObjectParent.getAsObject("childObject");

Like for an array::

JSONArray<String> array = jsonObjectParent.getAsArray("childObject");
for (String item : array) {
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions