Class JS
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic String
Returns a Javascript/JSON array in string representation from a Java string array.static String
array
(Collection<?> list) Returns a Javascript/JSON array from a Java object collection.static String
Returns a given JCR Node and its entire subtree as JSON object.static String
Returns a given JCR Node and its subtree up until a given depth as JSON.static String
Returns a Javascript string in string representation, including the surrounding double-quotes.static void
Writes the given JCR Node and its entire subtree as JSON into the given writer.static void
Writes the given JCR Node and its subtree up until a given depth as JSON into the given writer.
-
Constructor Details
-
JS
public JS()
-
-
Method Details
-
str
Returns a Javascript string in string representation, including the surrounding double-quotes. For example:"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)
.- Parameters:
text
- a Java string- Returns:
- a qualified Javascript string literal or the string
null
-
array
Returns a Javascript/JSON array in string representation from a Java 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.- Parameters:
array
- a string array- Returns:
- a qualified string representation of a Javascript array literal
-
array
Returns a Javascript/JSON array from a Java object collection. Same asnew
and behaves likeJSONArray
(list).toString();array(String[])
.- Parameters:
list
- a collection of objects- Returns:
- a qualified string representation of a Javascript array literal
-
writeNode
public static void writeNode(Writer out, Node node) throws IOException, RepositoryException, JSONException Writes the given JCR Node and its entire subtree as JSON into the given writer. UsewriteNode(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.- Parameters:
out
- a writer, such as ajavax.servlet.jsp.JspWriter
, to write the JSON into. Will automatically be flushed before and after.node
- the node to write- Throws:
IOException
- if there was a problem with the writerRepositoryException
- if some jcr error happenedJSONException
- if writing the json failed
-
writeNode
public static void writeNode(Writer out, Node node, int depth) throws IOException, RepositoryException, JSONException Writes the given JCR Node and its subtree up until a given depth as JSON into the given writer.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.- Parameters:
out
- a writer, such as ajavax.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.- Throws:
IOException
- if there was a problem with the writerRepositoryException
- if some jcr error happenedJSONException
- if writing the json failed
-
object
Returns a given JCR Node and its entire subtree as JSON object. Useobject(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.- Parameters:
node
- the node to write- Returns:
- a JSON object as string representation
- Throws:
RepositoryException
- if some jcr error happenedJSONException
- if writing the json failed
-
object
Returns a given JCR Node and its subtree up until a given depth as JSON.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.- Parameters:
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.- Returns:
- a JSON object as string representation
- Throws:
RepositoryException
- if some jcr error happenedJSONException
- if writing the json failed
-