Skip to content

Commit 76a0332

Browse files
committed
Use MutliJson for parsing, allows adapter to be set in options, or the best available used automtaictally. Fixes #18.
1 parent 8e3ac6a commit 76a0332

File tree

5 files changed

+44
-32
lines changed

5 files changed

+44
-32
lines changed

json-ld.gemspec

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ Gem::Specification.new do |gem|
2828
gem.required_ruby_version = '>= 1.9.2'
2929
gem.requirements = []
3030
gem.add_runtime_dependency 'rdf', '~> 1.1', '>= 1.1.7'
31+
gem.add_runtime_dependency 'multi_json', '~> 1.11'
3132
gem.add_development_dependency 'jsonlint', '~> 0.1.0' unless RUBY_ENGINE == "jruby"
33+
gem.add_development_dependency 'oj', '~> 2.12'
34+
gem.add_development_dependency 'yajl-ruby', '~> 1.2.1'
3235
gem.add_development_dependency "rack-cache", '~> 1.2'
3336
gem.add_development_dependency "rest-client", '~> 1.8'
3437
gem.add_development_dependency "rest-client-components", '~> 1.4'
@@ -37,7 +40,7 @@ Gem::Specification.new do |gem|
3740
gem.add_development_dependency 'rdf-trig', '~> 1.1'
3841
gem.add_development_dependency 'rdf-turtle', '~> 1.1'
3942
gem.add_development_dependency 'rdf-xsd', '~> 1.1'
40-
gem.add_development_dependency 'rspec', '~> 3.0'
43+
gem.add_development_dependency 'rspec', '~> 3.2.0'
4144
gem.add_development_dependency 'rspec-its', '~> 1.0'
4245
gem.add_development_dependency 'yard' , '~> 0.8'
4346

lib/json/ld.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
$:.unshift(File.expand_path(File.join(File.dirname(__FILE__), '..')))
22
require 'rdf' # @see http://rubygems.org/gems/rdf
3+
require 'multi_json'
34

45
module JSON
56
##

lib/json/ld/api.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ class API
8181
# Use unique bnode identifiers, defaults to using the identifier which the node was originall initialized with (if any).
8282
# @option options [Boolean] :simple_compact_iris (false)
8383
# When compacting IRIs, do not use terms with expanded term definitions
84+
# @option options [Symbol] :adapter used with MultiJson
8485
# @option options [Boolean] :validate Validate input, if a string or readable object.
8586
# @yield [api]
8687
# @yieldparam [API]
@@ -108,7 +109,7 @@ def initialize(input, context, options = {}, &block)
108109

109110
validate_input(input) if options[:validate]
110111

111-
JSON.parse(input.read)
112+
MultiJson.load(input.read, options)
112113
when String
113114
remote_doc = @options[:documentLoader].call(input, @options)
114115

@@ -118,7 +119,7 @@ def initialize(input, context, options = {}, &block)
118119
case remote_doc.document
119120
when String
120121
validate_input(remote_doc.document) if options[:validate]
121-
JSON.parse(remote_doc.document)
122+
MultiJson.load(remote_doc.document, options)
122123
else
123124
remote_doc.document
124125
end
@@ -332,11 +333,11 @@ def self.frame(input, frame, options = {})
332333
# de-reference frame to create the framing object
333334
frame = case frame
334335
when Hash then frame.dup
335-
when IO, StringIO then JSON.parse(frame.read)
336+
when IO, StringIO then MultiJson.load(frame.read)
336337
when String
337338
remote_doc = options[:documentLoader].call(frame)
338339
case remote_doc.document
339-
when String then JSON.parse(remote_doc.document)
340+
when String then MultiJson.load(remote_doc.document)
340341
else remote_doc.document
341342
end
342343
end

lib/json/ld/context.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ def parse(local_context, remote_contexts = [])
297297
@options[:documentLoader].call(context.to_s, context_opts) do |remote_doc|
298298
# 3.2.5) Dereference context. If the dereferenced document has no top-level JSON object with an @context member, an invalid remote context has been detected and processing is aborted; otherwise, set context to the value of that member.
299299
jo = case remote_doc.document
300-
when String then JSON.parse(remote_doc.document)
300+
when String then MultiJson.load(remote_doc.document)
301301
else remote_doc.document
302302
end
303303
raise JsonLdError::InvalidRemoteContext, "#{context}" unless jo.is_a?(Hash) && jo.has_key?('@context')
@@ -306,6 +306,9 @@ def parse(local_context, remote_contexts = [])
306306
context_no_base.provided_context = context.dup
307307
end
308308
end
309+
rescue JsonLdError::LoadingDocumentFailed => e
310+
debug("parse") {"Failed to retrieve @context from remote document at #{context_no_base.context_base.inspect}: #{e.message}"}
311+
raise JsonLdError::LoadingRemoteContextFailed, "#{context_no_base.context_base}", e.backtrace
309312
rescue JsonLdError
310313
raise
311314
rescue Exception => e

