Re: Type of external @context param when serializing RDF::Graph to JSON-LD

On Aug 17, 2013, at 3:55 AM, Jindřich Mynarz <mynarzjindrich@gmail.com> wrote:

> Hi,
> 
> I want to use ruby-rdf/json-ld to serialize RDF::Graph into JSON-LD given an external @context. The examples at https://github.com/ruby-rdf/json-ld/ suggest using JSON::LD::API::fromRDF(input, context). I noticed that the input param should be an instance of RDF::Graph, however, it's not clear to me what type should the context param be. Documentation for JSON::LD::API::fromRDF states that the second parameter is [Hash{Symbol => Object}] options, not @context. How should I provide an external @context when serializing RDF::Graph to JSON-LD? As a file path, String, Hash or as an instance of JSON::LD:Context? How could you do the same when using RDF::Graph#dump directly?

The JSON::LD gem closely follows the JSON-LD API specification, but is more of a ruby-like gem then a JavaScript/Promise API as it is synchronous. You can view the online documentation here: http://rubydoc.info/github/ruby-rdf/json-ld/master/frames.

You can either using the fromRDF API method, or the standard RDF.rb readers and writers, which allows the use of dump() method.

The fromRDF class method [1] does not take a context, as it outputs expanded JSON-LD. If you wanted to compact it using a context, you could add a compact call [2], such as the following:

    JSON::LD::API.fromRDF(graph) do |expanded|
      JSON::LD::API.compact(expanded, context) do |compacted|
        puts compacted.to_json
      end
    end

From the compact documentation, you can see that the context parameter accepts a String, something responding to #read, Hash, Array or JSON::LD::Context. In the case of a string, it is interpreted as a file path or URL from which to load a context.

From the JSON::LD::Writer.initialize documentation [3], you can see that there is a :context option, which is treated just like the context parameter to compact. Because this is a general Writer interface, as used elsewhere in RDF.rb, this means that it could be added to a dump() call, such as the following

    g = RDF::Graph.load("file.ttl")
    puts g.dump(:jsonld, :context => context)

If you use prefix and base definitions instead, it will create a new context and use that for compacting the output; this is common when transcoding between different RDF formats.

Gregg

[1] http://rubydoc.info/github/ruby-rdf/json-ld/master/JSON/LD/API#fromRDF-class_method
[2] http://rubydoc.info/github/ruby-rdf/json-ld/master/JSON/LD/API#compact-class_method
[3] http://rubydoc.info/github/ruby-rdf/json-ld/master/JSON/LD/Writer#initialize-instance_method


> Best,
> 
> Jindrich
> 
> -- 
> Jindrich Mynarz
> http://mynarz.net/#jindrich

Received on Saturday, 17 August 2013 18:14:49 UTC