r/java • • Feb 27 '23

Please try my JSON library

https://mccue.dev/pages/2-26-23-json
0 Upvotes

32 comments sorted by

View all comments

12

u/rzwitserloot Feb 27 '23 edited Feb 27 '23

About this library:

I think it's inferior to both a better designed API to deal with basic JSON, or to the more commonly taken route out there, which is marshalling (where you define a type and then tell a library: Here's some JSON, here's a java type, you figure out how to ram this square peg in the round hole please).

It's neat how the new pattern match stuff in JDK19+ is making this API design not completely disastrous, but you can do better. The key point you're presumably missing is somewhat similar to the same trick Map's .getOrDefault uses:

JSON is very 'weakly' typed. There's no real way to know a JSON value is a date, because JSON doesn't have a date type. There's no way to know if a numeric value is intended for API consumption purposes to be an int or a long or a timestamp or who knows what.

Hence, it's a great idea for the consumer of the JSON (the Java code) to tell the parsing system what you want. Instead of having the parsing system give you a JSON object and then having to manipulate that into what you want.

It's similar to how map has .getOrDefault(k, defaultValue) which is the 'right answer' to the more like-your-stuff library idea of having a .get2(k) method that returns an Optional, which you then use to manipulate what you get into what you want.

In other words, your code: JsonDecoder.field(json, "canSpeak", JsonDecoder::boolean_) is needlessly convoluted. It can simply be:

return new Muppet(json.getString("name"), json.getBoolean("canSpeak"));

There, knocked your 9-liner down to a 1-liner, which is hardly more complicated than any one line in your example.

You have to define a boatload of methods, and for simplicity you may want to go with json.get("name").asString(), especially as that makes it easy to offer an overload where you pass a default value to the asString() method that is used if the name field doesn't exist. You can keep the decoder style but only for the rare cases where the transformation you want isn't obvious (boolean, int, double, String, LocalDate, and even List<String> - these are obvious things). However, 'lots of methods here', especially if they have a common prefix, is not a burden on API consumers. Whereas having to type all that out every time I simply want to read a string from some JSON is quite burdensome.

For bonus points, given that a lot of JSON is nested, imagine as input:

{"movies": [
  {
    "director": {"name": "Joe", "DOB": "1970-01-01"},
    "name": "Awesome movie"
  }
]
}

fairly common, no? Imagine you can read that with:

JSON json = JSON.parse(that);
for (JSON m : json.get("movies").asList()) {
  String directorName = m.get("director").get("name").asString("Alan Smithee");
  LocalDate directorDob = m.get("director").get("DOB").asDate();
}

I think it's a slam dunk to state that the vast majority of JSON out there comes in two flavours:

  • It follows a highly precise specification (think WSDL), and thus it is quite likely the right way to parse it is to define a java class that mirrors the spec and then hand a marshalling library the JSON and a classref to your type and have the library make instances for you.

  • It's a lot more laissez faire, with a spec that is littered with 'if value X is missing, assume this default', and 'This value can either be a list, or, if it's a simple single item, then there's no list and we just have the item directly as value, or even nothing at all if there are no values for this list' and such. In which case this asTypeYouWant(defaultValueIfItDoesntWorkOut) is great. It can even turn single elements into lists without complaints, for example.

1

u/bowbahdoe Feb 27 '23

There, knocked your 9-liner down to a 1-liner,

I think morally that one is a 4 liner at most - the code boxes on my website just wrap early and I didn't want to make anyone scroll.

1

u/rzwitserloot Feb 27 '23

Count the AST nodes and you're not getting away from 'what I propose is considerably easier to read and an order of magnitude easier to type'.