Created
August 16, 2012 18:46
-
-
Save joeRinehart/3372577 to your computer and use it in GitHub Desktop.
Custom JSON marshalling
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Custom marshalling | |
springContext.getBean( "customObjectMarshallers" ).register() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package util.marshalling | |
class CustomObjectMarshallers { | |
List marshallers = [] | |
def register() { | |
marshallers.each{ it.register() } | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package recipes | |
import grails.converters.JSON | |
import recipes.Recipe | |
class RecipeMarshaller { | |
void register() { | |
JSON.registerObjectMarshaller( Recipe) { Recipe recipe -> | |
return [ | |
id : recipe.id, | |
content : recipe.content, | |
userId : recipe.user.id, | |
username : recipe.user.username | |
] | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import util.CustomObjectMarshallers | |
import recipe.RecipeMarshaller | |
// Place your Spring DSL code here | |
beans = { | |
customObjectMarshallers( CustomObjectMarshallers ) { | |
marshallers = [ | |
new RecipeMarshaller() | |
] | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ | |
{"id":1,"content":"foo foo bar bar","userId":"1","username":"joe"}, | |
{"id":2,"content":"woogie woogie whooo","userId":"1","username":"joe"} | |
] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
render Recipe.list() as JSON |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
JSON.registerObjectMarshaller( Recipe ) { Recipe recipe -> | |
return [ | |
id : recipe.id, | |
content : recipe.content, | |
userId : recipe.user.id, | |
username : recipe.user.username | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment