Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSimone Bordet2013-07-26 12:17:42 +0000
committerSimone Bordet2013-07-26 12:17:42 +0000
commitbc096cace4f51c056f83fb8003f80d19b9c01809 (patch)
treefe6317cfb3bcc0fb1461f17f10010c921fde38d3 /jetty-util-ajax/src
parentcea4ac5fe3e19ac0d8044c1295fcefa8af85b019 (diff)
downloadorg.eclipse.jetty.project-bc096cace4f51c056f83fb8003f80d19b9c01809.tar.gz
org.eclipse.jetty.project-bc096cace4f51c056f83fb8003f80d19b9c01809.tar.xz
org.eclipse.jetty.project-bc096cace4f51c056f83fb8003f80d19b9c01809.zip
Moved new JSON classes to the right module.
Diffstat (limited to 'jetty-util-ajax/src')
-rw-r--r--jetty-util-ajax/src/main/java/org/eclipse/jetty/util/ajax/JSONCollectionConvertor.java50
-rw-r--r--jetty-util-ajax/src/test/java/org/eclipse/jetty/util/ajax/JSONCollectionConvertorTest.java112
2 files changed, 162 insertions, 0 deletions
diff --git a/jetty-util-ajax/src/main/java/org/eclipse/jetty/util/ajax/JSONCollectionConvertor.java b/jetty-util-ajax/src/main/java/org/eclipse/jetty/util/ajax/JSONCollectionConvertor.java
new file mode 100644
index 0000000000..9629e3c8f6
--- /dev/null
+++ b/jetty-util-ajax/src/main/java/org/eclipse/jetty/util/ajax/JSONCollectionConvertor.java
@@ -0,0 +1,50 @@
+//
+// ========================================================================
+// Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd.
+// ------------------------------------------------------------------------
+// All rights reserved. This program and the accompanying materials
+// are made available under the terms of the Eclipse Public License v1.0
+// and Apache License v2.0 which accompanies this distribution.
+//
+// The Eclipse Public License is available at
+// http://www.eclipse.org/legal/epl-v10.html
+//
+// The Apache License v2.0 is available at
+// http://www.opensource.org/licenses/apache2.0.php
+//
+// You may elect to redistribute this code under either of these licenses.
+// ========================================================================
+//
+
+package org.eclipse.jetty.util.ajax;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Map;
+
+import org.eclipse.jetty.util.Loader;
+
+public class JSONCollectionConvertor implements JSON.Convertor
+{
+ public void toJSON(Object obj, JSON.Output out)
+ {
+ out.addClass(obj.getClass());
+ out.add("list", ((Collection)obj).toArray());
+ }
+
+ public Object fromJSON(Map object)
+ {
+ try
+ {
+ Collection result = (Collection)Loader.loadClass(getClass(), (String)object.get("class")).newInstance();
+ Collections.addAll(result, (Object[])object.get("list"));
+ return result;
+ }
+ catch (Exception x)
+ {
+ if (x instanceof RuntimeException)
+ throw (RuntimeException)x;
+ throw new RuntimeException(x);
+ }
+ }
+}
diff --git a/jetty-util-ajax/src/test/java/org/eclipse/jetty/util/ajax/JSONCollectionConvertorTest.java b/jetty-util-ajax/src/test/java/org/eclipse/jetty/util/ajax/JSONCollectionConvertorTest.java
new file mode 100644
index 0000000000..6534483947
--- /dev/null
+++ b/jetty-util-ajax/src/test/java/org/eclipse/jetty/util/ajax/JSONCollectionConvertorTest.java
@@ -0,0 +1,112 @@
+//
+// ========================================================================
+// Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd.
+// ------------------------------------------------------------------------
+// All rights reserved. This program and the accompanying materials
+// are made available under the terms of the Eclipse Public License v1.0
+// and Apache License v2.0 which accompanies this distribution.
+//
+// The Eclipse Public License is available at
+// http://www.eclipse.org/legal/epl-v10.html
+//
+// The Apache License v2.0 is available at
+// http://www.opensource.org/licenses/apache2.0.php
+//
+// You may elect to redistribute this code under either of these licenses.
+// ========================================================================
+//
+
+package org.eclipse.jetty.util.ajax;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.TreeSet;
+import java.util.concurrent.CopyOnWriteArrayList;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class JSONCollectionConvertorTest
+{
+ @Test
+ public void testArrayList() throws Exception
+ {
+ List<String> list = new ArrayList<String>();
+ Collections.addAll(list, "one", "two");
+ testList(list);
+ }
+
+ @Test
+ public void testLinkedList() throws Exception
+ {
+ List<String> list = new LinkedList<String>();
+ Collections.addAll(list, "one", "two");
+ testList(list);
+ }
+
+ @Test
+ public void testCopyOnWriteArrayList() throws Exception
+ {
+ List<String> list = new CopyOnWriteArrayList<String>();
+ Collections.addAll(list, "one", "two");
+ testList(list);
+ }
+
+ private void testList(List<String> list1) throws Exception
+ {
+ JSON json = new JSON();
+ json.addConvertor(List.class, new JSONCollectionConvertor());
+
+ Map<String, Object> object1 = new HashMap<String, Object>();
+ String field = "field";
+ object1.put(field, list1);
+
+ String string = json.toJSON(object1);
+ Assert.assertTrue(string.contains(list1.getClass().getName()));
+
+ @SuppressWarnings("unchecked")
+ Map<String, Object> object2 = (Map<String, Object>)json.parse(new JSON.StringSource(string));
+ @SuppressWarnings("unchecked")
+ List<String> list2 = (List<String>)object2.get(field);
+
+ Assert.assertSame(list1.getClass(), list2.getClass());
+ Assert.assertEquals(list1, list2);
+ }
+
+ @Test
+ public void testHashSet() throws Exception
+ {
+ Set<String> set = new HashSet<String>();
+ Collections.addAll(set, "one", "two", "three");
+ testSet(set);
+ }
+
+ @Test
+ public void testTreeSet() throws Exception
+ {
+ Set<String> set = new TreeSet<String>();
+ Collections.addAll(set, "one", "two", "three");
+ testSet(set);
+ }
+
+ private void testSet(Set<String> set1)
+ {
+ JSON json = new JSON();
+ json.addConvertor(Set.class, new JSONCollectionConvertor());
+
+ String string = json.toJSON(set1);
+ Assert.assertTrue(string.contains(set1.getClass().getName()));
+
+ @SuppressWarnings("unchecked")
+ Set<String> set2 = (Set<String>)json.parse(new JSON.StringSource(string));
+
+ Assert.assertSame(set1.getClass(), set2.getClass());
+ Assert.assertEquals(set1, set2);
+ }
+}

Back to the top