When I try to parse a simple domain model object containing a Java 8 LocalDateTime using a json view, I receive a stack overflow exception, that seems to be related to the fact that the GSON implentation does not know about Java 8 LocalDateTime.
Here is an example (documentation of grails views and JSON):
Domain class "Person"
import java.time.LocalDate
class Person
{
Sting name
LocalDate birthDate
}
PersonController
class PersonController
{
def index() {
respond Person.findAll() as List<Person>
}
}
index.gson view
@Field List<Person> personList
json tmpl.person(personList)
_person.gson template file
model
{
Person person
}
json g.parse(person)
When I try to open this view, a stack overflow exception is thrown:
Caused by: grails.views.ViewRenderException: Error rendering view: null at grails.views.AbstractWritableScript.writeTo(AbstractWritableScript.groovy:43) at grails.plugin.json.view.api.internal.DefaultGrailsJsonViewHelper$6.writeTo(DefaultGrailsJsonViewHelper.groovy:677)
When I simplify the _person.gson to this:
...
json
{
name person.name
}
Thew view works as expected.
My question is
Is there a default way to handle Java 8 LocalDate/Time properties in Grails JSON views? And If not, how can I register a new GSON builder permanently
- This question is related: How to serialize and deserialize Java 8's java.time types with Gson?
- I tried to implemented this: https://github.com/gkopff/gson-javatime-serialisers but this won't inject itself as new default de/serializer and needs to be used manually