felix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dav...@apache.org
Subject svn commit: r1748405 - in /felix/trunk/converter/src: main/java/org/apache/felix/converter/impl/yaml/ test/java/org/apache/felix/converter/impl/json/ test/java/org/apache/felix/converter/impl/yaml/
Date Tue, 14 Jun 2016 12:56:04 GMT
Author: davidb
Date: Tue Jun 14 12:56:04 2016
New Revision: 1748405

URL: http://svn.apache.org/viewvc?rev=1748405&view=rev
Log:
Felix Converter Service - Add a little more meat to the YAML encoding.

Modified:
    felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/yaml/YamlEncodingImpl.java
    felix/trunk/converter/src/test/java/org/apache/felix/converter/impl/json/JsonSerializationTest.java
    felix/trunk/converter/src/test/java/org/apache/felix/converter/impl/yaml/YamlSerializationTest.java

Modified: felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/yaml/YamlEncodingImpl.java
URL: http://svn.apache.org/viewvc/felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/yaml/YamlEncodingImpl.java?rev=1748405&r1=1748404&r2=1748405&view=diff
==============================================================================
--- felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/yaml/YamlEncodingImpl.java
(original)
+++ felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/yaml/YamlEncodingImpl.java
Tue Jun 14 12:56:04 2016
@@ -18,7 +18,11 @@ 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.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
 import java.util.Set;
@@ -75,6 +79,10 @@ public class YamlEncodingImpl implements
 
         if (obj instanceof Map) {
             return encodeMap((Map) obj, level);
+        } else if (obj instanceof Collection) {
+            return encodeCollection((Collection) obj, level);
+        } else if (obj.getClass().isArray()) {
+            return encodeCollection(asCollection(obj), level);
         } else if (obj instanceof Number)  {
             return obj.toString();
         } else if (obj instanceof Boolean) {
@@ -84,6 +92,27 @@ public class YamlEncodingImpl 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, int level) {
+        StringBuilder sb = new StringBuilder();
+        for (Object o : collection) {
+            sb.append("\n");
+            sb.append(getIdentPrefix(level));
+            sb.append("- ");
+            sb.append(encode(o));
+        }
+        return sb.toString();
+    }
+
     @SuppressWarnings({ "rawtypes", "unchecked" })
     private String encodeMap(Map m, int level) {
         StringBuilder sb = new StringBuilder();

Modified: felix/trunk/converter/src/test/java/org/apache/felix/converter/impl/json/JsonSerializationTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/converter/src/test/java/org/apache/felix/converter/impl/json/JsonSerializationTest.java?rev=1748405&r1=1748404&r2=1748405&view=diff
==============================================================================
--- felix/trunk/converter/src/test/java/org/apache/felix/converter/impl/json/JsonSerializationTest.java
(original)
+++ felix/trunk/converter/src/test/java/org/apache/felix/converter/impl/json/JsonSerializationTest.java
Tue Jun 14 12:56:04 2016
@@ -18,10 +18,9 @@ package org.apache.felix.converter.impl.
 
 import java.util.Arrays;
 import java.util.Collections;
-import java.util.HashMap;
+import java.util.LinkedHashMap;
 import java.util.Map;
 
-import org.apache.felix.converter.impl.json.JsonCodecImpl;
 import org.junit.Test;
 
 import static org.junit.Assert.assertEquals;
@@ -29,34 +28,34 @@ import static org.junit.Assert.assertEqu
 public class JsonSerializationTest {
     @Test
     public void testComplexMapSerialization() {
-        Map<String, Object> m = new HashMap<>();
+        Map<String, Object> m = new LinkedHashMap<>();
         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<>();
+        Map<String, Object> m1 = new LinkedHashMap<>();
         m1.put("a", 1L);
         m1.put("b", "hello");
         m.put("simpleObject", m1);
 
-        String expected = "{\"bKey\":true,"
-                + "\"simpleArray\":[1,2,3],"
+        String expected = "{\"sKey\":\"a string\","
                 + "\"iKey\":42,"
-                + "\"sKey\":\"a string\","
-                + "\"simpleObject\":{\"a\":1,\"b\":\"hello\"},"
-                + "\"noKey\":null}";
+                + "\"bKey\":true,"
+                + "\"noKey\":null,"
+                + "\"simpleArray\":[1,2,3],"
+                + "\"simpleObject\":{\"a\":1,\"b\":\"hello\"}}";
         assertEquals(expected, new JsonCodecImpl().encode(m).toString());
     }
 
     @Test
     public void testComplexMapSerialization2() {
-        Map<String, Object> m2 = new HashMap<>();
+        Map<String, Object> m2 = new LinkedHashMap<>();
         m2.put("yes", Boolean.TRUE);
         m2.put("no", Collections.singletonMap("maybe", false));
 
-        Map<String, Object> cm = new HashMap<>();
+        Map<String, Object> cm = new LinkedHashMap<>();
         cm.put("list", Arrays.asList(
                 Collections.singletonMap("x", "y"),
                 Collections.singletonMap("x", "b")));
@@ -64,7 +63,7 @@ public class JsonSerializationTest {
 
         String expected = "{\"list\":[{\"x\":\"y\"},{\"x\":\"b\"}],"
                 + "\"embedded\":"
-                + "{\"no\":{\"maybe\":false},\"yes\":true}}";
+                + "{\"yes\":true,\"no\":{\"maybe\":false}}}";
         assertEquals(expected, new JsonCodecImpl().encode(cm).toString());
     }
 }

Modified: felix/trunk/converter/src/test/java/org/apache/felix/converter/impl/yaml/YamlSerializationTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/converter/src/test/java/org/apache/felix/converter/impl/yaml/YamlSerializationTest.java?rev=1748405&r1=1748404&r2=1748405&view=diff
==============================================================================
--- felix/trunk/converter/src/test/java/org/apache/felix/converter/impl/yaml/YamlSerializationTest.java
(original)
+++ felix/trunk/converter/src/test/java/org/apache/felix/converter/impl/yaml/YamlSerializationTest.java
Tue Jun 14 12:56:04 2016
@@ -16,7 +16,6 @@
  */
 package org.apache.felix.converter.impl.yaml;
 
-import java.util.HashMap;
 import java.util.LinkedHashMap;
 import java.util.Map;
 
@@ -34,12 +33,22 @@ public class YamlSerializationTest {
         m.put("noKey", null);
         m.put("simpleArray", new int[] {1,2,3});
 
-        Map<String, Object> m1 = new HashMap<>();
+        Map<String, Object> m1 = new LinkedHashMap<>();
         m1.put("a", 1L);
         m1.put("b", "hello");
         m.put("simpleObject", m1);
 
-        String expected = "";
-        assertEquals(expected, new YamlCodecImpl().encode(m).toString());
+        String expected = "sKey: 'a string'\n" +
+                "iKey: 42\n" +
+                "bKey: true\n" +
+                "noKey: \n" +
+                "simpleArray: \n" +
+                "  - 1\n" +
+                "  - 2\n" +
+                "  - 3\n" +
+                "simpleObject: \n" +
+                "  a: 1\n" +
+                "  b: 'hello'";
+        assertEquals(expected, new YamlCodecImpl().encode(m).toString().trim());
     }
 }



Mime
View raw message