Skip to content

Commit

Permalink
Fix interleaved output in JsonReader's parseJsonNode method
Browse files Browse the repository at this point in the history
Replace parallelStream with stream to prevent thread-unsafe appends to
the shared StringBuilder. This fixes the issue of intermingled key-value
pairs in the generated Document content. Also, replace StringBuffer
with StringBuilder for better performance in single-threaded context.

The change ensures correct ordering of extracted JSON keys and their
values in the resulting Document, improving the reliability and
readability of the parsed output.
  • Loading branch information
alexcheng1982 authored and Mark Pollack committed Sep 24, 2024
1 parent 1673907 commit c205c7d
Showing 1 changed file with 6 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,19 @@
*/
public class JsonReader implements DocumentReader {

private Resource resource;
private final Resource resource;

private JsonMetadataGenerator jsonMetadataGenerator;
private final JsonMetadataGenerator jsonMetadataGenerator;

private final ObjectMapper objectMapper = new ObjectMapper();

/**
* The key from the JSON that we will use as the text to parse into the Document text
*/
private List<String> jsonKeysToUse;
private final List<String> jsonKeysToUse;

public JsonReader(Resource resource) {
this(resource, new ArrayList<>().toArray(new String[0]));
this(resource, new String[0]);
}

public JsonReader(Resource resource, String... jsonKeysToUse) {
Expand Down Expand Up @@ -92,9 +92,9 @@ public List<Document> get() {
private Document parseJsonNode(JsonNode jsonNode, ObjectMapper objectMapper) {
Map<String, Object> item = objectMapper.convertValue(jsonNode, new TypeReference<Map<String, Object>>() {
});
StringBuffer sb = new StringBuffer();
var sb = new StringBuilder();

jsonKeysToUse.parallelStream().filter(item::containsKey).forEach(key -> {
jsonKeysToUse.stream().filter(item::containsKey).forEach(key -> {
sb.append(key).append(": ").append(item.get(key)).append(System.lineSeparator());
});

Expand Down

0 comments on commit c205c7d

Please sign in to comment.