registerObjectMarshaller in Grails 1.1, suspected Bug


/ Published in: Groovy
Save to your folder(s)

Expected Behaviour
==================

* Both classes should be rendered in the specified custom format when `store/book/list.json` is loaded.
* Order of how the marshallers are added should not matter.
* It should be possible to register more than one custom object marshallers.


Bug
===

* Only the first registered marshaller is picked up as expected.
* The second one is ignored and the default JSON generated by Grails is returned.
* The order of the calls to `registerObjectMarshaller` matters, such that if the Book marshaller is added first, that one works. However if the Author marshaller is first, that one works.


Copy this code and paste it in your HTML
  1. /* Book domain class */
  2. int pages
  3. String title
  4. }
  5.  
  6. /* Author domain class */
  7. class Author {
  8. String name
  9. Date birthDate
  10. }
  11.  
  12. /* registering custom marshallers in Bootstrap.groovy */
  13. import grails.converters.JSON
  14.  
  15. class BootStrap {
  16.  
  17. def init = { servletContext ->
  18. JSON.registerObjectMarshaller(Author){ author, json ->
  19. def birthday = author.birthDate
  20. json.build{
  21. "class(author)"
  22. id(author.id)
  23. name(author.name)
  24. info{
  25. birthdate(year: birthday.year, month: birthday.month, day: birthday.day)
  26. }
  27. }
  28. }
  29.  
  30. JSON.registerObjectMarshaller(Book){ book, json ->
  31. json.build{
  32. "class(book)"
  33. id(book.id)
  34. info{
  35. pages(book.pages)
  36. }
  37. }
  38. }
  39. }
  40. }
  41.  
  42. /* code for output in controller */
  43. def list = {
  44. params.max = Math.min( params.max ? params.max.toInteger() : 10, 100)
  45. def authors = Author.list( params )
  46. withFormat{
  47. json{
  48. render authors as JSON
  49. }
  50. html{
  51. [ authorInstanceList: authors, authorInstanceTotal: Author.count() ]
  52. }
  53. }
  54. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.