|
![]() |
|||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectsunlabs.brazil.template.Template
sunlabs.brazil.json.JSONTemplate (view source)
public class JSONTemplate
Template for generating (or extracting) JSON format data.
<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.
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 |
---|
public JSONTemplate()
Method Detail |
---|
public boolean init(RewriteContext hr)
Template
init
in interface TemplateInterface
init
in class Template
public void tag_json(RewriteContext hr)
public void tag_object(RewriteContext hr)
public void tag_slash_object(RewriteContext hr)
public void tag_array(RewriteContext hr)
public void tag_slash_array(RewriteContext hr)
public void tag_item(RewriteContext hr)
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 hereThen 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=trueThen the markup: <object> <item name=implicit prefix="foo" /> </object> will produce the output:
{"implicit": { "a": "hi", "b": [ "nothing", "something" ], "number": 27, "ok": true }}
public void tag_slash_item(RewriteContext hr)
public void tag_json2props(RewriteContext hr)
public static void flatten(String prefix, String delim, Properties p, Object obj) throws org.json.JSONException
prefix
- The prefix for this property namedelim
- The delimiter to use between levelsp
- The properties object to store the flattened tree intoobj
- The object to flatten
org.json.JSONException
public static org.json.JSONObject merge(org.json.JSONObject jo1, org.json.JSONObject jo2) throws org.json.JSONException
org.json.JSONException
public static Object simplify(Object o) throws org.json.JSONException
org.json.JSONException
public static Object deflatten(Properties p, String prefix, String delim) throws org.json.JSONException
p
- The flattened name/value pairsprefix
- The prefix all names must matchdelim
- The property delimiter
org.json.JSONException
public boolean done(RewriteContext hr)
Template
done
in interface TemplateInterface
done
in class Template
|
Version Kenai-svn-r24, Generated 08/18/09 Copyright (c) 2001-2009, Sun Microsystems. |
|||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |