A reactive (or non-blocking, or asynchronous) JSON parser
MIT License
Actson is a reactive JSON parser (sometimes referred to as non-blocking or asynchronous). It is event-based and can be used together with reactive libraries/tool-kits such as RxJava or Vert.x.
The library is very small and has no dependencies. It only requires Java 8 (or higher).
InputStream
). If you want to develop a reactive application you should useThe following snippet demonstrates how you can use the parser sequentially.
// JSON text to parse
byte[] json = "{\"name\":\"Elvis\"}".getBytes(StandardCharsets.UTF_8);
JsonParser parser = new JsonParser(StandardCharsets.UTF_8);
int pos = 0; // position in the input JSON text
int event; // event returned by the parser
do {
// feed the parser until it returns a new event
while ((event = parser.nextEvent()) == JsonEvent.NEED_MORE_INPUT) {
// provide the parser with more input
pos += parser.getFeeder().feed(json, pos, json.length - pos);
// indicate end of input to the parser
if (pos == json.length) {
parser.getFeeder().done();
}
}
// handle event
System.out.println("JSON event: " + event);
if (event == JsonEvent.ERROR) {
throw new IllegalStateException("Syntax error in JSON text");
}
} while (event != JsonEvent.EOF);
Find more complex examples using RxJava or Vert.x below.
Binaries and dependency information for Maven, Gradle, Ivy and others can be found at http://search.maven.org.
Example for Maven:
<dependencies>
<dependency>
<groupId>de.undercouch</groupId>
<artifactId>actson</artifactId>
<version>2.1.0</version>
</dependency>
</dependencies>
Example for Gradle:
dependencies {
implementation 'de.undercouch:actson:2.1.0'
}
Execute the following command to compile the library and to run the unit tests:
./gradlew test
The script automatically downloads the correct Gradle version, so you won't have to do anything else. If everything runs successfully, you may create a .jar library:
./gradlew jar
The library will be located under the build/libs
directory.
The event-based parser code and the JSON files used for testing are largely based on the file JSON_checker.c and the JSON test suite from JSON.org originally released under this license (basically MIT license).
Actson is released under the MIT license. See the LICENSE file for more information.