incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Sling > Using the JSONGroovyBuilder
Date Thu, 28 Jan 2010 20:41:00 GMT
<html>
<head>
    <base href="http://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/1519/1/1/_/styles/combined.css?spaceKey=SLING&amp;forWysiwyg=true"
type="text/css">
    </head>
<body style="background-color: white" bgcolor="white">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
     <h2><a href="http://cwiki.apache.org/confluence/display/SLING/Using+the+JSONGroovyBuilder">Using
the JSONGroovyBuilder</a></h2>
     <h4>Page <b>edited</b> by             <a href="http://cwiki.apache.org/confluence/display/~justinedelson">Justin
Edelson</a>
    </h4>
     adding doc for jsonBuilder.write(out)
          <div id="versionComment" class="noteMacro" style="display:none; padding: 5px;">
     adding doc for jsonBuilder.write(out)<br />
     </div>
          <br/>
     <div class="notificationGreySide">
         <p>The extensions.groovy bundle includes a <a href="http://groovy.codehaus.org/Builders"
rel="nofollow">Groovy Builder</a> which makes it easy to create JSON documents from
Sling scripts. This code is inspired by the builder contained in 	Andres Almiray's json-lib
project (<a href="http://json-lib.sourceforge.net/" rel="nofollow">http://json-lib.sourceforge.net/</a>).
However, Sling's builder is <b>not</b> compatible with the json-lib builder.</p>

<p>An instance of this class is available to all Groovy scripts as a variable named
<tt>jsonBuilder</tt>.</p>

<p>A basic script looks like this:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
out.write jsonBuilder.json {
    text currentNode.getProperty(<span class="code-quote">"text"</span>).string
} as <span class="code-object">String</span>
</pre>
</div></div>

<p>This can be simplified by using <tt>JSONGroovyBuilder</tt>'s <tt>write</tt>
method:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
jsonBuilder.write(out).json {

}
</pre>
</div></div> 

<h1><a name="UsingtheJSONGroovyBuilder-Examples"></a>Examples</h1>

<p>These examples are taken from the test case: <a href="http://svn.apache.org/viewvc/sling/trunk/bundles/commons/json/src/test/groovy/org/apache/sling/commons/json/groovy/JSONGroovyBuilderTest.groovy?view=markup"
rel="nofollow">http://svn.apache.org/viewvc/sling/trunk/bundles/commons/json/src/test/groovy/org/apache/sling/commons/json/groovy/JSONGroovyBuilderTest.groovy?view=markup</a>.</p>

<div class='panelMacro'><table class='noteMacro'><colgroup><col width='24'><col></colgroup><tr><td
valign='top'><img src="/confluence/images/icons/emoticons/warning.gif" width="16" height="16"
align="absmiddle" alt="" border="0"></td><td><p>The JSON output below
is formatted for readability. Actual output will be compact.</p></td></tr></table></div>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
jsonBuilder.json { }
</pre>
</div></div>
<p>produces...</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
{}
</pre>
</div></div>

<hr />

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
jsonBuilder.json([])
</pre>
</div></div>
<p>produces...</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
[]
</pre>
</div></div>

<hr />

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
jsonBuilder.json {
  string <span class="code-quote">"json"</span>
  integer 1
  bool <span class="code-keyword">true</span>
}
</pre>
</div></div>
<p>produces...</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
{
 'string' : 'json',
 'integer' : 1,
 'bool' : <span class="code-keyword">true</span>
}
</pre>
</div></div>

<hr />

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
jsonBuilder.json {
  first { integer 42 }
  second { integer 48 }
}
</pre>
</div></div>
<p>produces...</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
{
  'first' : {
    'integer' : 42
  },
  'second' : {
    'integer' : 48
  }
}
</pre>
</div></div>

<hr />

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
jsonBuilder.json {
  books {
    book  {
      title  <span class="code-quote">"The Definitive Guide to Grails"</span>
      author <span class="code-quote">"Graeme Rocher"</span>
    }
    book  {
      title  <span class="code-quote">"Groovy in Action"</span>
      author  <span class="code-quote">"Dierk Konig"</span>
    }
  }
}
</pre>
</div></div>
<p>produces...</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
{
  'books' : {
    'book' : [
      { 'title' : 'The Definitive Guide to Grails', 'author' : 'Graeme Rocher' },
      { 'title' : 'Groovy in Action', 'author' : 'Dierk Konig' }
    ]
  }
}
</pre>
</div></div>
<h1><a name="UsingtheJSONGroovyBuilder-CreatingaWrapper"></a>Creating a
Wrapper</h1>

<p>The <tt>json</tt> pseudo-method of <tt>JSONGroovyBuilder</tt>
produces an instance of <tt>org.apache.sling.commons.json.JSONObject</tt> based
on the parameters passed to it. If a different psuedo-method is called, the method name will
be used to create a wrapper object. For example:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
builder.books {
  book  {
    title  <span class="code-quote">"The Definitive Guide to Grails"</span>
    author <span class="code-quote">"Graeme Rocher"</span>
  }
  book  {
    title  <span class="code-quote">"Groovy in Action"</span>
    author  <span class="code-quote">"Dierk Konig"</span>
  }
}
</pre>
</div></div>
<p>produces...</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
{
  'books' : {
    'book' : [
      { 'title' : 'The Definitive Guide to Grails', 'author' : 'Graeme Rocher' },
      { 'title' : 'Groovy in Action', 'author' : 'Dierk Konig' }
    ]
  }
}
</pre>
</div></div>
     </div>
     <div id="commentsSection" class="wiki-content pageSection">
       <div style="float: right;">
            <a href="http://cwiki.apache.org/confluence/users/viewnotifications.action"
class="grey">Change Notification Preferences</a>
       </div>

       <a href="http://cwiki.apache.org/confluence/display/SLING/Using+the+JSONGroovyBuilder">View
Online</a>
       |
       <a href="http://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=9798248&revisedVersion=4&originalVersion=3">View
Change</a>
              |
       <a href="http://cwiki.apache.org/confluence/display/SLING/Using+the+JSONGroovyBuilder?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message