felix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dav...@apache.org
Subject svn commit: r1762530 - in /felix/trunk/converter: converter/src/main/java/org/apache/felix/converter/impl/ serializer/src/main/java/org/apache/felix/serializer/impl/json/ serializer/src/test/java/org/apache/felix/serializer/impl/json/
Date Tue, 27 Sep 2016 18:09:06 GMT
Author: davidb
Date: Tue Sep 27 18:09:06 2016
New Revision: 1762530

URL: http://svn.apache.org/viewvc?rev=1762530&view=rev
Log:
Felix Converter - fix an issue with converting to a generics based type

Unit test in serializer included.

Modified:
    felix/trunk/converter/converter/src/main/java/org/apache/felix/converter/impl/ConvertingImpl.java
    felix/trunk/converter/serializer/src/main/java/org/apache/felix/serializer/impl/json/JsonDeserializingImpl.java
    felix/trunk/converter/serializer/src/main/java/org/apache/felix/serializer/impl/json/JsonSerializerImpl.java
    felix/trunk/converter/serializer/src/test/java/org/apache/felix/serializer/impl/json/JsonSerializerTest.java

Modified: felix/trunk/converter/converter/src/main/java/org/apache/felix/converter/impl/ConvertingImpl.java
URL: http://svn.apache.org/viewvc/felix/trunk/converter/converter/src/main/java/org/apache/felix/converter/impl/ConvertingImpl.java?rev=1762530&r1=1762529&r2=1762530&view=diff
==============================================================================
--- felix/trunk/converter/converter/src/main/java/org/apache/felix/converter/impl/ConvertingImpl.java
(original)
+++ felix/trunk/converter/converter/src/main/java/org/apache/felix/converter/impl/ConvertingImpl.java
Tue Sep 27 18:09:06 2016
@@ -252,11 +252,10 @@ public class ConvertingImpl implements C
         Map m = mapView(object, converter);
         if (m == null)
             return null;
