Skip to content

Commit

Permalink
XCOMMONS-130: Upgrade to Json-lib 2.4
Browse files Browse the repository at this point in the history
XCOMMONS-131: Add a Velocity tool to serialize Java objects to JSON format
  • Loading branch information
mflorea committed Mar 15, 2012
1 parent 66ff072 commit ac22d47
Show file tree
Hide file tree
Showing 5 changed files with 225 additions and 0 deletions.
8 changes: 8 additions & 0 deletions pom.xml
Expand Up @@ -194,6 +194,14 @@
<version>1.4.2</version>
</dependency>

<!-- Used in both commons and platform -->
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>

<!-- Everybody logs -->
<dependency>
<groupId>org.slf4j</groupId>
Expand Down
6 changes: 6 additions & 0 deletions xwiki-commons-core/xwiki-commons-velocity/pom.xml
Expand Up @@ -77,6 +77,12 @@
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
</dependency>
<dependency>
<!-- Used by JSONTool -->
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<classifier>jdk15</classifier>
</dependency>
<!-- Test Dependencies -->
<dependency>
<groupId>org.xwiki.commons</groupId>
Expand Down
Expand Up @@ -41,6 +41,7 @@
import org.xwiki.velocity.introspection.DeprecatedCheckUberspector;
import org.xwiki.velocity.tools.CollectionsTool;
import org.xwiki.velocity.tools.EscapeTool;
import org.xwiki.velocity.tools.JSONTool;
import org.xwiki.velocity.tools.RegexTool;

/**
Expand Down Expand Up @@ -87,6 +88,7 @@ public void initialize() throws InitializationException
this.defaultTools.setProperty("regextool", RegexTool.class.getName());
this.defaultTools.setProperty("collectionstool", CollectionsTool.class.getName());
this.defaultTools.setProperty("stringtool", StringUtils.class.getName());
this.defaultTools.setProperty("jsontool", JSONTool.class.getName());

// Default Velocity properties
this.defaultProperties.setProperty("directive.set.null.allowed", Boolean.TRUE.toString());
Expand Down
@@ -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();
}
}
@@ -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\"}"));
}
}

0 comments on commit ac22d47

Please sign in to comment.