[
https://issues.apache.org/jira/browse/GROOVY-7946?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15515900#comment-15515900
]
ASF GitHub Bot commented on GROOVY-7946:
----------------------------------------
GitHub user graemerocher opened a pull request:
https://github.com/apache/groovy/pull/429
GROOVY-7946 - allow writable values for StreamingJsonBuilder
See https://issues.apache.org/jira/browse/GROOVY-7946
You can merge this pull request into a Git repository by running:
$ git pull https://github.com/graemerocher/incubator-groovy
json-builder-writable-improvement
Alternatively you can review and apply these changes as the patch at:
https://github.com/apache/groovy/pull/429.patch
To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:
This closes #429
----
commit 6b3b0b8a03c4cbbbf2bd590138c8d860a052b868
Author: Graeme Rocher <[email protected]>
Date: 2016-09-23T09:12:30Z
GROOVY-7946 - allow writable values for StreamingJsonBuilder
----
> StreamingJsonBuilder should support writable values
> ---------------------------------------------------
>
> Key: GROOVY-7946
> URL: https://issues.apache.org/jira/browse/GROOVY-7946
> Project: Groovy
> Issue Type: Improvement
> Affects Versions: 2.4.7
> Reporter: Graeme Rocher
>
> 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:
> {code}
> 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()
> }
> }
> {code}
> Which is inefficient and eliminates the memory benefits of streaming. Ideally
> you want to do this:
> {code}
> 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
> }
> }
> {code}
> Which allows you to continue streaming for child attributes of a JSON
> document.
> I have a pull request incoming for this improvement
--
This message was sent by Atlassian JIRA
(v6.3.4#6332)