You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@johnzon.apache.org by rm...@apache.org on 2019/08/14 08:31:17 UTC

[johnzon] branch master updated: JOHNZON-251 ensure adapters are used in arrays

This is an automated email from the ASF dual-hosted git repository.

rmannibucau pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/johnzon.git


The following commit(s) were added to refs/heads/master by this push:
     new 203b7ae  JOHNZON-251 ensure adapters are used in arrays
203b7ae is described below

commit 203b7aed30bd8874ac965e6eab631054ac6310f1
Author: Romain Manni-Bucau <rm...@apache.org>
AuthorDate: Wed Aug 14 10:31:06 2019 +0200

    JOHNZON-251 ensure adapters are used in arrays
---
 .../java/org/apache/johnzon/jsonb/DefaultMappingTest.java  |  2 +-
 .../java/org/apache/johnzon/mapper/MappingParserImpl.java  | 11 +++++++++--
 .../org/apache/johnzon/mapper/access/MethodAccessMode.java | 14 ++++++++++++--
 3 files changed, 22 insertions(+), 5 deletions(-)

diff --git a/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/DefaultMappingTest.java b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/DefaultMappingTest.java
index 33c9fd6..af6930c 100644
--- a/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/DefaultMappingTest.java
+++ b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/DefaultMappingTest.java
@@ -669,7 +669,7 @@ public class DefaultMappingTest {
 
         POJOWithMixedFieldAccess pojoWithMixedFieldAccess = new POJOWithMixedFieldAccess();
 
-        assertEquals("{\"active\":true,\"id\":2,\"name\":\"pojoName\"}"/*,\"valid\":false}"*/, JSONB.toJson(pojoWithMixedFieldAccess));
+        assertEquals("{\"active\":true,\"id\":2,\"name\":\"pojoName\",\"valid\":false}", JSONB.toJson(pojoWithMixedFieldAccess));
 
         //composite class
         CompositePOJO compositePOJO = new CompositePOJO();
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 c259756..d6bf2cd 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
@@ -437,7 +437,8 @@ public class MappingParserImpl implements MappingParser {
         }
     }
 
-    private Object convertTo(final Adapter converter, final JsonValue jsonValue, JsonPointerTracker jsonPointer) {
+    private Object convertTo(final Adapter converter, final JsonValue jsonValue, final JsonPointerTracker jsonPointer,
+                             final Type targetType) {
         final JsonValue.ValueType valueType = jsonValue != null ? jsonValue.getValueType() : null;
 
         final AdapterKey key = getAdapterKey(converter);
@@ -504,6 +505,12 @@ public class MappingParserImpl implements MappingParser {
             if (JsonArray.class == key.getTo() || JsonStructure.class == key.getTo()) {
                 return converter.to(jsonValue.asJsonObject());
             }
+            if (TypeAwareAdapter.class.isInstance(converter)) {
+                final TypeAwareAdapter adapter = TypeAwareAdapter.class.cast(converter);
+                if (adapter.getFrom().equals(targetType)) {
+                    return converter.to(readObject(jsonValue, adapter.getTo()));
+                }
+            }
             return buildArray(key.getTo(), jsonValue.asJsonArray(), null, null, jsonPointer, null);
         }
         return converter.to(jsonValue.toString());
@@ -969,7 +976,7 @@ public class MappingParserImpl implements MappingParser {
         try {
             return converter == null ?
                     toObject(baseInstance, jsonValue, type, itemConverter, jsonPointer, rootType) :
-                    convertTo(converter, jsonValue, jsonPointer);
+                    convertTo(converter, jsonValue, jsonPointer, type);
         } catch (Exception e) {
             if (e instanceof MapperException) {
                 throw e;
diff --git a/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/access/MethodAccessMode.java b/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/access/MethodAccessMode.java
index e35c425..c43ef3b 100644
--- a/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/access/MethodAccessMode.java
+++ b/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/access/MethodAccessMode.java
@@ -48,11 +48,21 @@ public class MethodAccessMode extends BaseAccessMode {
         final PropertyDescriptor[] propertyDescriptors = getPropertyDescriptors(clazz);
         for (final PropertyDescriptor descriptor : propertyDescriptors) {
             final Method readMethod = descriptor.getReadMethod();
+            final String name = descriptor.getName();
             if (readMethod != null && readMethod.getDeclaringClass() != Object.class) {
-                if (isIgnored(descriptor.getName()) || Meta.getAnnotation(readMethod, JohnzonAny.class) != null) {
+                if (isIgnored(name) || Meta.getAnnotation(readMethod, JohnzonAny.class) != null) {
                     continue;
                 }
-                readers.put(extractKey(descriptor.getName(), readMethod, null), new MethodReader(readMethod, readMethod.getGenericReturnType()));
+                readers.put(extractKey(name, readMethod, null), new MethodReader(readMethod, readMethod.getGenericReturnType()));
+            } else if (readMethod == null && descriptor.getWriteMethod() != null && // isXXX, not supported by javabeans
+                    (descriptor.getPropertyType() == Boolean.class || descriptor.getPropertyType() == boolean.class)) {
+                try {
+                    final Method method = clazz.getMethod(
+                            "is" + Character.toUpperCase(name.charAt(0)) + (name.length() > 1 ? name.substring(1) : ""));
+                    readers.put(extractKey(name, method, null), new MethodReader(method, method.getGenericReturnType()));
+                } catch (final NoSuchMethodException e) {
+                    // no-op
+                }
             }
         }
         return readers;