public class JS
extends java.lang.Object
Constructor and Description |
---|
JS() |
Modifier and Type | Method and Description |
---|---|
static java.lang.String |
array(java.util.Collection<?> list)
Returns a Javascript/JSON array from a Java object collection.
|
static java.lang.String |
array(java.lang.String[] array)
Returns a Javascript/JSON array in string representation from a Java
string array.
|
static java.lang.String |
object(Node node)
Returns a given JCR Node and its entire subtree as JSON object.
|
static java.lang.String |
object(Node node,
int depth)
Returns a given JCR Node and its subtree up until a given depth as JSON.
|
static java.lang.String |
str(java.lang.String text)
Returns a Javascript string in string representation, including the
surrounding double-quotes.
|
static void |
writeNode(java.io.Writer out,
Node node)
Writes the given JCR Node and its entire subtree as JSON into the given
writer.
|
static void |
writeNode(java.io.Writer out,
Node node,
int depth)
Writes the given JCR Node and its subtree up until a given depth as JSON
into the given writer.
|
public static java.lang.String str(java.lang.String text)
"foobar"
.
If the string is null, null
(as a Javascript null-value)
will be returned.
The Javascript string will be properly escaped.
This is the same as
same behaviour as JSONObject.valueToString(text)
.
text
- a Java stringnull
public static java.lang.String array(java.lang.String[] array)
Output will have no indentation and no new lines, but a single whitespace
between elements: ["one", "two", "three"]
If the given array is null
, an empty array will be returned
([]
). The Javascript strings will be properly escaped.
str(String)
is used for each element of the array.
array
- a string arraypublic static java.lang.String array(java.util.Collection<?> list)
new JSONArray
(list).toString();
and behaves like
array(String[])
.list
- a collection of objectspublic static void writeNode(java.io.Writer out, Node node) throws java.io.IOException, RepositoryException, JSONException
writeNode(Writer, Node, int)
for controlling the
node depth.
This is the same JSON as returned by Sling's default JSON renderer. Can be used in JSPs to inline JSON for javascript code, for example:
var data = <% JS.writeNode(out, currentNode); %>;which might result in:
var data = { "jcr:primaryType": "nt:unstructured", "property" : "value" };
This writes directly to the (JSP) output stream and thus needs to flush
it. Because of this it also works good for larger trees. To return it as
a string, use object(Node)
. Any exception will be passed to the
caller.
out
- a writer, such as a javax.servlet.jsp.JspWriter
, to write the JSON into.
Will automatically be flushed before and after.node
- the node to writejava.io.IOException
- if there was a problem with the writerRepositoryException
- if some jcr error happenedJSONException
- if writing the json failedpublic static void writeNode(java.io.Writer out, Node node, int depth) throws java.io.IOException, RepositoryException, JSONException
This is the same JSON as returned by Sling's default JSON renderer. Can be used in JSPs to inline JSON for javascript code, for example:
var data = <% JS.writeNode(out, currentNode, 0); %>;which might result in:
var data = { "jcr:primaryType": "nt:unstructured", "property" : "value" };
This writes directly to the (JSP) output stream and thus needs to flush
it. Because of this it also works good for larger trees. To return it as
a string, use object(Node, int)
. Any exception will be passed to the
caller.
out
- a writer, such as a javax.servlet.jsp.JspWriter
, to write the JSON into.
Will automatically be flushed before and after.node
- the node to writedepth
- until which depth the tree should be written; 0 means the
current node and its properties only; -1 means the whole tree.java.io.IOException
- if there was a problem with the writerRepositoryException
- if some jcr error happenedJSONException
- if writing the json failedpublic static java.lang.String object(Node node) throws RepositoryException, JSONException
object(Node, int)
for controlling the node depth.
This is the same JSON as returned by Sling's default JSON renderer. Can be used in JSPs to inline JSON for javascript code, for example:
var data = <%= JS.object(currentNode) %>;which might result in:
var data = { "jcr:primaryType": "nt:unstructured", "property" : "value" };
For larger node trees it is probably more efficient to stream it by using
writeNode(Writer, Node)
. Any exception will be passed to the
caller.
node
- the node to writeRepositoryException
- if some jcr error happenedJSONException
- if writing the json failedpublic static java.lang.String object(Node node, int depth) throws RepositoryException, JSONException
This is the same JSON as returned by Sling's default JSON renderer. Can be used in JSPs to inline JSON for javascript code, for example:
var data = <%= JS.object(currentNode) %>;which might result in:
var data = { "jcr:primaryType": "nt:unstructured", "property" : "value" };
For larger node trees it is probably more efficient to stream it by using
writeNode(Writer, Node, int)
. Any exception will be passed to
the caller.
node
- the node to writedepth
- until which depth the tree should be written; 0 means the
current node and its properties only; -1 means the whole tree.RepositoryException
- if some jcr error happenedJSONException
- if writing the json failedCopyright © 2010 - 2020 Adobe. All Rights Reserved