google / google/gson

FR: Convenience methods for `get` in `JsonObject` and `JsonArray`

Open
#2,430 1 comment 0 reactions 0 assignees View on GitHub
enhancement
Dominant language
Java
Stars
24.2k
Forks
4.5k
Avg merge
6d 4h
Merged PRs (30d)
12

Description

# Problem solved by the feature

Makes traversing generic `JsonObject` and `JsonArray` objects faster and easier.

# Feature description

I'd like to add convenience methods to `JsonArray` and `JsonObject`, so that each has methods based on their respective `get` to directly get each type (int, double, boolean, etc.), rather than having to go through a `JsonElement` first.

For example, consider the following scenario: I want to retrieve the "name" property of the third employee.

currently, with a generic `JsonObject`, it would look something like this:

```
JsonObject obj;
String name = obj.get("employees").getAsJsonArray()
.get(2).getAsJsonObject()
.get("name").getAsString();
```

versus with my fork, which would allow the following as an alternative:

```
JsonObject obj;
String name = obj.getJsonArray("employees")
.getJsonObject(2)
.getString("name");
```

# Alternatives / workarounds

The alternative is described above in my example.

One final note, I'm more than willing to (and have began to) implement these convenience methods myself; I just want to make sure this is a welcome change before I create and document all of them in a new fork.

Thank you!

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.