Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-7946

StreamingJsonBuilder should support writable values

    XMLWordPrintableJSON

    Details

    • Type: Improvement
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 2.4.7
    • Fix Version/s: 2.4.8
    • Component/s: None
    • Labels:
      None

      Description

      In order to compose logic with StreamingJsonBuilder it is often desirable to split up the code into parts to make it more maintainable. For example with Grails' JSON views you may have several different templates that make up the entire JSON response.

      In order to support this we had to fork StreamingJsonBuilder and add the capability to pass a Writable as a value. The reason is, otherwise you have to buffer in memory an entire string for child template. For example now you would have to do something like this:

              new StringWriter().with { w ->
                  def builder = new StreamingJsonBuilder(w)
                  def sw = new StringWriter() 
                 new StreamingJsonBuilder(sw).call {
                              sectionId "world"
                  }
                  builder.response {
                      status "ok"
                      results sw.toString()
                  }
            }
      

      Which is inefficient and eliminates the memory benefits of streaming. Ideally you want to do this:

              new StringWriter().with { w ->
                  def builder = new StreamingJsonBuilder(w)
                  def writable = new Writable() {
                      @Override
                      Writer writeTo(Writer writer) throws IOException {
                          new StreamingJsonBuilder(writer).call {
                              sectionId "world"
                          }
                          return writer
                      }
                  }
                  builder.response {
                      status "ok"
                      results writable
                  }
              }
      

      Which allows you to continue streaming for child attributes of a JSON document.

      I have a pull request incoming for this improvement

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                graemerocher1 Graeme Rocher
              • Votes:
                0 Vote for this issue
                Watchers:
                3 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: