Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 669AE200B90 for ; Sun, 25 Sep 2016 22:26:22 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id 6536E160ACE; Sun, 25 Sep 2016 20:26:22 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id A22FA160ABE for ; Sun, 25 Sep 2016 22:26:21 +0200 (CEST) Received: (qmail 55814 invoked by uid 500); 25 Sep 2016 20:26:20 -0000 Mailing-List: contact notifications-help@groovy.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@groovy.apache.org Delivered-To: mailing list notifications@groovy.apache.org Received: (qmail 55800 invoked by uid 99); 25 Sep 2016 20:26:20 -0000 Received: from arcas.apache.org (HELO arcas) (140.211.11.28) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 25 Sep 2016 20:26:20 +0000 Received: from arcas.apache.org (localhost [127.0.0.1]) by arcas (Postfix) with ESMTP id 8BB6B2C002D for ; Sun, 25 Sep 2016 20:26:20 +0000 (UTC) Date: Sun, 25 Sep 2016 20:26:20 +0000 (UTC) From: "ASF GitHub Bot (JIRA)" To: notifications@groovy.apache.org Message-ID: In-Reply-To: References: Subject: [jira] [Commented] (GROOVY-7946) StreamingJsonBuilder should support writable values MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-JIRA-FingerPrint: 30527f35849b9dde25b450d4833f0394 archived-at: Sun, 25 Sep 2016 20:26:22 -0000 [ https://issues.apache.org/jira/browse/GROOVY-7946?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15521368#comment-15521368 ] ASF GitHub Bot commented on GROOVY-7946: ---------------------------------------- Github user jwagenleitner commented on a diff in the pull request: https://github.com/apache/groovy/pull/429#discussion_r80391169 --- Diff: subprojects/groovy-json/src/main/java/groovy/json/StreamingJsonBuilder.java --- @@ -651,6 +655,18 @@ public void call(String name, JsonOutput.JsonUnescaped json) throws IOException writer.write(json.toString()); } + /** + * Writes the given Writable as the value of the given attribute name + * + * @param name The attribute name The attribute name + * @param json The value The writable --- End diff -- descriptions for the params seems to be repeated twice. > 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)