|
18 | 18 | package tools.dynamia.viewers; |
19 | 19 |
|
20 | 20 |
|
| 21 | +import com.fasterxml.jackson.annotation.JsonIgnore; |
21 | 22 | import tools.dynamia.commons.ObjectOperations; |
22 | 23 | import tools.dynamia.commons.logger.LoggingService; |
23 | 24 | import tools.dynamia.commons.logger.SLF4JLoggingService; |
|
29 | 30 | import tools.jackson.databind.DeserializationContext; |
30 | 31 | import tools.jackson.databind.JsonNode; |
31 | 32 | import tools.jackson.databind.deser.std.StdDeserializer; |
32 | | -import tools.jackson.databind.util.StdDateFormat; |
33 | 33 |
|
34 | 34 | import java.math.BigDecimal; |
35 | 35 | import java.util.*; |
| 36 | +import java.util.concurrent.ConcurrentHashMap; |
| 37 | +import java.util.function.Function; |
36 | 38 |
|
37 | 39 | public class JsonViewDescriptorDeserializer extends StdDeserializer<Object> { |
38 | 40 |
|
39 | 41 | private final ViewDescriptor viewDescriptor; |
40 | | - private final StdDateFormat dateFormat = new StdDateFormat(); |
41 | 42 | private static final LoggingService LOGGER = new SLF4JLoggingService(JsonViewDescriptorDeserializer.class); |
42 | 43 |
|
43 | | - public JsonViewDescriptorDeserializer(ViewDescriptor viewDescriptor) { |
44 | | - this(viewDescriptor, null); |
| 44 | + private static final Map<Class<?>, Function<JsonNode, Object>> TYPE_EXTRACTORS = new HashMap<>(); |
| 45 | + private static final Map<Class<?>, ViewDescriptor> DESCRIPTOR_CACHE = new ConcurrentHashMap<>(); |
| 46 | + |
| 47 | + static { |
| 48 | + TYPE_EXTRACTORS.put(String.class, JsonNode::stringValue); |
| 49 | + TYPE_EXTRACTORS.put(Long.class, JsonNode::longValue); |
| 50 | + TYPE_EXTRACTORS.put(long.class, JsonNode::longValue); |
| 51 | + TYPE_EXTRACTORS.put(Integer.class, JsonNode::intValue); |
| 52 | + TYPE_EXTRACTORS.put(int.class, JsonNode::intValue); |
| 53 | + TYPE_EXTRACTORS.put(Float.class, JsonNode::floatValue); |
| 54 | + TYPE_EXTRACTORS.put(float.class, JsonNode::floatValue); |
| 55 | + TYPE_EXTRACTORS.put(Double.class, JsonNode::doubleValue); |
| 56 | + TYPE_EXTRACTORS.put(double.class, JsonNode::doubleValue); |
| 57 | + TYPE_EXTRACTORS.put(BigDecimal.class, JsonNode::decimalValue); |
| 58 | + TYPE_EXTRACTORS.put(Boolean.class, JsonNode::asBoolean); |
| 59 | + TYPE_EXTRACTORS.put(boolean.class, JsonNode::asBoolean); |
| 60 | + } |
45 | 61 |
|
| 62 | + public JsonViewDescriptorDeserializer(ViewDescriptor viewDescriptor) { |
| 63 | + this(viewDescriptor, Object.class); |
46 | 64 | } |
47 | 65 |
|
48 | 66 | public JsonViewDescriptorDeserializer(ViewDescriptor viewDescriptor, Class<Object> t) { |
49 | 67 | super(t); |
50 | 68 | this.viewDescriptor = viewDescriptor; |
51 | 69 | } |
52 | 70 |
|
53 | | - |
54 | 71 | @Override |
55 | 72 | public Object deserialize(JsonParser jp, DeserializationContext ctxt) { |
56 | | - |
57 | 73 | JsonNode node = jp.readValueAsTree(); |
58 | 74 | return parseNode(viewDescriptor.getBeanClass(), node, viewDescriptor); |
59 | | - |
60 | 75 | } |
61 | 76 |
|
62 | | - private Object parseNode(Class type, JsonNode node, ViewDescriptor viewDescriptor) { |
63 | | - @SuppressWarnings("unchecked") Object object = ObjectOperations.newInstance(type); |
64 | | - for (Field field : Viewers.getFields(viewDescriptor)) { |
| 77 | + private Object parseNode(Class<?> type, JsonNode node, ViewDescriptor descriptor) { |
| 78 | + Object object = ObjectOperations.newInstance(type); |
| 79 | + for (Field field : Viewers.getFields(descriptor)) { |
65 | 80 | PropertyInfo fieldInfo = field.getPropertyInfo(); |
66 | | - String fieldName = field.getName(); |
67 | | - JsonNode fieldNode = node.get(fieldName); |
| 81 | + if (fieldInfo.isAnnotationPresent(JsonIgnore.class)) { |
| 82 | + continue; |
| 83 | + } |
| 84 | + |
| 85 | + JsonNode fieldNode = node.get(field.getName()); |
68 | 86 | if (fieldNode == null) { |
69 | 87 | continue; |
70 | 88 | } |
71 | 89 |
|
72 | 90 | if (fieldInfo.isCollection()) { |
73 | | - Collection collection = (Collection) ObjectOperations.invokeGetMethod(object, fieldInfo); |
74 | | - if (collection == null) { |
75 | | - if (fieldInfo.getType() == List.class) { |
76 | | - collection = new ArrayList(); |
77 | | - } else if (fieldInfo.getType() == Set.class) { |
78 | | - collection = new HashSet(); |
79 | | - } else { |
80 | | - collection = (Collection) ObjectOperations.newInstance(fieldInfo.getType()); |
81 | | - } |
82 | | - ObjectOperations.setFieldValue(fieldInfo, object, collection); |
83 | | - } |
84 | | - |
85 | | - ViewDescriptor collectionDescriptor = Viewers.findViewDescriptor(fieldInfo.getGenericType(), "json-form"); |
86 | | - if (collectionDescriptor == null) { |
87 | | - collectionDescriptor = Viewers.getViewDescriptor(fieldInfo.getGenericType(), "form"); |
88 | | - } |
89 | | - String parentName = ObjectOperations.findParentPropertyName(type, fieldInfo.getGenericType()); |
90 | | - if (field.getParams().get("parentName") != null) { |
91 | | - parentName = field.getParams().get("parentName").toString(); |
92 | | - } |
93 | | - for (JsonNode child : fieldNode) { |
94 | | - Object item = parseNode(fieldInfo.getGenericType(), child, collectionDescriptor); |
95 | | - ObjectOperations.invokeSetMethod(item, parentName, object); |
96 | | - //noinspection unchecked |
97 | | - collection.add(item); |
98 | | - } |
99 | | - |
| 91 | + processCollectionField(object, field, fieldInfo, fieldNode, type); |
100 | 92 | } else { |
101 | | - Object fieldValue = getNodeValue(fieldInfo, fieldNode); |
102 | | - try { |
103 | | - ObjectOperations.invokeSetMethod(object, fieldName, fieldValue); |
104 | | - } catch (ReflectionException e) { |
105 | | - LOGGER.warn("Cannot parse json to field " + fieldName + " = " + fieldValue + ": " + e.getMessage()); |
106 | | - } |
107 | | - |
108 | | - |
| 93 | + setSimpleField(object, field.getName(), fieldInfo, fieldNode); |
109 | 94 | } |
110 | 95 | } |
111 | 96 | return object; |
112 | 97 | } |
113 | 98 |
|
114 | | - public static Object getNodeValue(PropertyInfo fieldInfo, JsonNode fieldNode) { |
115 | | - Object fieldValue = null; |
| 99 | + private void processCollectionField(Object object, Field field, PropertyInfo fieldInfo, |
| 100 | + JsonNode fieldNode, Class<?> parentType) { |
| 101 | + Collection<Object> collection = getOrCreateCollection(object, fieldInfo); |
| 102 | + ViewDescriptor collectionDescriptor = resolveCollectionDescriptor(fieldInfo.getGenericType()); |
| 103 | + String parentName = resolveParentName(field, fieldInfo, parentType); |
116 | 104 |
|
| 105 | + for (JsonNode child : fieldNode) { |
| 106 | + Object item = parseNode(fieldInfo.getGenericType(), child, collectionDescriptor); |
| 107 | + ObjectOperations.invokeSetMethod(item, parentName, object); |
| 108 | + collection.add(item); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + @SuppressWarnings("unchecked") |
| 113 | + private Collection<Object> getOrCreateCollection(Object object, PropertyInfo fieldInfo) { |
| 114 | + Collection<Object> collection = (Collection<Object>) ObjectOperations.invokeGetMethod(object, fieldInfo); |
| 115 | + if (collection == null) { |
| 116 | + collection = createEmptyCollection(fieldInfo.getType()); |
| 117 | + ObjectOperations.setFieldValue(fieldInfo, object, collection); |
| 118 | + } |
| 119 | + return collection; |
| 120 | + } |
| 121 | + |
| 122 | + @SuppressWarnings("unchecked") |
| 123 | + private Collection<Object> createEmptyCollection(Class<?> type) { |
| 124 | + if (type == List.class) return new ArrayList<>(); |
| 125 | + if (type == Set.class) return new HashSet<>(); |
| 126 | + return (Collection<Object>) ObjectOperations.newInstance(type); |
| 127 | + } |
| 128 | + |
| 129 | + private String resolveParentName(Field field, PropertyInfo fieldInfo, Class<?> parentType) { |
| 130 | + Object customParent = field.getParams().get("parentName"); |
| 131 | + return customParent != null |
| 132 | + ? customParent.toString() |
| 133 | + : ObjectOperations.findParentPropertyName(parentType, fieldInfo.getGenericType()); |
| 134 | + } |
| 135 | + |
| 136 | + private void setSimpleField(Object object, String fieldName, PropertyInfo fieldInfo, JsonNode fieldNode) { |
| 137 | + Object fieldValue = getNodeValue(fieldInfo, fieldNode); |
| 138 | + try { |
| 139 | + ObjectOperations.invokeSetMethod(object, fieldName, fieldValue); |
| 140 | + } catch (ReflectionException e) { |
| 141 | + LOGGER.warn("Cannot parse json to field " + fieldName + " = " + fieldValue + ": " + e.getMessage()); |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + private ViewDescriptor resolveCollectionDescriptor(Class<?> genericType) { |
| 146 | + return DESCRIPTOR_CACHE.computeIfAbsent(genericType, type -> { |
| 147 | + ViewDescriptor descriptor = Viewers.findViewDescriptor(type, "json-form"); |
| 148 | + return descriptor != null ? descriptor : Viewers.getViewDescriptor(type, "form"); |
| 149 | + }); |
| 150 | + } |
| 151 | + |
| 152 | + public static Object getNodeValue(PropertyInfo fieldInfo, JsonNode fieldNode) { |
117 | 153 | if (DomainUtils.isEntity(fieldInfo.getType()) && fieldNode.get("id") != null) { |
118 | | - long id = fieldNode.get("id").asLong(); |
119 | | - fieldValue = DomainUtils.lookupCrudService().find(fieldInfo.getType(), id); |
120 | | - } else if (fieldInfo.is(String.class)) { |
121 | | - fieldValue = fieldNode.textValue(); |
122 | | - } else if (fieldInfo.is(Long.class) || fieldInfo.is(long.class)) { |
123 | | - fieldValue = fieldNode.longValue(); |
124 | | - } else if (fieldInfo.is(Integer.class) || fieldInfo.is(int.class)) { |
125 | | - fieldValue = fieldNode.intValue(); |
126 | | - } else if (fieldInfo.is(Float.class) || fieldInfo.is(float.class)) { |
127 | | - fieldValue = fieldNode.floatValue(); |
128 | | - } else if (fieldInfo.is(Double.class) || fieldInfo.is(double.class)) { |
129 | | - fieldValue = fieldNode.doubleValue(); |
130 | | - } else if (fieldInfo.is(BigDecimal.class)) { |
131 | | - fieldValue = fieldNode.decimalValue(); |
132 | | - } else if (fieldInfo.is(Boolean.class)) { |
133 | | - fieldValue = fieldNode.booleanValue(); |
| 154 | + return DomainUtils.lookupCrudService().find(fieldInfo.getType(), fieldNode.get("id").asLong()); |
134 | 155 | } |
135 | | - return fieldValue; |
| 156 | + Function<JsonNode, Object> extractor = TYPE_EXTRACTORS.get(fieldInfo.getType()); |
| 157 | + return extractor != null ? extractor.apply(fieldNode) : null; |
136 | 158 | } |
137 | 159 | } |
0 commit comments