diff --git a/lib/json/ld/flatten.rb b/lib/json/ld/flatten.rb index 3d4111fe..1f3917bb 100644 --- a/lib/json/ld/flatten.rb +++ b/lib/json/ld/flatten.rb @@ -25,6 +25,7 @@ def generate_node_map(element, list = nil) depth do debug("node_map") {"active_graph: #{active_graph}, element: #{element.inspect}"} + debug(" =>") {"active_subject: #{active_subject.inspect}, active_property: #{active_property.inspect}, list: #{list.inspect}"} if element.is_a?(Array) # If element is an array, process each entry in element recursively by passing item for element, node map, active graph, active subject, active property, and list. element.map {|o| @@ -90,7 +91,10 @@ def generate_node_map(element, graph[id] ||= {'@id' => id} # If active property is not null, perform the following steps: - if active_property + if node?(active_subject) || node_reference?(active_subject) + debug("node_map") {"#{active_subject.inspect} is an object, merge into #{graph[id].inspect}"} + merge_value(graph[id], active_property, active_subject) + elsif active_property # Create a new JSON object reference consisting of a single member @id whose value is id. reference = {'@id' => id} @@ -125,13 +129,12 @@ def generate_node_map(element, element.delete('@reverse').each do |property, values| values.each do |value| debug("node_map") {"@reverse(#{id}): #{value.inspect}"} - # If value has a property member, append referenced node to its value; otherwise create a property member whose value is an array containing referenced node. - merge_value(value, property, {'@id' => id}) - # Recursively invoke this algorithm passing value for element, node map, and active graph. generate_node_map(value, node_map, - active_graph) + active_graph, + {'@id' => id}, + property) end end end diff --git a/spec/flatten_spec.rb b/spec/flatten_spec.rb index 3a97cd5d..2b3f259f 100644 --- a/spec/flatten_spec.rb +++ b/spec/flatten_spec.rb @@ -181,6 +181,35 @@ }] }), :options => {} + }, + "@reverse bnode issue (0045)" => { + :input => ::JSON.parse(%q{ + { + "@context": { + "foo": "http://example.org/foo", + "bar": { "@reverse": "http://example.org/bar", "@type": "@id" } + }, + "foo": "Foo", + "bar": [ "http://example.org/origin", "_:b0" ] + } + }), + :output => ::JSON.parse(%q{ + [ + { + "@id": "_:b0", + "http://example.org/foo": [ { "@value": "Foo" } ] + }, + { + "@id": "_:b1", + "http://example.org/bar": [ { "@id": "_:b0" } ] + }, + { + "@id": "http://example.org/origin", + "http://example.org/bar": [ { "@id": "_:b0" } ] + } + ] + }), + :options => {} } }.each do |title, params| it title do