File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Groovy and the fly likes Groovy/JSON/sql.Date Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Languages » Groovy
Bookmark "Groovy/JSON/sql.Date" Watch "Groovy/JSON/sql.Date" New topic
Author

Groovy/JSON/sql.Date

Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17258
    
    6

So we are converting our Java domain objects into Groovy and came across an issue that I hope someone has resolved.

Our class has a property that is of type java.sql.Date

This was working as a .java class, but as a .groovy it does not.

Here is what happens.

This domain object gets set into the Model that is attached to a JSONView object and returned to a web page. JSON has this great feature that it calls every single getter method of objects returned in the Model. Which means it is calling .getHours() in that Date property. The current implementation of getHours() is just

throw new IllegalArgumentException();

So, now as a groovy object the page gets this exception and won't display. As a java object it all works perfectly, no exception.

Thanks

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

This may not be a good long term solution but couldn't you just create your own date class that extends java.sql.Date and override that one particular method that is causing you fits?


GenRocket - Experts at Building Test Data
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17258
    
    6

Gregg Bolinger wrote:This may not be a good long term solution but couldn't you just create your own date class that extends java.sql.Date and override that one particular method that is causing you fits?


Yeah that was one of the hacks that we thought of, but threw it out because of that.

Mark
Peter Ledbrook
author
Greenhorn

Joined: Jul 15, 2009
Posts: 25
Hi,

I can't understand why there would be a difference between the Java and Groovy domain objects. How are they defined? Is the JSON serialiser (I assume that's what's triggering the exception) processing the date property when the domain object is Java?

The only thing I can think of is that Groovy automatically generates getter and setter methods for any class fields that have no scope. You can prevent the creation of those methods by adding private, protected or public to the field.

Cheers,

Peter
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17258
    
    6

Peter Ledbrook wrote:Hi,

I can't understand why there would be a difference between the Java and Groovy domain objects. How are they defined? Is the JSON serialiser (I assume that's what's triggering the exception) processing the date property when the domain object is Java?

The only thing I can think of is that Groovy automatically generates getter and setter methods for any class fields that have no scope. You can prevent the creation of those methods by adding private, protected or public to the field.

Cheers,

Peter


That is basically what is happening, as a Java object I saw that there wasn't any getters or setters for the two Date fields, so when changing them to Groovy, Groovy automatically created getters and setters, So I will just change it to private. But it will have to wait till the next sprint now. If we can get to it.

Thanks Peter.

Mark
Vyas Sanzgiri
Ranch Hand

Joined: Jun 16, 2007
Posts: 686

I didnt get you Mark, "wait" meaning?


===Vyas Sanzgiri===
My Blog
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17258
    
    6

Vyas Sanzgiri wrote:I didnt get you Mark, "wait" meaning?


We are using Agile/Scrum at our company, so we work in 2 week springs. Since we were at the end of the refactor sprint that had us doing the java to groovy conversion, it was too late to fix this problem and get a release done. So I have to change that java class to groovy later in another sprint. So I have to wait for another sprint that putting this story/task on will make sense and QA and the other teams allow us to put it on the sprint.

Mark
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Groovy/JSON/sql.Date