You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@johnzon.apache.org by rs...@apache.org on 2016/04/18 10:18:12 UTC

[4/7] incubator-johnzon git commit: JOHNZON-72 added ObjectConverter support for reading json

JOHNZON-72 added ObjectConverter support for reading json


Project: http://git-wip-us.apache.org/repos/asf/incubator-johnzon/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-johnzon/commit/e1b93f8b
Tree: http://git-wip-us.apache.org/repos/asf/incubator-johnzon/tree/e1b93f8b
Diff: http://git-wip-us.apache.org/repos/asf/incubator-johnzon/diff/e1b93f8b

Branch: refs/heads/master
Commit: e1b93f8b6940f2ed2e83f672e661b7801f95f965
Parents: cff8388
Author: Reinhard Sandtner <rs...@apache.org>
Authored: Fri Apr 15 12:06:26 2016 +0200
Committer: Reinhard Sandtner <rs...@apache.org>
Committed: Fri Apr 15 12:06:26 2016 +0200

----------------------------------------------------------------------
 .../apache/johnzon/mapper/MappingParser.java    |  2 +
 .../johnzon/mapper/MappingParserImpl.java       | 25 +++++-
 .../apache/johnzon/mapper/ObjectTypeTest.java   | 88 +++++++++++++++-----
 3 files changed, 92 insertions(+), 23 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-johnzon/blob/e1b93f8b/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/MappingParser.java
----------------------------------------------------------------------
diff --git a/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/MappingParser.java b/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/MappingParser.java
index cee68d8..98836d5 100644
--- a/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/MappingParser.java
+++ b/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/MappingParser.java
@@ -31,4 +31,6 @@ public interface MappingParser {
     <T> T readObject(Type targetType);
 
     <T> T readObject(JsonValue jsonValue, Type targetType);
+
+    <T> T readObject(JsonValue jsonValue, Type targetType, boolean applyObjectConverter);
 }