spec/api_spec.rb

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -65,36 +65,40 @@
6565
end
6666

6767
context "Test Files" do
68-
Dir.glob(File.expand_path(File.join(File.dirname(__FILE__), 'test-files/*-input.*'))) do |filename|
69-
test = File.basename(filename).sub(/-input\..*$/, '')
70-
frame = filename.sub(/-input\..*$/, '-frame.json')
71-
framed = filename.sub(/-input\..*$/, '-framed.json')
72-
compacted = filename.sub(/-input\..*$/, '-compacted.json')
73-
context = filename.sub(/-input\..*$/, '-context.json')
74-
expanded = filename.sub(/-input\..*$/, '-expanded.json')
75-
ttl = filename.sub(/-input\..*$/, '-rdf.ttl')
68+
%w(oj json_gem ok_json yajl).map(&:to_sym).each do |adapter|
69+
context "with MultiJson adapter #{adapter.inspect}" do
70+
Dir.glob(File.expand_path(File.join(File.dirname(__FILE__), 'test-files/*-input.*'))) do |filename|
71+
test = File.basename(filename).sub(/-input\..*$/, '')
72+
frame = filename.sub(/-input\..*$/, '-frame.json')
73+
framed = filename.sub(/-input\..*$/, '-framed.json')
74+
compacted = filename.sub(/-input\..*$/, '-compacted.json')
75+
context = filename.sub(/-input\..*$/, '-context.json')
76+
expanded = filename.sub(/-input\..*$/, '-expanded.json')
77+
ttl = filename.sub(/-input\..*$/, '-rdf.ttl')
7678

77-
context test do
78-
it "expands" do
79-
options = {debug: @debug}
80-
options[:expandContext] = File.open(context) if context
81-
jld = described_class.expand(File.open(filename), options)
82-
expect(jld).to produce(JSON.load(File.open(expanded)), @debug)
83-
end if File.exist?(expanded)
79+
context test do
80+
it "expands" do
81+
options = {debug: @debug, adapter: adapter}
82+
options[:expandContext] = File.open(context) if context
83+
jld = described_class.expand(File.open(filename), options)
84+
expect(jld).to produce(JSON.load(File.open(expanded)), @debug)
85+
end if File.exist?(expanded)
8486

85-
it "compacts" do
86-
jld = described_class.compact(File.open(filename), File.open(context), debug: @debug)
87-
expect(jld).to produce(JSON.load(File.open(compacted)), @debug)
88-
end if File.exist?(compacted) && File.exist?(context)
87+
it "compacts" do
88+
jld = described_class.compact(File.open(filename), File.open(context), adapter: adapter, debug: @debug)
89+
expect(jld).to produce(JSON.load(File.open(compacted)), @debug)
90+
end if File.exist?(compacted) && File.exist?(context)
8991

90-
it "frame" do
91-
jld = described_class.frame(File.open(filename), File.open(frame), debug: @debug)
92-
expect(jld).to produce(JSON.load(File.open(framed)), @debug)
93-
end if File.exist?(framed) && File.exist?(frame)
92+
it "frame" do
93+
jld = described_class.frame(File.open(filename), File.open(frame), adapter: adapter, debug: @debug)
94+
expect(jld).to produce(JSON.load(File.open(framed)), @debug)
95+
end if File.exist?(framed) && File.exist?(frame)
9496

95-
it "toRdf" do
96-
expect(RDF::Repository.load(filename, debug: @debug)).to be_equivalent_graph(RDF::Repository.load(ttl), trace: @debug)
97-
end if File.exist?(ttl)
97+
it "toRdf" do
98+
expect(RDF::Repository.load(filename, adapter: adapter, debug: @debug)).to be_equivalent_graph(RDF::Repository.load(ttl), trace: @debug)
99+
end if File.exist?(ttl)
100+
end
101+
end
98102
end
99103
end
100104
end

0 commit comments

Comments
 (0)