Tales from the jar side: Java Records and JSON serialization, It's a Wonderful Life spoilers, and entertaining tweets
Even George would admit that anchor chains, plane motors, and train whistles are not exciting sounds during a pandemic
Welcome to Tales from the jar side, the Kousen IT newsletter, for the week of Dec 13 - 20, 2020. This week I taught three one-day classes on the O’Reilly Learning Platform: JUnit 5, What’s New In Java, and Spring MVC. I also taught an NFJS Virtual Workshop on JUnit 5 and Mockito 3.
Records and JSON
During my What’s New In Java class, we talk about records, which are a preview feature of Java 15 and scheduled to be stable in Java 16. Records are simple Java classes, with auto-generated equals, hashCode, and toString methods, a specified constructor, and methods that return the individual properties. Those methods would be called “getters” in regular Java classes, and would follow the old JavaBeans naming convention, but for some odd reason the developers of records didn’t follow that pattern.
Since Java 11 added an HTTP client class, I do a demo where I access a restful web service, download the JSON data, and deserialize it to Java classes. The example I like to use is the service at Open Notify that displays the number of people in space. When you send a GET request to the proper URL you get:
The code to do the GET request is more complicated than it needs to be (after all, this is still Java). This method just retrieves the JSON data and shows the status code (200) and the headers:
I can deserialize this to POJOs (Plain Old Java Objects) easily enough, but this time in class I wanted to try to deserialize to records. In previous runs I’d always said that the current popular libraries don’t work for that, but I thought I’d see if anything changed yet.
My two records are:
Note that records put their properties inside a constructor declared before the body, which is really different for Java (though normal for Kotlin). The methods to retrieve the individual fields are named after the properties, so if you have an instance of Assignment, you get the field values from assignment.name() and assignment.craft().
Because that doesn’t follow the normal naming convention, the existing Java JSON serialization libraries don’t work. Or at least they mostly don’t work. Here’s what I eventually found:
I prefer the Gson library, but it doesn’t understand records. Neither does Moshi, which is a library that came out of the Android world. I finally tried the Jackson 2 library, however, and to my surprise my simple example worked fine.
I didn’t actually do all that during class, btw, though I did start the experiment there. Next time I can show it all working. I expect by the time Java 16 comes out (scheduled for March 16, 2021), all the serialization libraries will work.
I think I mentioned this last time, but the addition of the Gradle Java toolchain block worked great. In my build.gradle file I have:
That made the project build under Java 15 without me having to manually switch Java versions. Sweet.
This should probably be in a blog post, though that might require me to try to create custom serializers, which seems like actual work. Maybe I’ll get to that this week. In the meantime, if you want the code, it’s in this GitHub repository.
The other two courses I taught this week (JUnit 5 and Spring MVC) both went fine. I managed to make the JUnit / Mockito workshop more interactive than normal, which was good, though that meant I didn’t cover quite as much information. The Spring MVC course is always a bit ragged, because there are so many topics involved and my examples get complicated. Still, it all worked well enough.
It’s A Wonderful Spoiler
It’s that time of year again, so my wife and I watched It’s A Wonderful Life yet again. As I mentioned on Twitter:
Here are a few thoughts I have about the movie, in no particular order. And yes, spoilers for a movie that came out in 1946.
The title is rather odd, isn’t it? What does the “it” refer to? Everybody’s life? Life itself? Kind of an odd construction, and I’m a person who too frequently employs odd constructs.
When George says to Mr. Potter “I’ve lost eight thousand dollars,” it warms the heart, especially because he originally lost his temper at Uncle Billy and said “somebody is going to jail, and it’s not going to be me.” The thing is, though, nobody in their right mind would let Uncle Billy anywhere near money in amounts greater than the five cents he probably needs to make a phone call when he gets lost wandering drunkenly around town. So George is right, in a way.
Props to the costume department, but I’m sorry, it’s impossible to make Donna Reed look frumpy. So she’s a librarian and an “old maid”? Sorry, that’s the least believable part of the whole dream sequence. Mary could have had any guy in town, and she would have found somebody and made it work, assuming she wanted to. If she was alone, it’s because she preferred it that way.
Every time the dance floor opens up to reveal the pool underneath, I worry George and Mary are going to get seriously hurt when they fall in. I certainly would have.
It still amazes me that Jimmy Stewart had such incredible amounts of charm that he could make a banker into a sympathetic character.
After George turns down Mr. Potter’s job offer, I always wonder whether or not he ever told Mary about it. He got distracted by her announcement she was pregnant, but it could have come up again. It would be consistent with the character if he never said anything, and she would have backed up his decision to say no, but I wonder anyway.
I cringe every time Mr. Gower slaps George, bad ear or not. Wow, that part didn’t age well at all.
Speaking of young George, the kid was played by Bobbie Anderson, who is simply brilliant. He’s best at showing complete fearlessness, as when he yelled at Mr. Potter. It’s a great setup for George doing the same thing as an adult.
I still wonder what deal George was going to make with Mary to give her robe back.
We never found out what happened to Mr. Martini. Mr. Gower went to jail for poisoning that kid, Harry died because George wasn’t there to save him, Uncle Billy has been in the insane asylum since he lost the Building and Loan (whoa), but all we see about Martini is that someone else is running the bar. George asks where Martini is, but gets interrupted before anyone answers. I should probably check the IMDB page to see if the answer is there.
Records (physical ones; not a call-back to the first section of this newsletter) really were fragile back then. When Mary breaks her copy of Buffalo Gal, Won’t You Come Out Tonight, shards fly everywhere.
Obviously Zuzu didn’t catch a cold just from walking home without buttoning her coat (apparently the common cold virus wasn’t all that well known in 1946), but go with it.
Surprising that Mr. Welch (an uncredited Stanley Andrews) — Zuzu’s teacher’s husband, who punches George in the mouth yelling at his wife — was the only person in Bedford Falls who didn’t know George Bailey. What are the odds? I guess George didn’t make it to many PTA meetings. Of course, Mr. Welch probably wasn’t the type to get to know his wife’s pupil’s parents.
I like how Tom (the Building & Loan customer who insisted on withdrawing his full balance during the bank run) was first in line to contribute money when George needed it. I’ll bet his friends had been giving him a hard time about that for years, and deservedly so.
The parts with Lillian Randolph (who played the maid, Annie) have aged the worst. She did a great job, but yikes.
That’s enough. Now we need to watch A Christmas Carol again, either the one with George C. Scott or the one with Patrick Stewart, or both.
Side note: In 2005, Patrick Stewart was in the midst of performing his one-man version of A Christmas Carol. That year I had my first international training course. I traveled to Maidenhead, UK, for a two-week course on Java 2 Enterprise Edition (J2EE).
Over the weekend, I took the train to London and just wandered around. I got lucky and stumbled upon the theater where Stewart was performing his show. I managed to get a ticket to the last show he was planning for that run.
It was, of course, amazing. I was struck by how thin he was, but of course the performance was brilliant. And at his age (even then), when Scrooge starts coughing badly and eventually turns it into the first laugh he’s had in years, the whole audience worried that Stewart was actually in trouble. Live theater (or should I say theatre?) — there’s nothing like it.
Miscellaneous
Very nice of O’Reilly Media’s twitter feed to post this:
The first three episodes of The Expanse, season 5, dropped on Tuesday:
You might have heard that the Cleveland Indians are finally changing their name. My favorite baseball writer, Joe Sheehan, had this to say:
I’m a fan of the writer Laurie Penny, who, to everyone’s astonishment, recently got married. She naturally wrote a very entertaining article about it:
Here is a brilliant, graphical display of solutions to the Towers of Hanoi problem, using Sierpinski triangles:
Someone constructed a xylophone that plays Jesu, Joy of Man’s Desiring as a ball falls down a ramp:
This is topical:
Finally, we got about a foot of snow this week, so I can’t stop thinking about this rather disturbing image:
But I can’t end on that, so here’s something completely different:
Have a great holiday everyone.
Last week:
Java Testing with JUnit 5, on the O’Reilly Learning Platform
What’s New In Java, ditto
Spring MVC Fundamentals, ditto
JUnit 5 and Mockito 3, an NFJS Virtual Workshop
This week:
Spring and Spring Boot, private class