dev.mccue/json
dev.mccue.json
bowbahdoe/json
dev.mccue.json
provides the ability to read and write JSON data as well as to encode data into JSON and decode data from JSON.
Most popular JSON libraries use data-binding. You make a class, possibly annotate it a little bit, and then some automatic logic binds the data inside a JSON structure to the fields of your class.
This has clear upsides, but it's hard to explain to newcomers. The underlying mechanism of data-binding is either reflection or compile-time code generation. Both are processes that are hard to "touch." The escape hatches in libraries that assume data-binding is the default mode of operation are less than ergonomic.
This takes the other approach, making people manually extract data from JSON, but does so in a way that is composable. You have to write more code, but the mechanics of the code are more plain to see.
I've written about this before. I'm listing it here because some libraries I wrote that I'll introduce later depend on it.
import dev.mccue.json.Json;
import dev.mccue.json.JsonDecoder;
import dev.mccue.json.JsonEncodable;
Superhero(String name) implements JsonEncodable {
record @Override
public Json toJson() {
return Json.objectBuilder()
.put("name", name)
.build();
}
public static Superhero fromJson(Json json) {
return new Superhero(
.field(json, "name", JsonDecoder::string)
JsonDecoder);
}
}
void main() {
= new Superhero("superman");
var superhero
= superhero.toJson();
var json = Json.writeString(json);
var jsonStr
= Json.readString(jsonStr);
var roundTripped = Superhero.fromJson(roundTripped);
var newSuperhero
System.out.println(superhero);
System.out.println(jsonStr);
System.out.println(newSuperhero);
}