json-ld 3.1.6 → 3.1.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +20 -7
- data/VERSION +1 -1
- data/bin/jsonld +13 -11
- data/lib/json/ld/api.rb +34 -17
- data/lib/json/ld/compact.rb +14 -6
- data/lib/json/ld/context.rb +63 -57
- data/lib/json/ld/expand.rb +57 -19
- data/lib/json/ld/extensions.rb +4 -4
- data/lib/json/ld/flatten.rb +137 -9
- data/lib/json/ld/format.rb +8 -1
- data/lib/json/ld/frame.rb +8 -8
- data/lib/json/ld/from_rdf.rb +14 -8
- data/lib/json/ld/html/nokogiri.rb +3 -4
- data/lib/json/ld/reader.rb +1 -0
- data/lib/json/ld/streaming_reader.rb +5 -5
- data/lib/json/ld/streaming_writer.rb +4 -4
- data/lib/json/ld/to_rdf.rb +2 -2
- data/lib/json/ld/utils.rb +13 -13
- data/lib/json/ld/writer.rb +2 -2
- data/lib/json/ld.rb +3 -1
- data/spec/api_spec.rb +1 -1
- data/spec/compact_spec.rb +277 -3
- data/spec/context_spec.rb +8 -3
- data/spec/expand_spec.rb +385 -2
- data/spec/flatten_spec.rb +517 -1
- data/spec/frame_spec.rb +93 -3
- data/spec/from_rdf_spec.rb +1 -1
- data/spec/rdfstar_spec.rb +25 -0
- data/spec/spec_helper.rb +40 -2
- data/spec/suite_flatten_spec.rb +4 -0
- data/spec/suite_frame_spec.rb +7 -0
- data/spec/suite_helper.rb +13 -7
- data/spec/suite_to_rdf_spec.rb +1 -1
- data/spec/to_rdf_spec.rb +4 -4
- data/spec/writer_spec.rb +193 -0
- metadata +6 -4
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA256:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: 0324a9e3b617b6dabe52c3f36c81e4b7fc4b78eaab9450510f45a760933064ba
         | 
| 4 | 
            +
              data.tar.gz: 7cf759dd5940a052969477e50c50bb0dc31d7cac38a5ccc8e1bdc046e3994a43
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: 9d427823af59053d28e4999984f698cde37229fb9a3336a22f06517bcc65fbf80527a535b50dd28eaeca1e5d67e38d0d140dbace8854b9a62944f8ba149ea61c
         | 
| 7 | 
            +
              data.tar.gz: 83ba9860d9b7fe72924c9469a26bd5337a051003396b02fdcf5ae9e4b2bbd13940c37d7b4d95c16b427248fda64fa799551c3dcf5c767436e804ea89fba03b4f
         | 
    
        data/README.md
    CHANGED
    
    | @@ -4,7 +4,7 @@ | |
