Skip to content

Commit

Permalink
Use context loader when loading contexts.
Browse files Browse the repository at this point in the history
  • Loading branch information
gkellogg committed Aug 30, 2022
1 parent b5b019d commit e3b319a
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions lib/json/ld/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ def parse(local_context,
log_debug("parse") {"io: #{context}"}
# Load context document, if it is an open file
begin
ctx = JSON.load(context)
ctx = load_context(context, **@options)
raise JSON::LD::JsonLdError::InvalidRemoteContext, "Context missing @context key" if @options[:validate] && ctx['@context'].nil?
result = result.parse(ctx["@context"] ? ctx["@context"] : {})
rescue JSON::ParserError => e
Expand Down Expand Up @@ -911,8 +911,7 @@ def serialize(provided_context: nil, **options)
#log_debug "serlialize: reuse context: #{provided_context.inspect}"
provided_context
when IO, StringIO
provided_context.rewind
JSON.load(provided_context).fetch('@context', {})
load_context(provided_context, **@options).fetch('@context', {})
else
ctx = {}
ctx['@version'] = 1.1 if @processingMode == 'json-ld-1.1'
Expand Down Expand Up @@ -1689,6 +1688,16 @@ def alias(value)
CONTEXT_BASE_FRAG_OR_QUERY = %w(? #).freeze
CONTEXT_TYPE_ID_VOCAB = %w(@id @vocab).freeze

##
# Reads the `@context` from an IO
def load_context(io, **options)
io.rewind
remote_doc = API.loadRemoteDocument(io, **options)
remote_doc.document.is_a?(String) ?
MultiJson.load(remote_doc.document) :
remote_doc.document
end

def uri(value)
case value.to_s
when /^_:(.*)$/
Expand Down

0 comments on commit e3b319a

Please sign in to comment.