title: Can't get an implementation of Grails ObjectMarshaller<JSON> to work
body: <p>I'm trying to implement a custom marshaller in Grails. Here's the marshaller:</p>
  
  <pre><code>class AdultPlanningMarshaller implements ObjectMarshaller&lt;JSON&gt; {
      boolean supports(Object theObject)
      {
          return theObject instanceof AdultPlanning
      }
  
      void marshalObject(Object theObject, JSON theConverter)
      {
          AdultPlanning adult = (AdultPlanning)theObject
  
          JSONWriter writer = theConverter.getWriter()
  
          writer.object()
  
          writer.key('id').value(adult.id)
          ...    
          writer.endObject()
      }
  }
  </code></pre>
  
  <p>I'm registering it in bootstrap.groovy and when I run my integration tests, the supports method fires correctly and the marshalObject method is called with the right object and a JSON object.</p>
  
  <p>When I hit the:</p>
  
  <pre><code>writer.object()
  </code></pre>
  
  <p>call, an exception gets thrown:</p>
  
  <pre><code>org.codehaus.groovy.grails.web.json.JSONException: Misplaced object: expected mode of INIT, OBJECT or ARRAY but was DONE
  </code></pre>
  
  <p>So it looks like the writer has already done something to completion, but I have no clue what.</p>
  
  <p>There's not a lot of documentation on JSON marshallers and examples are thin on the ground, but I think I've done this right but it sure isn't working.  Any hints would be appreciated.</p>
  
  <p>Further work with the debugger seems to indicate that the object marshaller is being called twice, although breakpoints only happen on the 2nd call for some reason.  The first time through it seems to work just fine since the JSONWriter that I get via theConverter.getWriter() when the breakpoint DOES work has the JSON of the object correctly marshalled.  It's the 2nd call that is blowing up since the object has ALREADY been marshalled and the JSONWriter is no longer in the "init" state.  There's nothing obviously available to tell the difference between the two calls, but why it the marshaller being called twice?</p>
  
  <p>As requested, here's the controller.  It's the show action that's being triggered:</p>
  
  <pre><code>class PrimaryController extends RestfulController  implements AlwaysRenderJsonException {
  
      def springSecurityService
      def familyService
  
      static responseFormats = ['json']
  
      PrimaryController() {
          /*
           * Tell the base class the name of the resource under management.
           */
  
          super(Primary)
      }
  
      @Override
      protected Primary createResource() {
          //def instance = super.createResource()
          //TODO: Should be able to run the above line but there is an issue GRAILS-10411 that prevents it.
          // Code from parent is below, as soon as the jira is fixed, remove the following lines:
  
          Primary instance = resource.newInstance()
          bindData instance, this.getObjectToBind()
  
          //Code from super ends here
  
          def family = familyService.safeGetFamily(params.long('familyId'))
  
          familyService.addAdultToFamily(instance, family)    // Add the primary member to the family.
  
          return instance
      }
  
      /**
       * Deletes a resource for the given id
       * @param id The id
       */
  
      @Override
      def delete() {
          if(handleReadOnly()) {
              return
          }
  
          Child instance = queryForResource(params.id)
          if (instance == null) {
              notFound()
              return
          }
  
          /*
           * Because of the multiple belongsTo relationships of events, you have to get rid of all
           * the events and make the profiles consistent BEFORE deleting the person instance.
           */
  
          instance.removePerson()
  
          request.withFormat {
              '*'{ render status: NO_CONTENT } // NO CONTENT STATUS CODE
          }
      }
  
      @Override
      protected List&lt;Primary&gt; listAllResources(Map params) {
          if (params.familyId == null)
          {
              throw new ESPException("params.familyId may not be null")
          }
  
          def user = springSecurityService.loadCurrentUser()
  
          return \
              AdultPlanning.where {
                  family.id == params.familyId \
                  &amp;&amp; family.user == user \
                  &amp;&amp; typeOfPerson == PeopleTypeEnum.PRIMARY
              }.list()
      }
  
      @Override
      protected Primary queryForResource(Serializable id) {
          def inst = familyService.safeGetAdult(Long.parseLong(id), params.long('familyId'))
  
          /*
           * It was safe to access the requested id, but the requested id may NOT be a primary
           * so we need to check.
           */
  
          return (inst instanceof Primary ? inst : null)
      }
  
      /**
       * Show the primary for the specified family.
       *
       * @return
       */
  
      @Override
      def show() {
          Primary primary = familyService.safeGetFamily(params.long('familyId'))?.primary
  
          respond primary
      }
  }
  </code></pre>
  
  <p>And the Integration test that triggers it:</p>
  
  <pre><code>void "We should be able to show a primary."() {
      given:
      family.addToAdults(new Primary(firstName: "Barney"))
      family.save()
      family.adults.each { it.save() }
  
      when:
      controller.response.reset()
      resetParameters(controller.params, [familyId: family.id])
      controller.request.method = 'GET'
      controller.show()
  
      then:
      1 * mSpringSecurityService.loadCurrentUser() &gt;&gt; user
      controller.response.json
      controller.response.json.firstName == "Barney"
  }
  </code></pre>