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
Copy file name to clipboardExpand all lines: README.md
+60-46
Original file line number
Diff line number
Diff line change
@@ -25,21 +25,25 @@ This gem implements an optimized streaming reader used for generating RDF from l
25
25
* Keys in JSON objects must be ordered with any of `@context`, and/or `@type` coming before any other keys, in that order. This includes aliases of those keys. It is strongly encouraged that `@id` be present, and come immediately after.
26
26
* JSON-LD documents can be signaled or requested in [streaming document form](https://w3c.github.io/json-ld-streaming/#dfn-streaming-document-form). The profile URI identifying the [streaming document form](https://w3c.github.io/json-ld-streaming/#dfn-streaming-document-form) is `http://www.w3.org/ns/json-ld#streaming`.
27
27
28
-
### MultiJson parser
29
-
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.
30
-
31
-
### JSON-LD Streaming Profile
32
-
This gem implements an optimized streaming writer used for generating JSON-LD from large repositories. Such documents result in the JSON-LD Streaming Profile:
28
+
This gem also implements an optimized streaming writer used for generating JSON-LD from large repositories. Such documents result in the JSON-LD Streaming Profile:
33
29
34
30
* Each statement written as a separate node in expanded/flattened form.
35
-
* RDF Lists are written as separate nodes using `rdf:first` and `rdf:rest` properties.
31
+
*`RDF List`s are written as separate nodes using `rdf:first` and `rdf:rest` properties.
32
+
33
+
The order of triples retrieved from the `RDF::Enumerable` dataset determines the way that JSON-LD node objects are written; for best results, statements should be ordered by _graph name_, _subect_, _predicate_ and _object_.
34
+
35
+
### MultiJson parser
36
+
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.
In some cases, the built-in document loader {JSON::LD::API.documentLoader} is inadequate; for example, when using `http://schema.org` as a remote context, it will be re-loaded every time (however, see [json-ld-preloaded](https://rubygems.org/gems/json-ld-preloaded)).
236
241
237
242
All entries into the {JSON::LD::API} accept a `:documentLoader` option, which can be used to provide an alternative method to use when loading remote documents. For example:
In many cases, for small documents, processing time can be dominated by loading and parsing remote contexts. In particular, a small schema.org example may need to download a large context and turn it into an internal representation, before the actual document can be expanded for processing. Using {JSON::LD::Context.add_preloaded}, an implementation can perform this loading up-front, and make it available to the processor.
On lookup, URIs with an `https` prefix are normalized to `http`.
260
270
261
271
A context may be serialized to Ruby to speed this process using `Context#to_rb`. When loaded, this generated file will add entries to the {JSON::LD::Context::PRELOADED}.
262
272
263
273
## RDF Reader and Writer
264
274
{JSON::LD} also acts as a normal RDF reader and writer, using the standard RDF.rb reader/writer interfaces:
`RDF::GRAPH#dump` can also take a `:context` option to use a separately defined context
270
282
271
283
As JSON-LD may come from many different sources, included as an embedded script tag within an HTML document, the RDF Reader will strip input before the leading `{` or `[` and after the trailing `}` or `]`.
@@ -275,7 +287,7 @@ This implementation is being used as a test-bed for features planned for an upco
275
287
276
288
### Scoped Contexts
277
289
A term definition can include `@context`, which is applied to values of that object. This is also used when compacting. Taken together, this allows framing to effectively include context definitions more deeply within the framed structure.
278
-
```ruby
290
+
279
291
{
280
292
"@context": {
281
293
"ex": "http://example.com/",
@@ -290,10 +302,10 @@ A term definition can include `@context`, which is applied to values of that obj
290
302
},
291
303
"foo": "Bar"
292
304
}
293
-
```
305
+
294
306
### @id and @type maps
295
307
The value of `@container` in a term definition can include `@id` or `@type`, in addition to `@set`, `@list`, `@language`, and `@index`. This allows value indexing based on either the `@id` or `@type` of associated objects.
296
-
```ruby
308
+
297
309
{
298
310
"@context": {
299
311
"@vocab": "http://example/",
@@ -304,10 +316,10 @@ The value of `@container` in a term definition can include `@id` or `@type`, in
304
316
"_:bar": {"label": "Object with @id _:bar"}
305
317
}
306
318
}
307
-
```
319
+
308
320
### @graph containers and maps
309
321
A term can have `@container` set to include `@graph` optionally including `@id` or `@index` and `@set`. In the first form, with `@container` set to `@graph`, the value of a property is treated as a _simple graph object_, meaning that values treated as if they were contained in an object with `@graph`, creating _named graph_ with an anonymous name.
310
-
```ruby
322
+
311
323
{
312
324
"@context": {
313
325
"@vocab": "http://example.org/",
@@ -317,28 +329,28 @@ A term can have `@container` set to include `@graph` optionally including `@id`
317
329
"value": "x"
318
330
}
319
331
}
320
-
```
332
+
321
333
which expands to the following:
322
-
```ruby
334
+
323
335
[{
324
336
"http://example.org/input": [{
325
337
"@graph": [{
326
338
"http://example.org/value": [{"@value": "x"}]
327
339
}]
328
340
}]
329
341
}]
330
-
```
342
+
331
343
Compaction reverses this process, optionally ensuring that a single value is contained within an array of `@container` also includes `@set`:
332
-
```ruby
344
+
333
345
{
334
346
"@context": {
335
347
"@vocab": "http://example.org/",
336
348
"input": {"@container": ["@graph", "@set"]}
337
349
}
338
350
}
339
-
```
351
+
340
352
A graph map uses the map form already existing for `@index`, `@language`, `@type`, and `@id` where the index is either an index value or an id.
341
-
```ruby
353
+
342
354
{
343
355
"@context": {
344
356
"@vocab": "http://example.org/",
@@ -348,9 +360,9 @@ A graph map uses the map form already existing for `@index`, `@language`, `@type
348
360
"g1": {"value": "x"}
349
361
}
350
362
}
351
-
```
363
+
352
364
treats "g1" as an index, and expands to the following:
353
-
```ruby
365
+
354
366
[{
355
367
"http://example.org/input": [{
356
368
"@index": "g1",
@@ -359,11 +371,11 @@ treats "g1" as an index, and expands to the following:
359
371
}]
360
372
}]
361
373
}])
362
-
```
374
+
363
375
This can also include `@set` to ensure that, when compacting, a single value of an index will be in array form.
364
376
365
377
The _id_ version is similar:
366
-
```ruby
378
+
367
379
{
368
380
"@context": {
369
381
"@vocab": "http://example.org/",
@@ -373,9 +385,9 @@ The _id_ version is similar:
373
385
"http://example.com/g1": {"value": "x"}
374
386
}
375
387
}
376
-
```
388
+
377
389
which expands to:
378
-
```ruby
390
+
379
391
[{
380
392
"http://example.org/input": [{
381
393
"@id": "http://example.com/g1",
@@ -384,10 +396,10 @@ which expands to:
384
396
}]
385
397
}]
386
398
}])
387
-
```
399
+
388
400
### Transparent Nesting
389
401
Many JSON APIs separate properties from their entities using an intermediate object. For example, a set of possible labels may be grouped under a common property:
390
-
```json
402
+
391
403
{
392
404
"@context": {
393
405
"skos": "http://www.w3.org/2004/02/skos/core#",
@@ -403,9 +415,9 @@ Many JSON APIs separate properties from their entities using an intermediate obj
403
415
"other_label": "This is the other label"
404
416
}
405
417
}
406
-
```
418
+
407
419
In this case, the `labels` property is semantically meaningless. Defining it as equivalent to `@nest` causes it to be ignored when expanding, making it equivalent to the following:
408
-
```json
420
+
409
421
{
410
422
"@context": {
411
423
"skos": "http://www.w3.org/2004/02/skos/core#",
@@ -419,9 +431,9 @@ Many JSON APIs separate properties from their entities using an intermediate obj
419
431
"main_label": "This is the main label for my resource",
420
432
"other_label": "This is the other label"
421
433
}
422
-
```
434
+
423
435
Similarly, properties may be marked with "@nest": "nest-term", to cause them to be nested. Note that the `@nest` keyword can also be aliased in the context.
424
-
```json
436
+
425
437
{
426
438
"@context": {
427
439
"skos": "http://www.w3.org/2004/02/skos/core#",
@@ -437,7 +449,7 @@ Many JSON APIs separate properties from their entities using an intermediate obj
437
449
"other_label": "This is the other label"
438
450
}
439
451
}
440
-
```
452
+
441
453
In this way, nesting survives round-tripping through expansion, and framed output can include nested properties.
442
454
443
455
## Sinatra/Rack support
@@ -515,14 +527,14 @@ Note, the API method signatures differed in versions before 1.0, in that they al
515
527
## Installation
516
528
The recommended installation method is via [RubyGems](https://rubygems.org/).
517
529
To install the latest official release of the `JSON-LD` gem, do:
518
-
```bash
519
-
% [sudo] gem install json-ld
520
-
```
530
+
531
+
% [sudo] gem install json-ld
532
+
521
533
## Download
522
534
To get a local working copy of the development repository, do:
0 commit comments