digitalbazaar/jsonld.js — pull request #15 by jmandel (2012-05-19 00:15:37+00:00)
Title: Experimental `jsonld.objectify` API

This adds an API call that [SMART](http://smartplatforms.org) has found useful for in-memory traversal of graphs with cycles. It's an explicit `jsonld.objectify(graph, context, cb)` to build an in-memory graph with links that can be followed 'round and 'round.

I'm not proposing this for the official API, but others might also find it a nice-to-have addition in the client library. I figured I'd see if there's any broader interest.

In any case, this represents one reason why I'd like official support for `.flatten()` (json-ld/json-ld.org#109) (since `.objectify()` relies on it).

*Example usage:*

var graph = {
"@context": {
"dc": "http://purl.org/dc/elements/1.1/",
"ex": "http://example.org/vocab#",
"xsd": "http://www.w3.org/2001/XMLSchema#",
"ex:sees": {
"@type": "@id"
}
},
"@id": "http://example.org/node1",
"@type": "ex:Node",
"ex:sees": {
"@id": "http://example.org/node2",
"@type": "ex:Node",
"ex:sees": "http://example.org/node1",
"dc:title": "Node 2"
},
"dc:title": "Node 1"
};

var context = {
"dc": "http://purl.org/dc/elements/1.1/",
"ex": "http://example.org/vocab#",
"Node": { "@type": "@id", "@id":"ex:Node" },
"sees": { "@type": "@id", "@id":"ex:sees" }
};

jsonld.objectify(graph, context, function(err, obj){
if (err){ console.log(err) };
var reflection = obj.of_type.Node[0].sees.sees["dc:title"];
console.log(reflection);
});