Method: JSON::LD::API.flatten
- Defined in:
- lib/json/ld/api.rb
.flatten(input, context, expanded: false, serializer: nil, **options) {|jsonld| ... } ⇒ Object, Hash
This algorithm flattens an expanded JSON-LD document by collecting all properties of a node in a single JSON object and labeling all blank nodes with blank node identifiers. This resulting uniform shape of the document, may drastically simplify the code required to process JSON-LD data in certain applications.
The resulting ‘Array` is either returned, or yielded if a block is given.
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 |
# File 'lib/json/ld/api.rb', line 290 def self.flatten(input, context, expanded: false, serializer: nil, **) flattened = [] = { compactToRelative: true, extractAllScripts: true }.merge() # Expand input to simplify processing = if input else API.(input, **) do |result, base_iri| [:base] ||= RDF::URI(base_iri) if base_iri && [:compactToRelative] result end end # Initialize input using API.new(, context, no_default_base: true, **) do # log_debug(".flatten") {"expanded input: #{value.to_json(JSON_STATE) rescue 'malformed json'}"} # Rename blank nodes recusively. Note that this does not create new blank node identifiers where none exist, which is performed in the node map generation algorithm. @value = rename_bnodes(@value) if @options[:rename_bnodes] # Initialize node map to a JSON object consisting of a single member whose key is @default and whose value is an empty JSON object. graph_maps = { '@default' => {} } create_node_map(value, graph_maps) # If create annotations flag is set, then update each node map in graph maps with the result of calling the create annotations algorithm. if [:createAnnotations] graph_maps.each_value do |node_map| create_annotations(node_map) end end default_graph = graph_maps['@default'] graph_maps.keys.opt_sort(ordered: @options[:ordered]).each do |graph_name| next if graph_name == '@default' graph = graph_maps[graph_name] entry = default_graph[graph_name] ||= { '@id' => graph_name } nodes = entry['@graph'] ||= [] graph.keys.opt_sort(ordered: @options[:ordered]).each do |id| nodes << graph[id] unless node_reference?(graph[id]) end end default_graph.keys.opt_sort(ordered: @options[:ordered]).each do |id| flattened << default_graph[id] unless node_reference?(default_graph[id]) end if context && !flattened.empty? # 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. compacted = as_array(compact(flattened)) kwgraph = self.context.compact_iri('@graph', vocab: true) flattened = self.context .serialize(provided_context: context) .merge(kwgraph => compacted) end end flattened = serializer.call(flattened, **) if serializer block_given? ? yield(flattened) : flattened end |