Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Experimental jsonld.objectify API #15

Merged
merged 1 commit into from
Nov 26, 2012

Conversation

jmandel
Copy link
Contributor

@jmandel jmandel commented May 19, 2012

This adds an API call that SMART 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);
});

Corrected recursion bug:  follow the top-level flattened subject!

(Precious behavior was recursing down the already-replaced branches
instead of tracking the replacement.)

Updated err message
@gkellogg
Copy link
Collaborator

+1 on including this in jsonld.js, and I believe it will be useful in the JSON-LD API. I often get frustrated trying to get framing to do what I want; being able to turn all subject references into actual JavaScript object references would vastly simplify navigating such structures.

dlongley added a commit that referenced this pull request Nov 26, 2012
Experimental `jsonld.objectify` API. Name 'objectify' is unstable; will be changed.
@dlongley dlongley merged commit 893c7fe into digitalbazaar:master Nov 26, 2012
@dlongley
Copy link
Member

I'm merging this in despite not liking the name "objectify". This name will eventually be changed, but I expect the functionality to remain basically the same.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants