Crate json_ns[][src]

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:

This example is not tested
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:

This example is not tested
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.

Structs

Context

Structure holding the current context to interpret a document with.

Processor

A document processor.

TargetContext

Structure holding the target context to reword a document to.