3

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


Community
  • 1
  • 1
Nico O
  • 13,762
  • 9
  • 54
  • 69
  • What version of JSON views are you using? More recent versions add Java 8 date support – Graeme Rocher Dec 19 '16 at 09:19
  • Hello @GraemeRocher, thank you for your feedback. I solved this problem for me, by not using JSON views at all. Sadly. But maybe this thread can be of use for other persons running into this issue. I had the problem above using this maven package: org.grails.plugins:views-json:1.2.0.M2 – Nico O Dec 19 '16 at 09:24
  • 2
    You probably needed to add http://plugins.grails.org/plugin/grails-java8 to your classpath – Graeme Rocher Dec 19 '16 at 10:32
  • Good point. I did not know about this plugin and it was not present as the error above occurred. Thanks for your input. – Nico O Dec 19 '16 at 10:39

1 Answers1

1

These java 8 date support is not yet implemented in json views. This will work out of the box with views 1.2.0 (when it is released) and the latest version of the grails-java8 plugin at that time.

Edit: To clarify, it is only not supported when using the built in rendering strategy (g.render, hal.render, jsonapi.render, etc). You can still construct the views yourself in any way you want:

json {
    someLocalDate LocalDate.now.format(...)
}

Edit2: Json views 1.2 has been released along with the grails-java8 plugin (also version 1.2). Java 8 date type rendering is now supported out of the box with the 2 plugins together.

James Kleeh
  • 12,094
  • 5
  • 34
  • 61
  • so does that mean we cant use loacalDateTime in domain classes for rest until then ? i just fell over this in json views 1.1.5 and it made no difference if i had the java8 plugin in or not – WILLIAM WOODMAN Mar 08 '17 at 10:13
  • You can still use json views, you will just have to build them yourself instead of relying on the default renderer (g.render). – James Kleeh Mar 08 '17 at 16:01