| 4 4 |  | 
| 5 5 | 
             
            [](https://rubygems.org/gems/json-ld)
         | 
| 6 6 | 
             
            [](https://github.com/ruby-rdf/json-ld/actions?query=workflow%3ACI)
         | 
| 7 | 
            -
            [](https://coveralls.io/github/ruby-rdf/json-ld)
         | 
| 7 | 
            +
            [](https://coveralls.io/github/ruby-rdf/json-ld?branch=develop)
         | 
| 8 8 | 
             
            [](https://gitter.im/gitterHQ/gitter)
         | 
| 9 9 |  | 
| 10 10 | 
             
            ## Features
         | 
| @@ -14,10 +14,10 @@ JSON::LD parses and serializes [JSON-LD][] into [RDF][] and implements expansion | |
| 14 14 | 
             
            JSON::LD can now be used to create a _context_ from an RDFS/OWL definition, and optionally include a JSON-LD representation of the ontology itself. This is currently accessed through the `script/gen_context` script.
         | 
| 15 15 |  | 
| 16 16 | 
             
            * If the [jsonlint][] gem is installed, it will be used when validating an input document.
         | 
| 17 | 
            -
            * If available, uses [Nokogiri][]  | 
| 18 | 
            -
            * Provisional support for [JSON-LD | 
| 17 | 
            +
            * If available, uses [Nokogiri][] for parsing HTML, falls back to REXML otherwise.
         | 
| 18 | 
            +
            * Provisional support for [JSON-LD-star][JSON-LD-star].
         | 
| 19 19 |  | 
| 20 | 
            -
            [Implementation Report]( | 
| 20 | 
            +
            [Implementation Report](https://ruby-rdf.github.io/json-ld/etc/earl.html)
         | 
| 21 21 |  | 
| 22 22 | 
             
            Install with `gem install json-ld`
         | 
| 23 23 |  | 
| @@ -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 | 
| 40 | 
            +
            ### JSON-LD-star (RDFStar)
         | 
| 41 41 |  | 
| 42 | 
            -
            The {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 | 
| 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 |  | 
| @@ -55,6 +55,19 @@ In JSON-LD, with the `rdfstar` option set, the value of `@id`, in addition to an | |
| 55 55 | 
             
                  "ex:certainty": 0.9
         | 
| 56 56 | 
             
                }
         | 
| 57 57 |  | 
| 58 | 
            +
            Additionally, the `@annotation` property (or alias) may be used on a node object or value object to annotate the statement for which the associated node is the object of a triple.
         | 
| 59 | 
            +
             | 
| 60 | 
            +
                {
         | 
| 61 | 
            +
                  "@context": {"foaf": "http://xmlns.com/foaf/0.1/"},
         | 
| 62 | 
            +
                  "@id": "bob",
         | 
| 63 | 
            +
                  "foaf:age" 23,
         | 
| 64 | 
            +
                  "@annotation": {
         | 
| 65 | 
            +
                    "ex:certainty": 0.9
         | 
| 66 | 
            +
                  }
         | 
| 67 | 
            +
                }
         | 
| 68 | 
            +
             | 
| 69 | 
            +
            In the first case, the embedded node is not asserted, and only appears as the subject of a triple. In the second case, the triple is asserted and used as the subject in another statement which annotates it.
         | 
| 70 | 
            +
             | 
| 58 71 | 
             
            **Note: This feature is subject to change or elimination as the standards process progresses.**
         | 
| 59 72 |  | 
| 60 73 | 
             
            #### Serializing a Graph containing embedded statements
         | 
| @@ -623,7 +636,7 @@ see <https://unlicense.org/> or the accompanying {file:UNLICENSE} file. | |
| 623 636 | 
             
            [YARD-GS]:          https://rubydoc.info/docs/yard/file/docs/GettingStarted.md
         | 
| 624 637 | 
             
            [PDD]:              https://unlicense.org/#unlicensing-contributions
         | 
| 625 638 | 
             
            [RDF.rb]:           https://rubygems.org/gems/rdf
         | 
| 626 | 
            -
            [JSON-LD | 
| 639 | 
            +
            [JSON-LD-star]:             https://json-ld.github.io/json-ld-star/
         | 
| 627 640 | 
             
            [Rack::LinkedData]: https://rubygems.org/gems/rack-linkeddata
         | 
| 628 641 | 
             
            [Backports]:        https://rubygems.org/gems/backports
         | 
| 629 642 | 
             
            [JSON-LD]:          https://www.w3.org/TR/json-ld11/ "JSON-LD 1.1"
         | 
    
        data/VERSION
    CHANGED
    
    | @@ -1 +1 @@ | |
| 1 | 
            -
            3.1. | 
| 1 | 
            +
            3.1.10
         | 
    
        data/bin/jsonld
    CHANGED
    
    | @@ -24,36 +24,36 @@ def run(input, options, parser_options) | |
| 24 24 |  | 
| 25 25 | 
             
              start = Time.new
         | 
| 26 26 | 
             
              if options[:expand]
         | 
| 27 | 
            -
                parser_options = parser_options.merge(expandContext: parser_options.delete(:context)) if parser_options. | 
| 27 | 
            +
                parser_options = parser_options.merge(expandContext: parser_options.delete(:context)) if parser_options.key?(:context)
         | 
| 28 28 | 
             
                input = JSON::LD::API.fromRdf(reader) if reader
         | 
| 29 | 
            -
                output = JSON::LD::API.expand(input, parser_options)
         | 
| 29 | 
            +
                output = JSON::LD::API.expand(input, **parser_options)
         | 
| 30 30 | 
             
                secs = Time.new - start
         | 
| 31 31 | 
             
                options[:output].puts output.to_json(JSON::LD::JSON_STATE)
         | 
| 32 32 | 
             
                STDERR.puts "Expanded in #{secs} seconds." unless options[:quiet]
         | 
| 33 33 | 
             
              elsif options[:compact]
         | 
| 34 34 | 
             
                input = JSON::LD::API.fromRdf(reader) if reader
         | 
| 35 | 
            -
                output = JSON::LD::API.compact(input, parser_options[:context], parser_options)
         | 
| 35 | 
            +
                output = JSON::LD::API.compact(input, parser_options[:context], **parser_options)
         | 
| 36 36 | 
             
                secs = Time.new - start
         | 
| 37 37 | 
             
                options[:output].puts output.to_json(JSON::LD::JSON_STATE)
         | 
| 38 38 | 
             
                STDERR.puts "Compacted in #{secs} seconds." unless options[:quiet]
         | 
| 39 39 | 
             
              elsif options[:flatten]
         | 
| 40 40 | 
             
                input = JSON::LD::API.fromRdf(reader) if reader
         | 
| 41 | 
            -
                output = JSON::LD::API.flatten(input, parser_options[:context], parser_options)
         | 
| 41 | 
            +
                output = JSON::LD::API.flatten(input, parser_options[:context], **parser_options)
         | 
| 42 42 | 
             
                secs = Time.new - start
         | 
| 43 43 | 
             
                options[:output].puts output.to_json(JSON::LD::JSON_STATE)
         | 
| 44 44 | 
             
                STDERR.puts "Flattened in #{secs} seconds." unless options[:quiet]
         | 
| 45 45 | 
             
              elsif options[:frame]
         | 
| 46 46 | 
             
                input = JSON::LD::API.fromRdf(reader) if reader
         | 
| 47 | 
            -
                output = JSON::LD::API.frame(input, parser_options[:frame], parser_options)
         | 
| 47 | 
            +
                output = JSON::LD::API.frame(input, parser_options[:frame], **parser_options)
         | 
| 48 48 | 
             
                secs = Time.new - start
         | 
| 49 49 | 
             
                options[:output].puts output.to_json(JSON::LD::JSON_STATE)
         | 
| 50 50 | 
             
                STDERR.puts "Framed in #{secs} seconds." unless options[:quiet]
         | 
| 51 51 | 
             
              else
         | 
| 52 | 
            -
                parser_options = parser_options.merge(expandContext: parser_options.delete(:context)) if parser_options. | 
| 52 | 
            +
                parser_options = parser_options.merge(expandContext: parser_options.delete(:context)) if parser_options.key?(:context)
         | 
| 53 53 | 
             
                parser_options[:standard_prefixes] = true
         | 
| 54 | 
            -
                reader ||= JSON::LD::Reader.new(input, parser_options)
         | 
| 54 | 
            +
                reader ||= JSON::LD::Reader.new(input, **parser_options)
         | 
| 55 55 | 
             
                num = 0
         | 
| 56 | 
            -
                RDF::Writer.for(options[:output_format]).new(options[:output], parser_options) do |w|
         | 
| 56 | 
            +
                RDF::Writer.for(options[:output_format]).new(options[:output], **parser_options) do |w|
         | 
| 57 57 | 
             
                  reader.each do |statement|
         | 
| 58 58 | 
             
                    num += 1
         | 
| 59 59 | 
             
                    w << statement
         | 
| @@ -113,6 +113,7 @@ OPT_ARGS = [ | |
| 113 113 | 
             
              ["--processingMode",GetoptLong::REQUIRED_ARGUMENT,"Set processing mode, defaults to json-ld-1.1"],
         | 
| 114 114 | 
             
              ["--quiet",         GetoptLong::NO_ARGUMENT,      "Supress most output other than progress indicators"],
         | 
| 115 115 | 
             
              ["--rename_bnodes", GetoptLong::OPTIONAL_ARGUMENT,"Rename bnodes as part of expansion, or keep them the same"],
         | 
| 116 | 
            +
              ["--rdfstar",       GetoptLong::NO_ARGUMENT,      "Parse JSON-LD-star"],
         | 
| 116 117 | 
             
              ["--requireAll",    GetoptLong::OPTIONAL_ARGUMENT,"Rename bnodes as part of expansion, or keep them the same"],
         | 
| 117 118 | 
             
              ["--stream",        GetoptLong::NO_ARGUMENT,      "Use Streaming reader/writer"],
         | 
| 118 119 | 
             
              ["--unique_bnodes", GetoptLong::OPTIONAL_ARGUMENT,"Use unique bnode identifiers"],
         | 
| @@ -140,7 +141,7 @@ opts = GetoptLong.new(*OPT_ARGS.map {|o| o[0..-2]}) | |
| 140 141 | 
             
            opts.each do |opt, arg|
         | 
| 141 142 | 
             
              case opt
         | 
| 142 143 | 
             
              when '--debug'          then logger.level = Logger::DEBUG
         | 
| 143 | 
            -
              when '--compact'        then  | 
| 144 | 
            +
              when '--compact'        then options[:compact] = true
         | 
| 144 145 | 
             
              when "--compactArrays"  then parser_options[:compactArrays] = (arg || 'true') == 'true'
         | 
| 145 146 | 
             
              when '--context'        then parser_options[:context] = RDF::URI(arg).absolute? ? arg : File.open(arg)
         | 
| 146 147 | 
             
              when '--evaluate'       then input = arg
         | 
| @@ -158,6 +159,7 @@ opts.each do |opt, arg| | |
| 158 159 | 
             
              when '--quiet'
         | 
| 159 160 | 
             
                options[:quiet] = true
         | 
| 160 161 | 
             
                logger.level = Logger::FATAL
         | 
| 162 | 
            +
              when "--rdfstar"        then parser_options[:rdfstar] = true
         | 
| 161 163 | 
             
              when "--rename_bnodes"  then parser_options[:rename_bnodes] = (arg || 'true') == 'true'
         | 
| 162 164 | 
             
              when "--requireAll"     then parser_options[:requireAll] = (arg || 'true') == 'true'
         | 
| 163 165 | 
             
              when '--stream'         then parser_options[:stream] = true
         | 
| @@ -181,7 +183,7 @@ opts.each do |opt, arg| | |
| 181 183 | 
             
            end
         | 
| 182 184 |  | 
| 183 185 | 
             
            # Hack
         | 
| 184 | 
            -
            if !(options.keys &  | 
| 186 | 
            +
            if !(options.keys & %i{expand compact flatten frame}).empty? &&
         | 
| 185 187 | 
             
               (parser_options[:stream] || options[:output_format] != :jsonld)
         | 
| 186 188 | 
             
               STDERR.puts "Incompatible options"
         | 
| 187 189 | 
             
               exit(1)
         | 
| @@ -193,7 +195,7 @@ if ARGV.empty? | |
| 193 195 | 
             
            else
         | 
| 194 196 | 
             
              ARGV.each do |file|
         | 
| 195 197 | 
             
                # Call with opened files
         | 
| 196 | 
            -
                RDF::Util::File.open_file(file, options) {|f| run(f, options, parser_options)}
         | 
| 198 | 
            +
                RDF::Util::File.open_file(file, **options) {|f| run(f, options, parser_options)}
         | 
| 197 199 | 
             
              end
         | 
| 198 200 | 
             
            end
         | 
| 199 201 | 
             
            puts
         | 
    
        data/lib/json/ld/api.rb
    CHANGED
    
    | @@ -90,7 +90,7 @@ module JSON::LD | |
| 90 90 | 
             
                #   Processing mode, json-ld-1.0 or json-ld-1.1.
         | 
| 91 91 | 
             
                #   If `processingMode` is not specified, a mode of `json-ld-1.0` or `json-ld-1.1` is set, the context used for `expansion` or `compaction`.
         | 
| 92 92 | 
             
                # @option options [Boolean] rdfstar      (false)
         | 
| 93 | 
            -
                #   support parsing JSON-LD | 
| 93 | 
            +
                #   support parsing JSON-LD-star statement resources.
         | 
| 94 94 | 
             
                # @option options [Boolean] :rename_bnodes (true)
         | 
| 95 95 | 
             
                #   Rename bnodes as part of expansion, or keep them the same.
         | 
| 96 96 | 
             
                # @option options [Boolean]  :unique_bnodes   (false)
         | 
| @@ -102,13 +102,15 @@ module JSON::LD | |
| 102 102 | 
             
                # @yield [api]
         | 
| 103 103 | 
             
                # @yieldparam [API]
         | 
| 104 104 | 
             
                # @raise [JsonLdError]
         | 
| 105 | 
            -
                def initialize(input, context,  | 
| 105 | 
            +
                def initialize(input, context, **options, &block)
         | 
| 106 106 | 
             
                  @options = {
         | 
| 107 107 | 
             
                    compactArrays:      true,
         | 
| 108 108 | 
             
                    ordered:            false,
         | 
| 109 109 | 
             
                    extractAllScripts:  false,
         | 
| 110 | 
            +
                    rename_bnodes:      true,
         | 
| 111 | 
            +
                    unique_bnodes:      false,
         | 
| 110 112 | 
             
                  }.merge(options)
         | 
| 111 | 
            -
                  @namer = unique_bnodes ? BlankNodeUniqer.new : (rename_bnodes ? BlankNodeNamer.new("b") : BlankNodeMapper.new)
         | 
| 113 | 
            +
                  @namer = @options[:unique_bnodes] ? BlankNodeUniqer.new : (@options[:rename_bnodes] ? BlankNodeNamer.new("b") : BlankNodeMapper.new)
         | 
| 112 114 |  | 
| 113 115 | 
             
                  @options[:base] = RDF::URI(@options[:base]) if @options[:base] && !@options[:base].is_a?(RDF::URI)
         | 
| 114 116 | 
             
                  # For context via Link header
         | 
| @@ -202,9 +204,9 @@ module JSON::LD | |
| 202 204 | 
             
                #   The JSON-LD object to copy and perform the compaction upon.
         | 
| 203 205 | 
             
                # @param [String, #read, Hash, Array, JSON::LD::Context] context
         | 
| 204 206 | 
             
                #   The base context to use when compacting the input.
         | 
| 207 | 
            +
                # @param [Boolean] expanded (false) Input is already expanded
         | 
| 205 208 | 
             
                # @param  [Hash{Symbol => Object}] options
         | 
| 206 209 | 
             
                # @option options (see #initialize)
         | 
| 207 | 
            -
                # @option options [Boolean] :expanded Input is already expanded
         | 
| 208 210 | 
             
                # @yield jsonld
         | 
| 209 211 | 
             
                # @yieldparam [Hash] jsonld
         | 
| 210 212 | 
             
                #   The compacted JSON-LD document
         | 
| @@ -248,9 +250,11 @@ module JSON::LD | |
| 248 250 | 
             
                #   The JSON-LD object or array of JSON-LD objects to flatten or an IRI referencing the JSON-LD document to flatten.
         | 
| 249 251 | 
             
                # @param [String, #read, Hash, Array, JSON::LD::EvaluationContext] context
         | 
| 250 252 | 
             
                #   An optional external context to use additionally to the context embedded in input when expanding the input.
         | 
| 253 | 
            +
                # @param [Boolean] expanded (false) Input is already expanded
         | 
| 251 254 | 
             
                # @param  [Hash{Symbol => Object}] options
         | 
| 252 255 | 
             
                # @option options (see #initialize)
         | 
| 253 | 
            -
                # @option options [Boolean] : | 
| 256 | 
            +
                # @option options [Boolean] :createAnnotations
         | 
| 257 | 
            +
                #   Unfold embedded nodes which can be represented using `@annotation`.
         | 
| 254 258 | 
             
                # @yield jsonld
         | 
| 255 259 | 
             
                # @yieldparam [Hash] jsonld
         | 
| 256 260 | 
             
                #   The flattened JSON-LD document
         | 
| @@ -275,10 +279,20 @@ module JSON::LD | |
| 275 279 | 
             
                  API.new(expanded_input, context, no_default_base: true, **options) do
         | 
| 276 280 | 
             
                    log_debug(".flatten") {"expanded input: #{value.to_json(JSON_STATE) rescue 'malformed json'}"}
         | 
| 277 281 |  | 
| 282 | 
            +
                    # 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.
         | 
| 283 | 
            +
                    @value = rename_bnodes(@value) if @options[:rename_bnodes]
         | 
| 284 | 
            +
             | 
| 278 285 | 
             
                    # Initialize node map to a JSON object consisting of a single member whose key is @default and whose value is an empty JSON object.
         | 
| 279 286 | 
             
                    graph_maps = {'@default' => {}}
         | 
| 280 287 | 
             
                    create_node_map(value, graph_maps)
         | 
| 281 288 |  | 
| 289 | 
            +
                    # If create annotations flag is set, then update each node map in graph maps with the result of calling the create annotations algorithm.
         | 
| 290 | 
            +
                    if options[:createAnnotations]
         | 
| 291 | 
            +
                      graph_maps.values.each do |node_map|
         | 
| 292 | 
            +
                        create_annotations(node_map)
         | 
| 293 | 
            +
                      end
         | 
| 294 | 
            +
                    end
         | 
| 295 | 
            +
             | 
| 282 296 | 
             
                    default_graph = graph_maps['@default']
         | 
| 283 297 | 
             
                    graph_maps.keys.opt_sort(ordered: @options[:ordered]).each do |graph_name|
         | 
| 284 298 | 
             
                      next if graph_name == '@default'
         | 
| @@ -297,7 +311,7 @@ module JSON::LD | |
| 297 311 | 
             
                    if context && !flattened.empty?
         | 
| 298 312 | 
             
                      # 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.
         | 
| 299 313 | 
             
                      compacted = as_array(compact(flattened))
         | 
| 300 | 
            -
                      kwgraph = self.context.compact_iri('@graph')
         | 
| 314 | 
            +
                      kwgraph = self.context.compact_iri('@graph', vocab: true)
         | 
| 301 315 | 
             
                      flattened = self.context.
         | 
| 302 316 | 
             
                        serialize(provided_context: context).
         | 
| 303 317 | 
             
                        merge(kwgraph => compacted)
         | 
| @@ -316,6 +330,7 @@ module JSON::LD | |
| 316 330 | 
             
                #   The JSON-LD object to copy and perform the framing on.
         | 
| 317 331 | 
             
                # @param [String, #read, Hash, Array] frame
         | 
| 318 332 | 
             
                #   The frame to use when re-arranging the data.
         | 
| 333 | 
            +
                # @param [Boolean] expanded (false) Input is already expanded
         | 
| 319 334 | 
             
                # @option options (see #initialize)
         | 
| 320 335 | 
             
                # @option options ['@always', '@link', '@once', '@never'] :embed ('@once')
         | 
| 321 336 | 
             
                #   a flag specifying that objects should be directly embedded in the output, instead of being referred to by their IRI.
         | 
| @@ -325,7 +340,6 @@ module JSON::LD | |
| 325 340 | 
             
                #   A flag specifying that all properties present in the input frame must either have a default value or be present in the JSON-LD input for the frame to match.
         | 
| 326 341 | 
             
                # @option options [Boolean] :omitDefault (false)
         | 
| 327 342 | 
             
                #   a flag specifying that properties that are missing from the JSON-LD input should be omitted from the output.
         | 
| 328 | 
            -
                # @option options [Boolean] :expanded Input is already expanded
         | 
| 329 343 | 
             
                # @option options [Boolean] :pruneBlankNodeIdentifiers (true) removes blank node identifiers that are only used once.
         | 
| 330 344 | 
             
                # @option options [Boolean] :omitGraph does not use `@graph` at top level unless necessary to describe multiple objects, defaults to `true` if processingMode is 1.1, otherwise `false`.
         | 
| 331 345 | 
             
                # @yield jsonld
         | 
| @@ -391,10 +405,13 @@ module JSON::LD | |
| 391 405 | 
             
                    end
         | 
| 392 406 |  | 
| 393 407 | 
             
                    # Set omitGraph option, if not present, based on processingMode
         | 
| 394 | 
            -
                    unless options. | 
| 408 | 
            +
                    unless options.key?(:omitGraph)
         | 
| 395 409 | 
             
                      options[:omitGraph] = context.processingMode('json-ld-1.1')
         | 
| 396 410 | 
             
                    end
         | 
| 397 411 |  | 
| 412 | 
            +
                    # 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.
         | 
| 413 | 
            +
                    @value = rename_bnodes(@value)
         | 
| 414 | 
            +
             | 
| 398 415 | 
             
                    # Get framing nodes from expanded input, replacing Blank Node identifiers as necessary
         | 
| 399 416 | 
             
                    create_node_map(value, framing_state[:graphMap], active_graph: '@default')
         | 
| 400 417 |  | 
| @@ -415,7 +432,7 @@ module JSON::LD | |
| 415 432 | 
             
                    frame(framing_state, framing_state[:subjects].keys.opt_sort(ordered: @options[:ordered]), (expanded_frame.first || {}), parent: result, **options)
         | 
| 416 433 |  | 
| 417 434 | 
             
                    # Default to based on processinMode
         | 
| 418 | 
            -
                    if !options. | 
| 435 | 
            +
                    if !options.key?(:pruneBlankNodeIdentifiers)
         | 
| 419 436 | 
             
                      options[:pruneBlankNodeIdentifiers] = context.processingMode('json-ld-1.1')
         | 
| 420 437 | 
             
                    end
         | 
| 421 438 |  | 
| @@ -440,7 +457,7 @@ module JSON::LD | |
| 440 457 | 
             
                    result = if !compacted.is_a?(Array)
         | 
| 441 458 | 
             
                      compacted
         | 
| 442 459 | 
             
                    else
         | 
| 443 | 
            -
                      kwgraph = context.compact_iri('@graph')
         | 
| 460 | 
            +
                      kwgraph = context.compact_iri('@graph', vocab: true)
         | 
| 444 461 | 
             
                      {kwgraph => compacted}
         | 
| 445 462 | 
             
                    end
         | 
| 446 463 | 
             
                    # Only add context if one was provided
         | 
| @@ -458,10 +475,10 @@ module JSON::LD | |
| 458 475 | 
             
                #
         | 
| 459 476 | 
             
                # @param [String, #read, Hash, Array] input
         | 
| 460 477 | 
             
                #   The JSON-LD object to process when outputting statements.
         | 
| 478 | 
            +
                # @param [Boolean] expanded (false) Input is already expanded
         | 
| 461 479 | 
             
                # @option options (see #initialize)
         | 
| 462 480 | 
             
                # @option options [Boolean] :produceGeneralizedRdf (false)
         | 
| 463 481 | 
             
                #   If true, output will include statements having blank node predicates, otherwise they are dropped.
         | 
| 464 | 
            -
                # @option options [Boolean] :expanded Input is already expanded
         | 
| 465 482 | 
             
                # @raise [JsonLdError]
         | 
| 466 483 | 
             
                # @yield statement
         | 
| 467 484 | 
             
                # @yieldparam [RDF::Statement] statement
         | 
| @@ -470,7 +487,7 @@ module JSON::LD | |
| 470 487 | 
             
                  unless block_given?
         | 
| 471 488 | 
             
                    results = []
         | 
| 472 489 | 
             
                    results.extend(RDF::Enumerable)
         | 
| 473 | 
            -
                    self.toRdf(input, **options) do |stmt|
         | 
| 490 | 
            +
                    self.toRdf(input, expanded: expanded, **options) do |stmt|
         | 
| 474 491 | 
             
                      results << stmt
         | 
| 475 492 | 
             
                    end
         | 
| 476 493 | 
             
                    return results
         | 
| @@ -480,16 +497,16 @@ module JSON::LD | |
| 480 497 | 
             
                    extractAllScripts:  true,
         | 
| 481 498 | 
             
                  }.merge(options)
         | 
| 482 499 |  | 
| 483 | 
            -
                  #  | 
| 484 | 
            -
                   | 
| 500 | 
            +
                  # Flatten input to simplify processing
         | 
| 501 | 
            +
                  flattened_input = API.flatten(input, nil, expanded: expanded, ordered: false, **options)
         | 
| 485 502 |  | 
| 486 | 
            -
                  API.new( | 
| 503 | 
            +
                  API.new(flattened_input, nil, **options) do
         | 
| 487 504 | 
             
                    # 1) Perform the Expansion Algorithm on the JSON-LD input.
         | 
| 488 505 | 
             
                    #    This removes any existing context to allow the given context to be cleanly applied.
         | 
| 489 | 
            -
                    log_debug(".toRdf") {" | 
| 506 | 
            +
                    log_debug(".toRdf") {"flattened input: #{flattened_input.to_json(JSON_STATE) rescue 'malformed json'}"}
         | 
| 490 507 |  | 
| 491 508 | 
             
                    # Recurse through input
         | 
| 492 | 
            -
                     | 
| 509 | 
            +
                    flattened_input.each do |node|
         | 
| 493 510 | 
             
                      item_to_rdf(node) do |statement|
         | 
| 494 511 | 
             
                        next if statement.predicate.node? && !options[:produceGeneralizedRdf]
         | 
| 495 512 |  | 
    
        data/lib/json/ld/compact.rb
    CHANGED
    
    | @@ -64,7 +64,7 @@ module JSON::LD | |
| 64 64 | 
             
                      log_debug("prop-scoped", depth: log_depth.to_i) {"context: #{self.context.inspect}"}
         | 
| 65 65 | 
             
                    end
         | 
| 66 66 |  | 
| 67 | 
            -
                    if element.key?('@id') || element.key?('@value')
         | 
| 67 | 
            +
                    if (element.key?('@id') || element.key?('@value')) && !element.key?('@annotation')
         | 
| 68 68 | 
             
                      result = context.compact_value(property, element, base: @options[:base])
         | 
| 69 69 | 
             
                      if !result.is_a?(Hash) || context.coerce(property) == '@json'
         | 
| 70 70 | 
             
                        log_debug("", depth: log_depth.to_i) {"=> scalar result: #{result.inspect}"}
         | 
| @@ -98,8 +98,15 @@ module JSON::LD | |
| 98 98 | 
             
                      log_debug("", depth: log_depth.to_i) {"#{expanded_property}: #{expanded_value.inspect}"}
         | 
| 99 99 |  | 
| 100 100 | 
             
                      if expanded_property == '@id'
         | 
| 101 | 
            -
                        compacted_value =  | 
| 102 | 
            -
                           | 
| 101 | 
            +
                        compacted_value = as_array(expanded_value).map do |expanded_id|
         | 
| 102 | 
            +
                          if node?(expanded_id) && @options[:rdfstar]
         | 
| 103 | 
            +
                            # This can only really happen for valid RDF-star
         | 
| 104 | 
            +
                            compact(expanded_id, base: base,
         | 
| 105 | 
            +
                                    property: '@id',
         | 
| 106 | 
            +
                                    log_depth: log_depth.to_i + 1)
         | 
| 107 | 
            +
                          else
         | 
| 108 | 
            +
                            context.compact_iri(expanded_id, base: @options[:base])
         | 
| 109 | 
            +
                          end
         | 
| 103 110 | 
             
                        end
         | 
| 104 111 |  | 
| 105 112 | 
             
                        kw_alias = context.compact_iri('@id', vocab: true)
         | 
| @@ -138,7 +145,7 @@ module JSON::LD | |
| 138 145 | 
             
                        end
         | 
| 139 146 |  | 
| 140 147 | 
             
                        unless compacted_value.empty?
         | 
| 141 | 
            -
                          al = context.compact_iri('@reverse')
         | 
| 148 | 
            +
                          al = context.compact_iri('@reverse', vocab: true)
         | 
| 142 149 | 
             
                          log_debug("", depth: log_depth.to_i) {"remainder: #{al} => #{compacted_value.inspect}"}
         | 
| 143 150 | 
             
                          result[al] = compacted_value
         | 
| 144 151 | 
             
                        end
         | 
| @@ -224,7 +231,7 @@ module JSON::LD | |
| 224 231 | 
             
                          unless container.include?('@list')
         | 
| 225 232 | 
             
                            al = context.compact_iri('@list', vocab: true)
         | 
| 226 233 | 
             
                            compacted_item = {al => compacted_item}
         | 
| 227 | 
            -
                            if expanded_item. | 
| 234 | 
            +
                            if expanded_item.key?('@index')
         | 
| 228 235 | 
             
                              key = context.compact_iri('@index', vocab: true)
         | 
| 229 236 | 
             
                              compacted_item[key] = expanded_item['@index']
         | 
| 230 237 | 
             
                            end
         | 
| @@ -269,7 +276,7 @@ module JSON::LD | |
| 269 276 | 
             
                              al = context.compact_iri('@id', vocab: true)
         | 
| 270 277 | 
             
                              compacted_item[al] = context.compact_iri(expanded_item['@id'], vocab: false)
         | 
| 271 278 | 
             
                            end
         | 
| 272 | 
            -
                            if expanded_item. | 
| 279 | 
            +
                            if expanded_item.key?('@index')
         | 
| 273 280 | 
             
                              key = context.compact_iri('@index', vocab: true)
         | 
| 274 281 | 
             
                              compacted_item[key] = expanded_item['@index']
         | 
| 275 282 | 
             
                            end
         | 
| @@ -290,6 +297,7 @@ module JSON::LD | |
| 290 297 | 
             
                            if index_key == '@index'
         | 
| 291 298 | 
             
                              map_key = expanded_item['@index']
         | 
| 292 299 | 
             
                            else
         | 
| 300 | 
            +
                              index_key = context.expand_iri(index_key, vocab: true)
         | 
| 293 301 | 
             
                              container_key = context.compact_iri(index_key, vocab: true)
         | 
| 294 302 | 
             
                              map_key, *others = Array(compacted_item[container_key])
         | 
| 295 303 | 
             
                              if map_key.is_a?(String)
         |