|
42 | 42 | import java.text.DateFormat; |
43 | 43 | import java.text.SimpleDateFormat; |
44 | 44 | import java.time.temporal.TemporalAccessor; |
| 45 | +import java.util.ArrayList; |
45 | 46 | import java.util.Collection; |
46 | 47 | import java.util.Date; |
| 48 | +import java.util.LinkedHashMap; |
| 49 | +import java.util.List; |
47 | 50 | import java.util.Map; |
48 | 51 | import java.util.concurrent.ConcurrentHashMap; |
49 | 52 |
|
@@ -81,20 +84,157 @@ public void serialize(Object value, JsonGenerator gen, SerializationContext prov |
81 | 84 | if (id != null) { |
82 | 85 | writeField(gen, "id", id); |
83 | 86 | } |
84 | | - // writeField(gen, "name", value.toString()); |
85 | 87 |
|
| 88 | + // Group fields by their root prefix (part before the first dot). |
| 89 | + // Fields without a dot are stored under their own name as key. |
| 90 | + Map<String, List<Field>> groups = new LinkedHashMap<>(); |
86 | 91 | for (Field field : Viewers.getFields(viewDescriptor)) { |
87 | | - PropertyInfo fieldInfo = field.getPropertyInfo(); |
88 | | - if (field.isCollection() && fieldInfo != null) { |
89 | | - serializeCollectionField(field, fieldInfo, value, gen, provider); |
| 92 | + String name = field.getName(); |
| 93 | + String root = name.contains(".") ? name.substring(0, name.indexOf('.')) : name; |
| 94 | + groups.computeIfAbsent(root, k -> new ArrayList<>()).add(field); |
| 95 | + } |
| 96 | + |
| 97 | + for (Map.Entry<String, List<Field>> entry : groups.entrySet()) { |
| 98 | + List<Field> groupFields = entry.getValue(); |
| 99 | + boolean isPathGroup = groupFields.size() > 1 || groupFields.get(0).getName().contains("."); |
| 100 | + |
| 101 | + if (isPathGroup) { |
| 102 | + // All fields in this group are path-based; render them as a nested object |
| 103 | + serializePathGroup(entry.getKey(), groupFields, value, gen, provider); |
90 | 104 | } else { |
91 | | - serializeSimpleField(field, fieldInfo, value, gen); |
| 105 | + Field field = groupFields.get(0); |
| 106 | + PropertyInfo fieldInfo = field.getPropertyInfo(); |
| 107 | + if (field.isCollection() && fieldInfo != null) { |
| 108 | + serializeCollectionField(field, fieldInfo, value, gen, provider); |
| 109 | + } else { |
| 110 | + serializeSimpleField(field, fieldInfo, value, gen); |
| 111 | + } |
92 | 112 | } |
93 | 113 | } |
94 | 114 |
|
95 | 115 | gen.writeEndObject(); |
96 | 116 | } |
97 | 117 |
|
| 118 | + /** |
| 119 | + * Writes a group of dot-path fields as a nested JSON object, recursively. |
| 120 | + * Supports arbitrarily deep paths, e.g.: |
| 121 | + * <pre> |
| 122 | + * category.id, category.name, category.type.name → |
| 123 | + * "category": { "id": 1, "name": "...", "type": { "name": "..." } } |
| 124 | + * </pre> |
| 125 | + * |
| 126 | + * @param rootName the JSON key to write (current nesting level) |
| 127 | + * @param fields the fields whose names begin with rootName (full original paths) |
| 128 | + * @param value the root bean being serialized |
| 129 | + * @param pathSoFar the dot-path prefix already consumed (used to read values from the root bean) |
| 130 | + * @param gen the JSON generator |
| 131 | + */ |
| 132 | + private void serializePathGroup(String rootName, List<Field> fields, Object value, |
| 133 | + String pathSoFar, JsonGenerator gen) { |
| 134 | + // Split fields into: leaf fields (no more dots after stripping rootName) |
| 135 | + // and sub-groups (need another nesting level). |
| 136 | + Map<String, List<Field>> subGroups = new LinkedHashMap<>(); |
| 137 | + List<Field> leafFields = new ArrayList<>(); |
| 138 | + |
| 139 | + for (Field field : fields) { |
| 140 | + if (!field.isVisible()) { |
| 141 | + continue; |
| 142 | + } |
| 143 | + String fullName = field.getName(); |
| 144 | + // Strip the current root prefix to get the remainder |
| 145 | + String remainder = fullName.contains(".") ? fullName.substring(fullName.indexOf('.') + 1) : fullName; |
| 146 | + if (remainder.contains(".")) { |
| 147 | + // Still nested — group by the next segment |
| 148 | + String nextRoot = remainder.substring(0, remainder.indexOf('.')); |
| 149 | + subGroups.computeIfAbsent(nextRoot, k -> new ArrayList<>()).add(field); |
| 150 | + } else { |
| 151 | + leafFields.add(field); |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + // Only open the object if there is something to write |
| 156 | + boolean hasLeafValues = leafFields.stream().anyMatch(f -> { |
| 157 | + try { |
| 158 | + return ObjectOperations.invokeGetMethod(value, f.getName()) != null; |
| 159 | + } catch (Exception e) { |
| 160 | + return false; |
| 161 | + } |
| 162 | + }); |
| 163 | + boolean hasSubGroups = !subGroups.isEmpty(); |
| 164 | + |
| 165 | + if (!hasLeafValues && !hasSubGroups) { |
| 166 | + return; |
| 167 | + } |
| 168 | + |
| 169 | + gen.writeObjectPropertyStart(rootName); |
| 170 | + |
| 171 | + // Write leaf fields |
| 172 | + for (Field field : leafFields) { |
| 173 | + String fullName = field.getName(); |
| 174 | + String subName = fullName.contains(".") ? fullName.substring(fullName.lastIndexOf('.') + 1) : fullName; |
| 175 | + try { |
| 176 | + Object fieldValue = ObjectOperations.invokeGetMethod(value, fullName); |
| 177 | + if (fieldValue != null) { |
| 178 | + writeField(gen, subName, fieldValue); |
| 179 | + } |
| 180 | + } catch (Exception e) { |
| 181 | + LOGGER.warn("Cannot write path field " + fullName + " to json: " + e.getMessage()); |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + // Recurse into sub-groups |
| 186 | + for (Map.Entry<String, List<Field>> sub : subGroups.entrySet()) { |
| 187 | + // Re-root the field names so the next level sees them starting from sub.getKey() |
| 188 | + String nextRoot = sub.getKey(); |
| 189 | + List<Field> subFields = sub.getValue().stream() |
| 190 | + .map(f -> { |
| 191 | + // Trim one prefix level from the field name for the recursive call |
| 192 | + String trimmed = f.getName().substring(f.getName().indexOf('.') + 1); |
| 193 | + return new PathAliasField(f, trimmed); |
| 194 | + }) |
| 195 | + .collect(java.util.stream.Collectors.toList()); |
| 196 | + serializePathGroup(nextRoot, subFields, value, pathSoFar, gen); |
| 197 | + } |
| 198 | + |
| 199 | + gen.writeEndObject(); |
| 200 | + } |
| 201 | + |
| 202 | + /** Overload used by the top-level serialize() method — starts pathSoFar as empty. */ |
| 203 | + private void serializePathGroup(String rootName, List<Field> fields, Object value, |
| 204 | + JsonGenerator gen, SerializationContext provider) { |
| 205 | + serializePathGroup(rootName, fields, value, "", gen); |
| 206 | + } |
| 207 | + |
| 208 | + /** |
| 209 | + * A lightweight wrapper around a {@link Field} that overrides only {@link #getName()} |
| 210 | + * so that recursive calls see the trimmed path rather than the full original path. |
| 211 | + */ |
| 212 | + private static class PathAliasField extends Field { |
| 213 | + private final Field delegate; |
| 214 | + private final String aliasName; |
| 215 | + |
| 216 | + PathAliasField(Field delegate, String aliasName) { |
| 217 | + super(aliasName); |
| 218 | + this.delegate = delegate; |
| 219 | + this.aliasName = aliasName; |
| 220 | + } |
| 221 | + |
| 222 | + @Override |
| 223 | + public String getName() { |
| 224 | + return aliasName; |
| 225 | + } |
| 226 | + |
| 227 | + @Override |
| 228 | + public boolean isVisible() { |
| 229 | + return delegate.isVisible(); |
| 230 | + } |
| 231 | + |
| 232 | + @Override |
| 233 | + public java.util.Map<String, Object> getParams() { |
| 234 | + return delegate.getParams(); |
| 235 | + } |
| 236 | + } |
| 237 | + |
98 | 238 | private void serializeCollectionField(Field field, PropertyInfo fieldInfo, Object value, |
99 | 239 | JsonGenerator gen, SerializationContext provider) { |
100 | 240 | Collection<?> collection = null; |
|
0 commit comments