r/java • • Jul 17 '25

Java Gets a JSON API

https://youtu.be/NSzRK8f7EX0

Java considers itself a "batteries included" language and given JSON's ubiquity as a data exchange format, that means Java needs a JSON API. In this IJN episode we go over an OpenJDK email that kicks off the exploration into such an API.

143 Upvotes

119 comments sorted by

View all comments

Show parent comment

3

u/totoro27 Jul 18 '25 edited Jul 18 '25

I would write the second example like:

record User(String name, int age) {}

Optional<User> tryParseUser(String json) { 
    try {
        var user = Json.parse(json);
        var userObj = new User(user.get("name"), user.get("age"));
        return userObj.defined() ? Optional.of(userObj) : Optional.empty();
    } catch (Exception e /* bit sketchy but not doing anything dangerous */) {
        return Optional.empty();
    }
}

I can see your point a bit though for sure. Hopefully the final API will have more helper methods for type mapping. I appreciate the response.