actson 0.1.0

A reactive (or non-blocking, or asynchronous) JSON parser
Documentation

Actson Actions Status MIT license

Actson is a reactive JSON parser (sometimes referred to as non-blocking or asynchronous). It is event-based and can be used in asynchronous code (for example in combination with Tokio).

Why another JSON parser?

  • Non-blocking. Other JSON parsers use blocking I/O. If you want to develop a reactive application you should use non-blocking I/O (see the Reactive Manifesto).
  • Big Data. Most parsers read the full JSON text into memory to map it to a struct, for example. Actson can handle arbitrarily large JSON text. It is event-based and can be used for streaming.
  • GeoRocket. Actson was primarily developed for the GeoJSON support support in GeoRocket, a high-performance reactive data store for geospatial files.

Usage

use actson::{JsonParser, JsonEvent};
use actson::feeder::{DefaultJsonFeeder, JsonFeeder};

let json = r#"{"name": "Elvis"}"#.as_bytes();

let mut feeder = DefaultJsonFeeder::new();
let mut parser = JsonParser::new();
let mut i: usize = 0;
loop {
    // feed as many bytes as possible to the parser
    let mut event = parser.next_event(&mut feeder);
    while matches!(event, JsonEvent::NeedMoreInput) {
        i += feeder.feed_bytes(&json[i..]);
        if i == json.len() {
            feeder.done();
        }
        event = parser.next_event(&mut feeder);
    }

    // do something useful with `event`

    if matches!(event, JsonEvent::Error) {
       // do proper error handling here!
       panic!("Error while parsing JSON");
    }

    if matches!(event, JsonEvent::Eof) {
        break;
    }
}

Other languages

Besides this implementation in Rust here, there is a Java implementation.

Acknowledgments

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).

License

Actson is released under the MIT license. See the LICENSE file for more information.