felix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dav...@apache.org
Subject svn commit: r1756806 - in /felix/trunk/converter/src: main/java/org/apache/felix/converter/impl/ main/java/org/apache/felix/converter/impl/json/ test/java/org/apache/felix/converter/impl/json/
Date Thu, 18 Aug 2016 15:08:02 GMT
Author: davidb
Date: Thu Aug 18 15:08:02 2016
New Revision: 1756806

URL: http://svn.apache.org/viewvc?rev=1756806&view=rev
Log:
Felix Converter Service: Support converting DTOs to JSON and back

Modified:
    felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/ConvertingImpl.java
    felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/json/JsonCodecImpl.java
    felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/json/JsonEncodingImpl.java
    felix/trunk/converter/src/test/java/org/apache/felix/converter/impl/json/JsonCodecTest.java

Modified: felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/ConvertingImpl.java
URL: http://svn.apache.org/viewvc/felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/ConvertingImpl.java?rev=1756806&r1=1756805&r2=1756806&view=diff
==============================================================================
--- felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/ConvertingImpl.java
(original)
+++ felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/ConvertingImpl.java
Thu Aug 18 15:08:02 2016
@@ -230,11 +230,8 @@ public class ConvertingImpl implements C
             for (Map.Entry entry : (Set<Map.Entry>) m.entrySet()) {
                 try {
                     Field f = targetCls.getField(entry.getKey().toString());
-                    Object fVal = entry.getValue();
-                    if(DTO.class.isAssignableFrom( f.getType()))
-                        fVal = converter.convert(fVal).to(f.getType());
-                    // TODO convert other embedded objects that require conversion
-                    f.set(dto, fVal);
+                    Object val = entry.getValue();
+                    f.set(dto, converter.convert(val).to(f.getType()));
                 } catch (NoSuchFieldException e) {
                 }
             }

Modified: felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/json/JsonCodecImpl.java
URL: http://svn.apache.org/viewvc/felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/json/JsonCodecImpl.java?rev=1756806&r1=1756805&r2=1756806&view=diff
==============================================================================
--- felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/json/JsonCodecImpl.java
(original)
+++ felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/json/JsonCodecImpl.java
Thu Aug 18 15:08:02 2016
@@ -56,7 +56,7 @@ public class JsonCodecImpl implements Co
             if (top == null) {
                 threadLocal.set(Boolean.TRUE);
 
-                // TODO implement this properly
+                // TODO implement this properly, the following it just a dev temp thing
                 encoding = new EncodingWrapper("{}{}{}{}{}", encoding, "{}{}{}{}{}");
             }
         }

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=1756806&r1=1756805&r2=1756806&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
Thu Aug 18 15:08:02 2016
@@ -28,6 +28,7 @@ import java.util.Map;
 import java.util.Map.Entry;
 import java.util.Set;
 
+import org.osgi.dto.DTO;
 import org.osgi.service.converter.ConversionException;
 import org.osgi.service.converter.Converter;
 import org.osgi.service.converter.Encoding;
@@ -84,6 +85,8 @@ public class JsonEncodingImpl implements
             return encodeMap((Map) obj);
         } else if (obj instanceof Collection) {
             return encodeCollection((Collection) obj);
+        } else if (obj instanceof DTO) {
+            return encodeMap(converter.convert(obj).to(Map.class));
         } else if (obj.getClass().isArray()) {
             return encodeCollection(asCollection(obj));
         } else if (obj instanceof Number) {

Modified: felix/trunk/converter/src/test/java/org/apache/felix/converter/impl/json/JsonCodecTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/converter/src/test/java/org/apache/felix/converter/impl/json/JsonCodecTest.java?rev=1756806&r1=1756805&r2=1756806&view=diff
==============================================================================
--- felix/trunk/converter/src/test/java/org/apache/felix/converter/impl/json/JsonCodecTest.java
(original)
+++ felix/trunk/converter/src/test/java/org/apache/felix/converter/impl/json/JsonCodecTest.java
Thu Aug 18 15:08:02 2016
@@ -20,6 +20,10 @@ import java.util.HashMap;
 import java.util.Map;
 
 import org.apache.felix.converter.impl.ConverterImpl;
+import org.apache.felix.converter.impl.MyDTO;
+import org.apache.felix.converter.impl.MyDTO.Count;
+import org.apache.felix.converter.impl.MyEmbeddedDTO;
+import org.apache.felix.converter.impl.MyEmbeddedDTO.Alpha;
 import org.apache.sling.commons.json.JSONException;
 import org.apache.sling.commons.json.JSONObject;
 import org.junit.After;
@@ -94,6 +98,36 @@ public class JsonCodecTest {
         // TODO convert back into a Map<String, Foo> via TypeReference
     }
 
+    @Test
+    public void testDTO() {
+        MyDTO dto = new MyDTO();
+        dto.count = Count.ONE;
+        dto.ping = "'";
+        dto.pong = Long.MIN_VALUE;
+
+        MyEmbeddedDTO embedded = new MyEmbeddedDTO();
+        embedded.alpha = Alpha.B;
+        embedded.marco = "jo !";
+        embedded.polo = 327;
+        dto.embedded = embedded;
+
+        JsonCodecImpl jsonCodec = new JsonCodecImpl();
+        String json = jsonCodec.encode(dto).toString();
+        assertEquals(
+            "{\"ping\":\"'\",\"count\":\"ONE\",\"pong\":-9223372036854775808,"
+            + "\"embedded\":{\"polo\":327,\"alpha\":\"B\",\"marco\":\"jo !\"}}",
+            json);
+
+        MyDTO dto2 = jsonCodec.decode(MyDTO.class).from(json);
+        assertEquals(Count.ONE, dto2.count);
+        assertEquals("'", dto2.ping);
+        assertEquals(Long.MIN_VALUE, dto2.pong);
+        MyEmbeddedDTO embedded2 = dto2.embedded;
+        assertEquals(Alpha.B, embedded2.alpha);
+        assertEquals("jo !", embedded2.marco);
+        assertEquals(327, embedded2.polo);
+    }
+
     static class Foo {
         private final String val;
 



Mime
View raw message