@@ -124,20 +124,23 @@ import {JsonLdProcessor} from 'jsonld';
124
124
Examples
125
125
--------
126
126
127
+ Example data and context used throughout examples below:
127
128
``` js
128
- var doc = {
129
+ const doc = {
129
130
" http://schema.org/name" : " Manu Sporny" ,
130
131
" http://schema.org/url" : {" @id" : " http://manu.sporny.org/" },
131
132
" http://schema.org/image" : {" @id" : " http://manu.sporny.org/images/manu.png" }
132
133
};
133
- var context = {
134
+ const context = {
134
135
" name" : " http://schema.org/name" ,
135
136
" homepage" : {" @id" : " http://schema.org/url" , " @type" : " @id" },
136
137
" image" : {" @id" : " http://schema.org/image" , " @type" : " @id" }
137
138
};
139
+ ```
138
140
141
+ ### [ compact] ( http://json-ld.org/spec/latest/json-ld/#compacted-document-form )
142
+ ``` js
139
143
// compact a document according to a particular context
140
- // see: http://json-ld.org/spec/latest/json-ld/#compacted-document-form
141
144
jsonld .compact (doc, context, function (err , compacted ) {
142
145
console .log (JSON .stringify (compacted, null , 2 ));
143
146
/* Output:
@@ -153,8 +156,13 @@ jsonld.compact(doc, context, function(err, compacted) {
153
156
// compact using URLs
154
157
jsonld .compact (' http://example.org/doc' , ' http://example.org/context' , ... );
155
158
159
+ // or using promises
160
+ const compacted = await jsonld .compact (doc, context);
161
+ ```
162
+
163
+ ### [ expand] ( http://json-ld.org/spec/latest/json-ld/#expanded-document-form )
164
+ ``` js
156
165
// expand a document, removing its context
157
- // see: http://json-ld.org/spec/latest/json-ld/#expanded-document-form
158
166
jsonld .expand (compacted, function (err , expanded ) {
159
167
/* Output:
160
168
{
@@ -168,20 +176,36 @@ jsonld.expand(compacted, function(err, expanded) {
168
176
// expand using URLs
169
177
jsonld .expand (' http://example.org/doc' , ... );
170
178
179
+ // or using promises
180
+ const expanded = await jsonld .expand (doc);
181
+ ```
182
+
183
+ ### [ flatten] ( http://json-ld.org/spec/latest/json-ld/#flattened-document-form )
184
+ ``` js
171
185
// flatten a document
172
- // see: http://json-ld.org/spec/latest/json-ld/#flattened-document-form
173
186
jsonld .flatten (doc, (err , flattened ) => {
174
187
// all deep-level trees flattened to the top-level
175
188
});
176
189
190
+ // or using promises
191
+ const flattened = await jsonld .flatten (doc);
192
+ ```
193
+
194
+ ### [ frame] ( http://json-ld.org/spec/latest/json-ld-framing/#introduction )
195
+ ``` js
177
196
// frame a document
178
- // see: http://json-ld.org/spec/latest/json-ld-framing/#introduction
179
197
jsonld .frame (doc, frame, (err , framed ) => {
180
198
// document transformed into a particular tree structure per the given frame
181
199
});
182
200
201
+ // or using promises
202
+ const framed = await jsonld .frame (doc, frame);
203
+ ```
204
+
205
+ ### [ canonize] ( http://json-ld.github.io/normalization/spec/ ) (normalize)
206
+ ``` js
183
207
// canonize (normalize) a document using the RDF Dataset Normalization Algorithm
184
- // (URDNA2015), see: http://json-ld.github.io/normalization/spec/
208
+ // (URDNA2015), see:
185
209
jsonld .canonize (doc, {
186
210
algorithm: ' URDNA2015' ,
187
211
format: ' application/n-quads'
@@ -190,16 +214,34 @@ jsonld.canonize(doc, {
190
214
// that can be used for hashing, comparison, etc.
191
215
});
192
216
217
+ // or using promises
218
+ const canonized = await jsonld .canonize (doc, {format: ' application/n-quads' });
219
+ ```
220
+
221
+ ### toRDF (N-Quads)
222
+ ``` js
193
223
// serialize a document to N-Quads (RDF)
194
224
jsonld .toRDF (doc, {format: ' application/n-quads' }, (err , nquads ) => {
195
225
// nquads is a string of N-Quads
196
226
});
197
227
228
+ // or using promises
229
+ const rdf = await jsonld .toRDF (doc, {format: ' application/n-quads' });
230
+ ```
231
+
232
+ ### fromRDF (N-Quads)
233
+ ``` js
198
234
// deserialize N-Quads (RDF) to JSON-LD
199
235
jsonld .fromRDF (nquads, {format: ' application/n-quads' }, (err , doc ) => {
200
236
// doc is JSON-LD
201
237
});
202
238
239
+ // or using promises
240
+ const doc = await jsonld .fromRDF (nquads, {format: ' application/n-quads' });
241
+ ```
242
+
243
+ ### Custom RDF Praser
244
+ ``` js
203
245
// register a custom async-callback-based RDF parser
204
246
jsonld .registerRDFParser (contentType, (input , callback ) => {
205
247
// parse input to a jsonld.js RDF dataset object...
@@ -212,35 +254,15 @@ jsonld.registerRDFParser(contentType, input => {
212
254
return dataset;
213
255
});
214
256
215
- // use the promises API:
216
-
217
- // compaction
218
- const compacted = await jsonld .compact (doc, context);
219
-
220
- // expansion
221
- const expanded = await jsonld .expand (doc);
222
-
223
- // flattening
224
- const flattened = await jsonld .flatten (doc);
225
-
226
- // framing
227
- const framed = await jsonld .frame (doc, frame);
228
-
229
- // canonicalization (normalization)
230
- const canonized = await jsonld .canonize (doc, {format: ' application/n-quads' });
231
-
232
- // serialize to RDF
233
- const rdf = await jsonld .toRDF (doc, {format: ' application/n-quads' });
234
-
235
- // deserialize from RDF
236
- const doc = await jsonld .fromRDF (nquads, {format: ' application/n-quads' });
237
-
238
257
// register a custom promise-based RDF parser
239
258
jsonld .registerRDFParser (contentType, async input => {
240
259
// parse input into a jsonld.js RDF dataset object...
241
260
return new Promise (... );
242
261
});
262
+ ```
243
263
264
+ ### Custom Document Loader
265
+ ``` js
244
266
// how to override the default document loader with a custom one -- for
245
267
// example, one that uses pre-loaded contexts:
246
268
0 commit comments