felix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dav...@apache.org
Subject svn commit: r1748281 - in /felix/trunk/converter/src: main/java/org/apache/felix/converter/impl/JsonEncodingImpl.java test/java/org/apache/felix/converter/impl/JSONSerializationTest.java test/java/org/apache/felix/converter/impl/SerializationTest.java
Date Mon, 13 Jun 2016 16:22:44 GMT
Author: davidb
Date: Mon Jun 13 16:22:44 2016
New Revision: 1748281

URL: http://svn.apache.org/viewvc?rev=1748281&view=rev
Log:
Felix Converter - Additional JSON serialization tests.

Added:
    felix/trunk/converter/src/test/java/org/apache/felix/converter/impl/JSONSerializationTest.java
Removed:
    felix/trunk/converter/src/test/java/org/apache/felix/converter/impl/SerializationTest.java
Modified:
    felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/JsonEncodingImpl.java

Modified: felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/JsonEncodingImpl.java
URL: http://svn.apache.org/viewvc/felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/JsonEncodingImpl.java?rev=1748281&r1=1748280&r2=1748281&view=diff
==============================================================================
--- felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/JsonEncodingImpl.java
(original)
+++ felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/JsonEncodingImpl.java
Mon Jun 13 16:22:44 2016
@@ -18,8 +18,12 @@ package org.apache.felix.converter.impl;
 
 import java.io.IOException;
 import java.io.OutputStream;
+import java.lang.reflect.Array;
 import java.nio.charset.Charset;
 import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
 import java.util.Set;
@@ -56,6 +60,7 @@ public class JsonEncodingImpl implements
         return encode(object);
     }
 
+    @SuppressWarnings("rawtypes")
     public String encode(Object obj) {
         if (obj == null) {
             return ignoreNull() ? "" : "null";
@@ -63,6 +68,10 @@ public class JsonEncodingImpl implements
 
         if (obj instanceof Map) {
             return encodeMap((Map) obj);
+        } else if (obj instanceof Collection) {
+            return encodeCollection((Collection) obj);
+        } else if (obj.getClass().isArray()) {
+            return encodeCollection(asCollection(obj));
         } else if (obj instanceof Number) {
             return obj.toString();
         } else if (obj instanceof Boolean) {
@@ -72,6 +81,34 @@ public class JsonEncodingImpl implements
         return "\"" + converter.convert(obj).to(String.class) + "\"";
     }
 
+    private Collection<?> asCollection(Object arr) {
+        // Arrays.asList() doesn't work for primitive arrays
+        int len = Array.getLength(arr);
+        List<Object> l = new ArrayList<>(len);
+        for (int i=0; i<len; i++) {
+            l.add(Array.get(arr, i));
+        }
+        return l;
+    }
+
+    private String encodeCollection(Collection<?> collection) {
+        StringBuilder sb = new StringBuilder("[");
+
+        boolean first = true;
+        for (Object o : collection) {
+            if (first)
+                first = false;
+            else
+                sb.append(',');
+
+            sb.append(encode(o));
+        }
+
+        sb.append("]");
+        return sb.toString();
+    }
+
+    @SuppressWarnings({ "rawtypes", "unchecked" })
     private String encodeMap(Map m) {
         StringBuilder sb = new StringBuilder("{");
         for (Entry<?,?> entry : (Set<Entry>) m.entrySet()) {

Added: felix/trunk/converter/src/test/java/org/apache/felix/converter/impl/JSONSerializationTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/converter/src/test/java/org/apache/felix/converter/impl/JSONSerializationTest.java?rev=1748281&view=auto
==============================================================================
--- felix/trunk/converter/src/test/java/org/apache/felix/converter/impl/JSONSerializationTest.java
(added)
+++ felix/trunk/converter/src/test/java/org/apache/felix/converter/impl/JSONSerializationTest.java
Mon Jun 13 16:22:44 2016
@@ -0,0 +1,69 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.felix.converter.impl;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+
+public class JSONSerializationTest {
+    @Test
+    public void testComplexMapSerialization() {
+        Map<String, Object> m = new HashMap<>();
+        m.put("sKey", "a string");
+        m.put("iKey", 42);
+        m.put("bKey",  true);
+        m.put("noKey", null);
+        m.put("simpleArray", new int[] {1,2,3});
+
+        Map<String, Object> m1 = new HashMap<>();
+        m1.put("a", 1L);
+        m1.put("b", "hello");
+        m.put("simpleObject", m1);
+
+        String expected = "{\"bKey\":true,"
+                + "\"simpleArray\":[1,2,3],"
+                + "\"iKey\":42,"
+                + "\"sKey\":\"a string\","
+                + "\"simpleObject\":{\"a\":1,\"b\":\"hello\"},"
+                + "\"noKey\":null}";
+        assertEquals(expected, new JsonCodecImpl().encode(m).toString());
+    }
+
+    @Test
+    public void testComplexMapSerialization2() {
+        Map<String, Object> m2 = new HashMap<>();
+        m2.put("yes", Boolean.TRUE);
+        m2.put("no", Collections.singletonMap("maybe", false));
+
+        Map<String, Object> cm = new HashMap<>();
+        cm.put("list", Arrays.asList(
+                Collections.singletonMap("x", "y"),
+                Collections.singletonMap("x", "b")));
+        cm.put("embedded", m2);
+
+        String expected = "{\"list\":[{\"x\":\"y\"},{\"x\":\"b\"}],"
+                + "\"embedded\":"
+                + "{\"no\":{\"maybe\":false},\"yes\":true}}";
+        assertEquals(expected, new JsonCodecImpl().encode(cm).toString());
+    }
+}



Mime
View raw message