I hate JAXB. I hate Jackson. I hate Hibernate. I’m sick and tired of writing all these stupid mapping classes for JSON, XML, and ORM. Wouldn’t it be awesome if you could evaluate JSON and XML the same way Javascript can? You could if Java supported dynamic dispatch. Basically the ability to dispatch method invocations and field access dynamically at runtime without compiler checking code. Dynamic languages like Python, et. al. have always supported this type of feature, and I believe Scala does too. IMO, it should be as simple as implementing a DynamicDispatch interface
interface DynamicDispatch { Object invoke(Class returnType, Type genericReturnType, String methodName, Object... args); Object getField(Class type, Type genericType, String fieldName); void setField(String filedName, Object value); }
This would have a tremendous effect on productivity and maintenance as we could get rid of JSON and XML mapping. I would even say we could do some really innovative things in the ORM space as well.
Jun 09, 2013 @ 22:57:02
Bill, as the author of jsonschema2pojo (something like JAXB+XSD but for JSON… stay with me :))) I’m acutely aware of the pain you describe. I have a lot of respect for Jackson and must give huge props to Tatu for his efforts to bring JSON and Java together in a sane way. I actually like Jackson, but I think Tatu would also acknowledge that there’s something fundamentally at odds here.
You’re absolutely right, developing ‘enterprise’ Java has become an exercise in writing endless mapping layers from one thing to another. Working around the quirks at each stage (ugh). Just think of the vast amount of time and money spent on data mapping gymastics instead of solving the actual business problem at hand – will the madness never end?
Luckily, we can all breathe a sigh of relief because the solution to this problem, after a decade of mapping madness, is here. It’s viable, mature, it supports all your favourite Java libraries and it’s also great fun. I’m talking about Clojure.
I know you’ve mentioned dynamic languages already (Python et al), and Clojure is of course one of these, but its direct compatibility with the existing Java ecosystem makes all the difference. There’s very little reason for people to not jump in and give Clojure a try. In my experience, once people realise how productive they can be when they keep data in its most essential form (and abstain the problems you describe almost entirely), they’re hooked.
I remember a tweet I read once, something along the lines of: “Java is neat a DSL for turning XML into stacktraces”. Clearly flamebait, but I think it does strike a chord with most Java developers. If any of those are reading this comment, I urge you to give Clojure a try.
Jun 09, 2013 @ 23:10:33
Code in a LISP-like language? Are you *bleepin* serious!?!!?!!? I’d rather retire than code in Clojure. I believe Scala has dynamic dispatch and I also believe somebody has written some JSON support for it, but I haven’t looked in awhile. At least with Scala I can retain some measure of type safety along with the Java syntax.
Jun 09, 2013 @ 23:19:47
You can also retain some measure of type safety using Clojure if you wish.
Yes, I’m *bleepin* serious. Not only is it quite easy to pick up LISP-like languages (because of their inherent simplicity), my experience is that most people find it refreshing and fun. This might seem like an extreme solution to the problem, but LISPs tackle the pain you describe head on (and Clojure’s Java compatibility makes this move less painful than one might think).
Jun 10, 2013 @ 00:01:54
I did not enjoy coding in LISP in college 20 years ago when I was young and enthusiastic. All the parentheses made my head spin. Prefix notation also does not compute well for my brain.
Jun 10, 2013 @ 05:28:49
IMO you need to take a look at Groovy: http://groovy.codehaus.org/Dynamic+Groovy
Jun 11, 2013 @ 12:39:44
Well… Reflection API is a attempt to implement this kind of feature, isn’t? You should also take a look at mirror project: http://projetos.vidageek.net/mirror/mirror/
Jun 11, 2013 @ 12:52:26
Not the same. I’d want something like this:
JSON json = new JSON();
json.some.attribute = 5;
json["key"] = value;
json.another.attribute[0] = "hello";
Jun 11, 2013 @ 13:06:27
Well, unfortunately I believe line 3 will never be supported since it requires major modifications in language syntax, violating “backward compatibility” principles of Java. Google’s GSON project does quite a good job in building json objects “typesafely”, but is not as fluent as the syntax you’re suggesting.
Jun 11, 2013 @ 13:02:19
http://groovy.codehaus.org/gapi/groovy/json/JsonSlurper.html
http://groovy.codehaus.org/gapi/groovy/json/JsonBuilder.html
Jun 11, 2013 @ 13:07:16
But, I want to do this in Java, not another language 🙂 i.e. I want to do this in JAX-RS
Jun 11, 2013 @ 14:23:39
IMHO Groovy is the lesser of two evils 😉
Groovy is (by design) the most similar language to Java. You can incapsulate dynamic stuff and then use it in your java classes (see
http://www.ibm.com/developerworks/library/j-pg05245/)