1 unstable release

Uses old Rust 2015

0.1.0 Oct 20, 2018

#12 in #json-ld

MIT license

23KB
361 lines

JSON-NS

This is the reference implementation of JSON-NS, implemented in Rust. See the blog post for what this is and why it exists.

Documentation

A test suite is included, which you can run with:

cargo test

lib.rs:

The reference implementation for JSON-NS, a small and basic subset of JSON-LD. See the blog post for what this is and why it exists.

This implementation uses the serde_json crate types to represent JSON values. Doing basic processing involves creating a Processor, which holds some optional configuration, and giving it a Value to process:

#[macro_use]
extern crate serde_json as json;
extern crate json_ns;

use json_ns::Processor;

fn main() {
    // Some example input.
    let input = json!({
        "@context": {
            "foo": "http://example.com/ns#"
        },
        "foo:hello": "world"
    });

    // Process the document, and use `bar` instead as the output prefix.
    let output = Processor::new()
        .add_rule("bar", "http://example.com/ns#")
        .process_value(&input);

    // Check that the output is what we expected.
    assert_eq!(output, json!({
        "bar:hello": "world"
    }));
}

Without the processor configuration, this code can be even shorter:

let output = Processor::new().process_value(&input);

In this case, the output document contains a property named http://example.com/ns#hello.

Often, the bulk of the properties you expect are in a single namespace. In this case, it may be useful to set a default namespace on the output, for which properties are not prefixed at all:

processor.add_rule("", "http://example.com/ns#");

The output then contains a property named just hello. This is especially useful when passing the value on to serde_json::from_value to parse it into a struct that derives Deserialize.

Note that the output should not itself be considered a JSON-NS document. Running input through a processor twice may produce unexpected results.

That should cover the basics. More details can be found in the documentation of the structs, fields and functions.

Dependencies

~365–770KB
~17K SLoC