sunlabs.brazil.json
Class JSONTemplate

java.lang.Object
  extended by sunlabs.brazil.template.Template
      extended by sunlabs.brazil.json.JSONTemplate (view source)
All Implemented Interfaces:
TemplateInterface

public class JSONTemplate
extends Template

Template for generating (or extracting) JSON format data.

<json2props src="json text" prefix="..." [delim="..."]>
Flatten JSON (supplied in "src") into a set of properties prefixed by "prefix". This is the inverse of the <item, prefix="..."> tag (if the delim is ".", the default).

The <json2props> tag can be used to flatten a json object into a set of properties, which can be manipulate as a set of name/value pairs using the standard mechanisms, then turned it back into a JSON object again.

The following set of templates can be used to generate a JSON object via an *ML representation.

<array> ... </array>
Create a JSON array
<object> ... </object>
create a JSON object
<item value=xxxx />
<item valueinline /> ... </item>
Add an element onto a JSON array
<item name=xxx value=xxxx />
<item name=xxx> ... </item>
<item name=xxx valueinline /> ... </item>
Add an entry into a JSON object.
<item prepend=xxx />
<item name=xxx prepend=xxx />
Add items onto a JSON array or object from a set of Properties.
See the discussion under <item> for more specifics.

All the markup in the current template is replaced with the JSON generated by the "object", "array" and "item" tags.


Field Summary
 
Fields inherited from class sunlabs.brazil.template.Template
debug
 
Constructor Summary
JSONTemplate()
           
 
Method Summary
static Object deflatten(Properties p, String prefix, String delim)
          Un-Flatten a subset of a properties object into JSON object.
 boolean done(RewriteContext hr)
          Called after all tags have been processed, one final chance.
static void flatten(String prefix, String delim, Properties p, Object obj)
          .
 boolean init(RewriteContext hr)
          Called before this template processes any tags.
static org.json.JSONObject merge(org.json.JSONObject jo1, org.json.JSONObject jo2)
          Merge two json objects.
static Object simplify(Object o)
          Recursively turn any array like objects into arrays.
 void tag_array(RewriteContext hr)
           
 void tag_item(RewriteContext hr)
          If this is a singleton, then the name must be defined if in the context of an "object", and "value" must be defined in in the context of an "array".
 void tag_json(RewriteContext hr)
          This tag is used to map a set of properties to a JSON object.
 void tag_json2props(RewriteContext hr)
          Turn a JSON object into a set of properties.
 void tag_object(RewriteContext hr)
           
 void tag_slash_array(RewriteContext hr)
           
 void tag_slash_item(RewriteContext hr)
          We finished our item: pop and add to our parent (which must be an object)
 void tag_slash_object(RewriteContext hr)
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

JSONTemplate

public JSONTemplate()
Method Detail

init

public boolean init(RewriteContext hr)
Description copied from class: Template
Called before this template processes any tags.

Specified by:
init in interface TemplateInterface
Overrides:
init in class Template

tag_json

public void tag_json(RewriteContext hr)
This tag is used to map a set of properties to a JSON object. If more than one tag is present, a JSON array will be produced.
Deprecated - use the "prefix" attribute of the "item" tag instead.


tag_object

public void tag_object(RewriteContext hr)

tag_slash_object

public void tag_slash_object(RewriteContext hr)

tag_array

public void tag_array(RewriteContext hr)

tag_slash_array

public void tag_slash_array(RewriteContext hr)

tag_item

public void tag_item(RewriteContext hr)
If this is a singleton, then the name must be defined if in the context of an "object", and "value" must be defined in in the context of an "array".

Otherwise, "name" must be defined, and there should be either an <array>..</array> or <object>..</object> tag pair before the enclosing </item>.

if "valueinline" is defined, then all the markup until the matching closing </item> is taken as the value. The additional boolean attributes "trim" and "eval" can be used to trim whitespace from, and evaluate ${...} constructs from the value respectively. If "prefix" is defined instead of "value" or "valueinline" then the value of the object (or array element if name is missing and the current context is an array), the object value is generated implicitly from the current request properties of the described prefix. An optional attribute "delim" (which default to ".") is used to allow the generation of nested values.

If the name attribute is missing with "prefix", and the current context is an <object>, then the JSON object implied by the "prefix" is merged into the existing object. So, if the property is defined:

 foo.I=am here
 
Then The markup:
 <object>
   <item name="test" value="ing" />
   <item name=other prefix=foo />
 </object>
 
produces:
{
  "other": {"I": "am here"},
  "test": "ing"
}
 
Whereas without the "name" attribute the markup
 <object>
   <item name="test" value="ing" />
   <item prefix=foo />
 </object>
 
will produce:
{
  "I": "am here",
  "test": "ing"
}
 

Each property whose name is [prefix].a.b. ... n is created as a node in the resulting json object. Any objects whose entire entries consist of "0, 1, ... n" are converted into an array. Similarly, the values "true", "false", and "null" are treated as JSON booleans (or null). numbers are converted into JSON integers.
[Note: it is not possible to distinquish booleans, nulls and integers from their string equivalents: Properties only deal with strings.]

For example, if the following properties are defined:

  foo.a=hi
  foo.b.0=nothing
  foo.b.1=something
  foo.number=27
  foo.ok=true
 
Then the markup: <object> <item name=implicit prefix="foo" /> </object> will produce the output:
{"implicit": {
  "a": "hi",
  "b": [
    "nothing",
    "something"
  ],
  "number": 27,
  "ok": true
}}
 


tag_slash_item

public void tag_slash_item(RewriteContext hr)
We finished our item: pop and add to our parent (which must be an object)


tag_json2props

public void tag_json2props(RewriteContext hr)
Turn a JSON object into a set of properties. (This is not the proper way to do this - see the list template)


flatten

public static void flatten(String prefix,
                           String delim,
                           Properties p,
                           Object obj)
                    throws org.json.JSONException
. * Recursively extract a json object into a set of properties

Parameters:
prefix - The prefix for this property name
delim - The delimiter to use between levels
p - The properties object to store the flattened tree into
obj - The object to flatten
Throws:
org.json.JSONException

merge

public static org.json.JSONObject merge(org.json.JSONObject jo1,
                                        org.json.JSONObject jo2)
                                 throws org.json.JSONException
Merge two json objects. If the same name exists in both objects, use the 2nd one. This could be smarter, if required.

Throws:
org.json.JSONException

simplify

public static Object simplify(Object o)
                       throws org.json.JSONException
Recursively turn any array like objects into arrays.

Throws:
org.json.JSONException

deflatten

public static Object deflatten(Properties p,
                               String prefix,
                               String delim)
                        throws org.json.JSONException
Un-Flatten a subset of a properties object into JSON object.

Parameters:
p - The flattened name/value pairs
prefix - The prefix all names must match
delim - The property delimiter
Returns:
A JSONObject or JSONArray
Throws:
org.json.JSONException

done

public boolean done(RewriteContext hr)
Description copied from class: Template
Called after all tags have been processed, one final chance.

Specified by:
done in interface TemplateInterface
Overrides:
done in class Template

Version Kenai-svn-r24, Generated 08/18/09
Copyright (c) 2001-2009, Sun Microsystems.