felix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dav...@apache.org
Subject svn commit: r1751503 - in /felix/trunk/converter: ./ src/main/java/org/apache/felix/converter/impl/json/ src/main/java/org/apache/felix/converter/impl/yaml/
Date Tue, 05 Jul 2016 15:34:33 GMT
Author: davidb
Date: Tue Jul  5 15:34:33 2016
New Revision: 1751503

URL: http://svn.apache.org/viewvc?rev=1751503&view=rev
Log:
Felix Converter Service - additional methods implemented for codecs.

Modified:
    felix/trunk/converter/pom.xml
    felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/json/JsonDecodingImpl.java
    felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/json/JsonEncodingImpl.java
    felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/yaml/YamlDecodingImpl.java
    felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/yaml/YamlEncodingImpl.java

Modified: felix/trunk/converter/pom.xml
URL: http://svn.apache.org/viewvc/felix/trunk/converter/pom.xml?rev=1751503&r1=1751502&r2=1751503&view=diff
==============================================================================
--- felix/trunk/converter/pom.xml (original)
+++ felix/trunk/converter/pom.xml Tue Jul  5 15:34:33 2016
@@ -66,7 +66,7 @@
                 <configuration>
                     <instructions>
                         <Bundle-Activator>org.apache.felix.converter.impl.Activator</Bundle-Activator>
-                        <Private-Package>org.apache.felix.converter.*</Private-Package>
+                        <Private-Package>org.apache.felix.converter.*,org.yaml.snakeyaml.*</Private-Package>
                         <Export-Package>org.osgi.service.converter</Export-Package>
                         <Import-Package>*</Import-Package>
                     </instructions>

Modified: felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/json/JsonDecodingImpl.java
URL: http://svn.apache.org/viewvc/felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/json/JsonDecodingImpl.java?rev=1751503&r1=1751502&r2=1751503&view=diff
==============================================================================
--- felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/json/JsonDecodingImpl.java
(original)
+++ felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/json/JsonDecodingImpl.java
Tue Jul  5 15:34:33 2016
@@ -42,7 +42,7 @@ public class JsonDecodingImpl<T> impleme
     public T from(CharSequence in) {
         JsonParser jp = new JsonParser(in);
         Map<?,?> m = jp.getParsed();
-        if (Map.class.isAssignableFrom(clazz))
+        if (m.getClass().isAssignableFrom(clazz))
             return (T) m;
 
         return converter.convert(m).to(clazz);

Modified: felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/json/JsonEncodingImpl.java
URL: http://svn.apache.org/viewvc/felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/json/JsonEncodingImpl.java?rev=1751503&r1=1751502&r2=1751503&view=diff
==============================================================================
--- felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/json/JsonEncodingImpl.java
(original)
+++ felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/json/JsonEncodingImpl.java
Tue Jul  5 15:34:33 2016
@@ -49,11 +49,10 @@ public class JsonEncodingImpl implements
     public Appendable to(Appendable out) {
         try {
             out.append(encode(object));
+            return out;
         } catch (IOException e) {
             throw new ConversionException("Problem converting to JSON", e);
         }
-
-        return out;
     }
 
     @Override

Modified: 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=1751503&r1=1751502&r2=1751503&view=diff
==============================================================================
--- felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/yaml/YamlDecodingImpl.java
(original)
+++ felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/yaml/YamlDecodingImpl.java
Tue Jul  5 15:34:33 2016
@@ -16,9 +16,14 @@
  */
 package org.apache.felix.converter.impl.yaml;
 
+import java.io.IOException;
 import java.io.InputStream;
 import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
+import java.util.Scanner;
 
+import org.apache.felix.converter.impl.Util;
+import org.osgi.service.converter.ConversionException;
 import org.osgi.service.converter.Converter;
 import org.osgi.service.converter.Decoding;
 import org.yaml.snakeyaml.Yaml;
@@ -34,28 +39,37 @@ public class YamlDecodingImpl<T> impleme
 
     @Override
     public T from(InputStream in) {
-
-        // TODO Auto-generated method stub
-        return null;
+        return from(in, StandardCharsets.UTF_8);
     }
 
     @Override
     public T from(InputStream in, Charset charset) {
-        // TODO Auto-generated method stub
-        return null;
+        try {
+            byte[] bytes = Util.readStream(in);
+            String s = new String(bytes, charset);
+            return from(s);
+        } catch (IOException e) {
+            throw new ConversionException("Error reading inputstream", e);
+        }
     }
 
     @Override
     public T from(Readable in) {
-        // TODO Auto-generated method stub
-        return null;
+        try (Scanner s = new Scanner(in)) {
+            s.useDelimiter("\\Z");
+            return from(s.next());
+        }
     }
 
     @Override
+    @SuppressWarnings("unchecked")
     public T from(CharSequence in) {
         Yaml yaml = new Yaml();
         Object res = yaml.load(in.toString());
-        return (T) res;
-    }
 
+        if (res.getClass().isAssignableFrom(clazz))
+            return (T) res;
+
+        return converter.convert(res).to(clazz);
+    }
 }

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=1751503&r1=1751502&r2=1751503&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 Jul  5 15:34:33 2016
@@ -20,6 +20,7 @@ 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;
@@ -27,6 +28,7 @@ import java.util.Map;
 import java.util.Map.Entry;
 import java.util.Set;
 
+import org.osgi.service.converter.ConversionException;
 import org.osgi.service.converter.Converter;
 import org.osgi.service.converter.Encoding;
 
@@ -43,38 +45,31 @@ public class YamlEncodingImpl implements
     }
 
     @Override
-    public Encoding pretty() {
-        // TODO Auto-generated method stub
-        return null;
-    }
-
-    @Override
-    public Encoding ignoreNull() {
-        // TODO Auto-generated method stub
-        return null;
+    public Appendable to(Appendable out) {
+        try {
+            out.append(encode(object));
+            return out;
+        } catch (IOException e) {
+            throw new ConversionException("Problem converting to YAML", e);
+        }
     }
 
-    @Override
-    public void to(OutputStream out) throws IOException {
-        // TODO Auto-generated method stub
 
+   @Override
+    public void to(OutputStream os) throws IOException {
+       to(os, StandardCharsets.UTF_8);
     }
+
     @Override
     public void to(OutputStream os, Charset charset) {
         try {
             os.write(encode(object).getBytes(charset));
         } catch (IOException e) {
-            throw new RuntimeException(e);
+            throw new ConversionException("Problem converting to YAML", e);
         }
     }
 
     @Override
-    public Appendable to(Appendable out) {
-        // TODO Auto-generated method stub
-        return null;
-    }
-
-    @Override
     public String toString() {
         return encode(object);
     }
@@ -145,4 +140,16 @@ public class YamlEncodingImpl implements
             sb.append(' ');
         return sb.toString();
     }
+
+    @Override
+    public Encoding pretty() {
+        // TODO Auto-generated method stub
+        return null;
+    }
+
+    @Override
+    public Encoding ignoreNull() {
+        // TODO Auto-generated method stub
+        return null;
+    }
 }



Mime
View raw message