You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -37,9 +37,9 @@ The order of triples retrieved from the `RDF::Enumerable` dataset determines the
37
37
### MultiJson parser
38
38
The [MultiJson](https://rubygems.org/gems/multi_json) gem is used for parsing JSON; this defaults to the native JSON parser, but will use a more performant parser if one is available. A specific parser can be specified by adding the `:adapter` option to any API call. See [MultiJson](https://rubygems.org/gems/multi_json) for more information.
39
39
40
-
### JSON-LD* (RDFStar)
40
+
### JSON-LD-star (RDFStar)
41
41
42
-
The {JSON::LD::API.expand}, {JSON::LD::API.compact}, {JSON::LD::API.toRdf}, and {JSON::LD::API.fromRdf} API methods, along with the {JSON::LD::Reader} and {JSON::LD::Writer}, include provisional support for [JSON-LD*][JSON-LD*].
42
+
The {JSON::LD::API.expand}, {JSON::LD::API.compact}, {JSON::LD::API.toRdf}, and {JSON::LD::API.fromRdf} API methods, along with the {JSON::LD::Reader} and {JSON::LD::Writer}, include provisional support for [JSON-LD-star][JSON-LD-star].
43
43
44
44
Internally, an `RDF::Statement` is treated as another resource, along with `RDF::URI` and `RDF::Node`, which allows an `RDF::Statement` to have a `#subject` or `#object` which is also an `RDF::Statement`.
45
45
@@ -636,7 +636,7 @@ see <https://unlicense.org/> or the accompanying {file:UNLICENSE} file.
# Otherwise, return the result of compacting flattened according the Compaction algorithm passing context ensuring that the compaction result uses the @graph keyword (or its alias) at the top-level, even if the context is empty or if there is only one element to put in the @graph array. This ensures that the returned document has a deterministic structure.
Copy file name to clipboardExpand all lines: lib/json/ld/flatten.rb
+46-1
Original file line number
Diff line number
Diff line change
@@ -9,7 +9,7 @@ module Flatten
9
9
##
10
10
# This algorithm creates a JSON object node map holding an indexed representation of the graphs and nodes represented in the passed expanded document. All nodes that are not uniquely identified by an IRI get assigned a (new) blank node identifier. The resulting node map will have a member for every graph in the document whose value is another object with a member for every node represented in the document. The default graph is stored under the @default member, all other graphs are stored under their graph name.
11
11
#
12
-
# For RDF*/JSON-LD*:
12
+
# For RDF-star/JSON-LD-star:
13
13
# * Values of `@id` can be an object (embedded node); when these are used as keys in a Node Map, they are serialized as canonical JSON, and de-serialized when flattening.
14
14
# * The presence of `@annotation` implies an embedded node and the annotation object is removed from the node/value object in which it appears.
# Updates a node map from which annotations have been folded into embedded triples to re-extract the annotations.
198
+
#
199
+
# Map entries where the key is of the form of a canonicalized JSON object are used to find keys with the `@id` and property components. If found, the original map entry is removed and entries added to an `@annotation` property of the associated value.
200
+
#
201
+
# * Keys which are of the form of a canonicalized JSON object are examined in inverse order of length.
202
+
# * Deserialize the key into a map, and re-serialize the value of `@id`.
203
+
# * If the map contains an entry with that value (after re-canonicalizing, as appropriate), and the associated antry has a item which matches the non-`@id` item from the map, the node is used to create an `@annotation` entry within that value.
204
+
#
205
+
# @param [Hash{String => Hash}] input
206
+
# @return [Hash{String => Hash}]
207
+
defcreate_annotations(node_map)
208
+
node_map.keys.
209
+
select{|k| k.start_with?('{')}.
210
+
sort_by(&:length).
211
+
reverse.eachdo |key|
212
+
213
+
annotation=node_map[key]
214
+
# Deserialize key, and re-serialize the `@id` value.
215
+
emb=annotation['@id'].dup
216
+
id=emb.delete('@id')
217
+
property,value=emb.to_a.first
218
+
219
+
# If id is a map, set it to the result of canonicalizing that value, otherwise to itself.
220
+
id=id.to_json_c14nifid.is_a?(Hash)
221
+
222
+
nextunlessnode_map.key?(id)
223
+
# If node map has an entry for id and that entry contains the same property and value from entry:
# If object is an IRI, blank node identifier, or statement, and node map does not have an object member, create one and initialize its value to a new JSON object consisting of a single member @id whose value is set to object.
# If predicate equals rdf:type, and object is an IRI or blank node identifier, append object to the value of the @type member of node. If no such member exists, create one and initialize it to an array whose only item is object. Finally, continue to the next RDF triple.
58
-
# XXX JSON-LD* does not support embedded value of @type
0 commit comments