• Post Reply Bookmark Topic Watch Topic
  • New Topic

creating JSON from database records  RSS feed

 
J. Kevin Robbins
Bartender
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm curious how other people are doing this. I'm sure my method sucks needs improvement, but this is what I've come up with that has worked for me.



I realize the JSON library that I'm using is outdated and I should switch. I've tinkered with GSON, mostly when I have beans to convert to JSON. I haven't looked at Jackson yet. Advice on what I should be doing here that's better than my current method?

One thing that I really dislike about this technique is the need for the NUM_TABLE_COLS final variable. If the table structure changes, I have to change this variable. That's a serious weakness.

I actually wrote this helper method once, but there is something about this that seem kludgy and I've never been very happy with it because of the StringBuilder stuff.


 
Dave Tolls
Ranch Foreman
Posts: 3056
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why is your DAO returning a List of List of Strings, rather than a List of some Model class (presumably a DailyEqCare class)?

Then all you would need to do is use a json library to generate the String.
 
J. Kevin Robbins
Bartender
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because my JSON needs to look like this:

[{"3":"U35 ","2":"FF","1":"20160122","0":"20151204","6":"WELDING EXT BAR ON WALL SIDE

That doesn't lend itself well to bean properties, but if you have an idea of how I can do it with objects, please educate me. Seriously. I'm not being facetious; I really do want to learn a better way to do this. I work alone, so I have only the folks here on the Ranch to guide me to being a better programmer.

I also remembered another reason that I can't always use that column counter. Sometimes I am doing CRUD against a known table in which case it works, but more often I'm calling a stored procedure that creates a ResultSet on the fly so I have no library name or table name to reference.

[edit] I should have added that the weird JSON is because it's being fed to DataTables. The numerical field/property is the column number in the table.
 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JSON frameworks usually have ways to specify custom property name mappings for marshalling and unmarshalling. Seriously, 0, 1, 2, 3, 6 for JSON property names?! Sheesh, those are insane !

But as with anything crazy, and I'm trying my best to not make any political overtures here, you should try to hide that as deeply as possible and expose only sane things to the higher levels of your application.

So, I agree, you should have some kind of Java domain object that can be the more stable layer in which you can properly and sanely deal with comprehensible representations of your data. I think you got yourself in trouble in trying to cut some corners and go straight from a vague DB representation to an incomprehensible JSON representation. You can hide the craziness of having to translate a property name that is obviously some kind of date to something like "1" or "0" in your JSON. This dirty job should be handed off to the JSON framework you're using, to be done behind a curtain of privacy, in secret and in shame. That work can be done with a custom mapper of some sort or an annotation that the framework supports. See this example for GSON: http://www.javacreed.com/gson-annotations-example/

 
J. Kevin Robbins
Bartender
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:Seriously, 0, 1, 2, 3, 6 for JSON property names?! Sheesh, those are insane !

I was afraid that code might send you into convulsions. I have a pretty good idea of your standards, and this isn't even in the same time zone. I was almost afraid to air such dirty laundry, but the only way for me to learn is to admit that I'm doing it poorly.

Junilu Lacar wrote:you should try to hide that as deeply as possible and expose only sane things to the higher levels of your application.

No argument from me. But how to get started?

Junilu Lacar wrote:This dirty job should be handed off to the JSON framework you're using, to be done behind a curtain of privacy, in secret and in shame. That work can be done with a custom mapper of some sort or an annotation that the framework supports. See this example for GSON: http://www.javacreed.com/gson-annotations-example/

This is what I was looking for. I had no idea that this sort of property name mapping could be done. Thank you! It's time for me to fall back and see if I've learned anything from you about refactoring.
 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
J. Kevin Robbins wrote:
Junilu Lacar wrote:Seriously, 0, 1, 2, 3, 6 for JSON property names?! Sheesh, those are insane !

I was afraid that code might send you into convulsions. ... I was almost afraid to air such dirty laundry, but the only way for me to learn is to admit that I'm doing it poorly.

As bad things go, this would evoke a serious facial tick at best. Maybe. And you shouldn't worry about me, I'm just an old fart who likes to point out things in other people's code. I'm not the one who has to live with that code, you are. Worry about you

This is what I was looking for. I had no idea that this sort of property name mapping could be done. Thank you! It's time for me to fall back and see if I've learned anything from you about refactoring.

Good deal. Happy refactoring!
 
J. Kevin Robbins
Bartender
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote: I'm just an old fart who likes to point out things in other people's code.

Well, from one old fart to another, you just keep pointing them out. I can't learn and improve unless someone shows me a better way to do things.
 
Rob Spoor
Sheriff
Posts: 21131
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
J. Kevin Robbins wrote:

Why not use the actual size?
Or using for-each:
 
J. Kevin Robbins
Bartender
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why not, indeed? Why did it never occur to me that r.size() would be equal to the number of columns?

That's a double-facepalm right there.

This is one of my weaknesses. Once I find a solution that works, I stop looking for a better solution. I'm off to the next problem instead.

I really need to find a job at a place that does code reviews...
 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:Or using for-each:

That would take attention away from a more important question, IMO, like paying too close attention your dashboard widget that shows your average gas consumption but missing the fact that you're almost out of gas.

There is at least one insidious assumption that causes brittleness in that code. The assumption that an item's position in a list corresponds to the correct property name that the value should have in the generated JSON is highly susceptible to breakage. If the DAO changes the way it populates each list, with a deletion, addition, or reordering of elements, then that change will most probably propagate out to affect this code and whatever code is downstream from here that uses the generated JSON. That could be really bad. I would be more concerned about guarding against that kind of ripple effect first. But then again, what you pointed out seems like low hanging fruit that you can fix quickly. Just don't lose sight of the bigger problem.
 
Dave Tolls
Ranch Foreman
Posts: 3056
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
J. Kevin Robbins wrote:
This is what I was looking for. I had no idea that this sort of property name mapping could be done. Thank you! It's time for me to fall back and see if I've learned anything from you about refactoring.


I'm late as usual ...
But we've just recently looked into that sort of mapping as a Clojure web service we called had an attribute of the format silly-attribute-name, which is clearly not a valid Java name, consequently had to do the remapping thing.
It's a pain, but then different languages have different conventions I suppose.
 
J. Kevin Robbins
Bartender
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well that certainly looks a lot better. I was able to eliminate one class entirely, and reduce my DAO to just one method.

Added the annotations to the bean:


DAO returns JSON....

Which makes the servlet very simple...

One more question. From a design standpoint, does it matter if the DAO returns JSON, or should it return a List of beans and let the servlet do the gson step?

I can't think of a strong argument either way, except if the DAO returns a List of beans that might make it more flexible. And I guess it uncouples it from the web page. By returning JSON it's only good for this one type of web page (DataTables). So I'm thinking maybe the two lines of Gson code should be in the servlet and the DAO should return a List.
 
Rob Spoor
Sheriff
Posts: 21131
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's what I'd do.
 
Dave Tolls
Ranch Foreman
Posts: 3056
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
J. Kevin Robbins wrote:Well that certainly looks a lot better. I was able to eliminate one class entirely, and reduce my DAO to just one method.


I love it when that happens.


And yes, your DAO should return objects. By returning JSON you would be coupling it tightly to the servlet.
 
J. Kevin Robbins
Bartender
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, my last post sort of turned into a Rubber Duck stream of thoughts. By the end, I had pretty well convinced myself that was the way to go.

Damn. Do you suppose I'm actually learning?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!