Skip to content

Commit

Permalink
Add URI constants
Browse files Browse the repository at this point in the history
  • Loading branch information
abrisse authored and gkellogg committed Apr 17, 2023
1 parent 7fb89e6 commit a73b483
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 11 deletions.
5 changes: 5 additions & 0 deletions lib/json/ld.rb
Expand Up @@ -110,6 +110,11 @@ module LD

MAX_CONTEXTS_LOADED = 50

# URI Constants
RDF_JSON = RDF::URI("#{RDF.to_uri}JSON")
RDF_DIRECTION = RDF::URI("#{RDF.to_uri}direction")
RDF_LANGUAGE = RDF::URI("#{RDF.to_uri}language")

class JsonLdError < StandardError
def to_s
"#{self.class.instance_variable_get :@code}: #{super}"
Expand Down
10 changes: 5 additions & 5 deletions lib/json/ld/from_rdf.rb
Expand Up @@ -57,7 +57,7 @@ def from_statements(dataset, useRdfType: false, useNativeTypes: false, extendedR
extendedRepresentation)

# If predicate is rdf:datatype, note subject in compound literal subjects map
if @options[:rdfDirection] == 'compound-literal' && statement.predicate == RDF.to_uri + 'direction'
if @options[:rdfDirection] == 'compound-literal' && statement.predicate == RDF_DIRECTION
compound_literal_subjects[name][subject] ||= true
end

Expand Down Expand Up @@ -120,14 +120,14 @@ def from_statements(dataset, useRdfType: false, useNativeTypes: false, extendedR

v.delete('@id')
v['@value'] = cl_node[RDF.value.to_s].first['@value']
if cl_node[RDF.to_uri.to_s + 'language']
lang = cl_node[RDF.to_uri.to_s + 'language'].first['@value']
if (langs = cl_node[RDF_LANGUAGE.to_s])
lang = langs.first['@value']
unless /^[a-zA-Z]{1,8}(-[a-zA-Z0-9]{1,8})*$/.match?(lang)
warn "i18n datatype language must be valid BCP47: #{lang.inspect}"
end
v['@language'] = lang
end
v['@direction'] = cl_node[RDF.to_uri.to_s + 'direction'].first['@value']
v['@direction'] = cl_node[RDF_DIRECTION.to_s].first['@value']
end
end

Expand Down Expand Up @@ -209,7 +209,7 @@ def resource_representation(resource, useNativeTypes, extendedRepresentation)
rdfDirection = @options[:rdfDirection]
res = {}

if resource.datatype == RDF::URI(RDF.to_uri + "JSON") && @context.processingMode('json-ld-1.1')
if resource.datatype == RDF_JSON && @context.processingMode('json-ld-1.1')
res['@type'] = '@json'
res['@value'] = begin
::JSON.parse(resource.object)
Expand Down
2 changes: 1 addition & 1 deletion lib/json/ld/streaming_writer.rb
Expand Up @@ -60,7 +60,7 @@ def stream_statement(statement)

pd << if statement.object.resource?
{ '@id' => statement.object.to_s }
elsif statement.object.datatype == RDF::URI(RDF.to_uri + "JSON")
elsif statement.object.datatype == RDF_JSON
{ "@value" => MultiJson.load(statement.object.to_s), "@type" => "@json" }
else
lit = { "@value" => statement.object.to_s }
Expand Down
10 changes: 5 additions & 5 deletions lib/json/ld/to_rdf.rb
Expand Up @@ -24,7 +24,7 @@ def item_to_rdf(item, graph_name: nil, quoted: false, &block)
value = item.fetch('@value')
datatype = item.fetch('@type', nil)

datatype = RDF::URI(RDF.to_uri + "JSON") if datatype == '@json'
datatype = RDF_JSON if datatype == '@json'

case value
when RDF::Value
Expand All @@ -35,7 +35,7 @@ def item_to_rdf(item, graph_name: nil, quoted: false, &block)
datatype ||= RDF::XSD.boolean.to_s
when Numeric
# Otherwise, if value is a number, then set value to its canonical lexical form as defined in the section Data Round Tripping. If datatype is null, set it to either xsd:integer or xsd:double, depending on if the value contains a fractional and/or an exponential component.
value = if datatype == RDF::URI(RDF.to_uri + "JSON")
value = if datatype == RDF_JSON
value.to_json_c14n
else
# Don't serialize as double if there are no fractional bits
Expand All @@ -62,15 +62,15 @@ def item_to_rdf(item, graph_name: nil, quoted: false, &block)
when 'compound-literal'
cl = RDF::Node.new
yield RDF::Statement(cl, RDF.value, item['@value'].to_s)
yield RDF::Statement(cl, RDF.to_uri + 'language', item['@language'].downcase) if item['@language']
yield RDF::Statement(cl, RDF.to_uri + 'direction', item['@direction'])
yield RDF::Statement(cl, RDF_LANGUAGE, item['@language'].downcase) if item['@language']
yield RDF::Statement(cl, RDF_DIRECTION, item['@direction'])
return cl
end
end

# Otherwise, if datatype is null, set it to xsd:string or xsd:langString, depending on if item has a @language key.
datatype ||= item.key?('@language') ? RDF.langString : RDF::XSD.string
value = value.to_json_c14n if datatype == RDF::URI(RDF.to_uri + "JSON")
value = value.to_json_c14n if datatype == RDF_JSON
end
datatype = RDF::URI(datatype) if datatype && !datatype.is_a?(RDF::URI)

Expand Down

0 comments on commit a73b483

Please sign in to comment.