Skip to content

Commit 9665d09

Browse files
committed
Finish 2.1.4
2 parents 7f91961 + ec1f75b commit 9665d09

File tree

16 files changed

+296
-61
lines changed

16 files changed

+296
-61
lines changed

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2.1.3
1+
2.1.4

bin/jsonld

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ $:.unshift(File.expand_path("../../lib", __FILE__))
88
require 'json/ld'
99
require 'getoptlong'
1010
require 'open-uri'
11+
require 'logger'
1112

1213
def run(input, options)
1314
reader_class = RDF::Reader.for(options[:input_format].to_sym)
@@ -62,29 +63,39 @@ def run(input, options)
6263
STDERR.puts "\nParsed #{num} statements in #{secs} seconds @ #{num/secs} statements/second." unless options[:quiet]
6364
end
6465
rescue
65-
fname = input.respond_to?(:path) ? input.path : "-stdin-"
66+
fname = case
67+
when input.respond_to?(:path) then input.path
68+
when input.respond_to?(:requested_url) && input.requested_url then input.requested_url
69+
when input.respond_to?(:base_uri) then input.base_uri
70+
else "-stdin-"
71+
end
6672
STDERR.puts("Error in #{fname}")
6773
raise
6874
end
6975

76+
logger = Logger.new(STDERR)
77+
logger.level = Logger::WARN
78+
logger.formatter = lambda {|severity, datetime, progname, msg| "#{severity}: #{msg}\n"}
79+
7080
parser_options = {
7181
base: nil,
72-
progress: false,
7382
validate: false,
7483
stream: false,
7584
strict: false,
85+
logger: logger,
7686
}
7787

7888
options = {
7989
parser_options: parser_options,
8090
output: STDOUT,
8191
output_format: :jsonld,
8292
input_format: :jsonld,
93+
logger: logger,
8394
}
8495
input = nil
8596

8697
OPT_ARGS = [
87-
["--dbg", GetoptLong::NO_ARGUMENT, "Turn on verbose debugging"],
98+
["--debug", GetoptLong::NO_ARGUMENT, "Turn on verbose debugging"],
8899
["--compact", GetoptLong::NO_ARGUMENT, "Compact document, using --context"],
89100
["--compactArrays", GetoptLong::OPTIONAL_ARGUMENT, "Set compactArrays option"],
90101
["--context", GetoptLong::REQUIRED_ARGUMENT,"Context to apply for expand, compact and converting from RDF"],
@@ -130,7 +141,7 @@ opts = GetoptLong.new(*OPT_ARGS.map {|o| o[0..-2]})
130141

131142
opts.each do |opt, arg|
132143
case opt
133-
when '--dbg' then parser_options[:debug] = ::JSON::LD::debug = true
144+
when '--debug' then logger.level = Logger::DEBUG
134145
when '--compact' then options[:compact] = true
135146
when "--compactArrays" then options[:compactArrays] = (arg || 'true') == 'true'
136147
when '--context' then options[:context] = arg
@@ -146,7 +157,9 @@ opts.each do |opt, arg|
146157
when '--output' then options[:output] = File.open(arg, "w")
147158
when '--parse-only' then options[:parse_only] = true
148159
when '--processingMode' then options[:processingMode] = arg
149-
when '--quiet' then options[:quiet] = true
160+
when '--quiet'
161+
options[:quiet] = true
162+
logger.level = Logger::FATAL
150163
when "--rename_bnodes" then options[:rename_bnodes] = (arg || 'true') == 'true'
151164
when "--requireAll" then options[:requireAll] = (arg || 'true') == 'true'
152165
when '--stream' then parser_options[:stream] = true

example-files/issue-496.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
require 'pp'
2+
require 'linkeddata'
3+
4+
input = JSON.parse %({
5+
"@context": {
6+
"schema": "http://schema.org/",
7+
"name": "schema:name",
8+
"url": {"@id": "schema:url", "@type": "schema:URL"}
9+
},
10+
"name": "Jane Doe"
11+
})
12+
13+
frame = JSON.parse %({
14+
"@context": {
15+
"schema": "http://schema.org/",
16+
"name": "schema:name",
17+
"url": { "@id": "schema:url", "@type": "schema:URL"}
18+
},
19+
"name": {},
20+
"url": {"@default": {"@value": "http://example.com", "@type": "schema:URL"}}
21+
})
22+
23+
pp JSON::LD::API.frame(input, frame, logger: Logger.new(STDERR))

example-files/issue-499-frame.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{"@context": ["http://schema.org", {
2+
"maintainer": {"@type": "@id"}
3+
}],
4+
"@embed": "@always",
5+
"author": {
6+
"familyName": {},
7+
"affiliation":
8+
{"@default": "@null"}
9+
},
10+
"maintainer": {
11+
"familyName": { "@default": "none" }
12+
}
13+
}

example-files/issue-499.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"@context": "http://schema.org",
3+
"@type": "SoftwareSourceCode",
4+
"author":
5+
{
6+
"@type": "Person",
7+
"givenName": "Carl",
8+
"familyName": "Boettiger",
9+
"email": "[email protected]",
10+
"@id": "http://orcid.org/0000-0002-1642-628X"
11+
},
12+
"maintainer": "http://orcid.org/0000-0002-1642-628X"
13+
}