http://git-wip-us.apache.org/repos/asf/incubator-johnzon/blob/e1b93f8b/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/MappingParserImpl.java
----------------------------------------------------------------------
diff --git a/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/MappingParserImpl.java b/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/MappingParserImpl.java
index e13778d..80afdec 100644
--- a/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/MappingParserImpl.java
+++ b/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/MappingParserImpl.java
@@ -112,11 +112,15 @@ public class MappingParserImpl implements MappingParser {
 
     @Override
     public <T> T readObject(JsonValue jsonValue, Type targetType) {
+        return readObject(jsonValue, targetType, targetType instanceof Class);
+    }
+
+    public <T> T readObject(JsonValue jsonValue, Type targetType, boolean applyObjectConverter) {
         if (JsonStructure.class == targetType || JsonObject.class == targetType || JsonValue.class == targetType) {
             return (T) jsonValue;
         }
         if (JsonObject.class.isInstance(jsonValue)) {
-            return (T) buildObject(targetType, JsonObject.class.cast(jsonValue));
+            return (T) buildObject(targetType, JsonObject.class.cast(jsonValue), applyObjectConverter);
         }
         if (JsonString.class.isInstance(jsonValue) && targetType == String.class) {
             return (T) JsonString.class.cast(jsonValue).getString();
@@ -170,12 +174,24 @@ public class MappingParserImpl implements MappingParser {
     }
 
 
-    private Object buildObject(final Type inType, final JsonObject object) {
+    private Object buildObject(final Type inType, final JsonObject object, final boolean applyObjectConverter) {
         Type type = inType;
         if (inType == Object.class) {
             type = new JohnzonParameterizedType(Map.class, String.class, Object.class);
         }
 
+        if (applyObjectConverter && !(type instanceof JohnzonParameterizedType)) {
+
+            if (!(type instanceof Class)) {
+                throw new MapperException("ObjectConverters are only supported for Classes not Types");
+            }
+
+            ObjectConverter objectConverter = config.findObjectConverter((Class) type);
+            if (objectConverter != null) {
+                return objectConverter.fromJson(object, type, this);
+            }
+        }
+
         final Mappings.ClassMapping classMapping = mappings.findOrCreateClassMapping(type);
 
         if (classMapping == null) {
@@ -307,7 +323,8 @@ public class MappingParserImpl implements MappingParser {
 
             final Object param;
             try {
-                param = buildObject(adapterKey.getTo(), JsonObject.class.cast(jsonValue));
+                Type to = adapterKey.getTo();
+                param = buildObject(to, JsonObject.class.cast(jsonValue), to instanceof Class);
             } catch (final Exception e) {
                 throw new MapperException(e);
             }
@@ -366,7 +383,7 @@ public class MappingParserImpl implements MappingParser {
             final Object object = buildObject(
                     baseInstance != null ? baseInstance.getClass() : (
                             typedAdapter ? TypeAwareAdapter.class.cast(itemConverter).getTo() : type),
-                    JsonObject.class.cast(jsonValue));
+                    JsonObject.class.cast(jsonValue), type instanceof Class);
             return typedAdapter ? itemConverter.to(object) : object;
         } else if (JsonArray.class.isInstance(jsonValue)) {
             if (JsonArray.class == type || JsonStructure.class == type) {

http://git-wip-us.apache.org/repos/asf/incubator-johnzon/blob/e1b93f8b/johnzon-mapper/src/test/java/org/apache/johnzon/mapper/ObjectTypeTest.java
----------------------------------------------------------------------
diff --git a/johnzon-mapper/src/test/java/org/apache/johnzon/mapper/ObjectTypeTest.java b/johnzon-mapper/src/test/java/org/apache/johnzon/mapper/ObjectTypeTest.java
index e6c12d0..9f4c314 100644
--- a/johnzon-mapper/src/test/java/org/apache/johnzon/mapper/ObjectTypeTest.java
+++ b/johnzon-mapper/src/test/java/org/apache/johnzon/mapper/ObjectTypeTest.java
@@ -52,22 +52,28 @@ public class ObjectTypeTest {
                 .setAttributeOrder(String.CASE_INSENSITIVE_ORDER)
                 .build();
 
-        String expectedJsonString = "{" +
-                                      "\"//javaType\":\"org.apache.johnzon.mapper.ObjectTypeTest$Mutt\"," +
-                                      "\"father\":{" +
-                                        "\"//javaType\":\"org.apache.johnzon.mapper.ObjectTypeTest$Beagle\"," +
-                                          "\"father\":{" +
-                                            "\"//javaType\":\"org.apache.johnzon.mapper.ObjectTypeTest$Beagle\"," +
-                                            "\"name\":\"Wuffi\"" +
-                                          "}," +
-                                          "\"name\":\"Gnarl\"}," +
-                                      "\"mother\":{" +
-                                        "\"//javaType\":\"org.apache.johnzon.mapper.ObjectTypeTest$Poodle\"," +
-                                        "\"hairCut\":true," +
-                                        "\"name\":\"Rosa\"}," +
-                                      "\"name\":\"Snoopie\"" +
-                                    "}";
+        String expectedJsonString = getJson();
+        Mutt snoopie = getJavaObject();
+
+        String json = mapper.writeObjectAsString(snoopie);
+        Assert.assertNotNull(json);
+        Assert.assertEquals(expectedJsonString, json);
+    }
+
+    @Test
+    public void testReadWithObjectConverter() {
+
+        Mapper mapper = new MapperBuilder().setAccessModeName(accessMode)
+                                           .addObjectConverter(Dog.class, new TestWithTypeConverter())
+                                           .build();
+
+        Dog dog = mapper.readObject(getJson(), Dog.class);
+        Assert.assertNotNull(dog);
+        Assert.assertEquals(getJavaObject(), dog);
+    }
 
+
+    private Mutt getJavaObject() {
         Poodle mum = new Poodle();
         mum.setName("Rosa");
         mum.setHairCut(true);
@@ -83,10 +89,25 @@ public class ObjectTypeTest {
         snoopie.setName("Snoopie");
         snoopie.setFather(dad);
         snoopie.setMother(mum);
+        return snoopie;
+    }
 
-        String json = mapper.writeObjectAsString(snoopie);
-        Assert.assertNotNull(json);
-        Assert.assertEquals(expectedJsonString, json);
+    private String getJson() {
+        return "{" +
+                                          "\"//javaType\":\"org.apache.johnzon.mapper.ObjectTypeTest$Mutt\"," +
+                                          "\"father\":{" +
+                                            "\"//javaType\":\"org.apache.johnzon.mapper.ObjectTypeTest$Beagle\"," +
+                                              "\"father\":{" +
+                                                "\"//javaType\":\"org.apache.johnzon.mapper.ObjectTypeTest$Beagle\"," +
+                                                "\"name\":\"Wuffi\"" +
+                                              "}," +
+                                              "\"name\":\"Gnarl\"}," +
+                                          "\"mother\":{" +
+                                            "\"//javaType\":\"org.apache.johnzon.mapper.ObjectTypeTest$Poodle\"," +
+                                            "\"hairCut\":true," +
+                                            "\"name\":\"Rosa\"}," +
+                                          "\"name\":\"Snoopie\"" +
+                                        "}";
     }
 
 
@@ -102,7 +123,7 @@ public class ObjectTypeTest {
             String javaType = jsonObject.getString("//javaType");
             Class targetClass = javaType != null ? getSubClass(targetType, javaType) : (Class) targetType;
 
-            return parser.readObject(jsonObject, targetClass);
+            return parser.readObject(jsonObject, targetClass, false);
         }
 
 
@@ -167,6 +188,35 @@ public class ObjectTypeTest {
         public void setMother(Dog mother) {
             this.mother = mother;
         }
+
+        @Override
+        public boolean equals(Object o) {
+            if (this == o) {
+                return true;
+            }
+            if (o == null || getClass() != o.getClass()) {
+                return false;
+            }
+
+            Dog dog = (Dog) o;
+
+            if (name != null ? !name.equals(dog.name) : dog.name != null) {
+                return false;
+            }
+            if (father != null ? !father.equals(dog.father) : dog.father != null) {
+                return false;
+            }
+            return mother != null ? mother.equals(dog.mother) : dog.mother == null;
+
+        }
+
+        @Override
+        public int hashCode() {
+            int result = name != null ? name.hashCode() : 0;
+            result = 31 * result + (father != null ? father.hashCode() : 0);
+            result = 31 * result + (mother != null ? mother.hashCode() : 0);
+            return result;
+        }
     }
 
     public static class Beagle extends Dog {