-        Class<?> targetKeyType = null, targetValueType = null;
-        if (typeArguments != null && typeArguments.length > 1 &&
-                typeArguments[0] instanceof Class && typeArguments[1] instanceof
Class) {
-            targetKeyType = (Class<?>) typeArguments[0];
-            targetValueType = (Class<?>) typeArguments[1];
+        Type targetKeyType = null, targetValueType = null;
+        if (typeArguments != null && typeArguments.length > 1) {
+            targetKeyType = typeArguments[0];
+            targetValueType = typeArguments[1];
         }
 
         Class<?> ctrCls = interfaceImplementations.get(targetCls);

Modified: felix/trunk/converter/serializer/src/main/java/org/apache/felix/serializer/impl/json/JsonDeserializingImpl.java
URL: http://svn.apache.org/viewvc/felix/trunk/converter/serializer/src/main/java/org/apache/felix/serializer/impl/json/JsonDeserializingImpl.java?rev=1762530&r1=1762529&r2=1762530&view=diff
==============================================================================
--- felix/trunk/converter/serializer/src/main/java/org/apache/felix/serializer/impl/json/JsonDeserializingImpl.java
(original)
+++ felix/trunk/converter/serializer/src/main/java/org/apache/felix/serializer/impl/json/JsonDeserializingImpl.java
Tue Sep 27 18:09:06 2016
@@ -18,6 +18,7 @@ package org.apache.felix.serializer.impl
 
 import java.io.IOException;
 import java.io.InputStream;
+import java.lang.reflect.Type;
 import java.nio.charset.Charset;
 import java.nio.charset.StandardCharsets;
 import java.util.Map;
@@ -29,10 +30,10 @@ import org.osgi.service.converter.Conver
 import org.osgi.service.serializer.Deserializing;
 
 public class JsonDeserializingImpl<T> implements Deserializing<T> {
-    private final Class<T> clazz;
+    private final Type clazz;
     private volatile Converter converter;
 
-    public JsonDeserializingImpl(Converter c, Class<T> cls) {
+    public JsonDeserializingImpl(Converter c, Type cls) {
         converter = c;
         clazz = cls;
     }
@@ -42,10 +43,11 @@ public class JsonDeserializingImpl<T> im
     public T from(CharSequence in) {
         JsonParser jp = new JsonParser(in);
         Map<?,?> m = jp.getParsed();
-        if (m.getClass().isAssignableFrom(clazz))
-            return (T) m;
+        if (clazz instanceof Class)
+            if (m.getClass().isAssignableFrom((Class<?>) clazz))
+                return (T) m;
 
-        return converter.convert(m).to(clazz);
+        return (T) converter.convert(m).to(clazz);
     }
 
     @Override

Modified: felix/trunk/converter/serializer/src/main/java/org/apache/felix/serializer/impl/json/JsonSerializerImpl.java
URL: http://svn.apache.org/viewvc/felix/trunk/converter/serializer/src/main/java/org/apache/felix/serializer/impl/json/JsonSerializerImpl.java?rev=1762530&r1=1762529&r2=1762530&view=diff
==============================================================================
--- felix/trunk/converter/serializer/src/main/java/org/apache/felix/serializer/impl/json/JsonSerializerImpl.java
(original)
+++ felix/trunk/converter/serializer/src/main/java/org/apache/felix/serializer/impl/json/JsonSerializerImpl.java
Tue Sep 27 18:09:06 2016
@@ -103,7 +103,6 @@ public class JsonSerializerImpl implemen
         @Override
         public void to(OutputStream out, Charset charset) {
             // TODO Auto-generated method stub
-
         }
 
         @Override
@@ -121,13 +120,11 @@ public class JsonSerializerImpl implemen
 
     @Override
     public <T> Deserializing<T> deserialize(TypeReference<T> ref) {
-        // TODO Auto-generated method stub
-        return null;
+        return new JsonDeserializingImpl<T>(converter, ref.getType());
     }
 
-    @Override
+    @Override @SuppressWarnings("rawtypes")
     public Deserializing<?> deserialize(Type type) {
-        // TODO Auto-generated method stub
-        return null;
+        return new JsonDeserializingImpl(converter, type);
     }
 }

Modified: felix/trunk/converter/serializer/src/test/java/org/apache/felix/serializer/impl/json/JsonSerializerTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/converter/serializer/src/test/java/org/apache/felix/serializer/impl/json/JsonSerializerTest.java?rev=1762530&r1=1762529&r2=1762530&view=diff
==============================================================================
--- felix/trunk/converter/serializer/src/test/java/org/apache/felix/serializer/impl/json/JsonSerializerTest.java
(original)
+++ felix/trunk/converter/serializer/src/test/java/org/apache/felix/serializer/impl/json/JsonSerializerTest.java
Tue Sep 27 18:09:06 2016
@@ -28,6 +28,7 @@ import org.junit.Before;
 import org.junit.Test;
 import org.osgi.service.converter.Converter;
 import org.osgi.service.converter.StandardConverter;
+import org.osgi.service.converter.TypeReference;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertTrue;
@@ -78,7 +79,7 @@ public class JsonSerializerTest {
     public void testCodecWithAdapter() throws JSONException {
         Map<String, Foo> m1 = new HashMap<>();
         m1.put("f", new Foo("fofofo"));
-        Map<String, Object> m = new HashMap<>();
+        Map<String, Map<String,Foo>> m = new HashMap<>();
         m.put("submap", m1);
 
         Converter ca = converter.newConverterBuilder().
@@ -92,7 +93,10 @@ public class JsonSerializerTest {
         JSONObject jo1 = jo.getJSONObject("submap");
         assertEquals("<fofofo>", jo1.getString("f"));
 
-        // TODO convert back into a Map<String, Foo> via TypeReference
+        // And convert back
+        Map<String,Map<String,Foo>> m2 = jsonCodec.deserialize(new TypeReference<Map<String,Map<String,Foo>>>(){}).
+                with(ca).from(json);
+        assertEquals(m, m2);
     }
 
     @Test
@@ -139,5 +143,21 @@ public class JsonSerializerTest {
         public static Foo fsFun(String s) {
             return new Foo(s.substring(1, s.length() - 1));
         }
+
+        @Override
+        public int hashCode() {
+            return val.hashCode();
+        }
+
+        @Override
+        public boolean equals(Object obj) {
+            if (obj == this)
+                return true;
+            if (!(obj instanceof Foo))
+                return false;
+
+            Foo f = (Foo) obj;
+            return f.val.equals(val);
+        }
     }
 }



Mime
View raw message