JSON written by hand
The service answers JSON, and the four fields of a loan do not justify a library. The writer on this page is a hundred lines of Java and no dependency: a quote method, a field helper, a switch over the state and a loop over the list. It is a smaller version of the writer of capstone 1, adapted to a fixed shape so that the pages can keep it in one file.
The writer
Two methods do the work and one switch decides the state object. field(name, value) writes "name":value and takes either a string or an int, and quote wraps a string and escapes the two characters that would break it. array joins the loans with commas. The file below is the one from the model page with those additions, and its main now prints two loans and the array:
package library;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.List;
public class Desk {
record Loan(int id, String member, String title, LocalDate due, LocalDate returned) {
State state(LocalDate today) {
if (returned != null) {
return new Returned(returned);
}
long days = ChronoUnit.DAYS.between(due, today);
if (days > 0) {
return new Overdue(days);
}
if (days >= -2) {
return new DueSoon(-days);
}
return new OnLoan(-days);
}
String toJson(LocalDate today) {
return "{" + field("id", id) + "," + field("member", member) + ","
+ field("title", title) + "," + field("due", due.toString()) + ","
+ Q + "state" + Q + ":" + stateJson(state(today)) + "}";
}
}
sealed interface State permits OnLoan, DueSoon, Overdue, Returned {
}
record OnLoan(long daysLeft) implements State {
}
record DueSoon(long daysLeft) implements State {
}
record Overdue(long daysLate) implements State {
}
record Returned(LocalDate on) implements State {
}
static final LocalDate TODAY = LocalDate.of(2026, 11, 20);
static final List<Loan> LOANS = List.of(
new Loan(1, "Ada", "Dune", LocalDate.of(2026, 11, 25), null),
new Loan(2, "Grace", "The Dispossessed", LocalDate.of(2026, 11, 21), null),
new Loan(3, "Alan", "Snow Crash", LocalDate.of(2026, 11, 14), null),
new Loan(4, "Edsger", "Programming Pearls", LocalDate.of(2026, 11, 10), LocalDate.of(2026, 11, 18)));
static final String Q = String.valueOf((char) 34);
static final String B = String.valueOf((char) 92);
static String field(String name, String value) {
return Q + name + Q + ":" + quote(value);
}
static String field(String name, int value) {
return Q + name + Q + ":" + value;
}
static String quote(String text) {
StringBuilder out = new StringBuilder(Q);
for (char c : text.toCharArray()) {
if (c == 34) {
out.append(B).append(Q);
} else if (c == 92) {
out.append(B).append(B);
} else {
out.append(c);
}
}
return out.append(Q).toString();
}
static String stateJson(State state) {
return switch (state) {
case OnLoan onLoan -> "{" + field("kind", "on_loan") + "," + field("days", (int) onLoan.daysLeft()) + "}";
case DueSoon dueSoon -> "{" + field("kind", "due_soon") + "," + field("days", (int) dueSoon.daysLeft()) + "}";
case Overdue overdue -> "{" + field("kind", "overdue") + "," + field("days", (int) overdue.daysLate()) + "}";
case Returned returned -> "{" + field("kind", "returned") + "," + field("on", returned.on().toString()) + "}";
};
}
static String array(List<Loan> loans, LocalDate today) {
StringBuilder out = new StringBuilder("[");
for (int i = 0; i < loans.size(); i++) {
if (i > 0) {
out.append(',');
}
out.append(loans.get(i).toJson(today));
}
return out.append(']').toString();
}
public static void main(String[] args) {
System.out.println(LOANS.get(0).toJson(TODAY));
System.out.println(LOANS.get(3).toJson(TODAY));
System.out.println(array(LOANS, TODAY));
}
}
The two constants
Q and B are the quote and the backslash as one-character strings, built with String.valueOf((char) 34) and String.valueOf((char) 92). The reason is the page, not the program: a Java string literal for a quote is \" and for a backslash is two backslashes, and both are exactly the sequences that a single-quoted PHP string would eat. The constants keep the source free of them and make the escaping code easier to read. A program of your own writes the literals and means the same thing.
quote handles two characters and nothing else: a quote becomes a backslash and a quote, a backslash becomes two backslashes, and every other character is copied. That is correct for JSON as far as it goes. The full writer of the testing group and capstone 1 also turns a newline into \n and a control character into \u0001, which this data never contains.
The state object
The state is an object and not a string, because the four forms carry different numbers: on_loan and due_soon carry the days left, overdue the days late, and returned the day of the return. The switch is exhaustive, like the one in the model, so a new state would fail to compile here until its JSON was written as well.
The array
The array of the route is the same builder with a comma between elements, and the output below shows the whole list on one line. That is what the service returns: JSON has no requirement to be pretty, and a client parses one line as easily as twenty. The pretty form of capstone 1 is a switch over a depth; here the shape is fixed and the compact form is enough.
The run
javac -Xlint:all -d out src/library/Desk.java
java -cp out library.Desk
{"id":1,"member":"Ada","title":"Dune","due":"2026-11-25","state":{"kind":"on_loan","days":5}}
{"id":4,"member":"Edsger","title":"Programming Pearls","due":"2026-11-10","state":{"kind":"returned","on":"2026-11-18"}}
[{"id":1,"member":"Ada","title":"Dune","due":"2026-11-25","state":{"kind":"on_loan","days":5}},{"id":2,"member":"Grace","title":"The Dispossessed","due":"2026-11-21","state":{"kind":"due_soon","days":1}},{"id":3,"member":"Alan","title":"Snow Crash","due":"2026-11-14","state":{"kind":"overdue","days":6}},{"id":4,"member":"Edsger","title":"Programming Pearls","due":"2026-11-10","state":{"kind":"returned","on":"2026-11-18"}}]
What this writer does not do
It does not escape a newline, a tab or a control character, because the member and title of the demo data contain none. It does not check that the text is valid UTF-16 or that a surrogate pair is complete. And it does not parse, which is the other half of a JSON library and the half the service does not need: it reads no request body, so it has nothing to parse. A service that accepted a loan in a POST would need the reader of capstone 1, and the page would say so.
The static note is the honest one: the writer is safe for the data of this project and not a general library. That is a decision a small service is allowed to make, and the tests pin the two escapes it does implement.
The next page
The next page adds the routing: a Reply record, the path table with its 200 and 404 answers, and the overdue filter that reuses the sealed state. The Java course covers the same writer in lesson 130 and the switch in lesson 060.
The code on this page was compiled with OpenJDK 21.0.13 (javac) and run on Alpine Linux in a Docker container. The date of the demonstration is fixed, so the output is the same on every run.
Article author: Arthur Isaev