title: Groovy/Grails Proliferation of "\" escape character in a string
body: <p>This is a rather odd issue. I'm integration testing a Grails service and the associated domain class. One property of that domain class is a <code>String</code> that holds a JSON. The database field is <code>json</code> too and there's a custom Hibernate value type that performs the necessary conversion. It's already been working for years in production in another domain class.</p>
  
  <pre><code>class MyDomain {
      String data
      static mapping = {
          data type: StringJsonUserType
      }
  }
  </code></pre>
  
  <p>So far so good. In my test I mock an input object to my service method that ultimately will contain and return the desired JSON string.</p>
  
  <pre><code>private MockedClass mockClass() {
      // JsonRepresentable declares asJson() method.
      def data = GroovyMock(JsonRepresentable)
      data.asJson() &gt;&gt; "{\"content\":\"irrelevant\"}"
  
      def mockClass = GroovyMock(MockedClass)
      mockClass.getData() &gt;&gt; data
  
      return mockClass
  }
  </code></pre>
  
  <p>The service method (simplified):</p>
  
  <pre><code>void persist(MockedClass mock) {
      String string = mock.data.asJson()
      def domain = new MyDomain(data: mock.data.asJson())
      domain.save()
  }
  </code></pre>
  
  <p>When I step into this code with the debugger I can immediately see that the string has turned from <code>{"content":"irrelevant"}</code> in the <code>string</code> variable to <code>"{\"content\":\"irrelevant\"}"</code> in the <code>domain</code> variable.</p>
  
  <p>It's only logical now, that in my test a comparison of the saved domain class string does not match the mocked input. </p>
  
  <p>This is how <code>MyDomain.data</code> data looks when it's read from the database:</p>
  
  <p><code>"\"\\\"{\\\\\\\"content\\\\\\\":\\\\\\\"irrelevant\\\\\\\"}\\\"\""</code></p>
  
  <p>This is the same string parsed with <code>new JsonSlurper().parseText(MyDomain.data)</code>:</p>
  
  <p><code>"\"{\\\"content\\\":\\\"irrelevant\\\"}\""</code></p>
  
  <p>Here's the mocked string parsed with JsonSlurper (as above):</p>
  
  <p><code>[content:irrelevant]</code></p>
  
  <p>Obviously the last example is what I expect. Can anybody tell me why Groovy/Grails adds a bulk load of crappy \\ to my simple and properly escaped string? I could even try a Groovy string <code>'{"content":"irrelevant"}'</code> but that doesn't make the slightest difference.</p>