|Latest 7 Posts
| Custom JSON Serialization With GSON|
Mon, Jan 23rd 2017 8
| Recapping 2016|
Mon, Jan 16th 2017 12
| Rebirth: An App of Ice and Fire|
Wed, Dec 14th 2016 5
| Scripting Server Upgrades|
Fri, Nov 11th 2016 7
| Everything Old is New Again|
Mon, Oct 24th 2016 5
| Git Squash|
Thu, Oct 20th 2016 7
| MWLUG Success|
Wed, Aug 24th 2016 10
| Building Java Objects From JSON|
Thu, Jan 22nd 2015 16
| Git History Searching|
Tue, Jul 12th 2016 12
| Recapping 2016|
Mon, Jan 16th 2017 12
| Blue Chalky Soup|
Mon, Feb 2nd 2015 11
| House Keeping|
Tue, Feb 23rd 2016 11
| Fixing Dojo 1.6.1 in Domino 8.5.3|
Tue, Sep 2nd 2014 10
| When You Need a Comparator|
Thu, Jan 8th 2015 10
| MWLUG Success|
Wed, Aug 24th 2016 10
| Server REST Consumption with Authentication|
Mon, Aug 18th 2014 9
| Redmine, CodeRay, and Domino, Oh My!|
Mon, Aug 11th 2014 9
||Building Java Objects From JSON
JSON, as previously mentioned, is a data format which has been exploding in web development since it was first introduced in the early 2000s. And whether or not you as a developer prefer XML (it’s okay, they’re just formats), there are some good reasons to use JSON data. Ultimately, I don’t really care about the “XML vs JSON” debate, because some services use XML and some use JSON, neither are going away anytime soon, and XML is more flexible than most people give it credit for.
Note: I am more of a JSON fan, but that should be immaterial to relevance. The biggest argument I see in favor of JSON as opposed to XML is file size.
To date, when I’ve shown examples on this blog of how to build JSON, I’ve generally used Google’s GSON library. I’ve also only shown it in a fashion (for simplicity’s sake) that I refer to as the “old” way (below), because it maps easily to converting to using the IBM Commons JSON library (more below). I try to add Gson to the server when possible, but often will end up importing the JAR to an NSF, should I not have administrator blessing. Gson is supported from Java 1.5 through 1.8, according to their pom file.
This is in contrast to the provided com.ibm.commons.util.io.json package, which is included and makes it a convenient option for many/most.
Be forewarned! To use com.google.gson, you will need to grant permission for it in your java.policy file; you can run into trouble if you don’t. This is probably the second best argument against using com.google.Gson, but I’m still a fan.
Part of the reason com.ibm.commons.util.io.json is popular (aside that it comes with the server, a big plus) is that it maps well to how we think. Streaming in elements into an object tends to make sense to us, but there’s another way. Here’s what I’ll refer to as the “old” way (it works, it’s valid, but not ideal as I’ll show).
This will generate a resulting JSON string with an object, represented as such:
It may not be very exciting, but it sure gets the job done. Here’s what I’m excited about.
IBM Commons JSON
Using JsonParser, you can use fromJson, which returns a java.lang.Object. In other words, you need to do your tests and transforms to get a handle on its structure. This works, but takes more effort (I would be glad for someone to show me how to map the IBM Commons library to the approach I’ll show next).
The Gson approach is to take in a class definition (or type) as the second parameter in their fromJson method, immediately mapping your object to a well structured object that you can invoke for its properties. Here’s a quick demonstration.
Why The “New” Way?
It’s obviously more verbose up front, but done the “old” way, I didn’t show all the type checks and conversions I would have to do to keep things working as expected. The “new” way defines the data format and ensures consistently well-formed objects; they are POJO instances after all (beans, except for the implementing java.util.Serializable bit, as we are using getter/setter methods).
You’ve defined the format and instantiated data object, meaning that now all you need to do is use the standard EL get/set to interact with the data. That's it, you're done!
Jan 22, 2015
| Recent Blog Posts
Custom JSON Serialization With GSON|
Mon, Jan 23rd 2017 2:00p Eric McCormick
Mon, Jan 16th 2017 3:00p Eric McCormick
Per usual, I’ve had a little break between things and decided to catch up with a bit of a summary of some recent things that each didn’t necessitate their own post.
2017 IBM Champion
For starters, I’m honored to be named an IBM Champion in Collaboration Solutions (/ Social Business) for the third time. This would be a hat trick in (ice) hockey 🏒. I’m happy to be recognized with a group of people, developers and more, who are passionate about both their work and the plat
Rebirth: An App of Ice and Fire|
Wed, Dec 14th 2016 4:00p Eric McCormick
If you read my blog for any of the Saga of Servlets series, then I hope that you’re excited I’m returning to the application I put together for it. This time, it’s as a conversation piece in regards to some of the build process modernization I engaged in recently, in order to unify the code base in its git repository. In any case, it’s helping pave the way forward before I update some of the back-end elements, when it will again be a talking point for some additional rework and
Scripting Server Upgrades|
Fri, Nov 11th 2016 2:00p Eric McCormick
This one might be slight departure from my usual, but those that have followed my blogging this past year will have noticed a bit more of a leaning towards DevOps in some of my posts. This echoes a lot of what I’ve been concluding as increasingly a necessary part of development; that we need to consider a picture large enough to encompass the themes surrounding development functions and, like any good developer (DRY ~= “lazy”), automate the heck out of it.
I had p
Everything Old is New Again|
Mon, Oct 24th 2016 8:00p Eric McCormick
Every so often, it’s good to reassess one’s position. This is good from both a standpoint of being inquisitive and even interrogative, but when it comes to the ever changing landscape of the front-end development space, it’s not only inevitable, but must be embraced for what feels the need to “stay afloat”. I’m changing theme of my blog, hopefully for the better. The previous theme was good and did a great job of getting things started, but while I had forked a copy of a good
Thu, Oct 20th 2016 8:00a Eric McCormick
If you’re just here to learn a little about how to “squash” commits with git, skip down a ways. Otherwise, hold on, and I will catch you up on a couple of personal notes before we get there.
On the Blog
It’s been a little while since I blogged last. This has been due to a combination of reasons; specifically, I’ve been busy with:
my family, it was the end of summer with lots of things going on
a number of projects around the house (a deck removal and basement remodel
Wed, Aug 24th 2016 8:37a Eric McCormick
MWLUG was a great success as far as I’m concerned. Each time I’ve gone I’ve had the great enjoyment of being able to attend some high quality sessions, meet with lots of colleagues and friends from the community, and get a view into products and solutions many people are undertaking, over conversations and interactions outside of the sessions. This is always a great way of interacting with others who were able to make it. Unlike the IBM conference of Connect(EDsphere), this is purel
Manually Renewing HTTPS w/ Let's Encrypt|
Wed, Jul 27th 2016 10:40a Eric McCormick
A while back, I rolled a personal project, which is a Node app, to Bluemix for lightweight use. I managed to make use of Let’s Encrypt for the HTTPS certificate, but only after realizing that there was a bit of a manual aspect to it that is the antithesis of an automated script for such things. Ultimately, after finding some information in a blog post form Marky Roden (of all people), I was able to get moving. The only downside wound up being that time passed, and it came time to renew
Eric and the Quest for More Coffee, pt.2|
Fri, Jul 15th 2016 4:17p Eric McCormick
Posted in the “aside” category.
There were three submissions via the Google Form, and a couple more form messages via social media. Honestly, I had debated either a nondescript or far more overt mug w/ the likeness of one of the more iconic of H.P. Lovecraft’s imaginations, but this seemed a bit over the top.
a replacement for my alma matter
a Go Army, Beat Navy mug (which was never my thing)
this gem from shop.Scotch.io (again, pretty overt)
Git History Searching|
Tue, Jul 12th 2016 10:00a Eric McCormick
First, A Shout-Out
The recording of the session called “Normalizing XPages Web Development” that Shean P. McManus and I gave at the 2-day, virtual ICONUS (formerly IamLUG) event this year is now available from “Archive and Replays”. If you missed it, I recommend checking it out, it’s a great benefit of ICONUS and I hope that those who did get a chance to attend enjoyed the subject material. We covered a lot of ground and were able to demonstrate what is, in my opinion, one of the grea