-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Description
I would like to propose an enhancement to the field conversion logic within DocumentAdapters.from(Hit<?> hit, JsonpMapper jsonpMapper)
to improve both performance and type safety.
Currently, the method uses the following approach:
return new EntityAsMap().fromJson(sb.toString());
This builds a JSON string using a StringBuilder and then parses it back into an object. This pattern can lead to unnecessary performance overhead and increased GC pressure, especially when processing large numbers of fields. Additionally, the conversion may result in suboptimal type handling, as all values are initially treated as JSON strings.
Suggested Improvements
-
Performance Optimization
- By utilizing
JsonData.to(Object.class)
, each field can be converted directly to its corresponding Java type (List, Map, String, Number, etc.), eliminating the need for intermediate stringification and parsing. - This approach is more efficient, especially for documents with many fields.
- By utilizing
-
Enhanced Type Safety
- The proposed change ensures that values retain their native Java types, reducing the likelihood of type errors in later processing.
- Unlike the current implementation, which may store values as JSON strings, the improved logic preserves the structure and types of arrays, objects, numbers, and strings.
-
Sample Code
Map<String, Object> fieldMap = new LinkedHashMap<>();
hit.fields().forEach((key, jsonData) -> {
fieldMap.put(key, jsonData.to(Object.class));
});
EntityAsMap hitFieldsAsMap = new EntityAsMap();
hitFieldsAsMap.putAll(fieldMap);
I have conducted initial tests comparing both approaches with respect to result correctness and performance. If you need more details or sample benchmarks, I am happy to provide them.
Please let me know if there are any concerns or considerations to be aware of during review.