jungm commented on code in PR #100:
URL: https://github.com/apache/johnzon/pull/100#discussion_r1171230108


##########
johnzon-jsonb/src/main/java/org/apache/johnzon/jsonb/JsonbPolymorphismHandler.java:
##########
@@ -0,0 +1,191 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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 org.apache.johnzon.mapper.access.Meta;
+
+import jakarta.json.JsonObject;
+import jakarta.json.JsonString;
+import jakarta.json.JsonValue;
+import jakarta.json.bind.JsonbException;
+import jakarta.json.bind.annotation.JsonbSubtype;
+import jakarta.json.bind.annotation.JsonbTypeInfo;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+public class JsonbPolymorphismHandler {
+    public boolean hasPolymorphism(Class<?> clazz) {
+        return clazz.isAnnotationPresent(JsonbTypeInfo.class) || 
getParentWithTypeInfo(clazz) != null;
+    }
+
+    public Map.Entry<String, String>[] 
getPolymorphismPropertiesToSerialize(Class<?> clazz, Collection<String> 
otherProperties) {
+        List<Map.Entry<String, String>> result = new ArrayList<>();
+
+        Class<?> current = clazz;
+        while (current != null) {
+            // Only try to resolve types when there's a JsonbTypeInfo 
Annotation present on the current type, Meta.getAnnotation tries to
+            // walk up parents by itself until it finds the given Annotation 
and could incorrectly cause JsonbExceptions to be thrown
+            // (multiple JsonbTypeInfos with same key found even if thats not 
actually the case)
+            if (current.isAnnotationPresent(JsonbTypeInfo.class)) {
+                JsonbTypeInfo typeInfo = Meta.getAnnotation(current, 
JsonbTypeInfo.class);
+                if (otherProperties.contains(typeInfo.key())) {
+                    throw new JsonbException("JsonbTypeInfo key '" + 
typeInfo.key() + "' collides with other properties in json");
+                }
+
+                String bestMatchingAlias = null;
+                for (JsonbSubtype subtype : typeInfo.value()) {
+                    if (subtype.type().isAssignableFrom(clazz)) {
+                        bestMatchingAlias = subtype.alias();
+
+                        if (clazz == subtype.type()) { // Exact match found, 
no need to continue further
+                            break;
+                        }
+                    }
+                }
+
+                if (bestMatchingAlias != null) {
+                    result.add(0, Map.entry(typeInfo.key(), 
bestMatchingAlias));
+                }
+            }
+
+            current = getParentWithTypeInfo(current);
+        }
+
+        return result.toArray(Map.Entry[]::new);
+    }
+
+    public Class<?> getTypeToDeserialize(JsonObject jsonObject, Class<?> 
clazz) {

Review Comment:
   reworked and reflection free now. Except for `Class#getName`, is using it 
okay? Or should we possibly completely move away from having the class as a 
parameter?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscr...@johnzon.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to