Skip to content

Commit 91e20d3

Browse files
committed
Pass through RDF literals in ToRDF#item_to_rdf.
1 parent be7a179 commit 91e20d3

File tree

2 files changed

+135
-0
lines changed

2 files changed

+135
-0
lines changed

lib/json/ld/to_rdf.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ def item_to_rdf(item, graph_name: nil, quoted: false, &block)
2525
datatype = RDF::URI(RDF.to_uri + "JSON") if datatype == '@json'
2626

2727
case value
28+
when RDF::Value
29+
return value
2830
when TrueClass, FalseClass
2931
# If value is true or false, then set value its canonical lexical form as defined in the section Data Round Tripping. If datatype is null, set it to xsd:boolean.
3032
value = value.to_s

spec/to_rdf_spec.rb

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,139 @@
409409
end
410410
end
411411
end
412+
413+
context "with xsd: true" do
414+
{
415+
"true": {
416+
input: {
417+
"@context" => {
418+
"e" => "http://example.org/vocab#e"
419+
},
420+
"e" => RDF::Literal(true)
421+
},
422+
output: %(
423+
@prefix ex: <http://example.org/vocab#> .
424+
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
425+
[ex:e true] .
426+
)
427+
},
428+
"integer": {
429+
input: {
430+
"@context" => {
431+
"e" => "http://example.org/vocab#e"
432+
},
433+
"e" => RDF::Literal(1)
434+
},
435+
output: %(
436+
@prefix ex: <http://example.org/vocab#> .
437+
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
438+
[ex:e 1] .
439+
)
440+
},
441+
"decimal": {
442+
input: {
443+
"@context" => {
444+
"e" => "http://example.org/vocab#e"
445+
},
446+
"e" => RDF::Literal::Decimal.new("1.1")
447+
},
448+
output: %(
449+
@prefix ex: <http://example.org/vocab#> .
450+
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
451+
[ex:e 1.1] .
452+
)
453+
},
454+
"float": {
455+
input: {
456+
"@context" => {
457+
"e" => "http://example.org/vocab#e"
458+
},
459+
"e" => RDF::Literal.new("1.1e1", datatype: RDF::XSD.float)
460+
},
461+
output: %(
462+
@prefix ex: <http://example.org/vocab#> .
463+
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
464+
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
465+
[ex:e "1.1e1"^^xsd:float] .
466+
)
467+
},
468+
"double": {
469+
input: {
470+
"@context" => {
471+
"e" => "http://example.org/vocab#e"
472+
},
473+
"e" => RDF::Literal.new("1.1e1", datatype: RDF::XSD.double)
474+
},
475+
output: %(
476+
@prefix ex: <http://example.org/vocab#> .
477+
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
478+
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
479+
[ex:e 1.1e1] .
480+
)
481+
},
482+
"date": {
483+
input: {
484+
"@context" => {
485+
"e" => "http://example.org/vocab#e"
486+
},
487+
"e" => RDF::Literal.new("2022-08-27", datatype: RDF::XSD.date)
488+
},
489+
output: %(
490+
@prefix ex: <http://example.org/vocab#> .
491+
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
492+
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
493+
[ex:e "2022-08-27"^^xsd:date] .
494+
)
495+
},
496+
"time": {
497+
input: {
498+
"@context" => {
499+
"e" => "http://example.org/vocab#e"
500+
},
501+
"e" => RDF::Literal.new("12:00:00", datatype: RDF::XSD.time)
502+
},
503+
output: %(
504+
@prefix ex: <http://example.org/vocab#> .
505+
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
506+
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
507+
[ex:e "12:00:00"^^xsd:time] .
508+
)
509+
},
510+
"dateTime": {
511+
input: {
512+
"@context" => {
513+
"e" => "http://example.org/vocab#e"
514+
},
515+
"e" => RDF::Literal.new("2022-08-27T12:00:00", datatype: RDF::XSD.dateTime)
516+
},
517+
output: %(
518+
@prefix ex: <http://example.org/vocab#> .
519+
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
520+
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
521+
[ex:e "2022-08-27T12:00:00"^^xsd:dateTime] .
522+
)
523+
},
524+
"language": {
525+
input: {
526+
"@context" => {
527+
"e" => "http://example.org/vocab#e"
528+
},
529+
"e" => RDF::Literal.new("language", language: :"en-us")
530+
},
531+
output: %(
532+
@prefix ex: <http://example.org/vocab#> .
533+
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
534+
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
535+
[ex:e "language"@en-us] .
536+
)
537+
},
538+
}.each do |title, params|
539+
it title do
540+
params[:output] = RDF::Graph.new << RDF::Turtle::Reader.new(params[:output])
541+
run_to_rdf(params.merge(xsd: true))
542+
end
543+
end
544+
end
412545
end
413546

414547
context "prefixes" do

0 commit comments

Comments
 (0)