You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by da...@apache.org on 2016/07/05 14:40:40 UTC

svn commit: r1751492 - in /felix/trunk/converter: ./ src/main/java/org/apache/felix/converter/impl/yaml/ src/test/java/org/apache/felix/converter/impl/yaml/

Author: davidb
Date: Tue Jul  5 14:40:40 2016
New Revision: 1751492

URL: http://svn.apache.org/viewvc?rev=1751492&view=rev
Log:
Felix Converter Service - start of Yaml decoder.

Added:
    felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/yaml/YamlDecodingImpl.java
Modified:
    felix/trunk/converter/pom.xml
    felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/yaml/YamlCodecImpl.java
    felix/trunk/converter/src/test/java/org/apache/felix/converter/impl/yaml/YamlSerializationTest.java

Modified: felix/trunk/converter/pom.xml
URL: http://svn.apache.org/viewvc/felix/trunk/converter/pom.xml?rev=1751492&r1=1751491&r2=1751492&view=diff
==============================================================================
--- felix/trunk/converter/pom.xml (original)
+++ felix/trunk/converter/pom.xml Tue Jul  5 14:40:40 2016
@@ -115,7 +115,6 @@
             <groupId>org.yaml</groupId>
             <artifactId>snakeyaml</artifactId>
             <version>1.17</version>
-            <scope>provided</scope>
         </dependency>
         
         <dependency>

Modified: felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/yaml/YamlCodecImpl.java
URL: http://svn.apache.org/viewvc/felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/yaml/YamlCodecImpl.java?rev=1751492&r1=1751491&r2=1751492&view=diff
==============================================================================
--- felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/yaml/YamlCodecImpl.java (original)
+++ felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/yaml/YamlCodecImpl.java Tue Jul  5 14:40:40 2016
@@ -39,8 +39,7 @@ public class YamlCodecImpl implements Co
 
     @Override
     public <T> Decoding<T> decode(Class<T> cls) {
-        // TODO Auto-generated method stub
-        return null;
+        return new YamlDecodingImpl<T>(converter, cls);
     }
 
     @Override

Added: felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/yaml/YamlDecodingImpl.java
URL: http://svn.apache.org/viewvc/felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/yaml/YamlDecodingImpl.java?rev=1751492&view=auto
==============================================================================
--- felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/yaml/YamlDecodingImpl.java (added)
+++ felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/yaml/YamlDecodingImpl.java Tue Jul  5 14:40:40 2016
@@ -0,0 +1,61 @@
+/*
+ * 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.yaml;
+
+import java.io.InputStream;
+import java.nio.charset.Charset;
+
+import org.osgi.service.converter.Converter;
+import org.osgi.service.converter.Decoding;
+import org.yaml.snakeyaml.Yaml;
+
+public class YamlDecodingImpl<T> implements Decoding<T> {
+    private final Converter converter;
+    private final Class<T> clazz;
+
+    public YamlDecodingImpl(Converter c, Class<T> cls) {
+        converter = c;
+        clazz = cls;
+    }
+
+    @Override
+    public T from(InputStream in) {
+
+        // TODO Auto-generated method stub
+        return null;
+    }
+
+    @Override
+    public T from(InputStream in, Charset charset) {
+        // TODO Auto-generated method stub
+        return null;
+    }
+
+    @Override
+    public T from(Readable in) {
+        // TODO Auto-generated method stub
+        return null;
+    }
+
+    @Override
+    public T from(CharSequence in) {
+        Yaml yaml = new Yaml();
+        Object res = yaml.load(in.toString());
+        return (T) res;
+    }
+
+}

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=1751492&r1=1751491&r2=1751492&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 Jul  5 14:40:40 2016
@@ -27,6 +27,7 @@ import static org.junit.Assert.assertEqu
 
 public class YamlSerializationTest {
     @Test
+    @SuppressWarnings("unchecked")
     public void testComplexMapSerialization() {
         Map<String, Object> m = new LinkedHashMap<>();
         m.put("sKey", "a string");
@@ -52,6 +53,20 @@ public class YamlSerializationTest {
                 "  a: 1\n" +
                 "  b: 'hello'";
         assertEquals(expected, new YamlCodecImpl().encode(m).toString().trim());
+
+        Map<String, Object> dm = new YamlCodecImpl().decode(Map.class).from(expected);
+        Map<String, Object> expected2 = new LinkedHashMap<>();
+        expected2.put("sKey", "a string");
+        expected2.put("iKey", 42);
+        expected2.put("bKey",  true);
+        expected2.put("noKey", null);
+        expected2.put("simpleArray", Arrays.asList(1,2,3));
+
+        Map<String, Object> m2 = new LinkedHashMap<>();
+        m2.put("a", 1);
+        m2.put("b", "hello");
+        expected2.put("simpleObject", m2);
+        assertEquals(expected2, dm);
     }
 
     @Test