Permalink
Browse files

Plugin is now compatible with grails 2.0 (M2)

  • Loading branch information...
1 parent 08277a1 commit b7b313f3d60c41b329cfdae53df817e7aa5f51a9 @csperle csperle committed Sep 18, 2011
Showing with 17 additions and 4 deletions.
  1. +1 −1 JsonRestApiGrailsPlugin.groovy
  2. +16 −3 src/groovy/org/grails/plugins/rest/JSONDomainMarshaller.groovy
@@ -44,7 +44,7 @@ This plugin provides effortless JSON API for GORM classes
}
def doWithApplicationContext = { applicationContext ->
- grails.converters.JSON.registerObjectMarshaller(new org.grails.plugins.rest.JSONDomainMarshaller())
+ grails.converters.JSON.registerObjectMarshaller(new org.grails.plugins.rest.JSONDomainMarshaller(application))
}
def onChange = { event ->
@@ -7,6 +7,8 @@ package org.grails.plugins.rest
//
import grails.converters.JSON;
+import org.codehaus.groovy.grails.commons.GrailsApplication;
+import org.codehaus.groovy.grails.commons.DomainClassArtefactHandler;
import org.codehaus.groovy.grails.web.converters.ConverterUtil;
import org.codehaus.groovy.grails.web.converters.exceptions.ConverterException;
import org.codehaus.groovy.grails.web.converters.marshaller.ObjectMarshaller;
@@ -17,8 +19,14 @@ public class JSONDomainMarshaller implements ObjectMarshaller<JSON> {
static EXCLUDED = ['metaClass','class','version']
+ private GrailsApplication application
+
+ public JSONDomainMarshaller(GrailsApplication application) {
+ this.application = application
+ }
+
public boolean supports(Object object) {
- return ConverterUtil.isDomainClass(object.getClass());
+ return isDomainClass(object.getClass())
}
private getCustomApi(clazz) {
@@ -41,14 +49,14 @@ public class JSONDomainMarshaller implements ObjectMarshaller<JSON> {
writer.key(name);
writer.array()
value.each { item ->
- if (ConverterUtil.isDomainClass(item.getClass())) {
+ if (isDomainClass(item.getClass())) {
json.convertAnother(item.id);
} else {
json.convertAnother(item);
}
}
writer.endArray()
- } else if (ConverterUtil.isDomainClass(value.getClass())) {
+ } else if (isDomainClass(value.getClass())) {
writer.key(name);
json.convertAnother(value.id);
} else {
@@ -63,4 +71,9 @@ public class JSONDomainMarshaller implements ObjectMarshaller<JSON> {
throw new ConverterException("Exception in JSONDomainMarshaller", e);
}
}
+
+ private boolean isDomainClass(Class clazz) {
+ String name = ConverterUtil.trimProxySuffix(clazz.getName());
+ return application.isArtefactOfType(DomainClassArtefactHandler.TYPE, name);
+ }
}

0 comments on commit b7b313f

Please sign in to comment.