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 Wed, 30 Dec 2009 19:55: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>added</b> by             <a href="http://cwiki.apache.org/confluence/display/~justinedelson">Justin
Edelson</a>
    </h4>
         <br/>
    <div class="notificationGreySide">
         <p>The commons.json 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>A basic script looks like this:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
builder = <span class="code-keyword">new</span> org.apache.sling.commons.json.groovy.JSONGroovyBuilder()

out.write builder.json {
    text currentNode.getProperty(<span class="code-quote">"text"</span>).string
} as <span class="code-object">String</span>
</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">
builder.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">
builder.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">
builder.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">
builder.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">
builder.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 </p>
    </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/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