202 Lotus blogs updated hourly. Who will post next? Home | Blogs | Search | About 
 
Latest 7 Posts
Composing With Docker
Thu, Nov 2nd 2017 9
Hacktoberfest and More
Tue, Oct 24th 2017 5
Go Evergreen
Tue, Oct 17th 2017 11
Change is in the Air
Fri, Sep 1st 2017 6
Open Source Contribution
Fri, Jun 16th 2017 8
Docker Quick Tips
Fri, Apr 28th 2017 7
Notes in 9: Dev Tools Grab Bg
Tue, Apr 4th 2017 3
Top 10
Custom JSON Serialization With GSON
Mon, Jan 23rd 2017 12
Everything Old is New Again
Mon, Oct 24th 2016 11
Go Evergreen
Tue, Oct 17th 2017 11
Notes in 9: Docker + SonarQube
Wed, Feb 24th 2016 10
Building Java Objects From JSON
Thu, Jan 22nd 2015 9
Scripting Server Upgrades
Fri, Nov 11th 2016 9
Composing With Docker
Thu, Nov 2nd 2017 9
REST is Best
Wed, Sep 17th 2014 8
XSLTProc in the Buff
Thu, Mar 24th 2016 8
Git Squash
Thu, Oct 20th 2016 8


Building Java Objects From JSON
Twitter Google+ Facebook LinkedIn Addthis Email Gmail Flipboard Reddit Tumblr WhatsApp StumbleUpon Yammer Evernote Delicious
   

Intro

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.

JSON

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.

“Old” Way

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.

“New” Way

I first saw a technique in which a person used a Gson instance to generate, on the fly, application/json by merely calling the the Gson.toJson method. I thought this was cool, but it made good sense. The Java object already existed and inherited from a proper class, which can loosely map to the JavaScript prototypal elements (string, boolean, array, object, integer, etc.). Gson is not unique in this, as the IBM Commons JSON library can achieve the same thing, using a JsonGenerator. That’s the easy side of things, the tricky part is going backwards, consuming JSON into a Java Object (or just creating it from existing Java objects without being so linear in re-iterating properties just to change the format they’re stored in).

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).

Google Gson

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!



---------------------
https://edm00se.github.io/DevBlog/java/building-java-objects-from-json
Jan 22, 2015
10 hits



Recent Blog Posts
9
Composing With Docker
Thu, Nov 2nd 2017 2:00p   Eric McCormick
Background About a year ago, I blogged on automating server upgrades with Docker and a BASH script. This met the needs I had at the time, and worked itself out to be pretty stable. But, since I think about such things and always question my preconceptions, I went down a path of creating a Docker compose config file, something I wouldn’t have had to create from scratch by waiting a little while as one appeared as an example from GitLab. As it turns it, it was a great learning experience regardl
5
Hacktoberfest and More
Tue, Oct 24th 2017 2:00p   Eric McCormick
Hacktoberfest 2017 October brings many good things with it. It’s the beginning of autumnal colors here, along with some yard raking in my case. It also brings with it not just Oktoberfest, but Hacktoberfest! Hacktoberfest 2017 Hacktoberfest is a month long open source support initiative, sponsored by Digital Ocean, partnering with GitHub. It’s meant to promote open source involvement and contribution. As added incentive, if you meet the criteria, you can get a free t-shirt (and stickers).
11
Go Evergreen
Tue, Oct 17th 2017 4:00p   Eric McCormick
Happy 🎂 Day IE 11! On the 17th of October in 2013, Internet Explorer 11 was released from Microsoft. That means that as of today, this popular* browser is now four years old and, with all respect to it, it really ought to go. Good day sir. I said good day! Evergreen Browsers What makes a browser, or any software for that matter, evergreen? Well, the basic requirements for a browser, or any piece of software for that matter, are specifically the support of automatic updates, that bring in:
6
Change is in the Air
Fri, Sep 1st 2017 1:00p   Eric McCormick
I’m Back What Can I Say? In Case You Missed It If you find yourself asking “where was Eric?”, this should summarize it all: Instead of trying to do everything all summer, I tend to take a break from blogging and a lot of open source endeavors over the summer. It means I can focus on family time along with yard and house projects. Ah... Summer That’s all paid off and, with fall fast approaching, I’ve found myself wanting to start those things back up; ramping up into winter when
8
Open Source Contribution
Fri, Jun 16th 2017 5:00p   Eric McCormick
Intro It’s time to clear some of the backlog. I started this post a few months back and it should probably be sent on its way to clear the pile of drafts I haven’t finished yet… 🤔 I have a bit of a passion for open source software. My preferred distribution of Linux has been Ubuntu since 4.10, the Warty Warthog (I was even a minor contributor on a short lived, wildly popular project that aimed at improving the Ubuntu experience early on), I’ve enjoyed most open source projects I’ve
7
Docker Quick Tips
Fri, Apr 28th 2017 3:00p   Eric McCormick
Docker If you have been living under a rock, Docker is pretty much amazing. If you haven’t been living under a rock, you may be getting used to the idea of Docker, but still have the occasional question. I’ve found myself using Docker in increasing amounts and complexity over the last year or so. I’ve recently decided to start recording some of the tasks I’ve found useful, some of which may be less familiar to a beginner. If you’re so inclined, check out the playlist, embedded here.
3
Notes in 9: Dev Tools Grab Bg
Tue, Apr 4th 2017 1:00p   Eric McCormick
Intro I’m on Notes in 9 again, with a “grab bag” of a couple of tools I’ve put together recently that may be of a varying degree of useful for other Domino + XPages developers. You don’t need these to do development, but for the right person, they may help with their development workflow. Also of note, with the upgrade to Swiper with the FP8 release of Notes + Domino Designer, the limitations previously mentioned are no longer there! This means that my second tool I talked about, node-
12
Custom JSON Serialization With GSON
Mon, Jan 23rd 2017 2:00p   Eric McCormick
Intro Here’s a curious one, in which I found myself with a limitation of not being able to output JSON with scientific notation values. wait, what? If you’re wondering why that is, since both JSON and JavaScript allow scientific notation of number values, you are absolutely correct and that’s a great question. The strange thing was that I found myself outputting perfectly valid JSON to be consumed by something specific which didn’t allow scientific notation. I’m not entirely sure wh
3
Recapping 2016
Mon, Jan 16th 2017 3:00p   Eric McCormick
Intro 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
7
Rebirth: An App of Ice and Fire
Wed, Dec 14th 2016 4:00p   Eric McCormick
Intro 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




Created and Maintained by Yancy Lent - About - Planet Lotus Blog - Advertising - Mobile Edition