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 472d7b7  JOHNZON-223 ensure converters fallback on raw type for 
parameterized types
472d7b7 is described below

commit 472d7b7e88f0791f1648df7690f7e71a0a3338ac
Author: Romain Manni-Bucau <[email protected]>
AuthorDate: Wed Jul 24 09:24:57 2019 +0200

    JOHNZON-223 ensure converters fallback on raw type for parameterized types
---
 .../apache/johnzon/jsonb/GenericAdapterTest.java   | 119 +++++++++++++++++++++
 .../apache/johnzon/mapper/MappingParserImpl.java   |   5 +-
 2 files changed, 123 insertions(+), 1 deletion(-)

diff --git 
a/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/GenericAdapterTest.java 
b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/GenericAdapterTest.java
new file mode 100644
index 0000000..6e96ca6
--- /dev/null
+++ 
b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/GenericAdapterTest.java
@@ -0,0 +1,119 @@
+/**
+ * Copyright (C) 2006-2019 Talend Inc. - www.talend.com
+ * <p>
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.johnzon.jsonb;
+
+import static org.junit.Assert.assertEquals;
+
+import javax.json.bind.Jsonb;
+import javax.json.bind.JsonbBuilder;
+import javax.json.bind.JsonbConfig;
+import javax.json.bind.adapter.JsonbAdapter;
+import javax.json.bind.config.PropertyOrderStrategy;
+
+import org.junit.Test;
+
+public class GenericAdapterTest {
+
+    @Test
+    public void testJOHNZON223() throws Exception {
+        final JsonbConfig config = new JsonbConfig()
+                
.withPropertyOrderStrategy(PropertyOrderStrategy.LEXICOGRAPHICAL)
+                .withAdapters(new EnumAdapter());
+        try (final Jsonb jsonb = JsonbBuilder.create(config)) {
+
+            // setup bean
+            final MyBean expectedBean = new MyBean();
+            expectedBean.myEnum1 = new EnumHolder<>(MyEnum1.VALUE1);
+            expectedBean.myEnum2 = new EnumHolder<>(MyEnum2.VALUE2);
+
+            // write to string
+            final String jsonString = jsonb.toJson(expectedBean);
+            assertEquals(
+                    
"{\"myEnum1\":\"org.apache.johnzon.jsonb.GenericAdapterTest$MyEnum1.VALUE1\"," +
+                            
"\"myEnum2\":\"org.apache.johnzon.jsonb.GenericAdapterTest$MyEnum2.VALUE2\"}",
+                    jsonString);
+
+            // read from string
+            final MyBean actualBean = jsonb.fromJson(jsonString, MyBean.class);
+            assertEquals("MyBean [myEnum1=EnumHolder [value=VALUE1], 
myEnum2=EnumHolder [value=VALUE2]]",
+                    String.valueOf(actualBean));
+        }
+    }
+
+    public static class MyBean {
+        public EnumHolder<MyEnum1> myEnum1;
+        public EnumHolder<MyEnum2> myEnum2;
+
+
+        @Override
+        public String toString() {
+            return String.format("MyBean [myEnum1=%s, myEnum2=%s]", 
this.myEnum1, this.myEnum2);
+        }
+
+    }
+
+    public static class EnumHolder<E extends Enum<E>> {
+        public Enum<E> value;
+
+
+        public EnumHolder(final Enum<E> value) {
+            this.value = value;
+        }
+
+
+        @Override
+        public String toString() {
+            return String.format("EnumHolder [value=%s]", this.value);
+        }
+
+
+        public static EnumHolder<?> valueOf(final String value) {
+            final EnumHolder<?> result;
+            try {
+                final int idx = value.lastIndexOf(".");
+                final Class clazz = Class.forName(value.substring(0, idx));
+                return new EnumHolder<>(Enum.valueOf(clazz, 
value.substring(idx + 1)));
+            } catch (final ClassNotFoundException e) {
+                throw new IllegalStateException(e);
+            }
+        }
+    }
+
+    public enum MyEnum1 {
+        VALUE1
+    }
+
+    public enum MyEnum2 {
+        VALUE2
+    }
+
+    private static class EnumAdapter implements JsonbAdapter<EnumHolder, 
String> {
+
+        @Override
+        public String adaptToJson(final EnumHolder obj) {
+            return obj != null && obj.value != null
+                    ? obj.value.getClass().getName() + "." + obj.value.name()
+                    : null;
+        }
+
+        @Override
+        public EnumHolder adaptFromJson(final String obj) {
+            return EnumHolder.valueOf(obj);
+        }
+    }
+
+}
+
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 63549a4..f8bed55 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
@@ -611,7 +611,7 @@ public class MappingParserImpl implements MappingParser {
                         return o;
                     }
                 }
-                return convertTo(Class.class.cast(type), string);
+                return convertTo(type, string);
             } else {
                 return itemConverter.to(string);
             }
@@ -786,6 +786,9 @@ public class MappingParserImpl implements MappingParser {
             }
         }
         if (converter == null) {
+            if (ParameterizedType.class.isInstance(aClass)) {
+                return 
convertTo(ParameterizedType.class.cast(aClass).getRawType(), text);
+            }
             throw new MapperException("Missing a Converter for type " + aClass 
+ " to convert the JSON String '" +
                     text + "' . Please register a custom converter for it.");
         }

Reply via email to