example-files/mitar-in.jsonld

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"@context": {
3+
"@vocab": "http://example/",
4+
"@version": 1.1,
5+
"B": {"@context": {"c": "http://example.org/c"}}
6+
},
7+
"a": {
8+
"@context": {"@vocab": "http://example.com/"},
9+
"@type": "B",
10+
"a": "A in example.com",
11+
"c": "C in example.org"
12+
},
13+
"c": "C in example"
14+
}

example-files/workergnome-issue-35.rb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
require 'linkeddata'
2+
3+
graph = RDF::Graph.new
4+
graph << [RDF::URI("http://www.museum.com/fish"),
5+
RDF.type,
6+
RDF::URI("http://www.cidoc-crm.org/cidoc-crm/E55_Type")]
7+
8+
unframed_json = JSON::LD::API::fromRdf(graph)
9+
10+
11+
# Using this frame:
12+
frame = {
13+
"@context"=> [
14+
"https://linked.art/ns/context/1/full.jsonld",
15+
{"crm" => "http://www.cidoc-crm.org/cidoc-crm/"}
16+
]
17+
}
18+
19+
# This works
20+
puts JSON::LD::API.frame(unframed_json, frame, base: "http://www.example.com")
21+
22+
# But this doesn't.
23+
begin
24+
puts JSON::LD::API.frame(unframed_json, frame)
25+
rescue JSON::LD::JsonLdError::InvalidBaseIRI => e
26+
puts "This doesn't work: #{e}"
27+
end
28+
29+
30+
# But using this frame:
31+
frame = {
32+
"@context"=> [
33+
{"crm" => "http://www.cidoc-crm.org/cidoc-crm/"}
34+
]
35+
}
36+
# Both of these work
37+
puts JSON::LD::API.frame(unframed_json, frame, base: "http://www.example.com")
38+
puts JSON::LD::API.frame(unframed_json, frame)

lib/json/ld/api.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ def initialize(input, context, options = {}, &block)
136136
@context = Context.parse(context || {}, @options)
137137

138138
# If not set explicitly, the context figures out the processing mode
139-
@options[:processingMode] ||= @context.processingMode
139+
@options[:processingMode] ||= @context.processingMode || "json-ld-1.0"
140140
@options[:validate] ||= %w(json-ld-1.0 json-ld-1.1).include?(@options[:processingMode])
141141

142142
if block_given?

lib/json/ld/compact.rb

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def compact(element, property: nil)
3030
# If element has a single member and the active property has no
3131
# @container mapping to @list or @set, the compacted value is that
3232
# member; otherwise the compacted value is element
33-
if result.length == 1 && context.container(property).nil? && @options[:compactArrays]
33+
if result.length == 1 && !context.as_array?(property) && @options[:compactArrays]
3434
#log_debug("=> extract single element: #{result.first.inspect}")
3535
result.first
3636
else
@@ -85,7 +85,7 @@ def compact(element, property: nil)
8585
compacted_value.each do |prop, value|
8686
if context.reverse?(prop)
8787
value = [value] if !value.is_a?(Array) &&
88-
(context.container(prop) == '@set' || !@options[:compactArrays])
88+
(context.as_array?(prop) || !@options[:compactArrays])
8989
#log_debug("") {"merge #{prop} => #{value.inspect}"}
9090

9191
merge_compacted_value(result, prop, value)
@@ -101,6 +101,17 @@ def compact(element, property: nil)
101101
next
102102
end
103103

104+
if expanded_property == '@preserve'
105+
# Compact using `property`
106+
compacted_value = compact(expanded_value, property: property)
107+
#log_debug("@preserve") {"compacted_value: #{compacted_value.inspect}"}
108+
109+
unless compacted_value.is_a?(Array) && compacted_value.empty?
110+
result['@preserve'] = compacted_value
111+
end
112+
next
113+
end
114+
104115
if expanded_property == '@index' && context.container(property) == '@index'
105116
#log_debug("@index") {"drop @index"}
106117
next
@@ -150,6 +161,7 @@ def compact(element, property: nil)
150161
end
151162

152163
container = context.container(item_active_property)
164+
as_array = context.as_array?(item_active_property)
153165
value = list?(expanded_item) ? expanded_item['@list'] : expanded_item
154166
compacted_item = compact(value, property: item_active_property)
155167
#log_debug("") {" => compacted key: #{item_active_property.inspect} for #{compacted_item.inspect}"}
@@ -195,14 +207,11 @@ def compact(element, property: nil)
195207
end
196208
compacted_item
197209
end
210+
compacted_item = [compacted_item] if as_array && !compacted_item.is_a?(Array)
198211
merge_compacted_value(map_object, map_key, compacted_item)
199212
else
200213
compacted_item = [compacted_item] if
201-
!compacted_item.is_a?(Array) && (
202-
!@options[:compactArrays] ||
203-
%w(@set @list).include?(container) ||
204-
%w(@list @graph).include?(expanded_property)
205-
)
214+
!compacted_item.is_a?(Array) && (!@options[:compactArrays] || as_array)
206215
merge_compacted_value(nest_result, item_active_property, compacted_item)
207216
end
208217
end

0 commit comments

Comments
 (0)