Skip to content

Asynchronous XML-to-JSON Conversion

Latest

Choose a tag to compare

@JyothiSwaroopReddy07 JyothiSwaroopReddy07 released this 03 Jun 23:14
· 2 commits to master since this release

New Feature: Asynchronous XML-to-JSON Conversion

XML.toJSONObjectAsync(...)
A new asynchronous method toJSONObjectAsync has been introduced to improve performance when working with large XML files. This feature wraps the existing synchronous XML.toJSONObject(Reader) method with CompletableFuture to provide non-blocking execution.

Method Signature
public static void toJSONObjectAsync(
Reader reader,
Consumer onSuccess,
Consumer onError)

Parameters

  1. Reader reader: Input reader containing XML data.
  2. Consumer onSuccess: Callback function invoked with the resulting JSONObject if parsing is successful.
  3. Consumer onError: Callback function invoked with the exception if parsing fails.

Implementation Highlights

  • Internally uses CompletableFuture.supplyAsync(...) to offload XML parsing to a background thread.
  • Preserves existing logic from the synchronous XML.toJSONObject(...) method.
  • Uses CompletionException wrapping for proper exception propagation.

Example Usage
XML.toJSONObjectAsync(
new FileReader("largefile.xml"),
jsonObject -> System.out.println("Parsed JSON: " + jsonObject),
error -> System.err.println("Error: " + error.getMessage())
);

Use Case
This method is ideal for:

  • Handling large XML files without blocking the main thread.
  • UI applications where responsiveness is critical.
  • Server-side environments where non-blocking IO is preferred.

Backward Compatibility
No breaking changes. The existing synchronous XML.toJSONObject(...) methods remain fully supported.

Additional Notes
The asynchronous execution is performed using the common ForkJoinPool. For more advanced use cases, future releases may provide overloads accepting a custom ExecutorService.