Javid Jamae
Javid Jamae

Reputation: 8999

Why can't I import this Groovy library in Grails?

I'm creating a service class in grails that generates a JSON structure and uses it to do a REST call to another service. I'm trying to import some groovy.json classes as follows:

import groovy.json.JsonBuilder
import groovy.json.JsonOutput

class SomeService {
  ...
}

But, I'm get the following error:

1: unable to resolve class groovy.json.JsonBuilder
  [groovyc]  @ line 1, column 1.
  [groovyc]    import groovy.json.JsonBuilder
  [groovyc]    ^

When I try in groovysh, it seems to import these classes just fine. What do I need to do to pull in the standard Groovy JSON library in Grails?

Upvotes: 1

Views: 2394

Answers (2)

Burt Beckwith
Burt Beckwith

Reputation: 75671

That class was introduced in Groovy 1.8 which isn't used in Grails until 2.0.0+. You'll need to either upgrade to 2.0.x+ or use a different JSON library.

Upvotes: 3

ubiquibacon
ubiquibacon

Reputation: 10667

Not sure why you cannot import those classes, though I'm not sure you actually need them. If your just trying to generate JSON then it is probably easiest to create a Groovy map then render myMap as JSON, which is available when you import grails.converters.JSON.

grails.converters.JSON equivilent to groovy.json.JsonOutput

grails.web.JSONBuilder equivilent to groovy.json.JsonBuilder

Upvotes: 1

Related Questions