Package org.xwiki.velocity.tools
Class JSONTool
- java.lang.Object
-
- org.xwiki.velocity.tools.JSONTool
-
public class JSONTool extends Object
Velocity tool to facilitate serialization of Java objects to the JSON format.- Since:
- 4.0M2
- Version:
- $Id: c8a645c6c2f442099c9fd982fb8394762ff59b1d $
-
-
Constructor Summary
Constructors Constructor Description JSONTool()
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description Object
fromString(String str)
net.sf.json.JSON
parse(String json)
Deprecated.since 9.9RC1, usefromString(String)
insteadString
serialize(Object object)
Serialize a Java object to the JSON format.
-
-
-
Method Detail
-
serialize
public String serialize(Object object)
Serialize a Java object to the JSON format.Examples:
- numbers and boolean values: 23, 13.5, true, false
- strings: "one\"two'three" (quotes included)
- arrays and collections: [1, 2, 3]
- maps: {"number": 23, "boolean": false, "string": "value"}
- beans: {"enabled": true, "name": "XWiki"} for a bean that has #isEnabled() and #getName() getters
This also escapes "/" in the output and thus cannot close HTML tags or wiki macros.
- Parameters:
object
- the object to be serialized to the JSON format- Returns:
- the JSON-verified string representation of the given object
-
fromString
public Object fromString(String str)
- Parameters:
str
- the string to parse- Returns:
- the
Object
resolved from the string (usually a Maps or and Lists) - Since:
- 9.9RC1
-
parse
@Deprecated public net.sf.json.JSON parse(String json)
Deprecated.since 9.9RC1, usefromString(String)
insteadParse a serialized JSON into a real JSON object. Only valid JSON strings can be parsed, and doesn't support JSONP. If the argument is not valid JSON, thennull
is returned.- Parameters:
json
- the string to parse, must be valid JSON- Returns:
- the parsed JSON, either a
JSONObject
or aJSONArray
, ornull
if the argument is not a valid JSON - Since:
- 5.2M1
-
-