ruby-rdf / sparql

Ruby SPARQL library
http://rubygems.org/gems/sparql
The Unlicense
89 stars 14 forks source link

JSON output not emitting 'typed-literal' for boolean values #5

Closed ekolvets closed 11 years ago

ekolvets commented 11 years ago

SPARQL::Client expects boolean literals to have the type set to 'typed-literal' rather than just 'literal'. If you run the example below you will see that the JSON data has the type set to 'literal' and when this is parsed by SPARQL::Client.parse_json_bindings it becomes a RDF::Literal rather than a RDF::Literal::Boolean.

require 'rdf'
require 'sparql'
require 'json'
require 'pp'
require 'sparql/client'

class TestRepo < ::RDF::Repository

  def query(pattern, &block)
    statements = []
    statements << RDF::Statement.new(
        :subject   => RDF::URI.new('http://localhost/people/1'),
        :predicate => RDF::URI.new('http://localhost/attribute_types/has_name'),
        :object    => RDF::Literal.new(true))

    statements.each(&block)
  end
end

query = %q(
PREFIX a: <http://localhost/attribute_types/>
  SELECT ?entity ?has_name
  WHERE {
    ?entity a:has_name ?has_name .
  }
)

rep = TestRepo.new(:base_url => 'http://localhost')
sse = SPARQL.parse(query)

solutions = sse.execute(rep)
pp JSON.parse(solutions.to_json)
puts SPARQL::Client.parse_json_bindings(solutions.to_json).inspect
gkellogg commented 11 years ago

Yes, you're right. According to the SPARQL JSON Results spec, it should be typed-literal. I'll update this and push out an update to the gem directly.