Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
XCOMMONS-130: Upgrade to Json-lib 2.4
XCOMMONS-131: Add a Velocity tool to serialize Java objects to JSON format
- Loading branch information
Showing
5 changed files
with
225 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
...-commons-core/xwiki-commons-velocity/src/main/java/org/xwiki/velocity/tools/JSONTool.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* See the NOTICE file distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* This is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU Lesser General Public License as | ||
* published by the Free Software Foundation; either version 2.1 of | ||
* the License, or (at your option) any later version. | ||
* | ||
* This software is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this software; if not, write to the Free | ||
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA | ||
* 02110-1301 USA, or see the FSF site: http://www.fsf.org. | ||
*/ | ||
package org.xwiki.velocity.tools; | ||
|
||
import net.sf.json.JSON; | ||
import net.sf.json.JSONArray; | ||
import net.sf.json.JSONNull; | ||
import net.sf.json.JSONObject; | ||
import net.sf.json.util.JSONUtils; | ||
|
||
/** | ||
* Velocity tool to facilitate serialization of Java objects to the JSON format. | ||
* | ||
* @version $Id$ | ||
* @since 4.0M2 | ||
*/ | ||
public class JSONTool | ||
{ | ||
/** | ||
* Serialize a Java object to the JSON format. | ||
* <p> | ||
* Examples: | ||
* <ul> | ||
* <li>numbers and boolean values: 23, 13.5, true, false</li> | ||
* <li>strings: "one\"two'three" (quotes included)</li> | ||
* <li>arrays and collections: [1, 2, 3]</li> | ||
* <li>maps: {"number": 23, "boolean": false, "string": "value"}</li> | ||
* <li>beans: {"enabled": true, "name": "XWiki"} for a bean that has #isEnabled() and #getName() getters</li> | ||
* </ul> | ||
* | ||
* @param object the object to be serialized to the JSON format | ||
* @return the JSON-verified string representation of the given object | ||
*/ | ||
public String serialize(Object object) | ||
{ | ||
JSON json = null; | ||
if (object == null) { | ||
json = JSONNull.getInstance(); | ||
} else if (object instanceof String) { | ||
return JSONUtils.valueToString(object); | ||
} else if (JSONUtils.isBoolean(object)) { | ||
return object.toString(); | ||
} else if (JSONUtils.isNumber(object)) { | ||
return JSONUtils.numberToString((Number) object); | ||
} else if (JSONUtils.isArray(object)) { | ||
json = JSONArray.fromObject(object); | ||
} else { | ||
json = JSONObject.fromObject(object); | ||
} | ||
|
||
return json.toString(); | ||
} | ||
} |
139 changes: 139 additions & 0 deletions
139
...mons-core/xwiki-commons-velocity/src/test/java/org/xwiki/velocity/tools/JSONToolTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
/* | ||
* See the NOTICE file distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* This is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU Lesser General Public License as | ||
* published by the Free Software Foundation; either version 2.1 of | ||
* the License, or (at your option) any later version. | ||
* | ||
* This software is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this software; if not, write to the Free | ||
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA | ||
* 02110-1301 USA, or see the FSF site: http://www.fsf.org. | ||
*/ | ||
package org.xwiki.velocity.tools; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
/** | ||
* Unit tests for {@link JSONTool}. | ||
* | ||
* @version $Id$ | ||
* @since 4.0M2 | ||
*/ | ||
public class JSONToolTest | ||
{ | ||
public static class MockBean | ||
{ | ||
public boolean isEnabled() | ||
{ | ||
return true; | ||
} | ||
|
||
public int getAge() | ||
{ | ||
return 28; | ||
} | ||
|
||
public double getGrade() | ||
{ | ||
return 9.48; | ||
} | ||
|
||
public String getName() | ||
{ | ||
return "XWiki"; | ||
} | ||
|
||
public List<String> getItems() | ||
{ | ||
return Arrays.asList("one"); | ||
} | ||
|
||
public Map<String, String> getParameters() | ||
{ | ||
return Collections.singletonMap("foo", "bar"); | ||
} | ||
} | ||
|
||
/** | ||
* The object being tested. | ||
*/ | ||
private JSONTool tool = new JSONTool(); | ||
|
||
@Test | ||
public void testSerializeMap() | ||
{ | ||
Map<String, Object> map = new HashMap<String, Object>(); | ||
map.put("bool", false); | ||
map.put("int", 13); | ||
map.put("double", 0.78); | ||
map.put("string", "foo"); | ||
map.put("array", new int[] {9, 8}); | ||
map.put("list", Arrays.asList("one", "two")); | ||
map.put("map", Collections.singletonMap("level2", true)); | ||
|
||
String json = tool.serialize(map); | ||
// We can't predict the order in the map. | ||
Assert.assertTrue(json.contains("\"bool\":false")); | ||
Assert.assertTrue(json.contains("\"int\":13")); | ||
Assert.assertTrue(json.contains("\"double\":0.78")); | ||
Assert.assertTrue(json.contains("\"string\":\"foo\"")); | ||
Assert.assertTrue(json.contains("\"array\":[9,8]")); | ||
Assert.assertTrue(json.contains("\"list\":[\"one\",\"two\"]")); | ||
Assert.assertTrue(json.contains("\"map\":{\"level2\":true}")); | ||
} | ||
|
||
@Test | ||
public void testSerializeList() | ||
{ | ||
Assert.assertEquals("[1,2]", tool.serialize(Arrays.asList(1, 2))); | ||
Assert.assertEquals("[1.3,2.4]", tool.serialize(new double[] {1.3, 2.4})); | ||
} | ||
|
||
@Test | ||
public void testSerializeNumber() | ||
{ | ||
Assert.assertEquals("27", tool.serialize(27)); | ||
Assert.assertEquals("2.7", tool.serialize(2.7)); | ||
} | ||
|
||
@Test | ||
public void testSerializeBoolean() | ||
{ | ||
Assert.assertEquals("false", tool.serialize(false)); | ||
Assert.assertEquals("true", tool.serialize(true)); | ||
} | ||
|
||
@Test | ||
public void testSerializeString() | ||
{ | ||
Assert.assertEquals("\"\\\"te'st\\\"\"", tool.serialize("\"te'st\"")); | ||
} | ||
|
||
@Test | ||
public void testSerializeBean() | ||
{ | ||
String json = tool.serialize(new MockBean()); | ||
// We can't predict the order in the map. | ||
Assert.assertTrue(json.contains("\"age\":28")); | ||
Assert.assertTrue(json.contains("\"enabled\":true")); | ||
Assert.assertTrue(json.contains("\"grade\":9.48")); | ||
Assert.assertTrue(json.contains("\"items\":[\"one\"]")); | ||
Assert.assertTrue(json.contains("\"name\":\"XWiki\"")); | ||
Assert.assertTrue(json.contains("\"parameters\":{\"foo\":\"bar\"}")); | ||
} | ||
} |