Class: JSON::LD::Format

Inherits:
RDF::Format
  • Object
show all
Defined in:
lib/json/ld/format.rb

Overview

JSON-LD format specification.

Examples:

Obtaining an JSON-LD format class

RDF::Format.for(:jsonld)           #=> JSON::LD::Format
RDF::Format.for("etc/foaf.jsonld")
RDF::Format.for(:file_name         => "etc/foaf.jsonld")
RDF::Format.for(file_extension: "jsonld")
RDF::Format.for(:content_type   => "application/ld+json")

Obtaining serialization format MIME types

RDF::Format.content_types      #=> {"application/ld+json" => [JSON::LD::Format],
                                    "application/x-ld+json" => [JSON::LD::Format]}

Obtaining serialization format file extension mappings

RDF::Format.file_extensions    #=> {:jsonld => [JSON::LD::Format] }

See Also:

Class Method Summary collapse

Class Method Details

.cli_commandsHash{Symbol => Hash}

Hash of CLI commands appropriate for this format

Returns:

  • (Hash{Symbol => Hash})


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/json/ld/format.rb', line 50

def self.cli_commands
  {
    expand: {
      description: "Expand JSON-LD or parsed RDF",
      parse: false,
      help: "expand [--context <context-file>] files ...",
      lambda: ->(files, options) do
        out = options[:output] || $stdout
        out.set_encoding(Encoding::UTF_8) if RUBY_PLATFORM == "java"
        options = options.merge(expandContext: options.delete(:context)) if options.has_key?(:context)
        if options[:format] == :jsonld
          if files.empty?
            # If files are empty, either use options[:execute]
            input = options[:evaluate] ? StringIO.new(options[:evaluate]) : STDIN
            input.set_encoding(options.fetch(:encoding, Encoding::UTF_8))
            JSON::LD::API.expand(input, options.merge(validate: false)) do |expanded|
              out.puts expanded.to_json(JSON::LD::JSON_STATE)
            end
          else
            files.each do |file|
              JSON::LD::API.expand(file, options.merge(validate: false)) do |expanded|
                out.puts expanded.to_json(JSON::LD::JSON_STATE)
              end
            end
          end
        else
          # Turn RDF into JSON-LD first
          RDF::CLI.parse(files, options) do |reader|
            JSON::LD::API.fromRdf(reader) do |expanded|
              out.puts expanded.to_json(JSON::LD::JSON_STATE)
            end
          end
        end
      end
    },
    compact: {
      description: "Compact JSON-LD or parsed RDF",
      parse: false,
      help: "compact --context <context-file> files ...",
      lambda: ->(files, options) do
        raise ArgumentError, "Compacting requires a context" unless options[:context]
        out = options[:output] || $stdout
        out.set_encoding(Encoding::UTF_8) if RUBY_PLATFORM == "java"
        if options[:format] == :jsonld
          if files.empty?
            # If files are empty, either use options[:execute]
            input = options[:evaluate] ? StringIO.new(options[:evaluate]) : STDIN
            input.set_encoding(options.fetch(:encoding, Encoding::UTF_8))
            JSON::LD::API.compact(input, options[:context], options) do |compacted|
              out.puts compacted.to_json(JSON::LD::JSON_STATE)
            end
          else
            files.each do |file|
              JSON::LD::API.compact(file, options[:context], options) do |compacted|
                out.puts compacted.to_json(JSON::LD::JSON_STATE)
              end
            end
          end
        else
          # Turn RDF into JSON-LD first
          RDF::CLI.parse(files, options) do |reader|
            JSON::LD::API.fromRdf(reader) do |expanded|
              JSON::LD::API.compact(expanded, options[:context], options) do |compacted|
                out.puts compacted.to_json(JSON::LD::JSON_STATE)
              end
            end
          end
        end
      end
    },
    flatten: {
      description: "Flatten JSON-LD or parsed RDF",
      parse: false,
      help: "flatten [--context <context-file>] files ...",
      lambda: ->(files, options) do
        out = options[:output] || $stdout
        out.set_encoding(Encoding::UTF_8) if RUBY_PLATFORM == "java"
        if options[:format] == :jsonld
          if files.empty?
            # If files are empty, either use options[:execute]
            input = options[:evaluate] ? StringIO.new(options[:evaluate]) : STDIN
            input.set_encoding(options.fetch(:encoding, Encoding::UTF_8))
            JSON::LD::API.flatten(input, options[:context], options) do |flattened|
              out.puts flattened.to_json(JSON::LD::JSON_STATE)
            end
          else
            files.each do |file|
              JSON::LD::API.flatten(file, options[:context], options) do |flattened|
                out.puts flattened.to_json(JSON::LD::JSON_STATE)
              end
            end
          end
        else
          # Turn RDF into JSON-LD first
          RDF::CLI.parse(files, options) do |reader|
            JSON::LD::API.fromRdf(reader) do |expanded|
              JSON::LD::API.flatten(expanded, options[:context], options) do |flattened|
                out.puts flattened.to_json(JSON::LD::JSON_STATE)
              end
            end
          end
        end
      end
    },
    frame: {
      description: "Frame JSON-LD or parsed RDF",
      parse: false,
      help: "frame --frame <frame-file>  files ...",
      lambda: ->(files, options) do
        raise ArgumentError, "Framing requires a frame" unless options[:frame]
        out = options[:output] || $stdout
        out.set_encoding(Encoding::UTF_8) if RUBY_PLATFORM == "java"
        if options[:format] == :jsonld
          if files.empty?
            # If files are empty, either use options[:execute]
            input = options[:evaluate] ? StringIO.new(options[:evaluate]) : STDIN
            input.set_encoding(options.fetch(:encoding, Encoding::UTF_8))
            JSON::LD::API.frame(input, options[:frame], options) do |framed|
              out.puts framed.to_json(JSON::LD::JSON_STATE)
            end
          else
            files.each do |file|
              JSON::LD::API.frame(file, options[:frame], options) do |framed|
                out.puts framed.to_json(JSON::LD::JSON_STATE)
              end
            end
          end
        else
          # Turn RDF into JSON-LD first
          RDF::CLI.parse(files, options) do |reader|
            JSON::LD::API.fromRdf(reader) do |expanded|
              JSON::LD::API.frame(expanded, options[:frame], options) do |framed|
                out.puts framed.to_json(JSON::LD::JSON_STATE)
              end
            end
          end
        end
      end
    },
  }
end

.detect(sample) ⇒ Boolean

Sample detection to see if it matches JSON-LD

Use a text sample to detect the format of an input file. Sub-classes implement a matcher sufficient to detect probably format matches, including disambiguating between other similar formats.

Parameters:

  • sample (String)

    Beginning several bytes (~ 1K) of input.

Returns:

  • (Boolean)


41
42
43
44
45
# File 'lib/json/ld/format.rb', line 41

def self.detect(sample)
  !!sample.match(/\{\s*"@(id|context|type)"/m) &&
    # Exclude CSVW metadata
    !sample.include?("http://www.w3.org/ns/csvw")
end

.nameObject

Override normal format name



200
201
202
# File 'lib/json/ld/format.rb', line 200

def self.name
  "JSON-LD"
end

.to_symObject

Override normal symbol generation



194
195
196
# File 'lib/json/ld/format.rb', line 194

def self.to_sym
  :jsonld
end