Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Groovy/JSON/sql.Date

 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 686
Chrome Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didnt get you Mark, "wait" meaning?
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic